From fc6426534351fdcb0fa15d7d5f5aa17f29726c65 Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Wed, 11 May 2022 17:02:13 +0200 Subject: [PATCH] [1633][Mode] Add generic fallback option for modes. Add config option: ```css configuration { { fallback-icon: ""; } } ``` fixes: #1633 --- include/mode-private.h | 4 ++++ include/mode.h | 2 +- source/mode.c | 31 +++++++++++++++++++++++++++++-- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/include/mode-private.h b/include/mode-private.h index 99d48309..0f5bcaed 100644 --- a/include/mode-private.h +++ b/include/mode-private.h @@ -203,6 +203,10 @@ struct rofi_mode { /** Module */ GModule *module; + + /** Fallack icon.*/ + uint32_t fallback_icon_fetch_uid; + uint32_t fallback_icon_not_found; }; G_END_DECLS #endif // ROFI_MODE_PRIVATE_H diff --git a/include/mode.h b/include/mode.h index fc239223..b0b8314a 100644 --- a/include/mode.h +++ b/include/mode.h @@ -138,7 +138,7 @@ char *mode_get_display_value(const Mode *mode, unsigned int selected_line, * * @returns allocated new cairo_surface_t if applicable */ -cairo_surface_t *mode_get_icon(const Mode *mode, unsigned int selected_line, +cairo_surface_t *mode_get_icon(Mode *mode, unsigned int selected_line, int height); /** diff --git a/source/mode.c b/source/mode.c index 675bd519..3cb129aa 100644 --- a/source/mode.c +++ b/source/mode.c @@ -32,6 +32,7 @@ #include #include +#include "rofi-icon-fetcher.h" // This one should only be in mode implementations. #include "mode-private.h" /** @@ -42,6 +43,9 @@ int mode_init(Mode *mode) { g_return_val_if_fail(mode != NULL, FALSE); g_return_val_if_fail(mode->_init != NULL, FALSE); + // to make sure this is initialized correctly. + mode->fallback_icon_fetch_uid = 0; + mode->fallback_icon_not_found = FALSE; return mode->_init(mode); } @@ -68,13 +72,36 @@ char *mode_get_display_value(const Mode *mode, unsigned int selected_line, get_entry); } -cairo_surface_t *mode_get_icon(const Mode *mode, unsigned int selected_line, +cairo_surface_t *mode_get_icon(Mode *mode, unsigned int selected_line, int height) { g_assert(mode != NULL); if (mode->_get_icon != NULL) { - return mode->_get_icon(mode, selected_line, height); + cairo_surface_t *icon = mode->_get_icon(mode, selected_line, height); + if ( icon ) { + return icon; + } } + + + if ( mode->fallback_icon_not_found == TRUE) { + return NULL; + } + if (mode->fallback_icon_fetch_uid > 0) { + cairo_surface_t *icon = rofi_icon_fetcher_get(mode->fallback_icon_fetch_uid); + return icon; + } + ThemeWidget *wid = rofi_config_find_widget(mode->name, NULL, TRUE); + if ( wid ) { + /** Load user entires */ + Property *p = rofi_theme_find_property(wid, P_STRING, "fallback-icon", TRUE); + if (p != NULL && (p->type == P_STRING && p->value.s)) { + mode->fallback_icon_fetch_uid = + rofi_icon_fetcher_query(p->value.s, height); + return NULL; + } + } + mode->fallback_icon_not_found = TRUE; return NULL; }