[nautilus/wip/antoniof/gtk4-preparation-discontinued-api] toolbar: Use popovers for history menus



commit 9ff44ba0e73c80b0f86fc4788b4ec4077cd38e2a
Author: António Fernandes <antoniof gnome org>
Date:   Thu Dec 16 15:25:45 2021 +0000

    toolbar: Use popovers for history menus
    
    GtkMenu is gone in GTK 4.

 src/nautilus-bookmark.c              | 26 ----------
 src/nautilus-bookmark.h              |  5 +-
 src/nautilus-toolbar.c               | 94 +++++++++++-------------------------
 src/nautilus-window.c                | 30 +++++++++++-
 src/resources/ui/nautilus-toolbar.ui |  8 +++
 5 files changed, 64 insertions(+), 99 deletions(-)
---
diff --git a/src/nautilus-bookmark.c b/src/nautilus-bookmark.c
index f115f2487..bc3c3bea7 100644
--- a/src/nautilus-bookmark.c
+++ b/src/nautilus-bookmark.c
@@ -53,8 +53,6 @@ enum
     NUM_PROPERTIES
 };
 
-#define ELLIPSISED_MENU_ITEM_MIN_CHARS  32
-
 static GParamSpec *properties[NUM_PROPERTIES] = { NULL };
 static guint signals[LAST_SIGNAL];
 
@@ -802,30 +800,6 @@ nautilus_bookmark_new (GFile       *location,
     return new_bookmark;
 }
 
-/**
- * nautilus_bookmark_menu_item_new:
- *
- * Return a menu item representing a bookmark.
- * @bookmark: The bookmark the menu item represents.
- * Return value: A newly-created bookmark, not yet shown.
- **/
-GtkWidget *
-nautilus_bookmark_menu_item_new (NautilusBookmark *bookmark)
-{
-    GtkWidget *menu_item;
-    GtkLabel *label;
-    const char *name;
-
-    name = nautilus_bookmark_get_name (bookmark);
-    menu_item = gtk_menu_item_new_with_label (name);
-    label = GTK_LABEL (gtk_bin_get_child (GTK_BIN (menu_item)));
-    gtk_label_set_use_underline (label, FALSE);
-    gtk_label_set_ellipsize (label, PANGO_ELLIPSIZE_END);
-    gtk_label_set_max_width_chars (label, ELLIPSISED_MENU_ITEM_MIN_CHARS);
-
-    return menu_item;
-}
-
 void
 nautilus_bookmark_set_scroll_pos (NautilusBookmark *bookmark,
                                   const char       *uri)
diff --git a/src/nautilus-bookmark.h b/src/nautilus-bookmark.h
index ae03bbe88..6bf38526e 100644
--- a/src/nautilus-bookmark.h
+++ b/src/nautilus-bookmark.h
@@ -51,7 +51,4 @@ void                  nautilus_bookmark_set_scroll_pos         (NautilusBookmark
                                                                const char            *uri);
 char *                nautilus_bookmark_get_scroll_pos         (NautilusBookmark      *bookmark);
 
-/* Helper functions for displaying bookmarks */
-GtkWidget *           nautilus_bookmark_menu_item_new          (NautilusBookmark      *bookmark);
-
-G_END_DECLS
\ No newline at end of file
+G_END_DECLS
diff --git a/src/nautilus-toolbar.c b/src/nautilus-toolbar.c
index c6b927280..c8cc2265f 100644
--- a/src/nautilus-toolbar.c
+++ b/src/nautilus-toolbar.c
@@ -97,10 +97,12 @@ struct _NautilusToolbar
     GtkWidget *operations_icon;
 
     GtkWidget *forward_button;
+    GtkWidget *forward_menu;
     GtkGesture *forward_button_longpress_gesture;
     GtkGesture *forward_button_multi_press_gesture;
 
     GtkWidget *back_button;
+    GtkWidget *back_menu;
     GtkGesture *back_button_longpress_gesture;
     GtkGesture *back_button_multi_press_gesture;
 
@@ -158,42 +160,14 @@ toolbar_update_appearance (NautilusToolbar *self)
     }
 }
 
-static void
-activate_back_or_forward_menu_item (GtkMenuItem        *menu_item,
-                                    NautilusWindowSlot *window_slot,
-                                    gboolean            back)
-{
-    int index;
-
-    g_assert (GTK_IS_MENU_ITEM (menu_item));
-
-    index = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menu_item), "user_data"));
-
-    nautilus_window_slot_back_or_forward (window_slot, back, index);
-}
-
-static void
-activate_back_menu_item_callback (GtkMenuItem     *menu_item,
-                                  NautilusToolbar *self)
-{
-    activate_back_or_forward_menu_item (menu_item, self->window_slot, TRUE);
-}
-
-static void
-activate_forward_menu_item_callback (GtkMenuItem     *menu_item,
-                                     NautilusToolbar *self)
-{
-    activate_back_or_forward_menu_item (menu_item, self->window_slot, FALSE);
-}
-
 static void
 fill_menu (NautilusToolbar *self,
-           GtkWidget       *menu,
+           GMenu           *menu,
            gboolean         back)
 {
-    GtkWidget *menu_item;
-    int index;
+    guint index;
     GList *list;
+    const gchar *name;
 
     list = back ? nautilus_window_slot_get_back_history (self->window_slot) :
            nautilus_window_slot_get_forward_history (self->window_slot);
@@ -201,16 +175,15 @@ fill_menu (NautilusToolbar *self,
     index = 0;
     while (list != NULL)
     {
-        menu_item = nautilus_bookmark_menu_item_new (NAUTILUS_BOOKMARK (list->data));
-        g_object_set_data (G_OBJECT (menu_item), "user_data", GINT_TO_POINTER (index));
-        gtk_widget_show (GTK_WIDGET (menu_item));
-        g_signal_connect_object (menu_item, "activate",
-                                 back
-                                 ? G_CALLBACK (activate_back_menu_item_callback)
-                                 : G_CALLBACK (activate_forward_menu_item_callback),
-                                 self, 0);
-
-        gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
+        g_autoptr (GMenuItem) item = NULL;
+
+        name = nautilus_bookmark_get_name (NAUTILUS_BOOKMARK (list->data));
+        item = g_menu_item_new (name, NULL);
+        g_menu_item_set_action_and_target (item,
+                                           back ? "win.back-n" : "win.forward-n",
+                                           "u", index);
+        g_menu_append_item (menu, item);
+
         list = g_list_next (list);
         ++index;
     }
@@ -218,13 +191,13 @@ fill_menu (NautilusToolbar *self,
 
 static void
 show_menu (NautilusToolbar *self,
-           GtkWidget       *widget,
-           const GdkEvent  *event)
+           GtkWidget       *widget)
 {
-    GtkWidget *menu;
+    g_autoptr (GMenu) menu = NULL;
     NautilusNavigationDirection direction;
+    GtkPopover *popover;
 
-    menu = gtk_menu_new ();
+    menu = g_menu_new ();
 
     direction = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (widget),
                                                      "nav-direction"));
@@ -234,12 +207,14 @@ show_menu (NautilusToolbar *self,
         case NAUTILUS_NAVIGATION_DIRECTION_FORWARD:
         {
             fill_menu (self, menu, FALSE);
+            popover = GTK_POPOVER (self->forward_menu);
         }
         break;
 
         case NAUTILUS_NAVIGATION_DIRECTION_BACK:
         {
             fill_menu (self, menu, TRUE);
+            popover = GTK_POPOVER (self->back_menu);
         }
         break;
 
@@ -250,11 +225,8 @@ show_menu (NautilusToolbar *self,
         break;
     }
 
-    gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (self->window), NULL);
-    gtk_menu_popup_at_widget (GTK_MENU (menu), widget,
-                              GDK_GRAVITY_SOUTH_WEST,
-                              GDK_GRAVITY_NORTH_WEST,
-                              event);
+    gtk_popover_bind_model (popover, G_MENU_MODEL (menu), NULL);
+    gtk_popover_popup (popover);
 }
 
 static void
@@ -266,15 +238,11 @@ navigation_button_press_cb (GtkGestureMultiPress *gesture,
 {
     NautilusToolbar *self;
     GtkWidget *widget;
-    GdkEventSequence *sequence;
-    const GdkEvent *event;
 
     self = NAUTILUS_TOOLBAR (user_data);
     widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture));
-    sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
-    event = gtk_gesture_get_last_event (GTK_GESTURE (gesture), sequence);
 
-    show_menu (self, widget, event);
+    show_menu (self, widget);
 }
 
 static void
@@ -284,13 +252,8 @@ back_button_longpress_cb (GtkGestureLongPress *gesture,
                           gpointer             user_data)
 {
     NautilusToolbar *self = user_data;
-    GdkEventSequence *sequence;
-    const GdkEvent *event;
 
-    sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
-    event = gtk_gesture_get_last_event (GTK_GESTURE (gesture), sequence);
-
-    show_menu (self, self->back_button, event);
+    show_menu (self, self->back_button);
 }
 
 static void
@@ -300,13 +263,8 @@ forward_button_longpress_cb (GtkGestureLongPress *gesture,
                              gpointer             user_data)
 {
     NautilusToolbar *self = user_data;
-    GdkEventSequence *sequence;
-    const GdkEvent *event;
-
-    sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
-    event = gtk_gesture_get_last_event (GTK_GESTURE (gesture), sequence);
 
-    show_menu (self, self->forward_button, event);
+    show_menu (self, self->forward_button);
 }
 
 static gboolean
@@ -1234,7 +1192,9 @@ nautilus_toolbar_class_init (NautilusToolbarClass *klass)
     gtk_widget_class_bind_template_child (widget_class, NautilusToolbar, view_toggle_icon);
     gtk_widget_class_bind_template_child (widget_class, NautilusToolbar, app_menu);
     gtk_widget_class_bind_template_child (widget_class, NautilusToolbar, back_button);
+    gtk_widget_class_bind_template_child (widget_class, NautilusToolbar, back_menu);
     gtk_widget_class_bind_template_child (widget_class, NautilusToolbar, forward_button);
+    gtk_widget_class_bind_template_child (widget_class, NautilusToolbar, forward_menu);
     gtk_widget_class_bind_template_child (widget_class, NautilusToolbar, toolbar_switcher);
     gtk_widget_class_bind_template_child (widget_class, NautilusToolbar, search_container);
     gtk_widget_class_bind_template_child (widget_class, NautilusToolbar, path_bar_container);
diff --git a/src/nautilus-window.c b/src/nautilus-window.c
index ea55a18b4..b018b544f 100644
--- a/src/nautilus-window.c
+++ b/src/nautilus-window.c
@@ -291,6 +291,26 @@ action_forward (GSimpleAction *action,
     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,
@@ -1412,12 +1432,16 @@ nautilus_window_sync_location_widgets (NautilusWindow *window)
         nautilus_path_bar_set_path (NAUTILUS_PATH_BAR (path_bar), location);
     }
 
-    action = g_action_map_lookup_action (G_ACTION_MAP (window), "back");
     enabled = nautilus_window_slot_get_back_history (slot) != NULL;
+    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);
 
-    action = g_action_map_lookup_action (G_ACTION_MAP (window), "forward");
     enabled = nautilus_window_slot_get_forward_history (slot) != NULL;
+    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);
@@ -1916,6 +1940,8 @@ const GActionEntry win_entries[] =
 {
     { "back", action_back },
     { "forward", action_forward },
+    { "back-n", action_back_n, "u" },
+    { "forward-n", action_forward_n, "u" },
     { "up", action_up },
     { "view-menu", action_toggle_state_view_button, NULL, "false", NULL },
     { "reload", action_reload },
diff --git a/src/resources/ui/nautilus-toolbar.ui b/src/resources/ui/nautilus-toolbar.ui
index 359f80aec..e7b753b20 100644
--- a/src/resources/ui/nautilus-toolbar.ui
+++ b/src/resources/ui/nautilus-toolbar.ui
@@ -2,6 +2,14 @@
 <!-- Generated with glade 3.22.0 -->
 <interface>
   <requires lib="gtk+" version="3.22"/>
+  <object class="GtkPopover" id="back_menu">
+    <property name="position">bottom</property>
+    <property name="relative-to">back_button</property>
+  </object>
+  <object class="GtkPopover" id="forward_menu">
+    <property name="position">bottom</property>
+    <property name="relative-to">forward_button</property>
+  </object>
   <object class="GtkPopoverMenu" id="app_menu">
     <property name="can_focus">False</property>
     <child>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]