filechooser: use fixed-height mode for the file list
This commit is contained in:
parent
e98b38f46e
commit
32175fcd80
1 changed files with 139 additions and 8 deletions
|
|
@ -4791,6 +4791,136 @@ update_icon_view_cell_layout (GtkFileChooserDefault *impl)
|
|||
}
|
||||
}
|
||||
|
||||
static char *my_g_format_time_for_display (gint64 secs);
|
||||
|
||||
static gchar *
|
||||
file_list_format_size (goffset size)
|
||||
{
|
||||
#if GLIB_CHECK_VERSION(2,30,0)
|
||||
return g_format_size (size);
|
||||
#else
|
||||
return g_format_size_for_display (size);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Widest pixel width of @texts rendered with @widget's font. */
|
||||
static gint
|
||||
file_list_measure_texts (GtkWidget *widget,
|
||||
GPtrArray *texts)
|
||||
{
|
||||
PangoLayout *layout;
|
||||
gint max_width = 0;
|
||||
guint i;
|
||||
|
||||
layout = gtk_widget_create_pango_layout (widget, NULL);
|
||||
|
||||
for (i = 0; i < texts->len; i++)
|
||||
{
|
||||
gint width;
|
||||
|
||||
pango_layout_set_text (layout, g_ptr_array_index (texts, i), -1);
|
||||
pango_layout_get_pixel_size (layout, &width, NULL);
|
||||
max_width = MAX (max_width, width);
|
||||
}
|
||||
|
||||
g_object_unref (layout);
|
||||
|
||||
return max_width;
|
||||
}
|
||||
|
||||
/* Gives @column a fixed width that fits its title and every string in
|
||||
* @texts, so the tree view never has to measure rows to size it. */
|
||||
static void
|
||||
file_list_set_fixed_column_width (GtkWidget *tree_view,
|
||||
GtkTreeViewColumn *column,
|
||||
GPtrArray *texts)
|
||||
{
|
||||
GPtrArray *title;
|
||||
gint text_width, title_width;
|
||||
gint focus_width, focus_pad, separator;
|
||||
|
||||
gtk_widget_style_get (tree_view,
|
||||
"focus-line-width", &focus_width,
|
||||
"focus-padding", &focus_pad,
|
||||
"horizontal-separator", &separator,
|
||||
NULL);
|
||||
|
||||
title = g_ptr_array_new ();
|
||||
g_ptr_array_add (title, (gpointer) gtk_tree_view_column_get_title (column));
|
||||
/* Leave room for the sort arrow next to the title. */
|
||||
title_width = file_list_measure_texts (tree_view, title) + 24;
|
||||
g_ptr_array_free (title, TRUE);
|
||||
|
||||
/* Cell renderer padding (xpad 2 on each side) plus tree view spacing. */
|
||||
text_width = file_list_measure_texts (tree_view, texts) +
|
||||
2 * 2 + 2 * (focus_width + focus_pad) + separator + 4;
|
||||
|
||||
gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
|
||||
gtk_tree_view_column_set_fixed_width (column, MAX (text_width, title_width));
|
||||
}
|
||||
|
||||
/* The file list uses fixed-height mode: all rows have the same height and
|
||||
* the columns fixed widths, so the tree view does not measure every row of
|
||||
* a large folder. The size and date columns get the width of the widest
|
||||
* strings they can show in this locale; the name column takes the rest. */
|
||||
static void
|
||||
file_list_update_fixed_sizing (GtkFileChooserDefault *impl)
|
||||
{
|
||||
GtkWidget *tree_view = impl->browse_files_tree_view;
|
||||
GPtrArray *texts;
|
||||
GDateTime *now;
|
||||
gint64 now_secs;
|
||||
gint i;
|
||||
|
||||
/* Size column: the longest strings g_format_size() gives per unit. */
|
||||
texts = g_ptr_array_new_with_free_func (g_free);
|
||||
g_ptr_array_add (texts, file_list_format_size (999));
|
||||
g_ptr_array_add (texts, file_list_format_size (999949));
|
||||
g_ptr_array_add (texts, file_list_format_size (G_GOFFSET_CONSTANT (999949999)));
|
||||
g_ptr_array_add (texts, file_list_format_size (G_GOFFSET_CONSTANT (999949999999)));
|
||||
g_ptr_array_add (texts, file_list_format_size (G_GOFFSET_CONSTANT (999949999999999)));
|
||||
file_list_set_fixed_column_width (tree_view, impl->list_size_column, texts);
|
||||
g_ptr_array_free (texts, TRUE);
|
||||
|
||||
/* Date column: today, yesterday, every weekday and one date per month,
|
||||
* formatted exactly like the column does. */
|
||||
texts = g_ptr_array_new_with_free_func (g_free);
|
||||
now = g_date_time_new_now_local ();
|
||||
now_secs = g_date_time_to_unix (now);
|
||||
for (i = 0; i < 7; i++)
|
||||
g_ptr_array_add (texts, my_g_format_time_for_display (now_secs - i * 24 * 3600));
|
||||
for (i = 1; i <= 12; i++)
|
||||
{
|
||||
GDateTime *date;
|
||||
|
||||
date = g_date_time_new_local (g_date_time_get_year (now) - 1, i, 28, 23, 59, 0);
|
||||
if (date)
|
||||
{
|
||||
g_ptr_array_add (texts, my_g_format_time_for_display (g_date_time_to_unix (date)));
|
||||
g_date_time_unref (date);
|
||||
}
|
||||
}
|
||||
g_ptr_array_add (texts, g_strdup (_("Unknown")));
|
||||
g_date_time_unref (now);
|
||||
file_list_set_fixed_column_width (tree_view, impl->list_mtime_column, texts);
|
||||
g_ptr_array_free (texts, TRUE);
|
||||
|
||||
gtk_tree_view_column_set_sizing (impl->list_name_column, GTK_TREE_VIEW_COLUMN_FIXED);
|
||||
if (gtk_tree_view_column_get_fixed_width (impl->list_name_column) <= 0)
|
||||
gtk_tree_view_column_set_fixed_width (impl->list_name_column, 200);
|
||||
|
||||
gtk_tree_view_set_fixed_height_mode (GTK_TREE_VIEW (tree_view), TRUE);
|
||||
}
|
||||
|
||||
/* The widths depend on the font. */
|
||||
static void
|
||||
file_list_style_set_cb (GtkWidget *widget,
|
||||
GtkStyle *previous_style,
|
||||
GtkFileChooserDefault *impl)
|
||||
{
|
||||
file_list_update_fixed_sizing (impl);
|
||||
}
|
||||
|
||||
/* Creates the list view */
|
||||
static GtkWidget *
|
||||
create_browse_files_tree_view (GtkFileChooserDefault *impl)
|
||||
|
|
@ -4883,9 +5013,10 @@ create_browse_files_tree_view (GtkFileChooserDefault *impl)
|
|||
|
||||
renderer = gtk_cell_renderer_text_new ();
|
||||
g_object_set (renderer,
|
||||
"alignment", PANGO_ALIGN_RIGHT,
|
||||
"xalign", 1.0,
|
||||
"ellipsize", PANGO_ELLIPSIZE_END,
|
||||
NULL);
|
||||
gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
|
||||
gtk_tree_view_column_pack_start (column, renderer, TRUE);
|
||||
gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
|
||||
impl->list_size_column = column;
|
||||
|
||||
|
|
@ -4896,6 +5027,7 @@ create_browse_files_tree_view (GtkFileChooserDefault *impl)
|
|||
gtk_tree_view_column_set_title (column, _("Modified"));
|
||||
|
||||
renderer = gtk_cell_renderer_text_new ();
|
||||
g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
|
||||
gtk_tree_view_column_pack_start (column, renderer, TRUE);
|
||||
gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
|
||||
impl->list_mtime_column = column;
|
||||
|
|
@ -4903,6 +5035,10 @@ create_browse_files_tree_view (GtkFileChooserDefault *impl)
|
|||
file_list_set_sort_column_ids (impl);
|
||||
update_cell_renderer_attributes (impl);
|
||||
|
||||
file_list_update_fixed_sizing (impl);
|
||||
g_signal_connect (impl->browse_files_tree_view, "style-set",
|
||||
G_CALLBACK (file_list_style_set_cb), impl);
|
||||
|
||||
return impl->browse_files_tree_view;
|
||||
}
|
||||
|
||||
|
|
@ -7443,7 +7579,6 @@ load_set_model (GtkFileChooserDefault *impl)
|
|||
|
||||
if (impl->view_mode == VIEW_MODE_LIST)
|
||||
{
|
||||
gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
|
||||
gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
|
||||
MODEL_COL_NAME);
|
||||
file_list_set_sort_column_ids (impl);
|
||||
|
|
@ -8059,11 +8194,7 @@ file_system_model_set (GtkFileSystemModel *model,
|
|||
if (info == NULL || _gtk_file_info_consider_as_directory (info))
|
||||
g_value_set_string (value, NULL);
|
||||
else
|
||||
#if GLIB_CHECK_VERSION(2,30,0)
|
||||
g_value_take_string (value, g_format_size (g_file_info_get_size (info)));
|
||||
#else
|
||||
g_value_take_string (value, g_format_size_for_display (g_file_info_get_size (info)));
|
||||
#endif
|
||||
g_value_take_string (value, file_list_format_size (g_file_info_get_size (info)));
|
||||
break;
|
||||
case MODEL_COL_MTIME:
|
||||
case MODEL_COL_MTIME_TEXT:
|
||||
|
|
|
|||
Loading…
Reference in a new issue