[nautilus/wip/antoniof/experimental-gtk4-build: 8/52] popovers API update




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

    popovers API update

 src/nautilus-files-view.c                     | 23 +++++++++++++--------
 src/nautilus-list-view.c                      |  7 ++++++-
 src/nautilus-pathbar.c                        | 29 +++++++++++++++++----------
 src/nautilus-rename-file-popover-controller.c |  5 ++---
 src/nautilus-toolbar.c                        | 17 +++++++++++-----
 src/nautilus-window.c                         |  9 ++++++---
 src/resources/ui/nautilus-toolbar.ui          |  4 ++--
 src/resources/ui/nautilus-window.ui           |  2 +-
 8 files changed, 62 insertions(+), 34 deletions(-)
---
diff --git a/src/nautilus-files-view.c b/src/nautilus-files-view.c
index de1c1b737..6e0ccfd63 100644
--- a/src/nautilus-files-view.c
+++ b/src/nautilus-files-view.c
@@ -3180,6 +3180,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);
@@ -8254,12 +8257,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 */
@@ -8301,13 +8305,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..3be79bab3 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,9 @@ 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));
+    g_signal_connect (self->button_menu_popover, "destroy", G_CALLBACK (gtk_widget_unparent), NULL);
 
     /* Add current location menu, which matches the view's background context menu */
     gtk_builder_add_from_resource (builder,
@@ -271,7 +271,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 +309,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 +424,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 +601,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 908c3c728..455850c4a 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
@@ -886,6 +886,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
@@ -1050,6 +1055,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 8d30f8137..aa090059a 100644
--- a/src/nautilus-window.c
+++ b/src/nautilus-window.c
@@ -2152,6 +2152,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)
@@ -2638,9 +2640,10 @@ 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));
+    g_signal_connect (window->tab_menu, "destroy", G_CALLBACK (gtk_widget_unparent), NULL);
+    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 b3687f32c..8fcb590f8 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 e0fca161b..d37691fc2 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]