gtkfilechooser: add cached icon view tooltips

This commit is contained in:
Daemonratte 2026-05-19 13:11:13 +02:00
commit dc0b67f1e2
2 changed files with 238 additions and 2 deletions

View file

@ -259,8 +259,8 @@ typedef enum {
#define ICON_VIEW_ITEM_WIDTH 128
#define COMPACT_VIEW_ITEM_WIDTH 180
#define COMPACT_VIEW_SPACING 4
#define COMPACT_VIEW_ROW_SPACING 0
#define COMPACT_VIEW_COLUMN_SPACING 12
#define COMPACT_VIEW_ROW_SPACING 1
#define COMPACT_VIEW_COLUMN_SPACING 20
#define COMPACT_VIEW_MARGIN 2
#define ICON_VIEW_SCALE_MAX_VISIBLE_OVERSCAN 128
@ -891,6 +891,16 @@ gtk_file_chooser_default_finalize (GObject *object)
icon_view_scale_remove_timeout (impl);
if (impl->icon_tooltip_path)
{
gtk_tree_path_free (impl->icon_tooltip_path);
impl->icon_tooltip_path = NULL;
}
g_free (impl->icon_tooltip_text);
impl->icon_tooltip_text = NULL;
if (impl->preview_panel_resize_timeout_id)
{
g_source_remove (impl->preview_panel_resize_timeout_id);
@ -5591,6 +5601,220 @@ file_list_query_tooltip_cb (GtkWidget *widget,
return TRUE;
}
static void
file_icon_view_tooltip_cache_clear (GtkFileChooserDefault *impl)
{
if (impl->icon_tooltip_path)
{
gtk_tree_path_free (impl->icon_tooltip_path);
impl->icon_tooltip_path = NULL;
}
g_free (impl->icon_tooltip_text);
impl->icon_tooltip_text = NULL;
impl->icon_tooltip_width = 0;
impl->icon_tooltip_visible = FALSE;
}
static gboolean
file_name_needs_tooltip (GtkFileChooserDefault *impl,
const gchar *name,
gint available_width)
{
PangoLayout *layout;
gint width;
if (name == NULL || available_width <= 0)
return FALSE;
layout = gtk_widget_create_pango_layout (impl->browse_files_current_view, name);
pango_layout_get_pixel_size (layout, &width, NULL);
g_object_unref (layout);
return width > available_width;
}
static gint
file_icon_view_get_tooltip_width (GtkFileChooserDefault *impl)
{
gint text_xpad = 0;
gint width;
if (impl->list_name_renderer)
gtk_cell_renderer_get_padding (impl->list_name_renderer, &text_xpad, NULL);
if (impl->view_mode == VIEW_MODE_COMPACT)
{
gint icon_size;
gint icon_xpad = 0;
gint icon_cell_width;
icon_size = impl->list_view_icon_size;
if (icon_size <= 0)
icon_size = FALLBACK_LIST_VIEW_ICON_SIZE;
if (impl->list_icon_renderer)
gtk_cell_renderer_get_padding (impl->list_icon_renderer, &icon_xpad, NULL);
icon_cell_width = icon_xpad * 2 + icon_size;
width = COMPACT_VIEW_ITEM_WIDTH - icon_cell_width - COMPACT_VIEW_SPACING;
}
else if (impl->view_mode == VIEW_MODE_ICON)
{
gint icon_size;
gint icon_xpad = 0;
gint item_width;
icon_size = impl->icon_view_icon_size;
if (icon_size <= 0)
icon_size = FALLBACK_ICON_VIEW_ICON_SIZE;
if (impl->list_icon_renderer)
gtk_cell_renderer_get_padding (impl->list_icon_renderer, &icon_xpad, NULL);
item_width = MAX (ICON_VIEW_ITEM_WIDTH, icon_size + icon_xpad * 2);
width = item_width;
}
else
return 0;
/*
* GtkCellRendererText still has its own padding and ellipsize threshold.
* Be conservative so that names shortened by only a few characters still
* get a tooltip.
*/
width -= text_xpad * 2;
width -= 12;
return MAX (1, width);
}
static gboolean
file_icon_view_tooltip_cache_matches (GtkFileChooserDefault *impl,
GtkTreePath *path,
gint available_width)
{
return impl->icon_tooltip_path &&
gtk_tree_path_compare (impl->icon_tooltip_path, path) == 0 &&
impl->icon_tooltip_width == available_width &&
impl->icon_tooltip_view_mode == impl->view_mode &&
impl->icon_tooltip_operation_mode == impl->operation_mode;
}
static void
file_icon_view_tooltip_cache_set (GtkFileChooserDefault *impl,
GtkTreePath *path,
gint available_width,
gboolean visible,
const gchar *tooltip_text)
{
file_icon_view_tooltip_cache_clear (impl);
impl->icon_tooltip_path = gtk_tree_path_copy (path);
impl->icon_tooltip_width = available_width;
impl->icon_tooltip_view_mode = impl->view_mode;
impl->icon_tooltip_operation_mode = impl->operation_mode;
impl->icon_tooltip_visible = visible;
if (tooltip_text)
impl->icon_tooltip_text = g_strdup (tooltip_text);
}
static gboolean
file_icon_view_query_tooltip_cb (GtkWidget *widget,
gint x,
gint y,
gboolean keyboard_tip,
GtkTooltip *tooltip,
gpointer user_data)
{
GtkFileChooserDefault *impl = user_data;
GtkTreeModel *model;
GtkTreePath *path;
GtkTreeIter iter;
gint available_width;
gchar *display_name;
gchar *tooltip_text;
if (!gtk_icon_view_get_tooltip_context (GTK_ICON_VIEW (impl->browse_files_icon_view),
&x, &y,
keyboard_tip,
&model, &path, &iter))
return FALSE;
available_width = file_icon_view_get_tooltip_width (impl);
if (file_icon_view_tooltip_cache_matches (impl, path, available_width))
{
if (!impl->icon_tooltip_visible)
{
gtk_tree_path_free (path);
return FALSE;
}
gtk_tooltip_set_text (tooltip, impl->icon_tooltip_text);
gtk_icon_view_set_tooltip_item (GTK_ICON_VIEW (impl->browse_files_icon_view),
tooltip,
path);
gtk_tree_path_free (path);
return TRUE;
}
gtk_tree_model_get (model, &iter,
MODEL_COL_NAME, &display_name,
-1);
if (!file_name_needs_tooltip (impl, display_name, available_width))
{
file_icon_view_tooltip_cache_set (impl, path, available_width, FALSE, NULL);
g_free (display_name);
gtk_tree_path_free (path);
return FALSE;
}
if (impl->operation_mode == OPERATION_MODE_BROWSE)
{
tooltip_text = display_name;
}
else
{
GFile *file = NULL;
gtk_tree_model_get (model, &iter,
MODEL_COL_FILE, &file,
-1);
if (file)
{
tooltip_text = g_file_get_parse_name (file);
g_object_unref (file);
g_free (display_name);
}
else
{
tooltip_text = display_name;
}
}
file_icon_view_tooltip_cache_set (impl, path, available_width, TRUE, tooltip_text);
gtk_tooltip_set_text (tooltip, tooltip_text);
gtk_icon_view_set_tooltip_item (GTK_ICON_VIEW (impl->browse_files_icon_view),
tooltip,
path);
g_free (tooltip_text);
gtk_tree_path_free (path);
return TRUE;
}
static void
set_icon_cell_renderer_fixed_size (GtkFileChooserDefault *impl,
GtkCellRenderer *renderer,
@ -5659,6 +5883,8 @@ update_icon_view_cell_layout (GtkFileChooserDefault *impl)
if (!impl->browse_files_icon_view)
return;
file_icon_view_tooltip_cache_clear (impl);
if (impl->view_mode == VIEW_MODE_COMPACT)
{
GtkIconView *icon_view;
@ -5887,6 +6113,10 @@ create_browse_files_icon_view (GtkFileChooserDefault *impl)
g_signal_connect (impl->browse_files_icon_view, "scroll-event",
G_CALLBACK (browse_files_icon_view_scroll_event_cb), impl);
g_object_set (impl->browse_files_icon_view, "has-tooltip", TRUE, NULL);
g_signal_connect (impl->browse_files_icon_view, "query-tooltip",
G_CALLBACK (file_icon_view_query_tooltip_cb), impl);
gtk_drag_dest_set (impl->browse_files_icon_view,
GTK_DEST_DEFAULT_ALL,
NULL, 0,

View file

@ -181,6 +181,12 @@ struct _GtkFileChooserDefault
GtkWidget *browse_files_scrolled_window;
GtkWidget *browse_files_current_view;
GtkWidget *browse_files_icon_view;
GtkTreePath *icon_tooltip_path;
gchar *icon_tooltip_text;
gint icon_tooltip_width;
ViewMode icon_tooltip_view_mode;
OperationMode icon_tooltip_operation_mode;
guint icon_tooltip_visible : 1;
GtkWidget *browse_files_popup_menu;
GtkWidget *browse_files_popup_menu_add_shortcut_item;
GtkWidget *browse_files_popup_menu_copy_location_item;