From efbddb587587a13d8ffebe51807ec4c9450efa41 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 12 Oct 2015 00:25:39 +0200 Subject: [PATCH] Add cache for menubar.utils.lookup_icon I have tried to use `utils.icon_cache = require("gears.cache").new(utils.lookup_icon_uncached)`, but that appears to get garbage-collected too much?! --- lib/menubar/utils.lua | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/menubar/utils.lua b/lib/menubar/utils.lua index e56c72275..57868ee78 100644 --- a/lib/menubar/utils.lua +++ b/lib/menubar/utils.lua @@ -115,11 +115,11 @@ local function get_icon_lookup_path() end --- Lookup an icon in different folders of the filesystem. --- @param icon_file Short or full name of the icon. --- @return full name of the icon. -function utils.lookup_icon(icon_file) +-- @tparam string icon_file Short or full name of the icon. +-- @treturn string|boolean Full name of the icon, or false on failure. +function utils.lookup_icon_uncached(icon_file) if not icon_file or icon_file == "" then - return default_icon + return false end if icon_file:sub(1, 1) == '/' and is_format_supported(icon_file) then @@ -143,10 +143,21 @@ function utils.lookup_icon(icon_file) end end end - return default_icon + return false end end +local lookup_icon_cache = {} +--- Lookup an icon in different folders of the filesystem (cached). +-- @param icon_file Short or full name of the icon. +-- @return full name of the icon. +function utils.lookup_icon(icon) + if not lookup_icon_cache[icon] and lookup_icon_cache[icon] ~= false then + lookup_icon_cache[icon] = utils.lookup_icon_uncached(icon) + end + return lookup_icon_cache[icon] or default_icon +end + --- Parse a .desktop file. -- @param file The .desktop file. -- @return A table with file entries.