file chooser: Back and Forward through the folders shown, as in file managers: buttons before the view, Alt+Left and Alt+Right, the Back and Forward keys, Command+[ and ] on the Mac, the back and forward buttons of mice and so the swipes of touchpads

This commit is contained in:
Daemonratte 2026-10-18 15:49:27 +02:00
commit 3022ee44f3
No known key found for this signature in database
3 changed files with 257 additions and 1 deletions

View file

@ -174,6 +174,8 @@ enum {
SHOW_HIDDEN,
SEARCH_SHORTCUT,
RECENT_SHORTCUT,
BACK_FOLDER,
FORWARD_FOLDER,
LAST_SIGNAL
};
@ -413,6 +415,13 @@ static void location_popup_handler (GtkFileChooserDefault *impl,
static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
static void up_folder_handler (GtkFileChooserDefault *impl);
static void back_folder_handler (GtkFileChooserDefault *impl);
static gboolean history_button_press_cb (GtkWidget *widget,
GdkEventButton *event,
GtkFileChooserDefault *impl);
static void history_buttons_create (GtkFileChooserDefault *impl,
GtkWidget *box);
static void forward_folder_handler (GtkFileChooserDefault *impl);
static void down_folder_handler (GtkFileChooserDefault *impl);
static void home_folder_handler (GtkFileChooserDefault *impl);
static void desktop_folder_handler (GtkFileChooserDefault *impl);
@ -743,8 +752,39 @@ _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
_gtk_marshal_VOID__VOID,
G_TYPE_NONE, 0);
signals[BACK_FOLDER] =
g_signal_new_class_handler (I_("back-folder"),
G_OBJECT_CLASS_TYPE (class),
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
G_CALLBACK (back_folder_handler),
NULL, NULL,
_gtk_marshal_VOID__VOID,
G_TYPE_NONE, 0);
signals[FORWARD_FOLDER] =
g_signal_new_class_handler (I_("forward-folder"),
G_OBJECT_CLASS_TYPE (class),
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
G_CALLBACK (forward_folder_handler),
NULL, NULL,
_gtk_marshal_VOID__VOID,
G_TYPE_NONE, 0);
binding_set = gtk_binding_set_by_class (class);
/* Back and Forward as in file managers and browsers; the keys of
* keyboards that have them; Command+[ and ] as in the Finder */
gtk_binding_entry_add_signal (binding_set, GDK_KEY_Left, GDK_MOD1_MASK, "back-folder", 0);
gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Left, GDK_MOD1_MASK, "back-folder", 0);
gtk_binding_entry_add_signal (binding_set, GDK_KEY_Back, 0, "back-folder", 0);
gtk_binding_entry_add_signal (binding_set, GDK_KEY_Right, GDK_MOD1_MASK, "forward-folder", 0);
gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Right, GDK_MOD1_MASK, "forward-folder", 0);
gtk_binding_entry_add_signal (binding_set, GDK_KEY_Forward, 0, "forward-folder", 0);
#ifdef GDK_WINDOWING_QUARTZ
gtk_binding_entry_add_signal (binding_set, GDK_KEY_bracketleft, GDK_MOD2_MASK, "back-folder", 0);
gtk_binding_entry_add_signal (binding_set, GDK_KEY_bracketright, GDK_MOD2_MASK, "forward-folder", 0);
#endif
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_l, GDK_CONTROL_MASK,
"location-toggle-popup",
@ -5037,6 +5077,8 @@ shortcuts_list_create (GtkFileChooserDefault *impl)
g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
G_CALLBACK (shortcuts_popup_menu_cb), impl);
g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
G_CALLBACK (history_button_press_cb), impl);
g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
G_CALLBACK (shortcuts_button_press_event_cb), impl);
/* Accessible object name for the file chooser's shortcuts pane */
@ -7030,6 +7072,8 @@ create_browse_files_tree_view (GtkFileChooserDefault *impl)
G_CALLBACK (browse_files_key_press_event_cb), impl);
g_signal_connect (impl->browse_files_tree_view, "popup-menu",
G_CALLBACK (list_popup_menu_cb), impl);
g_signal_connect (impl->browse_files_tree_view, "button-press-event",
G_CALLBACK (history_button_press_cb), impl);
g_signal_connect (impl->browse_files_tree_view, "button-press-event",
G_CALLBACK (list_button_press_event_cb), impl);
@ -7305,6 +7349,8 @@ create_browse_files_icon_view (GtkFileChooserDefault *impl)
G_CALLBACK (icons_queue_update), impl);
g_signal_connect (impl->browse_files_icon_view, "popup-menu",
G_CALLBACK (list_popup_menu_cb), impl);
g_signal_connect (impl->browse_files_icon_view, "button-press-event",
G_CALLBACK (history_button_press_cb), impl);
g_signal_connect (impl->browse_files_icon_view, "button-press-event",
G_CALLBACK (list_button_press_event_cb), impl);
gtk_widget_add_events (impl->browse_files_icon_view, GDK_SCROLL_MASK);
@ -8509,6 +8555,9 @@ path_bar_widgets_create (GtkFileChooserDefault *impl)
impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
/* Back and Forward */
history_buttons_create (impl, impl->browse_path_bar_hbox);
/* View mode combo box */
view_mode_combo_box_create (impl);
gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->view_mode_combo_box, FALSE, FALSE, 0);
@ -8686,6 +8735,10 @@ gtk_file_chooser_default_constructor (GType type,
gtk_widget_pop_composite_child ();
update_appearance (impl);
/* presses elsewhere in the file chooser come up to here */
g_signal_connect (impl, "button-press-event",
G_CALLBACK (history_button_press_cb), impl);
profile_end ("end", NULL);
return object;
@ -9437,6 +9490,16 @@ gtk_file_chooser_default_dispose (GObject *object)
remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
g_list_free_full (impl->history_back, g_object_unref);
impl->history_back = NULL;
g_list_free_full (impl->history_forward, g_object_unref);
impl->history_forward = NULL;
if (impl->history_target)
{
g_object_unref (impl->history_target);
impl->history_target = NULL;
}
G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
}
@ -11660,6 +11723,180 @@ location_entry_follows_folder (GtkFileChooserDefault *impl,
compare_utf8_filenames (impl->browse_files_last_selected_name, text) == 0));
}
/* Back and Forward: the folders shown before, as in file managers */
#define HISTORY_LENGTH 50
static void
history_update_buttons (GtkFileChooserDefault *impl)
{
if (impl->history_back_button)
gtk_widget_set_sensitive (impl->history_back_button, impl->history_back != NULL);
if (impl->history_forward_button)
gtk_widget_set_sensitive (impl->history_forward_button, impl->history_forward != NULL);
}
static GList *
history_push (GList *list,
GFile *file)
{
GList *last;
list = g_list_prepend (list, g_object_ref (file));
if (g_list_length (list) > HISTORY_LENGTH)
{
last = g_list_last (list);
g_object_unref (last->data);
list = g_list_delete_link (list, last);
}
return list;
}
/* The folder changes from @old to @new: Back gets @old, and Forward is
* cleared, except when the change is Back or Forward itself */
static void
history_add (GtkFileChooserDefault *impl,
GFile *old,
GFile *new)
{
gint step = impl->history_step;
gboolean expected = impl->history_target && g_file_equal (impl->history_target, new);
impl->history_step = 0;
if (impl->history_target)
{
g_object_unref (impl->history_target);
impl->history_target = NULL;
}
if (g_file_equal (old, new))
return;
if (step < 0 && expected && impl->history_back)
{
g_object_unref (impl->history_back->data);
impl->history_back = g_list_delete_link (impl->history_back, impl->history_back);
impl->history_forward = history_push (impl->history_forward, old);
}
else if (step > 0 && expected && impl->history_forward)
{
g_object_unref (impl->history_forward->data);
impl->history_forward = g_list_delete_link (impl->history_forward, impl->history_forward);
impl->history_back = history_push (impl->history_back, old);
}
else
{
impl->history_back = history_push (impl->history_back, old);
g_list_free_full (impl->history_forward, g_object_unref);
impl->history_forward = NULL;
}
history_update_buttons (impl);
}
static void
history_go (GtkFileChooserDefault *impl,
gint step)
{
GList *list = step < 0 ? impl->history_back : impl->history_forward;
if (list == NULL)
{
gtk_widget_error_bell (GTK_WIDGET (impl));
return;
}
if (impl->history_target)
g_object_unref (impl->history_target);
impl->history_target = g_object_ref (list->data);
impl->history_step = step;
/* a folder that is gone is taken off, so that the next one is there */
if (!change_folder_and_display_error (impl, list->data, FALSE))
{
if (step < 0)
{
g_object_unref (impl->history_back->data);
impl->history_back = g_list_delete_link (impl->history_back, impl->history_back);
}
else
{
g_object_unref (impl->history_forward->data);
impl->history_forward = g_list_delete_link (impl->history_forward, impl->history_forward);
}
history_update_buttons (impl);
}
}
static void
back_folder_handler (GtkFileChooserDefault *impl)
{
history_go (impl, -1);
}
static void
forward_folder_handler (GtkFileChooserDefault *impl)
{
history_go (impl, 1);
}
/* The back and forward buttons of mice, and the swipes of touchpads,
* which GDK gives as these buttons; for a press anywhere in the file
* chooser (the views pass theirs on, too) */
static gboolean
history_button_press_cb (GtkWidget *widget,
GdkEventButton *event,
GtkFileChooserDefault *impl)
{
/* the back and forward buttons of mice: 8 and 9 on X11, 4 and 5 on
* Windows and the Mac, where the wheel does not press buttons */
#if defined (G_OS_WIN32) || defined (GDK_WINDOWING_QUARTZ)
const guint back = 4, forward = 5;
#else
const guint back = 8, forward = 9;
#endif
if (event->type != GDK_BUTTON_PRESS || (event->button != back && event->button != forward))
return FALSE;
history_go (impl, event->button == back ? -1 : 1);
return TRUE;
}
static void
history_buttons_create (GtkFileChooserDefault *impl,
GtkWidget *box)
{
GtkWidget *hbox, *image;
hbox = gtk_hbox_new (FALSE, 0);
image = gtk_image_new_from_stock (GTK_STOCK_GO_BACK, GTK_ICON_SIZE_BUTTON);
impl->history_back_button = gtk_button_new ();
gtk_button_set_image (GTK_BUTTON (impl->history_back_button), image);
gtk_widget_set_tooltip_text (impl->history_back_button, _("Back"));
atk_object_set_name (gtk_widget_get_accessible (impl->history_back_button), _("Back"));
gtk_button_set_focus_on_click (GTK_BUTTON (impl->history_back_button), FALSE);
g_signal_connect_swapped (impl->history_back_button, "clicked",
G_CALLBACK (back_folder_handler), impl);
gtk_box_pack_start (GTK_BOX (hbox), impl->history_back_button, FALSE, FALSE, 0);
image = gtk_image_new_from_stock (GTK_STOCK_GO_FORWARD, GTK_ICON_SIZE_BUTTON);
impl->history_forward_button = gtk_button_new ();
gtk_button_set_image (GTK_BUTTON (impl->history_forward_button), image);
gtk_widget_set_tooltip_text (impl->history_forward_button, _("Forward"));
atk_object_set_name (gtk_widget_get_accessible (impl->history_forward_button), _("Forward"));
gtk_button_set_focus_on_click (GTK_BUTTON (impl->history_forward_button), FALSE);
g_signal_connect_swapped (impl->history_forward_button, "clicked",
G_CALLBACK (forward_folder_handler), impl);
gtk_box_pack_start (GTK_BOX (hbox), impl->history_forward_button, FALSE, FALSE, 0);
gtk_size_group_add_widget (impl->browse_path_bar_size_group, hbox);
gtk_widget_show_all (hbox);
gtk_box_pack_start (GTK_BOX (box), hbox, FALSE, FALSE, 0);
history_update_buttons (impl);
}
static void
update_current_folder_get_info_cb (GCancellable *cancellable,
GFileInfo *info,
@ -11773,7 +12010,10 @@ update_current_folder_get_info_cb (GCancellable *cancellable,
if (impl->current_folder != data->file)
{
if (impl->current_folder)
g_object_unref (impl->current_folder);
{
history_add (impl, impl->current_folder, data->file);
g_object_unref (impl->current_folder);
}
impl->current_folder = g_object_ref (data->file);
}

View file

@ -259,6 +259,14 @@ struct _GtkFileChooserDefault
ViewMode view_mode;
GtkWidget *location_button;
GtkWidget *history_back_button;
GtkWidget *history_forward_button;
/* the folders that were shown, most recent first */
GList *history_back;
GList *history_forward;
GFile *history_target; /* where Back or Forward is going */
gint history_step; /* -1 back, 1 forward, 0 other changes */
GtkWidget *location_entry_box;
GtkWidget *location_label;
GtkWidget *location_entry;

View file

@ -6294,3 +6294,11 @@ msgstr "Nach _Größe sortieren"
#: gtk/gtkfilechooserdefault.c
msgid "Sort by Modification _Date"
msgstr "Nach Än_derungsdatum sortieren"
#: gtk/gtkfilechooserdefault.c
msgid "Back"
msgstr "Zurück"
#: gtk/gtkfilechooserdefault.c
msgid "Forward"
msgstr "Vor"