From 7999d260b60cde2144e0658d3fbab5139163cc41 Mon Sep 17 00:00:00 2001 From: Samuel Dionne-Riel Date: Sat, 22 Jan 2022 01:42:03 -0500 Subject: [PATCH] saturn: Fix icon handling and size - Harmonizes icon sizes - Fix absolute path icons handling - Adds a helper for managing paths --- saturn/main.fnl | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/saturn/main.fnl b/saturn/main.fnl index 77ff0c3..40e0bb5 100644 --- a/saturn/main.fnl +++ b/saturn/main.fnl @@ -3,6 +3,8 @@ (local dbus (require :dbus_proxy)) (local inspect (require :inspect)) +(local ICON_SIZE 64) + (local dbus-service-attrs { :bus dbus.Bus.SESSION @@ -72,7 +74,11 @@ (local inspect (require :inspect)) (local posix (require :posix)) +(local path { + :absolute? (fn [str] (= (str:sub 1 1) "/")) + }) (local Gtk lgi.Gtk) +(local GdkPixbuf lgi.GdkPixbuf) (local Pango lgi.Pango) (local icon-theme (Gtk.IconTheme.get_default)) @@ -85,15 +91,21 @@ })) (fn find-icon [name] (var found false) - (if (= (name.sub 1 1) "/") - (Gtk.Image.new_from_file name) - (let [sizes (icon-theme:get_icon_sizes name)] - - (each [_ res (pairs [64 48]) :until found] - (set found (icon-theme:load_icon - name res - (+ Gtk.IconLookupFlags.FORCE_SVG Gtk.IconLookupFlags.USE_BUILTIN)))) - (Gtk.Image.new_from_pixbuf found)))) + (if (path.absolute? name) + ;; From a direct path + (set found (GdkPixbuf.Pixbuf.new_from_file_at_size name ICON_SIZE ICON_SIZE)) + ;; From icon theme + (let [sizes (icon-theme:get_icon_sizes name)] + ;; Uses a list of "safe fallback" values + ;; Try the desired size first + (each [_ res (pairs [ICON_SIZE 128 64 48]) :until found] + (set found + (-?> (icon-theme:load_icon + name res + (+ Gtk.IconLookupFlags.FORCE_SVG Gtk.IconLookupFlags.USE_BUILTIN)) + (: :scale_simple ICON_SIZE ICON_SIZE GdkPixbuf.InterpType.BILINEAR)))) + )) + (Gtk.Image.new_from_pixbuf found)) (fn read-desktop-file [f] (let [parsed (inifile.parse f)