[nautilus/wip/antoniof/menu-reorganization: 25/29] files-view: Rebuild context menus every time




commit 55147fb5bfd5891544a3047a4dc9024a993af1bf
Author: António Fernandes <antoniof gnome org>
Date:   Sun Jul 31 16:29:17 2022 +0100

    files-view: Rebuild context menus every time
    
    In GTK3, we would reuse the same GtkMenu, but update the model.
    
    With GtkPopoverMenu, this is creating duplicate stack page each time
    our submenus are updated. And it turns out we update templates and
    scripts menu a lot of times, on directory monitor callbacks! Besides
    warnings, this causes increasing memory consumptions.
    
    Additionally, reusing the same popover while updating the model causes
    the old model do be temporarily displayed when the popover is opened,
    which sometimes even causes the popover to resize and jump around.
    This is obviously bad.
    
    Avoid both problems by creating a new popover menu every time we open
    the context menu. The old one is destroyed (by unparenting) right
    before this. (Not on GtkPopover::closed, because this would be too
    early and actions would fail to activate!)

 src/nautilus-files-view.c | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)
---
diff --git a/src/nautilus-files-view.c b/src/nautilus-files-view.c
index 3d644da3f..f553b74b4 100644
--- a/src/nautilus-files-view.c
+++ b/src/nautilus-files-view.c
@@ -8340,14 +8340,14 @@ nautilus_files_view_pop_up_selection_context_menu  (NautilusFilesView *view,
      */
     update_context_menus_if_pending (view);
 
-    if (NULL == priv->selection_menu)
-    {
-        priv->selection_menu = gtk_popover_menu_new_from_model (NULL);
-        gtk_widget_set_parent (priv->selection_menu, GTK_WIDGET (view));
-        gtk_popover_set_has_arrow (GTK_POPOVER (priv->selection_menu), FALSE);
-        gtk_widget_set_halign (priv->selection_menu, GTK_ALIGN_START);
-        g_signal_connect (priv->selection_menu, "destroy", G_CALLBACK (gtk_widget_unparent), NULL);
-    }
+    /* Destroy old popover and create a new one, to avoid duplicate submenu bugs
+     * and showing old model temporarily. We don't do this when popover is
+     * closed because it wouldn't activate the actions then. */
+    g_clear_pointer (&priv->selection_menu, gtk_widget_unparent);
+    priv->selection_menu = gtk_popover_menu_new_from_model (NULL);
+    gtk_widget_set_parent (priv->selection_menu, GTK_WIDGET (view));
+    gtk_popover_set_has_arrow (GTK_POPOVER (priv->selection_menu), FALSE);
+    gtk_widget_set_halign (priv->selection_menu, GTK_ALIGN_START);
 
     gtk_popover_menu_set_menu_model (GTK_POPOVER_MENU (priv->selection_menu),
                                      G_MENU_MODEL (priv->selection_menu_model));
@@ -8392,15 +8392,15 @@ nautilus_files_view_pop_up_background_context_menu (NautilusFilesView *view,
      */
     update_context_menus_if_pending (view);
 
+    /* Destroy old popover and create a new one, to avoid duplicate submenu bugs
+     * and showing old model temporarily. We don't do this when popover is
+     * closed because it wouldn't activate the actions then. */
+    g_clear_pointer (&priv->background_menu, gtk_widget_unparent);
+    priv->background_menu = gtk_popover_menu_new_from_model (NULL);
+    gtk_widget_set_parent (priv->background_menu, GTK_WIDGET (view));
+    gtk_popover_set_has_arrow (GTK_POPOVER (priv->background_menu), FALSE);
+    gtk_widget_set_halign (priv->background_menu, GTK_ALIGN_START);
 
-    if (NULL == priv->background_menu)
-    {
-        priv->background_menu = gtk_popover_menu_new_from_model (NULL);
-        gtk_widget_set_parent (priv->background_menu, GTK_WIDGET (view));
-        gtk_popover_set_has_arrow (GTK_POPOVER (priv->background_menu), FALSE);
-        gtk_widget_set_halign (priv->background_menu, GTK_ALIGN_START);
-        g_signal_connect (priv->background_menu, "destroy", G_CALLBACK (gtk_widget_unparent), NULL);
-    }
     gtk_popover_menu_set_menu_model (GTK_POPOVER_MENU (priv->background_menu),
                                      G_MENU_MODEL (priv->background_menu_model));
 


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