1
0
mirror of https://gitlab.gnome.org/GNOME/nautilus synced 2024-06-30 23:46:35 +00:00

window-slot: Import back/forward actions

This will allow them to be used by the upcoming FileChooser window.

Part of https://gitlab.gnome.org/GNOME/nautilus/-/work_items/3413
This commit is contained in:
António Fernandes 2024-05-05 23:56:02 +01:00
parent 13ae2e6fd3
commit 2d6f4b8280
4 changed files with 103 additions and 78 deletions

View File

@ -148,6 +148,12 @@ G_DEFINE_TYPE (NautilusWindowSlot, nautilus_window_slot, ADW_TYPE_BIN);
static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
static const GtkPadActionEntry pad_actions[] =
{
{ GTK_PAD_ACTION_BUTTON, 4, -1, N_("Back"), "back" },
{ GTK_PAD_ACTION_BUTTON, 5, -1, N_("Forward"), "forward" },
};
static void nautilus_window_slot_force_reload (NautilusWindowSlot *self);
static void change_view (NautilusWindowSlot *self);
static void nautilus_window_slot_connect_new_content_view (NautilusWindowSlot *self);
@ -875,6 +881,67 @@ nautilus_window_slot_constructed (GObject *object)
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TITLE]);
}
static void
update_back_forward_actions (NautilusWindowSlot *self)
{
GAction *action;
gboolean enabled;
enabled = (nautilus_window_slot_get_back_history (self) != NULL &&
!nautilus_window_slot_get_search_global (self));
action = g_action_map_lookup_action (G_ACTION_MAP (self->slot_action_group), "back");
g_simple_action_set_enabled (G_SIMPLE_ACTION (action), enabled);
action = g_action_map_lookup_action (G_ACTION_MAP (self->slot_action_group), "back-n");
g_simple_action_set_enabled (G_SIMPLE_ACTION (action), enabled);
enabled = (nautilus_window_slot_get_forward_history (self) != NULL &&
!nautilus_window_slot_get_search_global (self));
action = g_action_map_lookup_action (G_ACTION_MAP (self->slot_action_group), "forward");
g_simple_action_set_enabled (G_SIMPLE_ACTION (action), enabled);
action = g_action_map_lookup_action (G_ACTION_MAP (self->slot_action_group), "forward-n");
g_simple_action_set_enabled (G_SIMPLE_ACTION (action), enabled);
}
static void
action_back (GSimpleAction *action,
GVariant *state,
gpointer user_data)
{
NautilusWindowSlot *self = NAUTILUS_WINDOW_SLOT (user_data);
nautilus_window_slot_back_or_forward (self, TRUE, 0);
}
static void
action_forward (GSimpleAction *action,
GVariant *state,
gpointer user_data)
{
NautilusWindowSlot *self = NAUTILUS_WINDOW_SLOT (user_data);
nautilus_window_slot_back_or_forward (self, FALSE, 0);
}
static void
action_back_n (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
NautilusWindowSlot *self = NAUTILUS_WINDOW_SLOT (user_data);
nautilus_window_slot_back_or_forward (self, TRUE, g_variant_get_uint32 (parameter));
}
static void
action_forward_n (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
NautilusWindowSlot *self = NAUTILUS_WINDOW_SLOT (user_data);
nautilus_window_slot_back_or_forward (self, FALSE, g_variant_get_uint32 (parameter));
}
static void
action_focus_search (GSimpleAction *action,
GVariant *state,
@ -969,6 +1036,8 @@ action_search_global (GSimpleAction *action,
nautilus_window_slot_set_search_visible (self, search_global);
gtk_widget_set_visible (GTK_WIDGET (self->banner), !search_global);
update_back_forward_actions (self);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SEARCH_GLOBAL]);
}
}
@ -1059,6 +1128,10 @@ action_stop (GSimpleAction *action,
const GActionEntry slot_entries[] =
{
{ .name = "back", .activate = action_back },
{ .name = "forward", .activate = action_forward },
{ .name = "back-n", .activate = action_back_n, .parameter_type = "u" },
{ .name = "forward-n", .activate = action_forward_n, .parameter_type = "u" },
{
.name = "files-view-mode", .parameter_type = "u",
.state = "uint32 " G_STRINGIFY (NAUTILUS_VIEW_INVALID_ID),
@ -1127,6 +1200,22 @@ recursive_search_preferences_changed (GSettings *settings,
update_search_information (self);
}
static void
set_back_forward_accelerators (NautilusWindowSlot *self)
{
gboolean ltr = (gtk_widget_get_direction (GTK_WIDGET (self)) == GTK_TEXT_DIR_LTR);
#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.back", ltr ? "<alt>Left|Back" : "<alt>Right|Back");
ADD_SHORTCUT_FOR_ACTION (self->shortcuts, "slot.forward", ltr ? "<alt>Right|Forward" : "<alt>Left|Forward");
#undef ADD_SHORTCUT_FOR_ACTION
}
static void
nautilus_window_slot_init (NautilusWindowSlot *self)
{
@ -1173,6 +1262,14 @@ nautilus_window_slot_init (NautilusWindowSlot *self)
#undef ADD_SHORTCUT_FOR_ACTION
set_back_forward_accelerators (self);
g_signal_connect_swapped (self, "direction-changed",
G_CALLBACK (set_back_forward_accelerators), self);
GtkPadController *pad_controller = gtk_pad_controller_new (G_ACTION_GROUP (self->slot_action_group), NULL);
gtk_pad_controller_set_action_entries (pad_controller, pad_actions, G_N_ELEMENTS (pad_actions));
gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (pad_controller));
self->fd_holder = nautilus_fd_holder_new ();
self->view_mode_before_network = NAUTILUS_VIEW_INVALID_ID;
}
@ -1404,6 +1501,7 @@ nautilus_window_slot_set_location (NautilusWindowSlot *self,
}
nautilus_fd_holder_set_location (self->fd_holder, self->location);
update_back_forward_actions (self);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_LOCATION]);
}

View File

@ -160,8 +160,7 @@ static const GtkPadActionEntry pad_actions[] =
{ GTK_PAD_ACTION_BUTTON, 1, -1, N_("Home"), "go-home" },
{ GTK_PAD_ACTION_BUTTON, 2, -1, N_("New tab"), "new-tab" },
{ GTK_PAD_ACTION_BUTTON, 3, -1, N_("Close current view"), "close-current-view" },
{ GTK_PAD_ACTION_BUTTON, 4, -1, N_("Back"), "back" },
{ GTK_PAD_ACTION_BUTTON, 5, -1, N_("Forward"), "forward" },
/* Button number sequence continues in window-slot.c */
};
static AdwTabPage *
@ -259,42 +258,6 @@ action_down (GSimpleAction *action,
}
}
static void
action_back (GSimpleAction *action,
GVariant *state,
gpointer user_data)
{
nautilus_window_back_or_forward (NAUTILUS_WINDOW (user_data), TRUE, 0);
}
static void
action_forward (GSimpleAction *action,
GVariant *state,
gpointer user_data)
{
nautilus_window_back_or_forward (NAUTILUS_WINDOW (user_data), FALSE, 0);
}
static void
action_back_n (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
nautilus_window_back_or_forward (NAUTILUS_WINDOW (user_data),
TRUE,
g_variant_get_uint32 (parameter));
}
static void
action_forward_n (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
nautilus_window_back_or_forward (NAUTILUS_WINDOW (user_data),
FALSE,
g_variant_get_uint32 (parameter));
}
static void
action_bookmark_current_location (GSimpleAction *action,
GVariant *state,
@ -518,8 +481,6 @@ on_slot_search_global_changed (NautilusWindowSlot *slot,
{
nautilus_gtk_places_sidebar_set_location (sidebar, nautilus_window_slot_get_location (slot));
}
nautilus_window_sync_location_widgets (self);
}
static void
@ -1125,8 +1086,6 @@ nautilus_window_sync_location_widgets (NautilusWindow *window)
{
NautilusWindowSlot *slot = window->active_slot;
GFile *location;
GAction *action;
gboolean enabled;
/* This function can only be called when there is a slot. */
g_assert (slot != NULL);
@ -1149,20 +1108,6 @@ nautilus_window_sync_location_widgets (NautilusWindow *window)
g_file_has_uri_scheme (location, SCHEME_NETWORK_VIEW));
}
enabled = (nautilus_window_slot_get_back_history (slot) != NULL &&
!nautilus_window_slot_get_search_global (slot));
action = g_action_map_lookup_action (G_ACTION_MAP (window), "back");
g_simple_action_set_enabled (G_SIMPLE_ACTION (action), enabled);
action = g_action_map_lookup_action (G_ACTION_MAP (window), "back-n");
g_simple_action_set_enabled (G_SIMPLE_ACTION (action), enabled);
enabled = (nautilus_window_slot_get_forward_history (slot) != NULL &&
!nautilus_window_slot_get_search_global (slot));
action = g_action_map_lookup_action (G_ACTION_MAP (window), "forward");
g_simple_action_set_enabled (G_SIMPLE_ACTION (action), enabled);
action = g_action_map_lookup_action (G_ACTION_MAP (window), "forward-n");
g_simple_action_set_enabled (G_SIMPLE_ACTION (action), enabled);
nautilus_window_sync_bookmarks (window);
nautilus_window_sync_starred (window);
}
@ -1585,10 +1530,6 @@ extra_drag_drop_cb (AdwTabBar *self,
const GActionEntry win_entries[] =
{
{ .name = "back", .activate = action_back },
{ .name = "forward", .activate = action_forward },
{ .name = "back-n", .activate = action_back_n, .parameter_type = "u" },
{ .name = "forward-n", .activate = action_forward_n, .parameter_type = "u" },
{ .name = "up", .activate = action_up },
{ .name = "down", .activate = action_down },
{ .name = "current-location-menu", .activate = action_show_current_location_menu },
@ -1615,18 +1556,6 @@ const GActionEntry win_entries[] =
{ .name = "toggle-sidebar", .activate = action_toggle_sidebar },
};
static void
window_set_back_forward_accelerators (void)
{
GApplication *app = g_application_get_default ();
gboolean ltr = (gtk_widget_get_default_direction () == GTK_TEXT_DIR_LTR);
#define ACCELS(...) ((const char *[]) { __VA_ARGS__, NULL })
nautilus_application_set_accelerators (app, "win.back", ACCELS (ltr ? "<alt>Left" : "<alt>Right", "Back"));
nautilus_application_set_accelerators (app, "win.forward", ACCELS (ltr ? "<alt>Right" : "<alt>Left", "Forward"));
}
static void
nautilus_window_initialize_actions (NautilusWindow *window)
{
@ -1640,8 +1569,9 @@ nautilus_window_initialize_actions (NautilusWindow *window)
win_entries, G_N_ELEMENTS (win_entries),
window);
#define ACCELS(...) ((const char *[]) { __VA_ARGS__, NULL })
app = g_application_get_default ();
window_set_back_forward_accelerators ();
nautilus_application_set_accelerators (app, "win.enter-location", ACCELS ("<control>l", "Go", "OpenURL"));
nautilus_application_set_accelerator (app, "win.new-tab", "<control>t");
nautilus_application_set_accelerator (app, "win.close-current-view", "<control>w");
@ -2315,8 +2245,6 @@ nautilus_window_class_init (NautilusWindowClass *class)
gtk_widget_class_bind_template_child (wclass, NautilusWindow, tab_bar);
gtk_widget_class_bind_template_child (wclass, NautilusWindow, network_address_bar);
gtk_widget_class_bind_template_callback (wclass, window_set_back_forward_accelerators);
signals[SLOT_ADDED] =
g_signal_new ("slot-added",
G_TYPE_FROM_CLASS (class),

View File

@ -10,7 +10,7 @@
<object class="GtkButton" id="back_button">
<property name="tooltip_text" translatable="yes">Back</property>
<property name="valign">center</property>
<property name="action_name">win.back</property>
<property name="action_name">slot.back</property>
<property name="icon_name">go-previous-symbolic</property>
</object>
</child>
@ -18,7 +18,7 @@
<object class="GtkButton" id="forward_button">
<property name="tooltip_text" translatable="yes">Forward</property>
<property name="valign">center</property>
<property name="action_name">win.forward</property>
<property name="action_name">slot.forward</property>
<property name="icon_name">go-next-symbolic</property>
</object>
</child>

View File

@ -107,7 +107,6 @@
<lookup name="active-slot">NautilusWindow</lookup>
</lookup>
</binding>
<signal name="direction-changed" handler="window_set_back_forward_accelerators"/>
<style>
<class name="view"/>
</style>