[nautilus/wip/neilh/toolbar-reorg: 3/3] toolbar: move undo/redo toolbar menu code into toolbar



commit a312f5652756cda481f0808ed94116ae0f5eabda
Author: Neil Herald <neil herald gmail com>
Date:   Sun Jun 19 19:34:06 2016 +0100

    toolbar: move undo/redo toolbar menu code into toolbar
    
    Due to the toolbar menu reorganisation work, the code to create and
    manage the undo/redo items on the menu ended up in files-view.c. This
    isn't the correct place as they don't have much to do with the files
    view. Some refactoring was needed before the code for these items could
    be moved back into the toolbar, which has now been done in a previous
    commit.
    
    This commit moves the undo/redo creation and management code into the
    toolbar.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=764632

 src/nautilus-files-view.c                      |   92 +-----------------------
 src/nautilus-places-view.c                     |   22 ++++--
 src/nautilus-toolbar-menu-sections.h           |    1 +
 src/nautilus-toolbar.c                         |   90 +++++++++++++++++++++++
 src/nautilus-toolbar.h                         |    2 +
 src/nautilus-view.h                            |    3 +-
 src/nautilus-window.c                          |    2 +
 src/resources/ui/nautilus-toolbar-menu.ui      |   31 ++++++++
 src/resources/ui/nautilus-toolbar-view-menu.ui |   24 ------
 9 files changed, 144 insertions(+), 123 deletions(-)
---
diff --git a/src/nautilus-files-view.c b/src/nautilus-files-view.c
index 1655f05..4694437 100644
--- a/src/nautilus-files-view.c
+++ b/src/nautilus-files-view.c
@@ -269,9 +269,6 @@ struct NautilusFilesViewDetails
         GtkWidget *zoom_controls_box;
         GtkWidget *zoom_level_label;
 
-        GtkWidget *undo_button;
-        GtkWidget *redo_button;
-
         gulong stop_signal_handler;
         gulong reload_signal_handler;
 };
@@ -8001,90 +7998,6 @@ nautilus_files_view_is_loading (NautilusView *view)
 }
 
 static void
-update_menu_item (GtkWidget      *menu_item,
-                  NautilusWindow *window,
-                  const char     *action_name,
-                  gboolean        enabled,
-                  char           *label)
-{
-        GAction *action;
-        GValue val = G_VALUE_INIT;
-
-        /* Activate/deactivate */
-        action = g_action_map_lookup_action (G_ACTION_MAP (window), action_name);
-        g_simple_action_set_enabled (G_SIMPLE_ACTION (action), enabled);
-
-        /* Set the text of the menu item. Can't use gtk_button_set_label here
-         * as we need to set the text property, not the label. There's no equivalent
-         * gtk_model_button_set_text function
-         */
-        g_value_init (&val, G_TYPE_STRING);
-        g_value_set_string (&val, label);
-        g_object_set_property (G_OBJECT (menu_item), "text", &val);
-        g_value_unset (&val);
-}
-
-static void
-undo_manager_changed (NautilusFilesView *view)
-{
-        NautilusWindow *window;
-        NautilusFileUndoInfo *info;
-        NautilusFileUndoManagerState undo_state;
-        gboolean undo_active;
-        gboolean redo_active;
-        gchar *undo_label;
-        gchar *redo_label;
-        gchar *undo_description;
-        gchar *redo_description;
-        gboolean is_undo;
-
-        window = nautilus_files_view_get_window (view);
-        undo_label = undo_description = redo_label = redo_description = NULL;
-
-        /* Look up the last action from the undo manager, and get the text that
-         * describes it, e.g. "Undo Create Folder"/"Redo Create Folder"
-         */
-        info = nautilus_file_undo_manager_get_action ();
-        undo_state = nautilus_file_undo_manager_get_state ();
-        undo_active = redo_active = FALSE;
-        if (info != NULL && undo_state > NAUTILUS_FILE_UNDO_MANAGER_STATE_NONE) {
-                is_undo = undo_state == NAUTILUS_FILE_UNDO_MANAGER_STATE_UNDO;
-
-                /* The last action can either be undone/redone. Activate the corresponding
-                 * menu item and deactivate the other
-                 */
-                undo_active = is_undo;
-                redo_active = !is_undo;
-                nautilus_file_undo_info_get_strings (info, &undo_label, &undo_description,
-                                                     &redo_label, &redo_description);
-                g_free (undo_description);
-                g_free (redo_description);
-        }
-
-        /* Set the label of the undo and redo menu items, and activate them appropriately
-         */
-        undo_label = undo_active && undo_label != NULL ? undo_label : g_strdup (_("_Undo"));
-        update_menu_item (view->details->undo_button, window, "undo", undo_active, undo_label);
-
-        redo_label = redo_active && redo_label != NULL ? redo_label : g_strdup (_("_Redo"));
-        update_menu_item (view->details->redo_button, window, "redo", redo_active, redo_label);
-
-        g_free (undo_label);
-        g_free (redo_label);
-}
-
-static void
-nautilus_files_view_constructed (GObject *object)
-{
-        NautilusFilesView *view;
-
-        view = NAUTILUS_FILES_VIEW (object);
-        g_signal_connect_object (nautilus_file_undo_manager_get (), "undo-changed",
-                                 G_CALLBACK (undo_manager_changed), view, G_CONNECT_SWAPPED);
-        undo_manager_changed (view);
-}
-
-static void
 nautilus_files_view_iface_init (NautilusViewInterface *iface)
 {
         iface->get_icon = nautilus_files_view_get_icon;
@@ -8109,7 +8022,6 @@ nautilus_files_view_class_init (NautilusFilesViewClass *klass)
         oclass = G_OBJECT_CLASS (klass);
 
         oclass->finalize = nautilus_files_view_finalize;
-        oclass->constructed = nautilus_files_view_constructed;
         oclass->get_property = nautilus_files_view_get_property;
         oclass->set_property = nautilus_files_view_set_property;
 
@@ -8257,6 +8169,7 @@ nautilus_files_view_init (NautilusFilesView *view)
         /* Toolbar menu */
         builder = gtk_builder_new_from_resource ("/org/gnome/nautilus/ui/nautilus-toolbar-view-menu.ui");
         view->details->toolbar_menu_sections = g_new0 (NautilusToolbarMenuSections, 1);
+        view->details->toolbar_menu_sections->supports_undo_redo = TRUE;
         view->details->toolbar_menu_sections->zoom_section = g_object_ref_sink (gtk_builder_get_object 
(builder, "zoom_section"));
         view->details->toolbar_menu_sections->extended_section = g_object_ref_sink (gtk_builder_get_object 
(builder, "extended_section"));
         view->details->zoom_controls_box = GTK_WIDGET (gtk_builder_get_object (builder, 
"zoom_controls_box"));
@@ -8268,9 +8181,6 @@ nautilus_files_view_init (NautilusFilesView *view)
         view->details->reload =  GTK_WIDGET (gtk_builder_get_object (builder, "reload"));
         view->details->stop =  GTK_WIDGET (gtk_builder_get_object (builder, "stop"));
 
-        view->details->undo_button = GTK_WIDGET (gtk_builder_get_object (builder, "undo"));
-        view->details->redo_button = GTK_WIDGET (gtk_builder_get_object (builder, "redo"));
-
         g_signal_connect (view,
                           "end-file-changes",
                           G_CALLBACK (on_end_file_changes),
diff --git a/src/nautilus-places-view.c b/src/nautilus-places-view.c
index dfe5a7f..4b79da8 100644
--- a/src/nautilus-places-view.c
+++ b/src/nautilus-places-view.c
@@ -20,15 +20,17 @@
 #include "nautilus-places-view.h"
 #include "nautilus-window-slot.h"
 #include "nautilus-application.h"
+#include "nautilus-toolbar-menu-sections.h"
 #include "gtk/nautilusgtkplacesviewprivate.h"
 
 typedef struct
 {
-        GFile                  *location;
-        GIcon                  *icon;
-        NautilusQuery          *search_query;
+        GFile                       *location;
+        GIcon                       *icon;
+        NautilusQuery               *search_query;
+        NautilusToolbarMenuSections *toolbar_menu_sections;
 
-        GtkWidget              *places_view;
+        GtkWidget                   *places_view;
 } NautilusPlacesViewPrivate;
 
 struct _NautilusPlacesView
@@ -135,6 +137,8 @@ nautilus_places_view_finalize (GObject *object)
         g_clear_object (&priv->location);
         g_clear_object (&priv->search_query);
 
+        g_free (priv->toolbar_menu_sections);
+
         G_OBJECT_CLASS (nautilus_places_view_parent_class)->finalize (object);
 }
 
@@ -278,8 +282,11 @@ nautilus_places_view_set_search_query (NautilusView  *view,
 static NautilusToolbarMenuSections *
 nautilus_places_view_get_toolbar_menu_sections (NautilusView *view)
 {
-        /* By returning NULL, no sections will be added to the toolbar menu when this view is active */
-        return NULL;
+        NautilusPlacesViewPrivate *priv;
+
+        priv = nautilus_places_view_get_instance_private (NAUTILUS_PLACES_VIEW (view));
+
+        return priv->toolbar_menu_sections;
 }
 
 static gboolean
@@ -370,6 +377,9 @@ nautilus_places_view_init (NautilusPlacesView *self)
                                   G_CALLBACK (show_error_message_cb),
                                   self);
 
+        /* Toolbar menu */
+        priv->toolbar_menu_sections = g_new0 (NautilusToolbarMenuSections, 1);
+        priv->toolbar_menu_sections->supports_undo_redo = FALSE;
 }
 
 NautilusPlacesView *
diff --git a/src/nautilus-toolbar-menu-sections.h b/src/nautilus-toolbar-menu-sections.h
index 4bac24c..26b4051 100644
--- a/src/nautilus-toolbar-menu-sections.h
+++ b/src/nautilus-toolbar-menu-sections.h
@@ -26,6 +26,7 @@ typedef struct _NautilusToolbarMenuSections NautilusToolbarMenuSections;
 struct _NautilusToolbarMenuSections {
         GtkWidget *zoom_section;
         GtkWidget *extended_section;
+        gboolean   supports_undo_redo;
 };
 
 G_END_DECLS
diff --git a/src/nautilus-toolbar.c b/src/nautilus-toolbar.c
index 0a09dd9..fb14346 100644
--- a/src/nautilus-toolbar.c
+++ b/src/nautilus-toolbar.c
@@ -35,6 +35,7 @@
 #include "nautilus-ui-utilities.h"
 #include "nautilus-progress-info-manager.h"
 #include "nautilus-file-operations.h"
+#include "nautilus-file-undo-manager.h"
 #include "nautilus-toolbar-menu-sections.h"
 
 #include <glib/gi18n.h>
@@ -68,7 +69,10 @@ struct _NautilusToolbarPrivate {
        GtkWidget *operations_button;
         GtkWidget *view_button;
         GtkWidget *view_menu_zoom_section;
+        GtkWidget *view_menu_undo_redo_section;
         GtkWidget *view_menu_extended_section;
+        GtkWidget *undo_button;
+        GtkWidget *redo_button;
         GtkWidget *view_toggle_button;
         GtkWidget *view_toggle_icon;
 
@@ -755,6 +759,71 @@ on_progress_has_viewers_changed (NautilusProgressInfoManager *manager,
 }
 
 static void
+update_menu_item (GtkWidget       *menu_item,
+                  NautilusToolbar *self,
+                  const char      *action_name,
+                  gboolean         enabled,
+                  char            *label)
+{
+        GAction *action;
+        GValue val = G_VALUE_INIT;
+
+        /* Activate/deactivate */
+        action = g_action_map_lookup_action (G_ACTION_MAP (self->priv->window), action_name);
+        g_simple_action_set_enabled (G_SIMPLE_ACTION (action), enabled);
+
+        /* Set the text of the menu item. Can't use gtk_button_set_label here as
+         * we need to set the text property, not the label. There's no equivalent
+         * gtk_model_button_set_text function (refer to #766083 for discussion
+         * on adding a set_text function)
+         */
+        g_value_init (&val, G_TYPE_STRING);
+        g_value_set_string (&val, label);
+        g_object_set_property (G_OBJECT (menu_item), "text", &val);
+        g_value_unset (&val);
+}
+
+static void
+undo_manager_changed (NautilusToolbar *self)
+{
+        NautilusFileUndoInfo *info;
+        NautilusFileUndoManagerState undo_state;
+        gboolean undo_active;
+        gboolean redo_active;
+        g_autofree gchar *undo_label = NULL;
+        g_autofree gchar *redo_label = NULL;
+        g_autofree gchar *undo_description = NULL;
+        g_autofree gchar *redo_description = NULL;
+        gboolean is_undo;
+
+        /* Look up the last action from the undo manager, and get the text that
+         * describes it, e.g. "Undo Create Folder"/"Redo Create Folder"
+         */
+        info = nautilus_file_undo_manager_get_action ();
+        undo_state = nautilus_file_undo_manager_get_state ();
+        undo_active = redo_active = FALSE;
+        if (info != NULL && undo_state > NAUTILUS_FILE_UNDO_MANAGER_STATE_NONE) {
+                is_undo = undo_state == NAUTILUS_FILE_UNDO_MANAGER_STATE_UNDO;
+
+                /* The last action can either be undone/redone. Activate the corresponding
+                 * menu item and deactivate the other
+                 */
+                undo_active = is_undo;
+                redo_active = !is_undo;
+                nautilus_file_undo_info_get_strings (info, &undo_label, &undo_description,
+                                                     &redo_label, &redo_description);
+        }
+
+        /* Set the label of the undo and redo menu items, and activate them appropriately
+         */
+        undo_label = undo_active && undo_label != NULL ? undo_label : g_strdup (_("_Undo"));
+        update_menu_item (self->priv->undo_button, self, "undo", undo_active, undo_label);
+
+        redo_label = redo_active && redo_label != NULL ? redo_label : g_strdup (_("_Redo"));
+        update_menu_item (self->priv->redo_button, self, "redo", redo_active, redo_label);
+}
+
+static void
 nautilus_toolbar_init (NautilusToolbar *self)
 {
         GtkBuilder *builder;
@@ -766,7 +835,10 @@ nautilus_toolbar_init (NautilusToolbar *self)
         builder = gtk_builder_new_from_resource ("/org/gnome/nautilus/ui/nautilus-toolbar-menu.ui");
         menu_popover = GTK_WIDGET (gtk_builder_get_object (builder, "menu_popover"));
         self->priv->view_menu_zoom_section = GTK_WIDGET (gtk_builder_get_object (builder, 
"view_menu_zoom_section"));
+        self->priv->view_menu_undo_redo_section = GTK_WIDGET (gtk_builder_get_object (builder, 
"view_menu_undo_redo_section"));
         self->priv->view_menu_extended_section = GTK_WIDGET (gtk_builder_get_object (builder, 
"view_menu_extended_section"));
+        self->priv->undo_button = GTK_WIDGET (gtk_builder_get_object (builder, "undo"));
+        self->priv->redo_button = GTK_WIDGET (gtk_builder_get_object (builder, "redo"));
         gtk_menu_button_set_popover (GTK_MENU_BUTTON (self->priv->view_button), menu_popover);
         g_object_unref (builder);
 
@@ -807,6 +879,21 @@ nautilus_toolbar_init (NautilusToolbar *self)
        toolbar_update_appearance (self);
 }
 
+void
+nautilus_toolbar_on_window_constructed (NautilusToolbar *self)
+{
+        /* undo_manager_changed manipulates the window actions, so set it up
+         * after the window and it's actions have been constructed
+         */
+        g_signal_connect_object (nautilus_file_undo_manager_get (),
+                                 "undo-changed",
+                                 G_CALLBACK (undo_manager_changed),
+                                 self,
+                                 G_CONNECT_SWAPPED);
+
+        undo_manager_changed (self);
+}
+
 static void
 nautilus_toolbar_get_property (GObject *object,
                               guint property_id,
@@ -992,6 +1079,9 @@ on_slot_toolbar_menu_sections_changed (NautilusToolbar    *toolbar,
         if (new_sections == NULL)
                 return;
 
+        gtk_widget_set_visible (toolbar->priv->view_menu_undo_redo_section,
+                                new_sections->supports_undo_redo);
+
         if (new_sections->zoom_section != NULL)
                 gtk_box_pack_start (GTK_BOX (toolbar->priv->view_menu_zoom_section), 
new_sections->zoom_section, FALSE, FALSE, 0);
 
diff --git a/src/nautilus-toolbar.h b/src/nautilus-toolbar.h
index 41ec2ae..f445079 100644
--- a/src/nautilus-toolbar.h
+++ b/src/nautilus-toolbar.h
@@ -75,4 +75,6 @@ void       nautilus_toolbar_set_active_slot    (NautilusToolbar    *toolbar,
 
 gboolean   nautilus_toolbar_is_operations_button_active (NautilusToolbar *toolbar);
 
+void       nautilus_toolbar_on_window_constructed       (NautilusToolbar *toolbar);
+
 #endif /* __NAUTILUS_TOOLBAR_H__ */
diff --git a/src/nautilus-view.h b/src/nautilus-view.h
index ad019d6..4c76cd0 100644
--- a/src/nautilus-view.h
+++ b/src/nautilus-view.h
@@ -41,8 +41,7 @@ struct _NautilusViewInterface
 
         /*
          * Returns the menu sections that should be shown in the toolbar menu
-         * when this view is active. Implementations can return %NULL to
-         * indicate that no extra sections should be added to the menu
+         * when this view is active. Implementations must not return %NULL
          */
         NautilusToolbarMenuSections *   (*get_toolbar_menu_sections) (NautilusView         *view);
 
diff --git a/src/nautilus-window.c b/src/nautilus-window.c
index 4e4a5bc..6d5c419 100644
--- a/src/nautilus-window.c
+++ b/src/nautilus-window.c
@@ -2151,6 +2151,8 @@ nautilus_window_constructed (GObject *self)
                g_signal_connect_swapped (nautilus_application_get_bookmarks (application), "changed",
                                          G_CALLBACK (nautilus_window_sync_bookmarks), window);
 
+        nautilus_toolbar_on_window_constructed (NAUTILUS_TOOLBAR (window->priv->toolbar));
+
        nautilus_profile_end (NULL);
 }
 
diff --git a/src/resources/ui/nautilus-toolbar-menu.ui b/src/resources/ui/nautilus-toolbar-menu.ui
index 05ab33f..b4bceaa 100644
--- a/src/resources/ui/nautilus-toolbar-menu.ui
+++ b/src/resources/ui/nautilus-toolbar-menu.ui
@@ -96,6 +96,37 @@
           </object>
         </child>
         <child>
+          <object class="GtkBox" id="view_menu_undo_redo_section">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="orientation">vertical</property>
+            <child>
+              <object class="GtkSeparator">
+                <property name="visible">True</property>
+                <property name="orientation">horizontal</property>
+                <property name="margin-top">6</property>
+                <property name="margin-bottom">6</property>
+              </object>
+            </child>
+            <child>
+              <object class="GtkModelButton" id="undo">
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="text" translatable="yes">_Undo</property>
+                <property name="action-name">win.undo</property>
+              </object>
+            </child>
+            <child>
+              <object class="GtkModelButton" id="redo">
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="text" translatable="yes">_Redo</property>
+                <property name="action-name">win.redo</property>
+              </object>
+            </child>
+          </object>
+        </child>
+        <child>
           <!-- Other controls custom to the view are added to this -->
           <object class="GtkBox" id="view_menu_extended_section">
             <property name="visible">True</property>
diff --git a/src/resources/ui/nautilus-toolbar-view-menu.ui b/src/resources/ui/nautilus-toolbar-view-menu.ui
index 38968db..06ad3e9 100644
--- a/src/resources/ui/nautilus-toolbar-view-menu.ui
+++ b/src/resources/ui/nautilus-toolbar-view-menu.ui
@@ -91,30 +91,6 @@
     <property name="orientation">vertical</property>
     <property name="width_request">160</property>
     <child>
-      <object class="GtkSeparator">
-        <property name="visible">True</property>
-        <property name="orientation">horizontal</property>
-        <property name="margin-top">6</property>
-        <property name="margin-bottom">6</property>
-      </object>
-    </child>
-    <child>
-      <object class="GtkModelButton" id="undo">
-        <property name="visible">True</property>
-        <property name="can_focus">True</property>
-        <property name="text" translatable="yes">_Undo</property>
-        <property name="action-name">win.undo</property>
-      </object>
-    </child>
-    <child>
-      <object class="GtkModelButton" id="redo">
-        <property name="visible">True</property>
-        <property name="can_focus">True</property>
-        <property name="text" translatable="yes">_Redo</property>
-        <property name="action-name">win.redo</property>
-      </object>
-    </child>
-    <child>
       <object class="GtkBox" id="sort_menu">
         <property name="visible">True</property>
         <property name="can_focus">False</property>


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