[nautilus/wip/antoniof/gtk4-preparation-discontinued-api: 1/19] window: Stop using GtkMenuShell




commit 8ac2b0aa7457fdfc906a52e6c904281347e970af
Author: António Fernandes <antoniof gnome org>
Date:   Thu Dec 16 12:57:10 2021 +0000

    window: Stop using GtkMenuShell
    
    It's gone in GTK 4.
    
    Port the tabs context menu to and GtkPopover and GMenuModel.

 src/nautilus-gtk4-helpers.c         |   9 +++
 src/nautilus-gtk4-helpers.h         |   2 +
 src/nautilus-window.c               | 111 +++++-------------------------------
 src/resources/ui/nautilus-window.ui |  28 +++++++++
 4 files changed, 54 insertions(+), 96 deletions(-)
---
diff --git a/src/nautilus-gtk4-helpers.c b/src/nautilus-gtk4-helpers.c
index 60da7672a..356410872 100644
--- a/src/nautilus-gtk4-helpers.c
+++ b/src/nautilus-gtk4-helpers.c
@@ -73,6 +73,15 @@ gtk_revealer_set_child (GtkRevealer *revealer,
     gtk_container_add (GTK_CONTAINER (revealer), child);
 }
 
+void
+gtk_popover_set_child (GtkPopover *popover,
+                       GtkWidget  *child)
+{
+    g_assert (GTK_IS_POPOVER (popover));
+
+    gtk_container_add (GTK_CONTAINER (popover), child);
+}
+
 GtkWidget *
 gtk_widget_get_first_child (GtkWidget *widget)
 {
diff --git a/src/nautilus-gtk4-helpers.h b/src/nautilus-gtk4-helpers.h
index 201183c15..7b91d14cd 100644
--- a/src/nautilus-gtk4-helpers.h
+++ b/src/nautilus-gtk4-helpers.h
@@ -23,6 +23,8 @@ void gtk_info_bar_add_child        (GtkInfoBar        *info_bar,
                                     GtkWidget         *widget);
 void gtk_revealer_set_child        (GtkRevealer       *revealer,
                                     GtkWidget         *child);
+void gtk_popover_set_child         (GtkPopover        *popover,
+                                    GtkWidget         *child);
 
 GtkWidget *gtk_widget_get_first_child (GtkWidget *widget);
 
diff --git a/src/nautilus-window.c b/src/nautilus-window.c
index f052de77b..19bc39147 100644
--- a/src/nautilus-window.c
+++ b/src/nautilus-window.c
@@ -146,6 +146,9 @@ struct _NautilusWindow
     guint sidebar_width_handler_id;
     gulong bookmarks_id;
 
+    GtkWidget *tab_menu;
+    GMenuModel *tab_menu_model;
+
     GQueue *tab_data_queue;
 
     GtkPadController *pad_controller;
@@ -1718,99 +1721,15 @@ path_bar_location_changed_callback (GtkWidget      *widget,
     nautilus_window_open_location_full (window, location, 0, NULL, NULL);
 }
 
-static void
-notebook_popup_menu_new_tab_cb (GtkMenuItem *menuitem,
-                                gpointer     user_data)
-{
-    NautilusWindow *window = user_data;
-
-    nautilus_window_new_tab (window);
-}
-
-static void
-notebook_popup_menu_move_left_cb (GtkMenuItem *menuitem,
-                                  gpointer     user_data)
-{
-    NautilusWindow *window = user_data;
-
-    nautilus_notebook_reorder_current_child_relative (NAUTILUS_NOTEBOOK (window->notebook), -1);
-}
-
-static void
-notebook_popup_menu_move_right_cb (GtkMenuItem *menuitem,
-                                   gpointer     user_data)
-{
-    NautilusWindow *window = user_data;
-
-
-    nautilus_notebook_reorder_current_child_relative (NAUTILUS_NOTEBOOK (window->notebook), 1);
-}
-
-static void
-notebook_popup_menu_close_cb (GtkMenuItem *menuitem,
-                              gpointer     user_data)
-{
-    NautilusWindow *window = user_data;
-    NautilusWindowSlot *slot;
-
-    slot = window->active_slot;
-    nautilus_window_slot_close (window, slot);
-}
-
 static void
 notebook_popup_menu_show (NautilusWindow *window,
-                          const GdkEvent *event)
+                          gdouble         x,
+                          gdouble         y)
 {
-    GtkWidget *popup;
-    GtkWidget *item;
-    gboolean can_move_left, can_move_right;
-    NautilusNotebook *notebook;
-
-    notebook = NAUTILUS_NOTEBOOK (window->notebook);
-
-    can_move_left = nautilus_notebook_can_reorder_current_child_relative (notebook, -1);
-    can_move_right = nautilus_notebook_can_reorder_current_child_relative (notebook, 1);
-
-    popup = gtk_menu_new ();
+    GtkPopover *popover = GTK_POPOVER (window->tab_menu);
 
-    item = gtk_menu_item_new_with_mnemonic (_("_New Tab"));
-    g_signal_connect (item, "activate",
-                      G_CALLBACK (notebook_popup_menu_new_tab_cb),
-                      window);
-    gtk_menu_shell_append (GTK_MENU_SHELL (popup),
-                           item);
-
-    gtk_menu_shell_append (GTK_MENU_SHELL (popup),
-                           gtk_separator_menu_item_new ());
-
-    item = gtk_menu_item_new_with_mnemonic (_("Move Tab _Left"));
-    g_signal_connect (item, "activate",
-                      G_CALLBACK (notebook_popup_menu_move_left_cb),
-                      window);
-    gtk_menu_shell_append (GTK_MENU_SHELL (popup),
-                           item);
-    gtk_widget_set_sensitive (item, can_move_left);
-
-    item = gtk_menu_item_new_with_mnemonic (_("Move Tab _Right"));
-    g_signal_connect (item, "activate",
-                      G_CALLBACK (notebook_popup_menu_move_right_cb),
-                      window);
-    gtk_menu_shell_append (GTK_MENU_SHELL (popup),
-                           item);
-    gtk_widget_set_sensitive (item, can_move_right);
-
-    gtk_menu_shell_append (GTK_MENU_SHELL (popup),
-                           gtk_separator_menu_item_new ());
-
-    item = gtk_menu_item_new_with_mnemonic (_("_Close Tab"));
-    g_signal_connect (item, "activate",
-                      G_CALLBACK (notebook_popup_menu_close_cb), window);
-    gtk_menu_shell_append (GTK_MENU_SHELL (popup),
-                           item);
-
-    gtk_widget_show_all (popup);
-
-    gtk_menu_popup_at_pointer (GTK_MENU (popup), event);
+    gtk_popover_set_pointing_to (popover, &(GdkRectangle){x, y, 0, 0});
+    gtk_popover_popup (popover);
 }
 
 /* emitted when the user clicks the "close" button of tabs */
@@ -1830,8 +1749,6 @@ notebook_button_press_cb (GtkGestureMultiPress *gesture,
                           gpointer              user_data)
 {
     NautilusWindow *window;
-    GdkEventSequence *sequence;
-    const GdkEvent *event;
 
     window = NAUTILUS_WINDOW (user_data);
 
@@ -1840,10 +1757,7 @@ notebook_button_press_cb (GtkGestureMultiPress *gesture,
         return;
     }
 
-    sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
-    event = gtk_gesture_get_last_event (GTK_GESTURE (gesture), sequence);
-
-    notebook_popup_menu_show (window, event);
+    notebook_popup_menu_show (window, x, y);
 }
 
 static gboolean
@@ -1851,7 +1765,7 @@ notebook_popup_menu_cb (GtkWidget *widget,
                         gpointer   user_data)
 {
     NautilusWindow *window = user_data;
-    notebook_popup_menu_show (window, NULL);
+    notebook_popup_menu_show (window, 0, 0);
     return TRUE;
 }
 
@@ -2660,6 +2574,9 @@ nautilus_window_init (NautilusWindow *window)
     g_type_ensure (NAUTILUS_TYPE_NOTEBOOK);
     gtk_widget_init_template (GTK_WIDGET (window));
 
+    gtk_popover_bind_model (GTK_POPOVER (window->tab_menu),
+                            window->tab_menu_model,
+                            NULL);
     g_signal_connect (window, "notify::is-maximized",
                       G_CALLBACK (on_is_maximized_changed), NULL);
 
@@ -2740,6 +2657,8 @@ nautilus_window_class_init (NautilusWindowClass *class)
     gtk_widget_class_bind_template_child (wclass, NautilusWindow, places_sidebar);
     gtk_widget_class_bind_template_child (wclass, NautilusWindow, main_view);
     gtk_widget_class_bind_template_child (wclass, NautilusWindow, notebook);
+    gtk_widget_class_bind_template_child (wclass, NautilusWindow, tab_menu);
+    gtk_widget_class_bind_template_child (wclass, NautilusWindow, tab_menu_model);
     gtk_widget_class_bind_template_child (wclass, NautilusWindow, in_app_notification_undo);
     gtk_widget_class_bind_template_child (wclass, NautilusWindow, in_app_notification_undo_label);
     gtk_widget_class_bind_template_child (wclass, NautilusWindow, in_app_notification_undo_undo_button);
diff --git a/src/resources/ui/nautilus-window.ui b/src/resources/ui/nautilus-window.ui
index 853a693ca..7b06434d3 100644
--- a/src/resources/ui/nautilus-window.ui
+++ b/src/resources/ui/nautilus-window.ui
@@ -1,5 +1,33 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
+  <object class="GtkPopover" id="tab_menu">
+    <property name="position">bottom</property>
+    <property name="relative-to">notebook</property>
+  </object>
+  <menu id="tab_menu_model">
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">_New Tab</attribute>
+        <attribute name="action">win.new-tab</attribute>
+      </item>
+    </section>
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">Move Tab _Left</attribute>
+        <attribute name="action">win.tab-move-left</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">Move Tab _Reft</attribute>
+        <attribute name="action">win.tab-move-right</attribute>
+      </item>
+    </section>
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">_Close Tab</attribute>
+        <attribute name="action">win.close-current-view</attribute>
+      </item>
+    </section>
+  </menu>
   <template class="NautilusWindow" parent="HdyApplicationWindow">
     <property name="show-menubar">False</property>
     <property name="title" translatable="yes">_Files</property>


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