[nautilus/wip/antoniof/menu-reorganization: 1/3] files-view: Stop setting actions on submenus




commit f416c418a85bf32aa30af0d245dc436e6dad0e28
Author: António Fernandes <antoniof gnome org>
Date:   Sun Jul 31 19:33:57 2022 +0100

    files-view: Stop setting actions on submenus
    
    We show Templates and Scripts submenus conditionally, using the
    hidden-when=action-disabled attribute, by assigning a dummy action
    which we can set as active or inactive as fit.
    
    However, this causes criticals when the popover menu is destroyed:
    
    (org.gnome.Nautilus:21502): GLib-CRITICAL **: 16:15:32.870: g_hash_table_iter_next: assertion 
'ri->version == ri->hash_table->version' failed
    
    Indeed, submenus are not supposed to have actions. There is a
    "submenu-action" but it's for a different purpose.
    
    So, instead of controlling the visibility through a dummy action,
    set or unset the "hidden-when" attribute to control its visibility,
    the same way we already do for sort menu items.

 src/nautilus-files-view.c                          | 46 ++++++++++++++--------
 src/nautilus-pathbar.c                             |  9 +++++
 src/nautilus-ui-utilities.c                        | 11 +++++-
 .../ui/nautilus-files-view-context-menus.ui        | 11 +++---
 4 files changed, 54 insertions(+), 23 deletions(-)
---
diff --git a/src/nautilus-files-view.c b/src/nautilus-files-view.c
index 6df8e7a19..2f3ec45a1 100644
--- a/src/nautilus-files-view.c
+++ b/src/nautilus-files-view.c
@@ -7043,9 +7043,7 @@ const GActionEntry view_entries[] =
     { "paste_accel", action_paste_files_accel },
     { "create-link", action_create_links },
     { "create-link-shortcut", action_create_links },
-    { "new-document" },
     /* Selection menu */
-    { "scripts" },
     { "new-folder-with-selection", action_new_folder_with_selection },
     { "open-scripts-folder", action_open_scripts_folder },
     { "open-item-location", action_open_item_location },
@@ -7748,11 +7746,6 @@ real_update_actions_state (NautilusFilesView *view)
     g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
                                  show_detect_media);
 
-    action = g_action_map_lookup_action (G_ACTION_MAP (view_action_group),
-                                         "scripts");
-    g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
-                                 priv->scripts_menu != NULL);
-
     /* Background menu actions */
     action = g_action_map_lookup_action (G_ACTION_MAP (view_action_group),
                                          "open-current-directory-with-other-application");
@@ -7783,13 +7776,6 @@ real_update_actions_state (NautilusFilesView *view)
                                          "current-directory-properties");
     g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
                                  !selection_contains_search);
-    action = g_action_map_lookup_action (G_ACTION_MAP (view_action_group),
-                                         "new-document");
-    g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
-                                 can_create_files &&
-                                 !selection_contains_recent &&
-                                 !selection_contains_starred &&
-                                 priv->templates_menu != NULL);
 
     /* Actions that are related to the clipboard need request, request the data
      * and update them once we have the data */
@@ -7904,6 +7890,8 @@ update_selection_menu (NautilusFilesView *view,
     gboolean show_start;
     gboolean show_stop;
     gboolean show_detect_media;
+    gboolean show_scripts = FALSE;
+    gint i;
     GDriveStartStopType start_stop_type;
 
     selection = nautilus_view_get_selection (NAUTILUS_VIEW (view));
@@ -8128,8 +8116,20 @@ update_selection_menu (NautilusFilesView *view,
         update_scripts_menu (view, builder);
         priv->scripts_menu_updated = TRUE;
     }
-    object = gtk_builder_get_object (builder, "scripts-submenu-section");
-    nautilus_gmenu_set_from_model (G_MENU (object), priv->scripts_menu);
+
+    if (priv->scripts_menu != NULL)
+    {
+        show_scripts = TRUE;
+        object = gtk_builder_get_object (builder, "scripts-submenu-section");
+        nautilus_gmenu_set_from_model (G_MENU (object), priv->scripts_menu);
+    }
+
+    i = nautilus_g_menu_model_find_by_string (G_MENU_MODEL (priv->selection_menu_model),
+                                              "nautilus-menu-item",
+                                              "scripts-submenu");
+    nautilus_g_menu_replace_string_in_item (priv->selection_menu_model, i,
+                                            "hidden-when",
+                                            (!show_scripts) ? "action-missing" : NULL);
 }
 
 static void
@@ -8138,6 +8138,8 @@ update_background_menu (NautilusFilesView *view,
 {
     NautilusFilesViewPrivate *priv = nautilus_files_view_get_instance_private (view);
     GObject *object;
+    gboolean remove_submenu = TRUE;
+    gint i;
 
     if (nautilus_files_view_supports_creating_files (view) &&
         !showing_recent_directory (view) &&
@@ -8151,7 +8153,19 @@ update_background_menu (NautilusFilesView *view,
 
         object = gtk_builder_get_object (builder, "templates-submenu");
         nautilus_gmenu_set_from_model (G_MENU (object), priv->templates_menu);
+
+        if (priv->templates_menu != NULL)
+        {
+            remove_submenu = FALSE;
+        }
     }
+
+    i = nautilus_g_menu_model_find_by_string (G_MENU_MODEL (priv->background_menu_model),
+                                              "nautilus-menu-item",
+                                              "templates-submenu");
+    nautilus_g_menu_replace_string_in_item (priv->background_menu_model, i,
+                                            "hidden-when",
+                                            remove_submenu ? "action-missing" : NULL);
 }
 
 static void
diff --git a/src/nautilus-pathbar.c b/src/nautilus-pathbar.c
index 9b53d4317..f31cae62b 100644
--- a/src/nautilus-pathbar.c
+++ b/src/nautilus-pathbar.c
@@ -441,6 +441,8 @@ void
 nautilus_path_bar_set_templates_menu (NautilusPathBar *self,
                                       GMenuModel      *menu)
 {
+    gint i;
+
     g_return_if_fail (NAUTILUS_IS_PATH_BAR (self));
 
     if (!gtk_widget_is_visible (GTK_WIDGET (self->current_view_menu_popover)))
@@ -454,6 +456,13 @@ nautilus_path_bar_set_templates_menu (NautilusPathBar *self,
 
     nautilus_gmenu_set_from_model (self->templates_submenu, menu);
     g_idle_add ((GSourceFunc) bind_current_view_menu_model_to_popover, self);
+
+    i = nautilus_g_menu_model_find_by_string (G_MENU_MODEL (self->current_view_menu),
+                                              "nautilus-menu-item",
+                                              "templates-submenu");
+    nautilus_g_menu_replace_string_in_item (self->current_view_menu, i,
+                                            "hidden-when",
+                                            (menu == NULL) ? "action-missing" : NULL);
 }
 
 /* Public functions and their helpers */
diff --git a/src/nautilus-ui-utilities.c b/src/nautilus-ui-utilities.c
index 8e5933ee8..670b202d0 100644
--- a/src/nautilus-ui-utilities.c
+++ b/src/nautilus-ui-utilities.c
@@ -130,7 +130,16 @@ nautilus_g_menu_replace_string_in_item (GMenu       *menu,
     g_return_if_fail (i != -1);
     item = g_menu_item_new_from_model (G_MENU_MODEL (menu), i);
     g_return_if_fail (item != NULL);
-    g_menu_item_set_attribute (item, attribute, "s", string);
+
+    if (string != NULL)
+    {
+        g_menu_item_set_attribute (item, attribute, "s", string);
+    }
+    else
+    {
+        g_menu_item_set_attribute (item, attribute, NULL);
+    }
+
     g_menu_remove (menu, i);
     g_menu_insert_item (menu, i, item);
 }
diff --git a/src/resources/ui/nautilus-files-view-context-menus.ui 
b/src/resources/ui/nautilus-files-view-context-menus.ui
index a88e5c5a0..e6735ee3a 100644
--- a/src/resources/ui/nautilus-files-view-context-menus.ui
+++ b/src/resources/ui/nautilus-files-view-context-menus.ui
@@ -6,11 +6,11 @@
       <attribute name="label" translatable="yes">New _Folder</attribute>
       <attribute name="action">view.new-folder</attribute>
     </item>
-    <submenu id="templates-submenu">
+    <item>
       <attribute name="label" translatable="yes">New _Document</attribute>
-      <attribute name="action">view.new-document</attribute>
-      <attribute name="hidden-when">action-disabled</attribute>
-    </submenu>
+      <attribute name="nautilus-menu-item">templates-submenu</attribute>
+      <link name="submenu" id="templates-submenu"/>
+    </item>
     <item>
       <attribute name="label" translatable="yes">Add to _Bookmarks</attribute>
       <attribute name="action">win.bookmark-current-location</attribute>
@@ -72,8 +72,7 @@
     </section>
     <submenu>
       <attribute name="label" translatable="yes">_Scripts</attribute>
-      <attribute name="action">view.scripts</attribute>
-      <attribute name="hidden-when">action-disabled</attribute>
+      <attribute name="nautilus-menu-item">scripts-submenu</attribute>
       <section id="scripts-submenu-section"/>
       <section>
         <item>


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