Skip to content

Commit

Permalink
[IconFetcher] Re-introduce icon scaling on wayland
Browse files Browse the repository at this point in the history
This time, preserving ABI compatibility with upstream rofi

Fixes #96
  • Loading branch information
moetayuko authored and lbonn committed Nov 30, 2023
1 parent 102e6de commit 36621af
Show file tree
Hide file tree
Showing 17 changed files with 122 additions and 42 deletions.
1 change: 1 addition & 0 deletions include/modes/dmenuscriptshared.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ typedef struct {
/** Async icon fetch handler. */
uint32_t icon_fetch_uid;
uint32_t icon_fetch_size;
guint icon_fetch_scale;
/** Hidden meta keywords. */
char *meta;

Expand Down
13 changes: 13 additions & 0 deletions include/theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,17 @@ GList *rofi_theme_get_list_distance(const widget *widget, const char *property);
* @returns a GList of strings.
*/
GList *rofi_theme_get_list_strings(const widget *widget, const char *property);

/**
* Display scale function type
*/
typedef guint (*disp_scale_func)(void);

/**
* @param func The function pointer to scale getter.
*
* Dependency injection for changing display scale without theme library
* depending on the display library
*/
void rofi_theme_set_disp_scale_func(disp_scale_func func);
#endif
4 changes: 2 additions & 2 deletions source/modes/combi.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,5 +342,5 @@ Mode combi_mode = {.name = "combi",
._get_icon = combi_get_icon,
._preprocess_input = combi_preprocess_input,
.private_data = NULL,
.free = NULL,
.type = MODE_TYPE_SWITCHER };
.free = NULL,
.type = MODE_TYPE_SWITCHER};
8 changes: 7 additions & 1 deletion source/modes/dmenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#define G_LOG_DOMAIN "Modes.DMenu"
#include "config.h"

#include "display.h"
#include "helper.h"
#include "modes/dmenu.h"
#include "rofi-icon-fetcher.h"
Expand Down Expand Up @@ -135,6 +136,7 @@ static void read_add_block(DmenuModePrivateData *pd, Block **block, char *data,
// Init.
(*block)->values[(*block)->length].icon_fetch_uid = 0;
(*block)->values[(*block)->length].icon_fetch_size = 0;
(*block)->values[(*block)->length].icon_fetch_scale = 0;
(*block)->values[(*block)->length].icon_name = NULL;
(*block)->values[(*block)->length].meta = NULL;
(*block)->values[(*block)->length].info = NULL;
Expand Down Expand Up @@ -165,6 +167,7 @@ static void read_add(DmenuModePrivateData *pd, char *data, gsize len) {
// Init.
pd->cmd_list[pd->cmd_list_length].icon_fetch_uid = 0;
pd->cmd_list[pd->cmd_list_length].icon_fetch_size = 0;
pd->cmd_list[pd->cmd_list_length].icon_fetch_scale = 0;
pd->cmd_list[pd->cmd_list_length].icon_name = NULL;
pd->cmd_list[pd->cmd_list_length].meta = NULL;
pd->cmd_list[pd->cmd_list_length].info = NULL;
Expand Down Expand Up @@ -697,18 +700,21 @@ static cairo_surface_t *dmenu_get_icon(const Mode *sw,
unsigned int selected_line,
unsigned int height) {
DmenuModePrivateData *pd = (DmenuModePrivateData *)mode_get_private_data(sw);
const guint scale = display_scale();

g_return_val_if_fail(pd->cmd_list != NULL, NULL);
DmenuScriptEntry *dr = &(pd->cmd_list[selected_line]);
if (dr->icon_name == NULL) {
return NULL;
}
if (dr->icon_fetch_uid > 0 && dr->icon_fetch_size == height) {
if (dr->icon_fetch_uid > 0 && dr->icon_fetch_size == height &&
dr->icon_fetch_scale == scale) {
return rofi_icon_fetcher_get(dr->icon_fetch_uid);
}
uint32_t uid = dr->icon_fetch_uid =
rofi_icon_fetcher_query(dr->icon_name, height);
dr->icon_fetch_size = height;
dr->icon_fetch_scale = scale;

return rofi_icon_fetcher_get(uid);
}
Expand Down
8 changes: 7 additions & 1 deletion source/modes/drun.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <sys/types.h>
#include <unistd.h>

#include "display.h"
#include "helper.h"
#include "history.h"
#include "mode-private.h"
Expand Down Expand Up @@ -130,6 +131,7 @@ typedef struct {
/* UID for the icon to display */
uint32_t icon_fetch_uid;
uint32_t icon_fetch_size;
guint icon_fetch_scale;
/* Type of desktop file */
DRunDesktopEntryType type;
} DRunModeEntry;
Expand Down Expand Up @@ -623,6 +625,7 @@ static void read_desktop_file(DRunModePrivateData *pd, const char *root,
pd->entry_list[pd->cmd_list_length].icon_size = 0;
pd->entry_list[pd->cmd_list_length].icon_fetch_uid = 0;
pd->entry_list[pd->cmd_list_length].icon_fetch_size = 0;
pd->entry_list[pd->cmd_list_length].icon_fetch_scale = 0;
pd->entry_list[pd->cmd_list_length].root = g_strdup(root);
pd->entry_list[pd->cmd_list_length].path = g_strdup(path);
pd->entry_list[pd->cmd_list_length].desktop_id = g_strdup(id);
Expand Down Expand Up @@ -1349,18 +1352,21 @@ static char *_get_display_value(const Mode *sw, unsigned int selected_line,
static cairo_surface_t *_get_icon(const Mode *sw, unsigned int selected_line,
unsigned int height) {
DRunModePrivateData *pd = (DRunModePrivateData *)mode_get_private_data(sw);
const guint scale = display_scale();
if (pd->file_complete) {
return pd->completer->_get_icon(pd->completer, selected_line, height);
}
g_return_val_if_fail(pd->entry_list != NULL, NULL);
DRunModeEntry *dr = &(pd->entry_list[selected_line]);
if (dr->icon_name != NULL) {
if (dr->icon_fetch_uid > 0 && dr->icon_fetch_size == height) {
if (dr->icon_fetch_uid > 0 && dr->icon_fetch_size == height &&
dr->icon_fetch_scale == scale) {
cairo_surface_t *icon = rofi_icon_fetcher_get(dr->icon_fetch_uid);
return icon;
}
dr->icon_fetch_uid = rofi_icon_fetcher_query(dr->icon_name, height);
dr->icon_fetch_size = height;
dr->icon_fetch_scale = scale;
cairo_surface_t *icon = rofi_icon_fetcher_get(dr->icon_fetch_uid);
return icon;
}
Expand Down
10 changes: 9 additions & 1 deletion source/modes/filebrowser.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <sys/stat.h>
#include <sys/types.h>

#include "display.h"
#include "helper.h"
#include "history.h"
#include "mode-private.h"
Expand Down Expand Up @@ -94,6 +95,7 @@ typedef struct {
enum FBFileType type;
uint32_t icon_fetch_uid;
uint32_t icon_fetch_size;
guint icon_fetch_scale;
gboolean link;
time_t time;
} FBFile;
Expand Down Expand Up @@ -249,6 +251,7 @@ static void get_file_browser(Mode *sw) {
pd->array[pd->array_length].type = UP;
pd->array[pd->array_length].icon_fetch_uid = 0;
pd->array[pd->array_length].icon_fetch_size = 0;
pd->array[pd->array_length].icon_fetch_scale = 0;
pd->array[pd->array_length].link = FALSE;
pd->array[pd->array_length].time = -1;
pd->array_length++;
Expand Down Expand Up @@ -284,6 +287,7 @@ static void get_file_browser(Mode *sw) {
(rd->d_type == DT_DIR) ? DIRECTORY : RFILE;
pd->array[pd->array_length].icon_fetch_uid = 0;
pd->array[pd->array_length].icon_fetch_size = 0;
pd->array[pd->array_length].icon_fetch_scale = 0;
pd->array[pd->array_length].link = FALSE;

if (file_browser_config.sorting_method == FB_SORT_TIME) {
Expand All @@ -304,6 +308,7 @@ static void get_file_browser(Mode *sw) {
g_build_filename(cdir, rd->d_name, NULL);
pd->array[pd->array_length].icon_fetch_uid = 0;
pd->array[pd->array_length].icon_fetch_size = 0;
pd->array[pd->array_length].icon_fetch_scale = 0;
pd->array[pd->array_length].link = TRUE;
// Default to file.
pd->array[pd->array_length].type = RFILE;
Expand Down Expand Up @@ -607,9 +612,11 @@ static cairo_surface_t *_get_icon(const Mode *sw, unsigned int selected_line,
unsigned int height) {
FileBrowserModePrivateData *pd =
(FileBrowserModePrivateData *)mode_get_private_data(sw);
const guint scale = display_scale();
g_return_val_if_fail(pd->array != NULL, NULL);
FBFile *dr = &(pd->array[selected_line]);
if (dr->icon_fetch_uid > 0 && dr->icon_fetch_size == height) {
if (dr->icon_fetch_uid > 0 && dr->icon_fetch_size == height &&
dr->icon_fetch_scale == scale) {
return rofi_icon_fetcher_get(dr->icon_fetch_uid);
}
if (rofi_icon_fetcher_file_is_image(dr->path)) {
Expand All @@ -618,6 +625,7 @@ static cairo_surface_t *_get_icon(const Mode *sw, unsigned int selected_line,
dr->icon_fetch_uid = rofi_icon_fetcher_query(icon_name[dr->type], height);
}
dr->icon_fetch_size = height;
dr->icon_fetch_scale = scale;
return rofi_icon_fetcher_get(dr->icon_fetch_uid);
}

Expand Down
11 changes: 9 additions & 2 deletions source/modes/recursivebrowser.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <sys/stat.h>
#include <sys/types.h>

#include "display.h"
#include "helper.h"
#include "history.h"
#include "mode-private.h"
Expand Down Expand Up @@ -73,6 +74,7 @@ typedef struct {
enum FBFileType type;
uint32_t icon_fetch_uid;
uint32_t icon_fetch_size;
guint icon_fetch_scale;
gboolean link;
time_t time;
} FBFile;
Expand Down Expand Up @@ -212,6 +214,7 @@ static void scan_dir(FileBrowserModePrivateData *pd, GFile *path) {
f->type = (rd->d_type == DT_DIR) ? DIRECTORY : RFILE;
f->icon_fetch_uid = 0;
f->icon_fetch_size = 0;
f->icon_fetch_scale = 0;
f->link = FALSE;

g_async_queue_push(pd->async_queue, f);
Expand All @@ -238,6 +241,7 @@ static void scan_dir(FileBrowserModePrivateData *pd, GFile *path) {
}
f->icon_fetch_uid = 0;
f->icon_fetch_size = 0;
f->icon_fetch_scale = 0;
f->link = TRUE;
// Default to file.
f->type = RFILE;
Expand Down Expand Up @@ -440,12 +444,14 @@ static int recursive_browser_token_match(const Mode *sw,
}

static cairo_surface_t *_get_icon(const Mode *sw, unsigned int selected_line,
unsigned int height, guint scale) {
unsigned int height) {
FileBrowserModePrivateData *pd =
(FileBrowserModePrivateData *)mode_get_private_data(sw);
const guint scale = display_scale();
g_return_val_if_fail(pd->array != NULL, NULL);
FBFile *dr = &(pd->array[selected_line]);
if (dr->icon_fetch_uid > 0 && dr->icon_fetch_size == height) {
if (dr->icon_fetch_uid > 0 && dr->icon_fetch_size == height &&
dr->icon_fetch_scale == scale) {
return rofi_icon_fetcher_get(dr->icon_fetch_uid);
}
if (rofi_icon_fetcher_file_is_image(dr->path)) {
Expand All @@ -455,6 +461,7 @@ static cairo_surface_t *_get_icon(const Mode *sw, unsigned int selected_line,
rofi_icon_fetcher_query(rb_icon_name[dr->type], height);
}
dr->icon_fetch_size = height;
dr->icon_fetch_scale = scale;
return rofi_icon_fetcher_get(dr->icon_fetch_uid);
}

Expand Down
7 changes: 6 additions & 1 deletion source/modes/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <sys/types.h>
#include <unistd.h>

#include "display.h"
#include "helper.h"
#include "history.h"
#include "modes/filebrowser.h"
Expand All @@ -66,6 +67,7 @@ typedef struct {
char *entry;
uint32_t icon_fetch_uid;
uint32_t icon_fetch_size;
guint icon_fetch_scale;
/* Surface holding the icon. */
cairo_surface_t *icon;
} RunEntry;
Expand Down Expand Up @@ -539,13 +541,15 @@ static char *run_get_message(const Mode *sw) {
static cairo_surface_t *_get_icon(const Mode *sw, unsigned int selected_line,
unsigned int height) {
RunModePrivateData *pd = (RunModePrivateData *)mode_get_private_data(sw);
const guint scale = display_scale();
if (pd->file_complete) {
return pd->completer->_get_icon(pd->completer, selected_line, height);
}
g_return_val_if_fail(pd->cmd_list != NULL, NULL);
RunEntry *dr = &(pd->cmd_list[selected_line]);

if (dr->icon_fetch_uid > 0 && dr->icon_fetch_size == height) {
if (dr->icon_fetch_uid > 0 && dr->icon_fetch_size == height &&
dr->icon_fetch_scale == scale) {
cairo_surface_t *icon = rofi_icon_fetcher_get(dr->icon_fetch_uid);
return icon;
}
Expand All @@ -554,6 +558,7 @@ static cairo_surface_t *_get_icon(const Mode *sw, unsigned int selected_line,
if (str) {
dr->icon_fetch_uid = rofi_icon_fetcher_query(str[0], height);
dr->icon_fetch_size = height;
dr->icon_fetch_scale = scale;
g_strfreev(str);
cairo_surface_t *icon = rofi_icon_fetcher_get(dr->icon_fetch_uid);
return icon;
Expand Down
7 changes: 6 additions & 1 deletion source/modes/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#define G_LOG_DOMAIN "Modes.Script"

#include "modes/script.h"
#include "display.h"
#include "helper.h"
#include "rofi.h"
#include <assert.h>
Expand Down Expand Up @@ -248,6 +249,7 @@ static DmenuScriptEntry *execute_executor(Mode *sw, char *arg,
retv[(*length)].urgent = FALSE;
retv[(*length)].icon_fetch_uid = 0;
retv[(*length)].icon_fetch_size = 0;
retv[(*length)].icon_fetch_scale = 0;
retv[(*length)].nonselectable = FALSE;
if (buf_length > 0 && (read_length > (ssize_t)buf_length)) {
dmenuscript_parse_entry_extras(sw, &(retv[(*length)]),
Expand Down Expand Up @@ -483,16 +485,19 @@ static cairo_surface_t *script_get_icon(const Mode *sw,
unsigned int height) {
ScriptModePrivateData *pd =
(ScriptModePrivateData *)mode_get_private_data(sw);
const guint scale = display_scale();
g_return_val_if_fail(pd->cmd_list != NULL, NULL);
DmenuScriptEntry *dr = &(pd->cmd_list[selected_line]);
if (dr->icon_name == NULL) {
return NULL;
}
if (dr->icon_fetch_uid > 0 && dr->icon_fetch_size == height) {
if (dr->icon_fetch_uid > 0 && dr->icon_fetch_size == height &&
dr->icon_fetch_scale == scale) {
return rofi_icon_fetcher_get(dr->icon_fetch_uid);
}
dr->icon_fetch_uid = rofi_icon_fetcher_query(dr->icon_name, height);
dr->icon_fetch_size = height;
dr->icon_fetch_scale = scale;
return rofi_icon_fetcher_get(dr->icon_fetch_uid);
}

Expand Down
7 changes: 6 additions & 1 deletion source/modes/wayland-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <glib.h>
#include <wayland-client.h>

#include "display.h"
#include "helper.h"
#include "modes/wayland-window.h"
#include "rofi.h"
Expand Down Expand Up @@ -95,6 +96,7 @@ typedef struct {

unsigned int cached_icon_uid;
unsigned int cached_icon_size;
guint cached_icon_scale;
} ForeignToplevelHandle;

static void foreign_toplevel_handle_free(ForeignToplevelHandle *self) {
Expand Down Expand Up @@ -574,6 +576,7 @@ static cairo_surface_t *_get_icon(const Mode *sw, unsigned int selected_line,
unsigned int height) {
WaylandWindowModePrivateData *pd =
(WaylandWindowModePrivateData *)mode_get_private_data(sw);
const guint scale = display_scale();

g_return_val_if_fail(pd != NULL, NULL);

Expand All @@ -586,7 +589,8 @@ static cairo_surface_t *_get_icon(const Mode *sw, unsigned int selected_line,
return NULL;
}

if (toplevel->cached_icon_uid > 0 && toplevel->cached_icon_size == height) {
if (toplevel->cached_icon_uid > 0 && toplevel->cached_icon_size == height &&
toplevel->cached_icon_scale == scale) {
return rofi_icon_fetcher_get(toplevel->cached_icon_uid);
}

Expand All @@ -597,6 +601,7 @@ static cairo_surface_t *_get_icon(const Mode *sw, unsigned int selected_line,
*/
gchar *app_id_lower = g_utf8_strdown(toplevel->app_id, -1);
toplevel->cached_icon_size = height;
toplevel->cached_icon_scale = scale;
toplevel->cached_icon_uid = rofi_icon_fetcher_query(app_id_lower, height);
g_free(app_id_lower);

Expand Down
Loading

0 comments on commit 36621af

Please sign in to comment.