Merge pull request #2156 from varingst/fix-icon-lookup

Fix: #2152
This commit is contained in:
Emmanuel Lepage Vallée 2018-01-16 21:16:34 -05:00 committed by GitHub
commit ad68d3bf5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 133 additions and 36 deletions

View File

@ -20,6 +20,7 @@ local w_textbox = require("wibox.widget.textbox")
local gdebug = require("gears.debug")
local protected_call = require("gears.protected_call")
local gstring = require("gears.string")
local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility with Lua 5.1)
local utils = {}
@ -123,6 +124,7 @@ do
end
local all_icon_sizes = {
'scalable',
'128x128',
'96x96',
'72x72',
@ -154,48 +156,63 @@ local icon_lookup_path = nil
--- Get a list of icon lookup paths.
-- @treturn table A list of directories, without trailing slash.
local function get_icon_lookup_path()
if not icon_lookup_path then
local add_if_readable = function(t, path)
if icon_lookup_path then return icon_lookup_path end
local function ensure_args(t, paths)
if type(paths) == 'string' then paths = { paths } end
return t or {}, paths
end
local function add_if_readable(t, paths)
t, paths = ensure_args(t, paths)
for _, path in ipairs(paths) do
if gfs.dir_readable(path) then
table.insert(t, path)
end
end
return t
end
local function add_with_dir(t, paths, dir)
t, paths = ensure_args(t, paths)
dir = { nil, dir }
for _, path in ipairs(paths) do
dir[1] = path
table.insert(t, glib.build_filenamev(dir))
end
return t
end
icon_lookup_path = {}
local theme_priority = { 'hicolor' }
if theme.icon_theme then table.insert(theme_priority, 1, theme.icon_theme) end
local paths = add_with_dir({}, glib.get_home_dir(), '.icons')
add_with_dir(paths, {
glib.get_user_data_dir(), -- $XDG_DATA_HOME, typically $HOME/.local/share
unpack(glib.get_system_data_dirs()) -- $XDG_DATA_DIRS, typically /usr/{,local/}share
}, 'icons')
add_with_dir(paths, glib.get_system_data_dirs(), 'pixmaps')
local icon_theme_paths = {}
local icon_theme = theme.icon_theme
local paths = glib.get_system_data_dirs()
table.insert(paths, 1, glib.get_user_data_dir())
table.insert(paths, 1, glib.build_filenamev({glib.get_home_dir(),
'.icons'}))
for _,dir in ipairs(paths) do
local icons_dir = glib.build_filenamev({dir, 'icons'})
if gfs.dir_readable(icons_dir) then
if icon_theme then
for _, theme_dir in ipairs(theme_priority) do
add_if_readable(icon_theme_paths,
glib.build_filenamev({icons_dir,
icon_theme}))
end
-- Fallback theme.
add_if_readable(icon_theme_paths,
glib.build_filenamev({icons_dir, 'hicolor'}))
end
add_with_dir({}, paths, theme_dir))
end
local app_in_theme_paths = {}
for _, icon_theme_directory in ipairs(icon_theme_paths) do
for _, size in ipairs(all_icon_sizes) do
add_if_readable(icon_lookup_path,
table.insert(app_in_theme_paths,
glib.build_filenamev({ icon_theme_directory,
size, 'apps' }))
end
end
for _,dir in ipairs(paths)do
-- lowest priority fallbacks
add_if_readable(icon_lookup_path,
glib.build_filenamev({dir, 'pixmaps'}))
add_if_readable(icon_lookup_path,
glib.build_filenamev({dir, 'icons'}))
end
end
return icon_lookup_path
add_if_readable(icon_lookup_path, app_in_theme_paths)
return add_if_readable(icon_lookup_path, paths)
end
--- Remove CR newline from the end of the string.

View File

@ -0,0 +1 @@
../../icons/awesome

View File

@ -0,0 +1 @@
../../icons/fallback.png

View File

@ -0,0 +1 @@
../../../../../../icons/awesome16.png

View File

@ -0,0 +1 @@
../../../../../../icons/awesome16.png

View File

@ -0,0 +1 @@
awesome.png

View File

@ -0,0 +1 @@
../../64x64/apps/awesome.png

View File

@ -0,0 +1 @@
../../../../icons/fallback.png

View File

@ -0,0 +1 @@
../../../../icons/fallback.png

View File

@ -0,0 +1 @@
../../../../icons/fallback.png

View File

@ -0,0 +1 @@
../../../icons/fallback.png

View File

@ -0,0 +1 @@
../../../icons/fallback.png

View File

@ -0,0 +1 @@
../../../icons/fallback.png

View File

@ -0,0 +1 @@
../../../icons/fallback.png

View File

@ -0,0 +1 @@
../../../icons/fallback.png

View File

@ -0,0 +1 @@
../../../icons/fallback.png

View File

@ -0,0 +1 @@
../../../icons/fallback.png

View File

@ -0,0 +1 @@
../../../icons/fallback.png

View File

@ -0,0 +1 @@
../../../icons/fallback.png

View File

@ -4,6 +4,8 @@
---------------------------------------------------------------------------
local utils = require("menubar.utils")
local theme = require("beautiful")
local glib = require("lgi").GLib
describe("menubar.utils unescape", function()
local single_strings = {
@ -50,4 +52,64 @@ describe("menubar.utils unescape", function()
end
end)
describe("menubar.utils lookup_icon_uncached", function()
local shimmed = {}
local icon_theme
local function assert_found_in_path(icon, path)
assert.matches(path .. '$', utils.lookup_icon_uncached(icon) or '')
end
setup(function()
local root = (os.getenv("SOURCE_DIRECTORY") or '.') .. "/spec/menubar"
local function shim(name, retval)
shimmed[name] = glib[name]
glib[name] = function() return retval end
end
shim('get_home_dir', root .. "/home")
shim('get_user_data_dir', root .. "/home/.local/share")
shim('get_system_data_dirs', {
root .. "/usr/local/share",
root .. "/usr/share"
})
icon_theme = theme.icon_theme
theme.icon_theme = 'awesome'
end)
teardown(function()
for name, func in pairs(shimmed) do
glib[name] = func
end
theme.icon_theme = icon_theme
end)
it('finds icons in icon base directories, in correct order', function()
-- Shimmed icon base directories contain the following icons:
--
-- usr/share/pixmaps/icon[1-5].png
-- usr/share/icons/icon[2-5].png
-- usr/local/share/icons/icon[3-5].png
-- .local/share/icons/icon[4-5].png
-- .icons/icon5.png
assert_found_in_path('icon1', '/usr/share/pixmaps/icon1.png')
assert_found_in_path('icon2', '/usr/share/icons/icon2.png')
assert_found_in_path('icon3', '/usr/local/share/icons/icon3.png')
assert_found_in_path('icon4', '/.local/share/icons/icon4.png')
assert_found_in_path('icon5', '/.icons/icon5.png')
end)
it('finds icons in $HOME/.icons/<theme>/<size>/apps/', function()
-- Theme 'awesome' in shimmed $HOME/.icons:
assert_found_in_path('awesome', '/.icons/awesome/64x64/apps/awesome.png')
assert_found_in_path('awesome2', '/.icons/awesome/scalable/apps/awesome2.png')
end)
end)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80