[nautilus/gtk4-preparation-trunk: 11/50] window: Stop using GtkMenuShell




commit 7bbbe9a5002af5add72bd438e85c0a44bc8ce91a
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               | 112 ++++++------------------------------
 src/resources/ui/nautilus-window.ui |  28 +++++++++
 4 files changed, 55 insertions(+), 96 deletions(-)
---
diff --git a/src/nautilus-gtk4-helpers.c b/src/nautilus-gtk4-helpers.c
index 6dde5152f..926dca8e3 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 52038d360..bea0892be 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 6ed00bab6..74b263294 100644
--- a/src/nautilus-window.c
+++ b/src/nautilus-window.c
@@ -148,6 +148,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;
@@ -1733,99 +1736,15 @@ on_path_bar_open_location (NautilusWindow    *window,
     nautilus_window_open_location_full (window, location, open_flags, 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);
+    GtkPopover *popover = GTK_POPOVER (window->tab_menu);
 
-    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 ();
-
-    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 */
@@ -1845,8 +1764,6 @@ notebook_button_press_cb (GtkGestureMultiPress *gesture,
                           gpointer              user_data)
 {
     NautilusWindow *window;
-    GdkEventSequence *sequence;
-    const GdkEvent *event;
 
     window = NAUTILUS_WINDOW (user_data);
 
@@ -1855,10 +1772,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
@@ -1866,7 +1780,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;
 }
 
@@ -2673,6 +2587,10 @@ 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);
+
     window->places_sidebar = nautilus_gtk_places_sidebar_new ();
     g_object_set (window->places_sidebar,
                   "vexpand", TRUE,
@@ -2773,6 +2691,8 @@ nautilus_window_class_init (NautilusWindowClass *class)
     gtk_widget_class_bind_template_child (wclass, NautilusWindow, 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 472ee7fce..d16e6dcf3 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]