From 9dbe8569c3dc210a7f5bbf2a1b429b51a41dfaf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Fernandes?= Date: Sun, 5 May 2024 22:52:51 +0100 Subject: [PATCH] window-slot: Don't add accels to application We've been using GtkApplication, through a NautilusApplication method, to set accelerators (i.e., global shortctus which trigger regardless of where the focus is on the application window). This will not work if NautilusWindowSlot is added to a window which is nor a GtkApplicationWindow instance, as will be the case for the upcoming FileChooser window. Let's instead add them to a shortcut controller in the slow, but with a "managed" scope which allows it to trigger even if the focus is not on the slot. Part of: https://gitlab.gnome.org/GNOME/nautilus/-/work_items/3411 --- src/nautilus-window-slot.c | 49 +++++++++++++++++-------------- src/resources/gtk/help-overlay.ui | 2 +- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/nautilus-window-slot.c b/src/nautilus-window-slot.c index 53be012c6..cee6bf25f 100644 --- a/src/nautilus-window-slot.c +++ b/src/nautilus-window-slot.c @@ -80,6 +80,7 @@ struct _NautilusWindowSlot /* Slot actions */ GActionGroup *slot_action_group; + GtkShortcutController *shortcuts; /* Current location. */ GFile *location; @@ -1129,16 +1130,6 @@ recursive_search_preferences_changed (GSettings *settings, static void nautilus_window_slot_init (NautilusWindowSlot *self) { - GApplication *app; - const gchar *search_visible_accels[] = - { - "f", - "Search", - NULL - }; - - app = g_application_get_default (); - g_signal_connect_object (nautilus_preferences, "changed::recursive-search", G_CALLBACK (recursive_search_preferences_changed), @@ -1153,20 +1144,34 @@ nautilus_window_slot_init (NautilusWindowSlot *self) "slot", G_ACTION_GROUP (self->slot_action_group)); -#define ACCELS(...) ((const char *[]) { __VA_ARGS__, NULL }) + self->shortcuts = GTK_SHORTCUT_CONTROLLER (gtk_shortcut_controller_new ()); - nautilus_application_set_accelerator (app, - "slot.files-view-mode(uint32 " G_STRINGIFY (NAUTILUS_VIEW_LIST_ID) ")", - "1"); - nautilus_application_set_accelerator (app, - "slot.files-view-mode(uint32 " G_STRINGIFY (NAUTILUS_VIEW_GRID_ID) ")", - "2"); - nautilus_application_set_accelerators (app, "slot.focus-search", search_visible_accels); - nautilus_application_set_accelerator (app, "slot.search-global", "f"); - nautilus_application_set_accelerators (app, "slot.reload", ACCELS ("F5", "r", "Refresh", "Reload")); - nautilus_application_set_accelerator (app, "slot.stop", "Stop"); + gtk_shortcut_controller_set_scope (self->shortcuts, GTK_SHORTCUT_SCOPE_MANAGED); + gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (self->shortcuts)); -#undef ACCELS + g_autoptr (GtkShortcutAction) view_mode_action = gtk_named_action_new ("slot.files-view-mode"); + GtkShortcut *shortcut; + + shortcut = gtk_shortcut_new_with_arguments (gtk_shortcut_trigger_parse_string ("1"), + g_object_ref (view_mode_action), + "u", NAUTILUS_VIEW_LIST_ID); + gtk_shortcut_controller_add_shortcut (self->shortcuts, shortcut); + shortcut = gtk_shortcut_new_with_arguments (gtk_shortcut_trigger_parse_string ("2"), + g_object_ref (view_mode_action), + "u", NAUTILUS_VIEW_GRID_ID); + gtk_shortcut_controller_add_shortcut (self->shortcuts, shortcut); + +#define ADD_SHORTCUT_FOR_ACTION(controller, action, trigger) \ + (gtk_shortcut_controller_add_shortcut ((controller), \ + gtk_shortcut_new (gtk_shortcut_trigger_parse_string ((trigger)), \ + gtk_named_action_new ((action))))) + + ADD_SHORTCUT_FOR_ACTION (self->shortcuts, "slot.focus-search", "f|Search"); + ADD_SHORTCUT_FOR_ACTION (self->shortcuts, "slot.search-global", "f"); + ADD_SHORTCUT_FOR_ACTION (self->shortcuts, "slot.reload", "F5|r|Refresh|Reload"); + ADD_SHORTCUT_FOR_ACTION (self->shortcuts, "slot.stop", "Stop"); + +#undef ADD_SHORTCUT_FOR_ACTION self->fd_holder = nautilus_fd_holder_new (); self->view_mode_before_network = NAUTILUS_VIEW_INVALID_ID; diff --git a/src/resources/gtk/help-overlay.ui b/src/resources/gtk/help-overlay.ui index 17236176f..4408f286f 100644 --- a/src/resources/gtk/help-overlay.ui +++ b/src/resources/gtk/help-overlay.ui @@ -30,7 +30,7 @@ Search Everywhere - slot.search-global + <Primary><Shift>F