]> git.mdlowis.com Git - proto/labwc.git/commitdiff
desktop-entry: separate icon and app_id icon lookup
authorConsolatis <35009135+Consolatis@users.noreply.github.com>
Tue, 7 Jan 2025 14:21:30 +0000 (15:21 +0100)
committerHiroaki Yamamoto <hrak1529@gmail.com>
Wed, 8 Jan 2025 15:57:06 +0000 (00:57 +0900)
This patch splits desktop_entry_icon_lookup() into two separate functions
- desktop_entry_load_icon(): load a icon from the configured icon theme
- desktop_entry_load_icon_from_app_id(): load a icon name from a .desktop
  file based on the given app_id and supply it to _load_icon().

The _load_icon() function will be used in a future menu icon
implementation whereas the _load_icon_from_app_id() function is used
within the SSD titlebar window icon lookup routine.

include/desktop-entry.h
src/desktop-entry.c
src/ssd/ssd-titlebar.c

index 75643067c2f0de6887e9a3ad0b187b014a86304c..ca3f77e4de1f39487da5e2b586eeed56104bf68b 100644 (file)
@@ -7,8 +7,11 @@ struct server;
 void desktop_entry_init(struct server *server);
 void desktop_entry_finish(struct server *server);
 
-struct lab_img *desktop_entry_icon_lookup(struct server *server,
-       const char *app_id, int size, float scale);
+struct lab_img *desktop_entry_load_icon_from_app_id(
+       struct server *server, const char *app_id, int size, float scale);
+
+struct lab_img *desktop_entry_load_icon(
+       struct server *server, const char *icon_name, int size, float scale);
 
 /**
  * desktop_entry_name_lookup() - return the application name
index c5ed846db599981fead846cdb08b581640c1d527..f28ce9cb7c5f83576db7012635ef3f629c627e7e 100644 (file)
@@ -299,10 +299,10 @@ convert_img_type(enum sfdo_icon_file_format fmt)
 }
 
 struct lab_img *
-desktop_entry_icon_lookup(struct server *server, const char *app_id, int size,
-               float scale)
+desktop_entry_load_icon(struct server *server, const char *icon_name, int size, float scale)
 {
-       if (string_null_or_empty(app_id)) {
+       /* static analyzer isn't able to detect the NULL check in string_null_or_empty() */
+       if (string_null_or_empty(icon_name) || !icon_name) {
                return NULL;
        }
 
@@ -311,12 +311,6 @@ desktop_entry_icon_lookup(struct server *server, const char *app_id, int size,
                return NULL;
        }
 
-       const char *icon_name = NULL;
-       struct sfdo_desktop_entry *entry = get_desktop_entry(sfdo, app_id);
-       if (entry) {
-               icon_name = sfdo_desktop_entry_get_icon(entry, NULL);
-       }
-
        /*
         * libsfdo doesn't support loading icons for fractional scales,
         * so round down and increase the icon size to compensate.
@@ -326,20 +320,13 @@ desktop_entry_icon_lookup(struct server *server, const char *app_id, int size,
 
        struct icon_ctx ctx = {0};
        int ret;
-       if (!icon_name) {
-               /* fall back to app id */
-               ret = process_rel_name(&ctx, app_id, sfdo, lookup_size, lookup_scale);
-       } else if (icon_name[0] == '/') {
+       if (icon_name[0] == '/') {
                ret = process_abs_name(&ctx, icon_name);
        } else {
-               /* this should be the case for most icons */
                ret = process_rel_name(&ctx, icon_name, sfdo, lookup_size, lookup_scale);
-               /* Icon defined in .desktop file could not be loaded, retry with app_id */
-               if (ret < 0) {
-                       ret = process_rel_name(&ctx, app_id, sfdo, lookup_size, lookup_scale);
-               }
        }
        if (ret < 0) {
+               wlr_log(WLR_INFO, "failed to load icon file %s", icon_name);
                return NULL;
        }
 
@@ -347,7 +334,33 @@ desktop_entry_icon_lookup(struct server *server, const char *app_id, int size,
        struct lab_img *img = lab_img_load(convert_img_type(ctx.format), ctx.path, NULL);
 
        free(ctx.path);
+       return img;
+}
+
+struct lab_img *
+desktop_entry_load_icon_from_app_id(struct server *server,
+               const char *app_id, int size, float scale)
+{
+       if (string_null_or_empty(app_id)) {
+               return NULL;
+       }
+
+       struct sfdo *sfdo = server->sfdo;
+       if (!sfdo) {
+               return NULL;
+       }
 
+       const char *icon_name = NULL;
+       struct sfdo_desktop_entry *entry = get_desktop_entry(sfdo, app_id);
+       if (entry) {
+               icon_name = sfdo_desktop_entry_get_icon(entry, NULL);
+       }
+
+       struct lab_img *img = desktop_entry_load_icon(server, icon_name, size, scale);
+       if (!img) {
+               /* Icon not defined in .desktop file or could not be loaded */
+               img = desktop_entry_load_icon(server, app_id, size, scale);
+       }
        return img;
 }
 
index 46ba2e21963a7cb8c31d5f19305480dec9aa48ea..26b5635beae57f2645646c597b506d4bf3308aa1 100644 (file)
@@ -609,7 +609,7 @@ ssd_update_window_icon(struct ssd *ssd)
         */
        float icon_scale = output_max_scale(ssd->view->server);
 
-       struct lab_img *icon_img = desktop_entry_icon_lookup(
+       struct lab_img *icon_img = desktop_entry_load_icon_from_app_id(
                ssd->view->server, app_id, icon_size, icon_scale);
        if (!icon_img) {
                wlr_log(WLR_DEBUG, "icon could not be loaded for %s", app_id);