bling/helpers/icon_theme.lua

142 lines
3.9 KiB
Lua
Raw Normal View History

local lgi = require("lgi")
local Gio = lgi.Gio
local Gtk = lgi.require("Gtk", "3.0")
local gobject = require("gears.object")
local gtable = require("gears.table")
local setmetatable = setmetatable
local ipairs = ipairs
local icon_theme = { mt = {} }
local name_lookup =
{
["jetbrains-studio"] = "android-studio"
}
local function get_icon_by_pid_command(self, client, apps)
local pid = client.pid
if pid ~= nil then
local handle = io.popen(string.format("ps -p %d -o comm=", pid))
local pid_command = handle:read("*a"):gsub("^%s*(.-)%s*$", "%1")
handle:close()
for _, app in ipairs(apps) do
local executable = app:get_executable()
if executable and executable:find(pid_command, 1, true) then
return self:get_gicon_path(app:get_icon())
end
end
end
end
local function get_icon_by_icon_name(self, client, apps)
local icon_name = client.icon_name and client.icon_name:lower() or nil
if icon_name ~= nil then
for _, app in ipairs(apps) do
local name = app:get_name():lower()
if name and name:find(icon_name, 1, true) then
return self:get_gicon_path(app:get_icon())
end
end
end
end
local function get_icon_by_class(self, client, apps)
if client.class ~= nil then
local class = name_lookup[client.class] or client.class:lower()
-- Try to remove dashes
local class_1 = class:gsub("[%-]", "")
-- Try to replace dashes with dot
local class_2 = class:gsub("[%-]", ".")
-- Try to match only the first word
local class_3 = class:match("(.-)-") or class
class_3 = class_3:match("(.-)%.") or class_3
class_3 = class_3:match("(.-)%s+") or class_3
local possible_icon_names = { class, class_3, class_2, class_1 }
for _, app in ipairs(apps) do
local id = app:get_id():lower()
for _, possible_icon_name in ipairs(possible_icon_names) do
if id and id:find(possible_icon_name, 1, true) then
return self:get_gicon_path(app:get_icon())
end
end
end
end
end
function icon_theme:get_client_icon_path(client)
local apps = Gio.AppInfo.get_all()
return get_icon_by_pid_command(self, client, apps) or
get_icon_by_icon_name(self, client, apps) or
get_icon_by_class(self, client, apps) or
client.icon or
self:choose_icon({"window", "window-manager", "xfwm4-default", "window_list" })
end
More app launcher improvements (#132) * Make sure there is always a default icon * Use get_example_icon_path to get the default icon * Refactor scrolling to add support for left and right scrolling * fixup! Make sure there is always a default icon * Fix calculation happening at the wrong time * Fix default icons again * Where did that come from? * Fix calculation happening at the wrong time for scroll_up as well * Fix error on scroll right when scrolling to page where amount of rows is smaller than the selected row * Sort search results by string similarity * Don't sort by similarity when the search string is empty * Add hover effects * try_to_keep_index_after_searching should be false by default * This should only trigger for lmb * Add an option to hide the app launcher when clicked with lmb/rmb outside of it * Use gtk-launch so terminal apps spawn correctly * Use get_executable instead of get_commandline * Add an option to set the hover colors * Further improvements for the spawn function * Fix scrolling/searching errors when there app list is empty * This should never be nil anyway * whitespace * Refactor show, hide and toggle method + remove support for manually setting x an y (use placement) * Add arguements for custom icon_theme (defaults to the selected system gtk theme) and icon_size (defaults to 48) * Pass the app table instead of individual keys to create_app_widget * Add an arguement to pass the default terminal for terminal apps as gtk-launch only uses xterm * Reformating * Rename 'mark_app' and 'unmark_app' to 'select_app' and 'unselect_app' * Call :hide() from app.spawn() to avoid calling hide() twice on some cases * Fix escape not closing the launcher after b7e44ec4 * Reduce code duplication and only reset the launcher when the animation is over (if not nil) * Set active_widget to nil when the grid is empty to prevent from spawning the wrong app * Override the default exe_callback * Override the default behaviour for 'Return' via hooks instead because overriding only exe_callback still doesn't stop it from pausing the prompt keygrabber * Set active_widget to nil on unselect * Unselect previous app on search to avoid from spawning it when the grid is empty * Use double quotes for everything
2021-11-05 04:38:54 +01:00
function icon_theme:choose_icon(icons_names)
local icon_info = self.gtk_theme:choose_icon(icons_names, self.icon_size, 0);
More app launcher improvements (#132) * Make sure there is always a default icon * Use get_example_icon_path to get the default icon * Refactor scrolling to add support for left and right scrolling * fixup! Make sure there is always a default icon * Fix calculation happening at the wrong time * Fix default icons again * Where did that come from? * Fix calculation happening at the wrong time for scroll_up as well * Fix error on scroll right when scrolling to page where amount of rows is smaller than the selected row * Sort search results by string similarity * Don't sort by similarity when the search string is empty * Add hover effects * try_to_keep_index_after_searching should be false by default * This should only trigger for lmb * Add an option to hide the app launcher when clicked with lmb/rmb outside of it * Use gtk-launch so terminal apps spawn correctly * Use get_executable instead of get_commandline * Add an option to set the hover colors * Further improvements for the spawn function * Fix scrolling/searching errors when there app list is empty * This should never be nil anyway * whitespace * Refactor show, hide and toggle method + remove support for manually setting x an y (use placement) * Add arguements for custom icon_theme (defaults to the selected system gtk theme) and icon_size (defaults to 48) * Pass the app table instead of individual keys to create_app_widget * Add an arguement to pass the default terminal for terminal apps as gtk-launch only uses xterm * Reformating * Rename 'mark_app' and 'unmark_app' to 'select_app' and 'unselect_app' * Call :hide() from app.spawn() to avoid calling hide() twice on some cases * Fix escape not closing the launcher after b7e44ec4 * Reduce code duplication and only reset the launcher when the animation is over (if not nil) * Set active_widget to nil when the grid is empty to prevent from spawning the wrong app * Override the default exe_callback * Override the default behaviour for 'Return' via hooks instead because overriding only exe_callback still doesn't stop it from pausing the prompt keygrabber * Set active_widget to nil on unselect * Unselect previous app on search to avoid from spawning it when the grid is empty * Use double quotes for everything
2021-11-05 04:38:54 +01:00
if icon_info then
local icon_path = icon_info:get_filename()
More app launcher improvements (#132) * Make sure there is always a default icon * Use get_example_icon_path to get the default icon * Refactor scrolling to add support for left and right scrolling * fixup! Make sure there is always a default icon * Fix calculation happening at the wrong time * Fix default icons again * Where did that come from? * Fix calculation happening at the wrong time for scroll_up as well * Fix error on scroll right when scrolling to page where amount of rows is smaller than the selected row * Sort search results by string similarity * Don't sort by similarity when the search string is empty * Add hover effects * try_to_keep_index_after_searching should be false by default * This should only trigger for lmb * Add an option to hide the app launcher when clicked with lmb/rmb outside of it * Use gtk-launch so terminal apps spawn correctly * Use get_executable instead of get_commandline * Add an option to set the hover colors * Further improvements for the spawn function * Fix scrolling/searching errors when there app list is empty * This should never be nil anyway * whitespace * Refactor show, hide and toggle method + remove support for manually setting x an y (use placement) * Add arguements for custom icon_theme (defaults to the selected system gtk theme) and icon_size (defaults to 48) * Pass the app table instead of individual keys to create_app_widget * Add an arguement to pass the default terminal for terminal apps as gtk-launch only uses xterm * Reformating * Rename 'mark_app' and 'unmark_app' to 'select_app' and 'unselect_app' * Call :hide() from app.spawn() to avoid calling hide() twice on some cases * Fix escape not closing the launcher after b7e44ec4 * Reduce code duplication and only reset the launcher when the animation is over (if not nil) * Set active_widget to nil when the grid is empty to prevent from spawning the wrong app * Override the default exe_callback * Override the default behaviour for 'Return' via hooks instead because overriding only exe_callback still doesn't stop it from pausing the prompt keygrabber * Set active_widget to nil on unselect * Unselect previous app on search to avoid from spawning it when the grid is empty * Use double quotes for everything
2021-11-05 04:38:54 +01:00
if icon_path then
return icon_path
end
end
return ""
end
function icon_theme:get_gicon_path(gicon)
if gicon == nil then
return ""
end
local icon_info = self.gtk_theme:lookup_by_gicon(gicon, self.icon_size, 0);
if icon_info then
local icon_path = icon_info:get_filename()
if icon_path then
return icon_path
end
end
return ""
end
function icon_theme:get_icon_path(icon_name)
local icon_info = self.gtk_theme:lookup_icon(icon_name, self.icon_size, 0)
if icon_info then
local icon_path = icon_info:get_filename()
if icon_path then
return icon_path
end
end
return ""
end
local function new(theme_name, icon_size)
local ret = gobject{}
gtable.crush(ret, icon_theme, true)
ret.name = theme_name or nil
ret.icon_size = icon_size or 48
if theme_name then
ret.gtk_theme = Gtk.IconTheme.new()
Gtk.IconTheme.set_custom_theme(ret.gtk_theme, theme_name);
else
ret.gtk_theme = Gtk.IconTheme.get_default()
end
return ret
end
function icon_theme.mt:__call(...)
return new(...)
end
return setmetatable(icon_theme, icon_theme.mt)