[nautilus/wip/ernestask/men-use] files-view: Stop leaking context menus



commit 2af6b8c9f684eaf8f916db3aa9f98568b1e4f27b
Author: Ernestas Kulik <ekulik redhat com>
Date:   Sun Dec 16 11:41:47 2018 +0100

    files-view: Stop leaking context menus
    
    Currently, the context menus are created on the spot and attached to the
    view widget, which, somehow, does not mean that it will get detached
    after being closed. This commit makes the GtkMenu objects permanent.
    
    Fixes https://gitlab.gnome.org/GNOME/nautilus/issues/791

 src/nautilus-files-view.c | 101 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 75 insertions(+), 26 deletions(-)
---
diff --git a/src/nautilus-files-view.c b/src/nautilus-files-view.c
index 5b2fea566..585033d56 100644
--- a/src/nautilus-files-view.c
+++ b/src/nautilus-files-view.c
@@ -235,8 +235,11 @@ typedef struct
 
     GList *subdirectory_list;
 
-    GMenu *selection_menu;
-    GMenu *background_menu;
+    GMenu *selection_menu_model;
+    GMenu *background_menu_model;
+
+    GtkWidget *selection_menu;
+    GtkWidget *background_menu;
 
     GActionGroup *view_action_group;
 
@@ -3253,8 +3256,8 @@ nautilus_files_view_finalize (GObject *object)
     priv = nautilus_files_view_get_instance_private (view);
 
     g_clear_object (&priv->view_action_group);
-    g_clear_object (&priv->background_menu);
-    g_clear_object (&priv->selection_menu);
+    g_clear_object (&priv->background_menu_model);
+    g_clear_object (&priv->selection_menu_model);
     g_clear_object (&priv->toolbar_menu_sections->zoom_section);
     g_clear_object (&priv->toolbar_menu_sections->extended_section);
     g_clear_object (&priv->extensions_background_menu);
@@ -5019,7 +5022,7 @@ update_extensions_menus (NautilusFilesView *view)
         selection_menu = build_menu_for_extension_menu_items (view, "extensions",
                                                               selection_items);
 
-        nautilus_gmenu_merge (priv->selection_menu,
+        nautilus_gmenu_merge (priv->selection_menu_model,
                               selection_menu,
                               "extensions",
                               FALSE);
@@ -5032,7 +5035,7 @@ update_extensions_menus (NautilusFilesView *view)
         background_menu = build_menu_for_extension_menu_items (view, "extensions",
                                                                background_items);
 
-        nautilus_gmenu_merge (priv->background_menu,
+        nautilus_gmenu_merge (priv->background_menu_model,
                               background_menu,
                               "extensions",
                               FALSE);
@@ -5527,7 +5530,7 @@ update_scripts_menu (NautilusFilesView *view)
     submenu = update_directory_in_scripts_menu (view, directory);
     if (submenu != NULL)
     {
-        nautilus_gmenu_merge (priv->selection_menu,
+        nautilus_gmenu_merge (priv->selection_menu_model,
                               submenu,
                               "scripts-submenu",
                               TRUE);
@@ -5788,7 +5791,7 @@ update_templates_menu (NautilusFilesView *view)
     submenu = update_directory_in_templates_menu (view, directory);
     if (submenu != NULL)
     {
-        nautilus_gmenu_merge (priv->background_menu,
+        nautilus_gmenu_merge (priv->background_menu_model,
                               submenu,
                               "templates-submenu",
                               FALSE);
@@ -7775,7 +7778,7 @@ update_selection_menu (NautilusFilesView *view)
                                   selection_count);
     menu_item = g_menu_item_new (item_label, "view.new-folder-with-selection");
     g_menu_item_set_attribute (menu_item, "hidden-when", "s", "action-disabled");
-    nautilus_gmenu_add_item_in_submodel (priv->selection_menu,
+    nautilus_gmenu_add_item_in_submodel (priv->selection_menu_model,
                                          menu_item,
                                          "new-folder-with-selection-section",
                                          FALSE);
@@ -7860,7 +7863,7 @@ update_selection_menu (NautilusFilesView *view)
         g_menu_item_set_icon (menu_item, app_icon);
     }
 
-    nautilus_gmenu_add_item_in_submodel (priv->selection_menu,
+    nautilus_gmenu_add_item_in_submodel (priv->selection_menu_model,
                                          menu_item,
                                          "open-with-default-application-section",
                                          FALSE);
@@ -7933,7 +7936,7 @@ update_selection_menu (NautilusFilesView *view)
         }
 
         menu_item = g_menu_item_new (item_label, "view.start-volume");
-        nautilus_gmenu_add_item_in_submodel (priv->selection_menu,
+        nautilus_gmenu_add_item_in_submodel (priv->selection_menu_model,
                                              menu_item,
                                              "drive-section",
                                              FALSE);
@@ -7977,7 +7980,7 @@ update_selection_menu (NautilusFilesView *view)
         }
 
         menu_item = g_menu_item_new (item_label, "view.stop-volume");
-        nautilus_gmenu_add_item_in_submodel (priv->selection_menu,
+        nautilus_gmenu_add_item_in_submodel (priv->selection_menu_model,
                                              menu_item,
                                              "drive-section",
                                              FALSE);
@@ -8008,14 +8011,14 @@ real_update_context_menus (NautilusFilesView *view)
     priv = nautilus_files_view_get_instance_private (view);
     builder = gtk_builder_new_from_resource ("/org/gnome/nautilus/ui/nautilus-files-view-context-menus.ui");
 
-    g_clear_object (&priv->background_menu);
-    g_clear_object (&priv->selection_menu);
+    g_clear_object (&priv->background_menu_model);
+    g_clear_object (&priv->selection_menu_model);
 
     object = gtk_builder_get_object (builder, "background-menu");
-    priv->background_menu = g_object_ref (G_MENU (object));
+    priv->background_menu_model = g_object_ref (G_MENU (object));
 
     object = gtk_builder_get_object (builder, "selection-menu");
-    priv->selection_menu = g_object_ref (G_MENU (object));
+    priv->selection_menu_model = g_object_ref (G_MENU (object));
 
     update_selection_menu (view);
     update_background_menu (view);
@@ -8108,6 +8111,19 @@ nautilus_files_view_reveal_for_selection_context_menu (NautilusFilesView *view)
     return NAUTILUS_FILES_VIEW_CLASS (G_OBJECT_GET_CLASS (view))->reveal_for_selection_context_menu (view);
 }
 
+static void
+nautilus_files_view_detach_selection_menu (GtkWidget *attach_widget,
+                                           GtkMenu   *menu)
+{
+    NautilusFilesView *self;
+    NautilusFilesViewPrivate *priv;
+
+    self = NAUTILUS_FILES_VIEW (attach_widget);
+    priv = nautilus_files_view_get_instance_private (self);
+
+    priv->selection_menu = NULL;
+}
+
 /**
  * nautilus_files_view_pop_up_selection_context_menu
  *
@@ -8121,7 +8137,6 @@ nautilus_files_view_pop_up_selection_context_menu  (NautilusFilesView *view,
                                                     const GdkEvent    *event)
 {
     NautilusFilesViewPrivate *priv;
-    g_autoptr (GtkWidget) gtk_menu = NULL;
 
     g_assert (NAUTILUS_IS_FILES_VIEW (view));
 
@@ -8132,11 +8147,23 @@ nautilus_files_view_pop_up_selection_context_menu  (NautilusFilesView *view,
      */
     update_context_menus_if_pending (view);
 
-    gtk_menu = g_object_ref_sink (gtk_menu_new_from_model (G_MENU_MODEL (priv->selection_menu)));
-    gtk_menu_attach_to_widget (GTK_MENU (gtk_menu), GTK_WIDGET (view), NULL);
+    if (NULL == priv->selection_menu)
+    {
+        priv->selection_menu = gtk_menu_new ();
+
+        gtk_menu_attach_to_widget (GTK_MENU (priv->selection_menu),
+                                   GTK_WIDGET (view),
+                                   nautilus_files_view_detach_selection_menu);
+    }
+
+    gtk_menu_shell_bind_model (GTK_MENU_SHELL (priv->selection_menu),
+                               G_MENU_MODEL (priv->selection_menu_model),
+                               NULL,
+                               TRUE);
+
     if (event != NULL)
     {
-        gtk_menu_popup_at_pointer (GTK_MENU (gtk_menu), event);
+        gtk_menu_popup_at_pointer (GTK_MENU (priv->selection_menu), event);
     }
     else
     {
@@ -8146,7 +8173,7 @@ nautilus_files_view_pop_up_selection_context_menu  (NautilusFilesView *view,
         rectangle = nautilus_files_view_reveal_for_selection_context_menu (view);
         g_return_if_fail (rectangle != NULL);
 
-        gtk_menu_popup_at_rect (GTK_MENU (gtk_menu),
+        gtk_menu_popup_at_rect (GTK_MENU (priv->selection_menu),
                                 gtk_widget_get_window (GTK_WIDGET (view)),
                                 rectangle,
                                 GDK_GRAVITY_SOUTH_WEST,
@@ -8155,6 +8182,19 @@ nautilus_files_view_pop_up_selection_context_menu  (NautilusFilesView *view,
     }
 }
 
+static void
+nautilus_files_view_detach_background_menu (GtkWidget *attach_widget,
+                                            GtkMenu   *menu)
+{
+    NautilusFilesView *self;
+    NautilusFilesViewPrivate *priv;
+
+    self = NAUTILUS_FILES_VIEW (attach_widget);
+    priv = nautilus_files_view_get_instance_private (self);
+
+    priv->background_menu = NULL;
+}
+
 /**
  * nautilus_files_view_pop_up_background_context_menu
  *
@@ -8167,7 +8207,6 @@ nautilus_files_view_pop_up_background_context_menu (NautilusFilesView *view,
                                                     const GdkEvent    *event)
 {
     NautilusFilesViewPrivate *priv;
-    g_autoptr (GtkWidget) gtk_menu = NULL;
 
     g_assert (NAUTILUS_IS_FILES_VIEW (view));
 
@@ -8178,17 +8217,27 @@ nautilus_files_view_pop_up_background_context_menu (NautilusFilesView *view,
      */
     update_context_menus_if_pending (view);
 
-    gtk_menu = g_object_ref_sink (gtk_menu_new_from_model (G_MENU_MODEL (priv->background_menu)));
-    gtk_menu_attach_to_widget (GTK_MENU (gtk_menu), GTK_WIDGET (view), NULL);
+    if (NULL == priv->background_menu)
+    {
+        priv->background_menu = gtk_menu_new ();
+
+        gtk_menu_attach_to_widget (GTK_MENU (priv->background_menu),
+                                   GTK_WIDGET (view),
+                                   nautilus_files_view_detach_background_menu);
+    }
+    gtk_menu_shell_bind_model (GTK_MENU_SHELL (priv->background_menu),
+                               G_MENU_MODEL (priv->background_menu_model),
+                               NULL,
+                               TRUE);
     if (event != NULL)
     {
-        gtk_menu_popup_at_pointer (GTK_MENU (gtk_menu), event);
+        gtk_menu_popup_at_pointer (GTK_MENU (priv->background_menu), event);
     }
     else
     {
         /* It was triggered from the keyboard, so pop up from the center of view.
          */
-        gtk_menu_popup_at_widget (GTK_MENU (gtk_menu),
+        gtk_menu_popup_at_widget (GTK_MENU (priv->background_menu),
                                   GTK_WIDGET (view),
                                   GDK_GRAVITY_CENTER,
                                   GDK_GRAVITY_CENTER,


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