[nautilus/wip/antoniof/switch-to-gtk4: 9/40] general: Adapt to popover API changes




commit 3f52cc13d1ca70dbb7fb2aaceda7973fee29f8c5
Author: António Fernandes <antoniof gnome org>
Date:   Mon Dec 20 19:22:49 2021 +0000

    general: Adapt to popover API changes
    
    GMenuModel-bound popovers become GtkPopoverMenu and have a parent
    instead of an attach widget. If the parent doesn't have built-in
    popover support (such as GtkMenuButton), then we must manage the
    popover lifecycle explicitly and call gtk_popover_present() when
    moving parent.

 src/nautilus-files-view.c                     | 23 ++++++++++++++--------
 src/nautilus-list-view.c                      |  7 ++++++-
 src/nautilus-pathbar.c                        | 28 ++++++++++++++++-----------
 src/nautilus-rename-file-popover-controller.c |  5 ++---
 src/nautilus-toolbar.c                        | 17 +++++++++++-----
 src/nautilus-window.c                         |  8 +++++---
 src/resources/ui/nautilus-toolbar.ui          |  4 ++--
 src/resources/ui/nautilus-window.ui           |  2 +-
 8 files changed, 60 insertions(+), 34 deletions(-)
---
diff --git a/src/nautilus-files-view.c b/src/nautilus-files-view.c
index 4437a25b3..d43aa26a7 100644
--- a/src/nautilus-files-view.c
+++ b/src/nautilus-files-view.c
@@ -3190,6 +3190,9 @@ nautilus_files_view_dispose (GObject *object)
     priv->in_destruction = TRUE;
     nautilus_files_view_stop_loading (view);
 
+    g_clear_pointer (&priv->selection_menu, gtk_widget_unparent);
+    g_clear_pointer (&priv->background_menu, gtk_widget_unparent);
+
     if (priv->model)
     {
         nautilus_directory_unref (priv->model);
@@ -8268,12 +8271,13 @@ nautilus_files_view_pop_up_selection_context_menu  (NautilusFilesView *view,
 
     if (NULL == priv->selection_menu)
     {
-        priv->selection_menu = gtk_popover_new (GTK_WIDGET (view));
+        priv->selection_menu = gtk_popover_menu_new_from_model (NULL);
+        gtk_widget_set_parent (priv->selection_menu, GTK_WIDGET (view));
+        g_signal_connect (priv->selection_menu, "destroy", G_CALLBACK (gtk_widget_unparent), NULL);
     }
 
-    gtk_popover_bind_model (GTK_POPOVER (priv->selection_menu),
-                            G_MENU_MODEL (priv->selection_menu_model),
-                            NULL);
+    gtk_popover_menu_set_menu_model (GTK_POPOVER_MENU (priv->selection_menu),
+                                     G_MENU_MODEL (priv->selection_menu_model));
     if (x == -1 && y == -1)
     {
         /* If triggered from the keyboard, popup at selection, not pointer */
@@ -8315,13 +8319,16 @@ nautilus_files_view_pop_up_background_context_menu (NautilusFilesView *view,
      */
     update_context_menus_if_pending (view);
 
+
     if (NULL == priv->background_menu)
     {
-        priv->background_menu = gtk_popover_new (GTK_WIDGET (view));
+        priv->background_menu = gtk_popover_menu_new_from_model (NULL);
+        gtk_widget_set_parent (priv->background_menu, GTK_WIDGET (view));
+        g_signal_connect (priv->background_menu, "destroy", G_CALLBACK (gtk_widget_unparent), NULL);
     }
-    gtk_popover_bind_model (GTK_POPOVER (priv->background_menu),
-                            G_MENU_MODEL (priv->background_menu_model),
-                            NULL);
+    gtk_popover_menu_set_menu_model (GTK_POPOVER_MENU (priv->background_menu),
+                                     G_MENU_MODEL (priv->background_menu_model));
+
     gtk_popover_set_pointing_to (GTK_POPOVER (priv->background_menu),
                                  &(GdkRectangle){x, y, 0, 0});
     gtk_popover_popup (GTK_POPOVER (priv->background_menu));
diff --git a/src/nautilus-list-view.c b/src/nautilus-list-view.c
index 5309f1dfd..ee96ba626 100644
--- a/src/nautilus-list-view.c
+++ b/src/nautilus-list-view.c
@@ -3581,6 +3581,7 @@ nautilus_list_view_dispose (GObject *object)
     g_clear_object (&list_view->details->motion_controller);
     g_clear_object (&list_view->details->key_controller);
     g_clear_object (&list_view->details->long_press_gesture);
+    g_clear_pointer (&list_view->details->columns_popover, gtk_widget_unparent);
 
     G_OBJECT_CLASS (nautilus_list_view_parent_class)->dispose (object);
 }
@@ -4007,7 +4008,11 @@ nautilus_list_view_init (NautilusListView *list_view)
     gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (list_view)),
                                  "nautilus-list-view");
 
-    list_view->details->columns_popover = gtk_popover_new (GTK_WIDGET (list_view));
+    list_view->details->columns_popover = gtk_popover_new ();
+    gtk_widget_set_parent (list_view->details->columns_popover,
+                           GTK_WIDGET (list_view));
+    g_signal_connect (list_view->details->columns_popover, "destroy", G_CALLBACK (gtk_widget_unparent), 
NULL);
+
     list_view->details->columns_popover_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
     gtk_widget_set_margin_top (list_view->details->columns_popover_box, 6);
     gtk_widget_set_margin_bottom (list_view->details->columns_popover_box, 6);
diff --git a/src/nautilus-pathbar.c b/src/nautilus-pathbar.c
index f7e2e895a..b7fe3370a 100644
--- a/src/nautilus-pathbar.c
+++ b/src/nautilus-pathbar.c
@@ -98,7 +98,7 @@ struct _NautilusPathBar
     GActionGroup *action_group;
 
     NautilusFile *context_menu_file;
-    GtkPopover *current_view_menu_popover;
+    GtkPopoverMenu *current_view_menu_popover;
     GtkWidget *current_view_menu_button;
     GtkWidget *button_menu_popover;
     GMenu *current_view_menu;
@@ -213,9 +213,8 @@ on_adjustment_changed (GtkAdjustment *adjustment)
 static gboolean
 bind_current_view_menu_model_to_popover (NautilusPathBar *self)
 {
-    gtk_popover_bind_model (self->current_view_menu_popover,
-                            G_MENU_MODEL (self->current_view_menu),
-                            NULL);
+    gtk_popover_menu_set_menu_model (self->current_view_menu_popover,
+                                     G_MENU_MODEL (self->current_view_menu));
     return G_SOURCE_REMOVE;
 }
 
@@ -257,8 +256,8 @@ nautilus_path_bar_init (NautilusPathBar *self)
         g_error ("Failed to add pathbar-context-menu.ui: %s", error->message);
     }
     self->button_menu = g_object_ref_sink (G_MENU (gtk_builder_get_object (builder, "button-menu")));
-    self->button_menu_popover = g_object_ref_sink (gtk_popover_new_from_model (NULL,
-                                                                               G_MENU_MODEL 
(self->button_menu)));
+    self->button_menu_popover = gtk_popover_menu_new_from_model (G_MENU_MODEL (self->button_menu));
+    gtk_widget_set_parent (self->button_menu_popover, GTK_WIDGET (self));
 
     /* Add current location menu, which matches the view's background context menu */
     gtk_builder_add_from_resource (builder,
@@ -271,7 +270,7 @@ nautilus_path_bar_init (NautilusPathBar *self)
     self->current_view_menu = g_object_ref_sink (G_MENU (gtk_builder_get_object (builder, 
"background-menu")));
     self->extensions_section = g_object_ref (G_MENU (gtk_builder_get_object (builder, 
"background-extensions-section")));
     self->templates_submenu = g_object_ref (G_MENU (gtk_builder_get_object (builder, "templates-submenu")));
-    self->current_view_menu_popover = g_object_ref_sink (GTK_POPOVER (gtk_popover_new (NULL)));
+    self->current_view_menu_popover = g_object_ref_sink (GTK_POPOVER_MENU (gtk_popover_menu_new_from_model 
(NULL)));
 
     g_object_unref (builder);
 
@@ -309,7 +308,7 @@ nautilus_path_bar_finalize (GObject *object)
     g_clear_object (&self->extensions_section);
     g_clear_object (&self->templates_submenu);
     g_clear_object (&self->button_menu);
-    g_clear_object (&self->button_menu_popover);
+    g_clear_pointer (&self->button_menu_popover, gtk_widget_unparent);
     g_clear_object (&self->current_view_menu_popover);
 
     unschedule_pop_up_context_menu (NAUTILUS_PATH_BAR (object));
@@ -424,7 +423,7 @@ nautilus_path_bar_set_templates_menu (NautilusPathBar *self,
          * templates menu is set. Unbinding the model is the only way to clear
          * all children. After that's done, on idle, we rebind it.
          * See https://gitlab.gnome.org/GNOME/nautilus/-/issues/1705 */
-        gtk_popover_bind_model (self->current_view_menu_popover, NULL, NULL);
+        gtk_popover_menu_set_menu_model (self->current_view_menu_popover, NULL);
     }
 
     nautilus_gmenu_set_from_model (self->templates_submenu, menu);
@@ -601,8 +600,15 @@ on_multi_press_gesture_pressed (GtkGestureMultiPress *gesture,
             }
             else
             {
-                gtk_popover_set_relative_to (GTK_POPOVER (self->button_menu_popover),
-                                             button_data->button);
+                /* Hold a reference to keep the popover from destroying itself
+                 * when unparented. */
+                g_object_ref (self->button_menu_popover);
+                gtk_widget_unparent (self->button_menu_popover);
+                gtk_widget_set_parent (self->button_menu_popover,
+                                       button_data->button);
+                gtk_popover_present (GTK_POPOVER (self->button_menu_popover));
+                g_object_unref (self->button_menu_popover);
+
                 pop_up_pathbar_context_menu (self, button_data->file);
             }
         }
diff --git a/src/nautilus-rename-file-popover-controller.c b/src/nautilus-rename-file-popover-controller.c
index b2491404b..6f52f8da6 100644
--- a/src/nautilus-rename-file-popover-controller.c
+++ b/src/nautilus-rename-file-popover-controller.c
@@ -376,7 +376,7 @@ nautilus_rename_file_popover_controller_show_for_file   (NautilusRenameFilePopov
     gtk_entry_set_text (GTK_ENTRY (self->name_entry), edit_name);
 
     gtk_popover_set_pointing_to (GTK_POPOVER (self->rename_file_popover), pointing_to);
-    gtk_popover_set_relative_to (GTK_POPOVER (self->rename_file_popover), relative_to);
+    gtk_widget_set_parent (self->rename_file_popover, relative_to);
 
     gtk_popover_popup (GTK_POPOVER (self->rename_file_popover));
 
@@ -423,8 +423,7 @@ nautilus_rename_file_popover_controller_finalize (GObject *object)
 
     reset_state (self);
 
-    gtk_widget_destroy (self->rename_file_popover);
-    g_clear_object (&self->rename_file_popover);
+    g_clear_pointer (&self->rename_file_popover, gtk_widget_unparent);
 
     g_clear_object (&self->key_controller);
 
diff --git a/src/nautilus-toolbar.c b/src/nautilus-toolbar.c
index e725869fc..7e71e5f2e 100644
--- a/src/nautilus-toolbar.c
+++ b/src/nautilus-toolbar.c
@@ -185,7 +185,7 @@ show_menu (NautilusToolbar *self,
 {
     g_autoptr (GMenu) menu = NULL;
     NautilusNavigationDirection direction;
-    GtkPopover *popover;
+    GtkPopoverMenu *popover;
 
     menu = g_menu_new ();
 
@@ -197,14 +197,14 @@ show_menu (NautilusToolbar *self,
         case NAUTILUS_NAVIGATION_DIRECTION_FORWARD:
         {
             fill_menu (self, menu, FALSE);
-            popover = GTK_POPOVER (self->forward_menu);
+            popover = GTK_POPOVER_MENU (self->forward_menu);
         }
         break;
 
         case NAUTILUS_NAVIGATION_DIRECTION_BACK:
         {
             fill_menu (self, menu, TRUE);
-            popover = GTK_POPOVER (self->back_menu);
+            popover = GTK_POPOVER_MENU (self->back_menu);
         }
         break;
 
@@ -215,8 +215,8 @@ show_menu (NautilusToolbar *self,
         break;
     }
 
-    gtk_popover_bind_model (popover, G_MENU_MODEL (menu), NULL);
-    gtk_popover_popup (popover);
+    gtk_popover_menu_set_menu_model (popover, G_MENU_MODEL (menu));
+    gtk_popover_popup (GTK_POPOVER (popover));
 }
 
 static void
@@ -870,6 +870,11 @@ static void
 nautilus_toolbar_init (NautilusToolbar *self)
 {
     gtk_widget_init_template (GTK_WIDGET (self));
+
+    gtk_widget_set_parent (self->back_menu, self->back_button);
+    g_signal_connect (self->back_menu, "destroy", G_CALLBACK (gtk_widget_unparent), NULL);
+    gtk_widget_set_parent (self->forward_menu, self->forward_button);
+    g_signal_connect (self->forward_menu, "destroy", G_CALLBACK (gtk_widget_unparent), NULL);
 }
 
 void
@@ -1034,6 +1039,8 @@ nautilus_toolbar_dispose (GObject *object)
     g_clear_object (&self->back_button_multi_press_gesture);
     g_clear_pointer (&self->icon_binding, g_binding_unbind);
     g_clear_pointer (&self->search_binding, g_binding_unbind);
+    g_clear_pointer (&self->back_menu, gtk_widget_unparent);
+    g_clear_pointer (&self->forward_menu, gtk_widget_unparent);
 
     G_OBJECT_CLASS (nautilus_toolbar_parent_class)->dispose (object);
 }
diff --git a/src/nautilus-window.c b/src/nautilus-window.c
index 4272d607d..6d6ec4b61 100644
--- a/src/nautilus-window.c
+++ b/src/nautilus-window.c
@@ -2139,6 +2139,8 @@ nautilus_window_dispose (GObject *object)
 
     DEBUG ("Destroying window");
 
+    g_clear_pointer (&window->tab_menu, gtk_widget_unparent);
+
     /* close all slots safely */
     slots_copy = g_list_copy (window->slots);
     if (window->active_slot != NULL)
@@ -2625,9 +2627,9 @@ nautilus_window_init (NautilusWindow *window)
                              window,
                              G_CONNECT_SWAPPED);
 
-    gtk_popover_bind_model (GTK_POPOVER (window->tab_menu),
-                            window->tab_menu_model,
-                            NULL);
+    gtk_widget_set_parent (window->tab_menu, GTK_WIDGET (window));
+    gtk_popover_menu_set_menu_model (GTK_POPOVER_MENU (window->tab_menu),
+                                     G_MENU_MODEL (window->tab_menu_model));
 
     g_signal_connect (window, "notify::is-maximized",
                       G_CALLBACK (on_is_maximized_changed), NULL);
diff --git a/src/resources/ui/nautilus-toolbar.ui b/src/resources/ui/nautilus-toolbar.ui
index c4ff2191d..a95cbfaae 100644
--- a/src/resources/ui/nautilus-toolbar.ui
+++ b/src/resources/ui/nautilus-toolbar.ui
@@ -1,8 +1,8 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <requires lib="gtk" version="4.0"/>
-  <object class="GtkPopover" id="back_menu"/>
-  <object class="GtkPopover" id="forward_menu"/>
+  <object class="GtkPopoverMenu" id="back_menu"/>
+  <object class="GtkPopoverMenu" id="forward_menu"/>
   <menu id="view_menu">
     <section>
       <attribute name="display-hint">horizontal-buttons</attribute>
diff --git a/src/resources/ui/nautilus-window.ui b/src/resources/ui/nautilus-window.ui
index 3071879e4..cc033a153 100644
--- a/src/resources/ui/nautilus-window.ui
+++ b/src/resources/ui/nautilus-window.ui
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <requires lib="gtk" version="4.0"/>
-  <object class="GtkPopover" id="tab_menu"/>
+  <object class="GtkPopoverMenu" id="tab_menu"/>
   <menu id="tab_menu_model">
     <section>
       <item>


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