desktop-entry: separate icon and app_id icon lookup

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.
This commit is contained in:
Consolatis 2025-01-07 15:21:30 +01:00 committed by Hiroaki Yamamoto
parent d53cf642f5
commit 4ecedcdb3b
3 changed files with 51 additions and 35 deletions

View file

@ -7,8 +7,11 @@ struct server;
void desktop_entry_init(struct server *server); void desktop_entry_init(struct server *server);
void desktop_entry_finish(struct server *server); void desktop_entry_finish(struct server *server);
struct lab_img *desktop_entry_icon_lookup(struct server *server, struct lab_img *desktop_entry_load_icon_from_app_id(
const char *app_id, int size, float scale); 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 * desktop_entry_name_lookup() - return the application name

View file

@ -299,8 +299,47 @@ convert_img_type(enum sfdo_icon_file_format fmt)
} }
struct lab_img * struct lab_img *
desktop_entry_icon_lookup(struct server *server, const char *app_id, int size, desktop_entry_load_icon(struct server *server, const char *icon_name, int size, float scale)
float scale) {
/* 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;
}
struct sfdo *sfdo = server->sfdo;
if (!sfdo) {
return NULL;
}
/*
* libsfdo doesn't support loading icons for fractional scales,
* so round down and increase the icon size to compensate.
*/
int lookup_scale = MAX((int)scale, 1);
int lookup_size = lroundf(size * scale / lookup_scale);
struct icon_ctx ctx = {0};
int ret;
if (icon_name[0] == '/') {
ret = process_abs_name(&ctx, icon_name);
} else {
ret = process_rel_name(&ctx, icon_name, sfdo, lookup_size, lookup_scale);
}
if (ret < 0) {
wlr_log(WLR_INFO, "failed to load icon file %s", icon_name);
return NULL;
}
wlr_log(WLR_DEBUG, "loading icon file %s", ctx.path);
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)) { if (string_null_or_empty(app_id)) {
return NULL; return NULL;
@ -317,37 +356,11 @@ desktop_entry_icon_lookup(struct server *server, const char *app_id, int size,
icon_name = sfdo_desktop_entry_get_icon(entry, NULL); icon_name = sfdo_desktop_entry_get_icon(entry, NULL);
} }
/* struct lab_img *img = desktop_entry_load_icon(server, icon_name, size, scale);
* libsfdo doesn't support loading icons for fractional scales, if (!img) {
* so round down and increase the icon size to compensate. /* Icon not defined in .desktop file or could not be loaded */
*/ img = desktop_entry_load_icon(server, app_id, size, scale);
int lookup_scale = MAX((int)scale, 1);
int lookup_size = lroundf(size * scale / lookup_scale);
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] == '/') {
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) {
return NULL;
}
wlr_log(WLR_DEBUG, "loading icon file %s", ctx.path);
struct lab_img *img = lab_img_load(convert_img_type(ctx.format), ctx.path, NULL);
free(ctx.path);
return img; return img;
} }

View file

@ -609,7 +609,7 @@ ssd_update_window_icon(struct ssd *ssd)
*/ */
float icon_scale = output_max_scale(ssd->view->server); 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); ssd->view->server, app_id, icon_size, icon_scale);
if (!icon_img) { if (!icon_img) {
wlr_log(WLR_DEBUG, "icon could not be loaded for %s", app_id); wlr_log(WLR_DEBUG, "icon could not be loaded for %s", app_id);