From 5fea2245f119029f6b2699dda11aaf8f5e461e4e Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch Date: Sun, 21 Nov 2021 20:46:50 +0100 Subject: Remove insanity --- fastiv-browser.c | 22 +++++++++++++--------- fastiv-io.c | 31 ++++++++++++------------------- fastiv-io.h | 16 +++++++++------- fastiv.c | 16 +++++----------- 4 files changed, 39 insertions(+), 46 deletions(-) diff --git a/fastiv-browser.c b/fastiv-browser.c index e025d66..09d7ab0 100644 --- a/fastiv-browser.c +++ b/fastiv-browser.c @@ -41,7 +41,8 @@ struct _FastivBrowser { GtkWidget parent_instance; - FastivIoThumbnailSize item_size; ///< Thumbnail height in pixels + FastivIoThumbnailSize item_size; ///< Thumbnail size + int item_height; ///< Thumbnail height in pixels GArray *entries; ///< [Entry] GArray *layouted_rows; ///< [Row] @@ -115,7 +116,7 @@ append_row(FastivBrowser *self, int *y, int x, GArray *items_array) })); // Not trying to pack them vertically, but this would be the place to do it. - *y += (int) self->item_size; + *y += self->item_height; *y += self->item_border_y; } @@ -206,7 +207,7 @@ item_extents(FastivBrowser *self, const Item *item, const Row *row) int height = cairo_image_surface_get_height(item->entry->thumbnail); return (GdkRectangle) { .x = row->x_offset + item->x_offset, - .y = row->y_offset + (int) self->item_size - height, + .y = row->y_offset + self->item_height - height, .width = width, .height = height, }; @@ -354,9 +355,10 @@ entry_add_thumbnail(gpointer data, gpointer user_data) if (self->thumbnail) cairo_surface_destroy(self->thumbnail); - FastivIoThumbnailSize size = FASTIV_BROWSER(user_data)->item_size; + FastivBrowser *browser = FASTIV_BROWSER(user_data); self->thumbnail = rescale_thumbnail( - fastiv_io_lookup_thumbnail(self->filename, size), (int) size); + fastiv_io_lookup_thumbnail(self->filename, browser->item_size), + browser->item_height); if (self->thumbnail) return; @@ -390,7 +392,7 @@ materialize_icon(FastivBrowser *self, Entry *entry) // TODO(p): Make sure we have /some/ icon for every entry. // TODO(p): We might want to populate these on an as-needed basis. GtkIconInfo *icon_info = gtk_icon_theme_lookup_by_gicon( - gtk_icon_theme_get_default(), entry->icon, (int) self->item_size / 2, + gtk_icon_theme_get_default(), entry->icon, self->item_height / 2, GTK_ICON_LOOKUP_FORCE_SYMBOLIC); if (!icon_info) return; @@ -401,7 +403,7 @@ materialize_icon(FastivBrowser *self, Entry *entry) GdkPixbuf *pixbuf = gtk_icon_info_load_symbolic( icon_info, &white, &white, &white, &white, NULL, NULL); if (pixbuf) { - int outer_size = (int) self->item_size; + int outer_size = self->item_height; entry->thumbnail = cairo_image_surface_create(CAIRO_FORMAT_A8, outer_size, outer_size); @@ -492,6 +494,7 @@ fastiv_browser_set_property( case PROP_THUMBNAIL_SIZE: if (g_value_get_enum(value) != (int) self->item_size) { self->item_size = g_value_get_enum(value); + self->item_height = fastiv_io_thumbnail_sizes[self->item_size].size; reload_thumbnails(self); } break; @@ -516,7 +519,7 @@ fastiv_browser_get_preferred_width( GtkBorder padding = {}; gtk_style_context_get_padding(style, GTK_STATE_FLAG_NORMAL, &padding); *minimum = *natural = - g_permitted_width_multiplier * (int) self->item_size + + g_permitted_width_multiplier * self->item_height + padding.left + 2 * self->item_border_x + padding.right; } @@ -594,7 +597,7 @@ fastiv_browser_draw(GtkWidget *widget, cairo_t *cr) .x = 0, .y = row->y_offset - self->item_border_y, .width = allocation.width, - .height = (int) self->item_size + 2 * self->item_border_y, + .height = self->item_height + 2 * self->item_border_y, }; if (!have_clip || gdk_rectangle_intersect(&clip, &extents, NULL)) draw_row(self, cr, row); @@ -755,6 +758,7 @@ fastiv_browser_init(FastivBrowser *self) g_array_set_clear_func(self->layouted_rows, (GDestroyNotify) row_free); self->item_size = FASTIV_IO_THUMBNAIL_SIZE_NORMAL; + self->item_height = fastiv_io_thumbnail_sizes[self->item_size].size; self->selected = -1; self->glow = cairo_image_surface_create(CAIRO_FORMAT_A1, 0, 0); diff --git a/fastiv-io.c b/fastiv-io.c index 60c0292..d9588cc 100644 --- a/fastiv-io.c +++ b/fastiv-io.c @@ -118,9 +118,10 @@ fastiv_io_thumbnail_size_get_type(void) return guard; } -#define XX(name, value, dir) {FASTIV_IO_THUMBNAIL_SIZE_ ## name, dir}, -FastivIoThumbnailSizeInfo fastiv_io_thumbnail_sizes[] = { - FASTIV_IO_THUMBNAIL_SIZES(XX) {}}; +#define XX(name, value, dir) {value, dir}, +FastivIoThumbnailSizeInfo + fastiv_io_thumbnail_sizes[FASTIV_IO_THUMBNAIL_SIZE_COUNT] = { + FASTIV_IO_THUMBNAIL_SIZES(XX)}; #undef XX // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1006,17 +1007,8 @@ fail_init: cairo_surface_t * fastiv_io_lookup_thumbnail(const gchar *target, FastivIoThumbnailSize size) { - // TODO(p): Consider having linear enum constants, - // and using a trivial lookup table. This is ridiculous. - int size_index = 0; - while (fastiv_io_thumbnail_sizes[size_index].size && - fastiv_io_thumbnail_sizes[size_index].size < size) - size_index++; - int size_count = size_index; - while (fastiv_io_thumbnail_sizes[size_count].size) - size_count++; - - g_return_val_if_fail(fastiv_io_thumbnail_sizes[size_index].size, NULL); + g_return_val_if_fail(size >= FASTIV_IO_THUMBNAIL_SIZE_MIN && + size <= FASTIV_IO_THUMBNAIL_SIZE_MAX, NULL); GStatBuf st; if (g_stat(target, &st)) @@ -1031,15 +1023,16 @@ fastiv_io_lookup_thumbnail(const gchar *target, FastivIoThumbnailSize size) gchar *sum = g_compute_checksum_for_string(G_CHECKSUM_MD5, uri, -1); gchar *cache_dir = get_xdg_home_dir("XDG_CACHE_HOME", ".cache"); + // The lookup sequence is: nominal..max, then mirroring back to ..min. cairo_surface_t *result = NULL; GError *error = NULL; - for (int i = 0; i < size_count; i++) { - int size_use = size_index + i; - if (size_use >= size_count) - size_use = size_count - i - 1; + for (int i = 0; i < FASTIV_IO_THUMBNAIL_SIZE_COUNT; i++) { + int use = size + i; + if (use > FASTIV_IO_THUMBNAIL_SIZE_MAX) + use = FASTIV_IO_THUMBNAIL_SIZE_MAX - i; gchar *path = g_strdup_printf("%s/thumbnails/%s/%s.png", cache_dir, - fastiv_io_thumbnail_sizes[size_use].directory_name, sum); + fastiv_io_thumbnail_sizes[use].thumbnail_spec_name, sum); result = read_spng_thumbnail(path, uri, st.st_mtim.tv_sec, &error); if (error) { g_debug("%s: %s", path, error->message); diff --git a/fastiv-io.h b/fastiv-io.h index 101db6c..5ee0f5b 100644 --- a/fastiv-io.h +++ b/fastiv-io.h @@ -32,23 +32,25 @@ typedef enum _FastivIoThumbnailSize { XX(NORMAL, 256, "large") \ XX(LARGE, 512, "x-large") \ XX(HUGE, 1024, "xx-large") -#define XX(name, value, dir) FASTIV_IO_THUMBNAIL_SIZE_ ## name = value, +#define XX(name, value, dir) FASTIV_IO_THUMBNAIL_SIZE_ ## name, FASTIV_IO_THUMBNAIL_SIZES(XX) #undef XX - FASTIV_IO_THUMBNAIL_SIZE_MIN = FASTIV_IO_THUMBNAIL_SIZE_SMALL, - FASTIV_IO_THUMBNAIL_SIZE_MAX = FASTIV_IO_THUMBNAIL_SIZE_HUGE + FASTIV_IO_THUMBNAIL_SIZE_COUNT, + + FASTIV_IO_THUMBNAIL_SIZE_MIN = 0, + FASTIV_IO_THUMBNAIL_SIZE_MAX = FASTIV_IO_THUMBNAIL_SIZE_COUNT - 1 } FastivIoThumbnailSize; GType fastiv_io_thumbnail_size_get_type(void) G_GNUC_CONST; #define FASTIV_TYPE_IO_THUMBNAIL_SIZE (fastiv_io_thumbnail_size_get_type()) typedef struct _FastivIoThumbnailSizeInfo { - FastivIoThumbnailSize size; ///< Nominal size in pixels - const char *directory_name; ///< thumbnail-spec directory name + int size; ///< Nominal size in pixels + const char *thumbnail_spec_name; ///< thumbnail-spec directory name } FastivIoThumbnailSizeInfo; -// The array is null-terminated. -extern FastivIoThumbnailSizeInfo fastiv_io_thumbnail_sizes[]; +extern FastivIoThumbnailSizeInfo + fastiv_io_thumbnail_sizes[FASTIV_IO_THUMBNAIL_SIZE_COUNT]; cairo_surface_t *fastiv_io_open(const gchar *path, GError **error); cairo_surface_t *fastiv_io_open_from_data( diff --git a/fastiv.c b/fastiv.c index bfc8432..f7e174f 100644 --- a/fastiv.c +++ b/fastiv.c @@ -335,20 +335,14 @@ on_open_location(G_GNUC_UNUSED GtkPlacesSidebar *sidebar, GFile *location, static void on_toolbar_zoom(G_GNUC_UNUSED GtkButton *button, gpointer user_data) { - FastivIoThumbnailSize size = FASTIV_IO_THUMBNAIL_SIZE_MIN; + FastivIoThumbnailSize size = FASTIV_IO_THUMBNAIL_SIZE_COUNT; g_object_get(g.browser, "thumbnail-size", &size, NULL); - gintptr position = -1, count = 0, adjustment = (gintptr) user_data; - while (fastiv_io_thumbnail_sizes[count].size) { - if (fastiv_io_thumbnail_sizes[count].size == size) - position = count; - count++; - } + size += (gintptr) user_data; + g_return_if_fail(size >= FASTIV_IO_THUMBNAIL_SIZE_MIN && + size <= FASTIV_IO_THUMBNAIL_SIZE_MAX); - position += adjustment; - g_return_if_fail(position >= 0 && position < count); - g_object_set(g.browser, "thumbnail-size", - fastiv_io_thumbnail_sizes[position].size, NULL); + g_object_set(g.browser, "thumbnail-size", size, NULL); } static void -- cgit v1.2.3-70-g09d2