filechooser: show truncated names in a tooltip

This commit is contained in:
Daemonratte 2026-10-16 06:40:50 +02:00
commit a73f195c16
2 changed files with 131 additions and 16 deletions

View file

@ -942,6 +942,8 @@ gtk_file_chooser_default_finalize (GObject *object)
g_free (impl->edited_new_text);
if (impl->rename_file)
g_object_unref (impl->rename_file);
if (impl->tooltip_file)
g_object_unref (impl->tooltip_file);
G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
}
@ -5167,6 +5169,94 @@ file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
}
}
static PangoLayout *name_renderer_create_layout (GtkCellRenderer *cell,
GtkWidget *widget,
gint width);
static gint get_icon_view_item_width (GtkFileChooserDefault *impl);
/* Whether the name of @file is cut short at @width; the answer for the
* last name is kept, as tooltips ask at every motion while shown */
static gboolean
file_name_ellipsized (GtkFileChooserDefault *impl,
GtkWidget *view,
GFile *file,
const gchar *name,
gint width)
{
PangoLayout *layout;
if (impl->tooltip_file && g_file_equal (impl->tooltip_file, file) &&
impl->tooltip_width == width)
return impl->tooltip_ellipsized;
if (GTK_IS_ICON_VIEW (view))
{
g_object_set (impl->list_name_renderer, "text", name, NULL);
layout = name_renderer_create_layout (GTK_CELL_RENDERER (impl->list_name_renderer), view, width);
}
else
{
layout = gtk_widget_create_pango_layout (view, name);
pango_layout_set_width (layout, MAX (width, 1) * PANGO_SCALE);
pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
}
if (impl->tooltip_file)
g_object_unref (impl->tooltip_file);
impl->tooltip_file = g_object_ref (file);
impl->tooltip_width = width;
impl->tooltip_ellipsized = pango_layout_is_ellipsized (layout);
g_object_unref (layout);
return impl->tooltip_ellipsized;
}
/* Tooltips: in search and recent results, where the file is; elsewhere
* the name, if it is cut short */
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;
GFile *file;
gchar *name, *text = NULL;
if (!gtk_icon_view_get_tooltip_context (GTK_ICON_VIEW (widget), &x, &y, keyboard_tip,
&model, &path, &iter))
return FALSE;
gtk_tree_model_get (model, &iter, MODEL_COL_FILE, &file, MODEL_COL_NAME, &name, -1);
if (file && name)
{
if (impl->operation_mode != OPERATION_MODE_BROWSE)
text = g_file_get_parse_name (file);
else if (file_name_ellipsized (impl, widget, file, name, get_icon_view_item_width (impl)))
text = g_strdup (name);
}
if (text)
{
gtk_tooltip_set_text (tooltip, text);
gtk_icon_view_set_tooltip_item (GTK_ICON_VIEW (widget), tooltip, path);
}
g_free (text);
g_free (name);
if (file)
g_object_unref (file);
gtk_tree_path_free (path);
return text != NULL;
}
static gboolean
file_list_query_tooltip_cb (GtkWidget *widget,
gint x,
@ -5180,11 +5270,7 @@ file_list_query_tooltip_cb (GtkWidget *widget,
GtkTreePath *path;
GtkTreeIter iter;
GFile *file;
char *parse_name;
if (impl->operation_mode == OPERATION_MODE_BROWSE)
return FALSE;
gchar *name, *text = NULL;
if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
&x, &y,
@ -5194,25 +5280,45 @@ file_list_query_tooltip_cb (GtkWidget *widget,
gtk_tree_model_get (model, &iter,
MODEL_COL_FILE, &file,
MODEL_COL_NAME, &name,
-1);
if (file == NULL)
if (file && name)
{
gtk_tree_path_free (path);
return FALSE;
if (impl->operation_mode != OPERATION_MODE_BROWSE)
text = g_file_get_parse_name (file);
else
{
gint start, width, xpad;
if (gtk_tree_view_column_cell_get_position (impl->list_name_column,
GTK_CELL_RENDERER (impl->list_name_renderer),
&start, &width))
{
gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (impl->list_name_renderer), &xpad, NULL);
/* the column may be narrower than its cells want */
width = MIN (width, gtk_tree_view_column_get_width (impl->list_name_column) - start);
if (file_name_ellipsized (impl, widget, file, name, width - 2 * xpad))
text = g_strdup (name);
}
}
}
parse_name = g_file_get_parse_name (file);
gtk_tooltip_set_text (tooltip, parse_name);
gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
tooltip,
path);
if (text)
{
gtk_tooltip_set_text (tooltip, text);
gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
tooltip,
path);
}
g_free (parse_name);
g_object_unref (file);
g_free (text);
g_free (name);
if (file)
g_object_unref (file);
gtk_tree_path_free (path);
return TRUE;
return text != NULL;
}
static void
@ -5746,6 +5852,10 @@ create_browse_files_icon_view (GtkFileChooserDefault *impl)
impl->list_name_renderer = g_object_new (name_renderer_get_type (), NULL);
update_icon_view_cell_layout (impl);
gtk_widget_set_has_tooltip (impl->browse_files_icon_view, TRUE);
g_signal_connect (impl->browse_files_icon_view, "query-tooltip",
G_CALLBACK (file_icon_view_query_tooltip_cb), impl);
g_signal_connect (impl->list_name_renderer, "edited",
G_CALLBACK (renderer_edited_cb), impl);
g_signal_connect (impl->list_name_renderer, "editing-started",

View file

@ -310,6 +310,11 @@ struct _GtkFileChooserDefault
GtkFileSystemModel *rename_select_model;
gulong rename_select_handler;
/* the last name a tooltip was considered for */
GFile *tooltip_file;
gint tooltip_width;
guint tooltip_ellipsized : 1;
gulong settings_signal_id;
int list_view_icon_size;
int icon_view_icon_size;