diff options
author | Dave Davenport <qball@blame.services> | 2021-06-15 17:18:34 +0200 |
---|---|---|
committer | Dave Davenport <qball@blame.services> | 2021-06-15 17:18:34 +0200 |
commit | 4c1a74e15ed3fe35538a00d5351cebfd7ab574af (patch) | |
tree | b96f16aff80f9d40445199987ce3a5f102fa33ce /source | |
parent | f72bae02bb846af0091ff083e34325314907ea52 (diff) |
[Widget] Add scaling option to background-image.
Diffstat (limited to 'source')
-rw-r--r-- | source/rofi-icon-fetcher.c | 50 | ||||
-rw-r--r-- | source/theme.c | 23 |
2 files changed, 64 insertions, 9 deletions
diff --git a/source/rofi-icon-fetcher.c b/source/rofi-icon-fetcher.c index 12e912a1..6a09d22a 100644 --- a/source/rofi-icon-fetcher.c +++ b/source/rofi-icon-fetcher.c @@ -77,7 +77,8 @@ typedef struct unsigned int *acount; uint32_t uid; - int size; + int wsize; + int hsize; cairo_surface_t *surface; IconFetcherNameEntry *entry; @@ -301,13 +302,13 @@ static void rofi_icon_fetcher_worker ( thread_state *sdata, G_GNUC_UNUSED gpoint icon_path = sentry->entry->name; } else { - icon_path = icon_path_ = nk_xdg_theme_get_icon ( rofi_icon_fetcher_data->xdg_context, themes, NULL, sentry->entry->name, sentry->size, 1, TRUE ); + icon_path = icon_path_ = nk_xdg_theme_get_icon ( rofi_icon_fetcher_data->xdg_context, themes, NULL, sentry->entry->name, MIN(sentry->wsize,sentry->hsize), 1, TRUE ); if ( icon_path_ == NULL ) { - g_debug ( "failed to get icon %s(%d): n/a", sentry->entry->name, sentry->size ); + g_debug ( "failed to get icon %s(%dx%d): n/a", sentry->entry->name, sentry->wsize, sentry->hsize ); return; } else{ - g_debug ( "found icon %s(%d): %s", sentry->entry->name, sentry->size, icon_path ); + g_debug ( "found icon %s(%dx%d): %s", sentry->entry->name, sentry->wsize, sentry->hsize, icon_path ); } } cairo_surface_t *icon_surf = NULL; @@ -318,7 +319,7 @@ static void rofi_icon_fetcher_worker ( thread_state *sdata, G_GNUC_UNUSED gpoint } GError *error = NULL; - GdkPixbuf *pb = gdk_pixbuf_new_from_file_at_scale ( icon_path, sentry->size, sentry->size, TRUE, &error ); + GdkPixbuf *pb = gdk_pixbuf_new_from_file_at_scale ( icon_path, sentry->wsize, sentry->hsize, TRUE, &error ); if ( error != NULL ) { g_warning ( "Failed to load image: %s", error->message ); g_error_free ( error ); @@ -336,6 +337,40 @@ static void rofi_icon_fetcher_worker ( thread_state *sdata, G_GNUC_UNUSED gpoint rofi_view_reload (); } +uint32_t rofi_icon_fetcher_query_advanced ( const char *name, const int wsize, const int hsize ) +{ + g_debug ( "Query: %s(%dx%d)", name, wsize, hsize ); + IconFetcherNameEntry *entry = g_hash_table_lookup ( rofi_icon_fetcher_data->icon_cache, name ); + if ( entry == NULL ) { + entry = g_new0 ( IconFetcherNameEntry, 1 ); + entry->name = g_strdup ( name ); + g_hash_table_insert ( rofi_icon_fetcher_data->icon_cache, entry->name, entry ); + } + IconFetcherEntry *sentry; + for ( GList *iter = g_list_first ( entry->sizes ); iter; iter = g_list_next ( iter ) ) { + sentry = iter->data; + if ( sentry->wsize == wsize && sentry->hsize == hsize ) { + return sentry->uid; + } + } + + // Not found. + sentry = g_new0 ( IconFetcherEntry, 1 ); + sentry->uid = ++( rofi_icon_fetcher_data->last_uid ); + sentry->wsize = wsize; + sentry->hsize = hsize; + sentry->entry = entry; + sentry->surface = NULL; + + entry->sizes = g_list_prepend ( entry->sizes, sentry ); + g_hash_table_insert ( rofi_icon_fetcher_data->icon_cache_uid, GINT_TO_POINTER ( sentry->uid ), sentry ); + + // Push into fetching queue. + sentry->state.callback = rofi_icon_fetcher_worker; + g_thread_pool_push ( tpool, sentry, NULL ); + + return sentry->uid; +} uint32_t rofi_icon_fetcher_query ( const char *name, const int size ) { g_debug ( "Query: %s(%d)", name, size ); @@ -348,7 +383,7 @@ uint32_t rofi_icon_fetcher_query ( const char *name, const int size ) IconFetcherEntry *sentry; for ( GList *iter = g_list_first ( entry->sizes ); iter; iter = g_list_next ( iter ) ) { sentry = iter->data; - if ( sentry->size == size ) { + if ( sentry->wsize == size && sentry->hsize == size ) { return sentry->uid; } } @@ -356,7 +391,8 @@ uint32_t rofi_icon_fetcher_query ( const char *name, const int size ) // Not found. sentry = g_new0 ( IconFetcherEntry, 1 ); sentry->uid = ++( rofi_icon_fetcher_data->last_uid ); - sentry->size = size; + sentry->wsize = size; + sentry->hsize = size; sentry->entry = entry; sentry->surface = NULL; diff --git a/source/theme.c b/source/theme.c index fa8ac220..cd6546af 100644 --- a/source/theme.c +++ b/source/theme.c @@ -996,8 +996,27 @@ gboolean rofi_theme_get_image ( const widget *widget, const char *property, cair return FALSE; } if ( p->value.image.type == ROFI_IMAGE_URL ) { - if ( p->value.image.surface_id == 0 ) { - p->value.image.surface_id = rofi_icon_fetcher_query ( p->value.image.url, -1 ); + int wsize = -1; + int hsize = -1; + switch ( p->value.image.scaling ) { + case ROFI_SCALE_BOTH: + wsize = widget->w; + hsize = widget->h; + break; + case ROFI_SCALE_WIDTH: + wsize = widget->w; + break; + case ROFI_SCALE_HEIGHT: + hsize = widget->h; + break; + case ROFI_SCALE_NONE: + default: + break; + } + if ( p->value.image.surface_id == 0 || p->value.image.wsize != wsize || p->value.image.hsize != hsize) { + p->value.image.surface_id = rofi_icon_fetcher_query_advanced ( p->value.image.url, wsize, hsize ); + p->value.image.wsize = wsize; + p->value.image.hsize = hsize; } cairo_surface_t *img = rofi_icon_fetcher_get ( p->value.image.surface_id ); |