[nautilus/wip/toolbar-start-end: 5/8] toolbar: Factor out view controls




commit dd407da7da81cbc02a3e8ec1a03f447cea0ac65f
Author: António Fernandes <antoniof gnome org>
Date:   Sat Aug 6 22:56:57 2022 +0100

    toolbar: Factor out view controls
    
    Same rationale as previous 2 commits.

 src/meson.build                            |   2 +
 src/nautilus-toolbar.c                     |  53 +-------
 src/nautilus-view-controls.c               | 187 +++++++++++++++++++++++++++++
 src/nautilus-view-controls.h               |  20 +++
 src/resources/nautilus.gresource.xml       |   1 +
 src/resources/ui/nautilus-toolbar.ui       |  36 +-----
 src/resources/ui/nautilus-view-controls.ui |  42 +++++++
 7 files changed, 256 insertions(+), 85 deletions(-)
---
diff --git a/src/meson.build b/src/meson.build
index a060526e8..339a50aa0 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -135,6 +135,8 @@ libnautilus_sources = [
   'nautilus-view.h',
   'nautilus-view-cell.c',
   'nautilus-view-cell.h',
+  'nautilus-view-controls.c',
+  'nautilus-view-controls.h',
   'nautilus-view-item.c',
   'nautilus-view-item.h',
   'nautilus-view-model.c',
diff --git a/src/nautilus-toolbar.c b/src/nautilus-toolbar.c
index cf570342a..edff8f2ff 100644
--- a/src/nautilus-toolbar.c
+++ b/src/nautilus-toolbar.c
@@ -33,7 +33,7 @@
 #include "nautilus-location-entry.h"
 #include "nautilus-pathbar.h"
 #include "nautilus-progress-indicator.h"
-#include "nautilus-toolbar-menu-sections.h"
+#include "nautilus-view-controls.h"
 #include "nautilus-ui-utilities.h"
 #include "nautilus-window.h"
 
@@ -53,9 +53,6 @@ struct _NautilusToolbar
     gboolean show_location_entry;
     gboolean location_entry_should_auto_hide;
 
-    GtkWidget *view_split_button;
-    GMenuModel *view_menu;
-
     GtkWidget *app_button;
     GMenuModel *undo_redo_section;
 
@@ -242,6 +239,7 @@ nautilus_toolbar_init (NautilusToolbar *self)
 {
     g_type_ensure (NAUTILUS_TYPE_HISTORY_CONTROLS);
     g_type_ensure (NAUTILUS_TYPE_PROGRESS_INDICATOR);
+    g_type_ensure (NAUTILUS_TYPE_VIEW_CONTROLS);
 
     gtk_widget_init_template (GTK_WIDGET (self));
 }
@@ -515,8 +513,6 @@ nautilus_toolbar_class_init (NautilusToolbarClass *klass)
     gtk_widget_class_set_template_from_resource (widget_class,
                                                  "/org/gnome/nautilus/ui/nautilus-toolbar.ui");
 
-    gtk_widget_class_bind_template_child (widget_class, NautilusToolbar, view_menu);
-    gtk_widget_class_bind_template_child (widget_class, NautilusToolbar, view_split_button);
     gtk_widget_class_bind_template_child (widget_class, NautilusToolbar, app_button);
     gtk_widget_class_bind_template_child (widget_class, NautilusToolbar, undo_redo_section);
     gtk_widget_class_bind_template_child (widget_class, NautilusToolbar, toolbar_switcher);
@@ -595,46 +591,6 @@ slot_on_templates_menu_changed (NautilusToolbar    *self,
                                           menu);
 }
 
-static void
-on_slot_toolbar_menu_sections_changed (NautilusToolbar    *self,
-                                       GParamSpec         *param,
-                                       NautilusWindowSlot *slot)
-{
-    NautilusToolbarMenuSections *new_sections;
-    g_autoptr (GMenuItem) zoom_item = NULL;
-    g_autoptr (GMenuItem) sort_item = NULL;
-
-    new_sections = nautilus_window_slot_get_toolbar_menu_sections (slot);
-
-    gtk_widget_set_sensitive (self->view_split_button, (new_sections != NULL));
-    if (new_sections == NULL)
-    {
-        return;
-    }
-
-    /* Let's assume that sort section is the first item
-     * in view_menu, as per nautilus-toolbar.ui. */
-
-    sort_item = g_menu_item_new_from_model (self->view_menu, 0);
-    g_menu_remove (G_MENU (self->view_menu), 0);
-    g_menu_item_set_section (sort_item, new_sections->sort_section);
-    g_menu_insert_item (G_MENU (self->view_menu), 0, sort_item);
-}
-
-
-static void
-disconnect_toolbar_menu_sections_change_handler (NautilusToolbar *self)
-{
-    if (self->window_slot == NULL)
-    {
-        return;
-    }
-
-    g_signal_handlers_disconnect_by_func (self->window_slot,
-                                          G_CALLBACK (on_slot_toolbar_menu_sections_changed),
-                                          self);
-}
-
 /* Called from on_window_slot_destroyed(), since bindings and signal handlers
  * are automatically removed once the slot goes away.
  */
@@ -643,7 +599,6 @@ nautilus_toolbar_set_window_slot_real (NautilusToolbar    *self,
                                        NautilusWindowSlot *slot)
 {
     g_autoptr (GList) children = NULL;
-    GtkWidget *toggle_button;
 
     self->window_slot = slot;
 
@@ -657,9 +612,6 @@ nautilus_toolbar_set_window_slot_real (NautilusToolbar    *self,
                                                        self, "searching",
                                                        G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
 
-        on_slot_toolbar_menu_sections_changed (self, NULL, self->window_slot);
-        g_signal_connect_swapped (self->window_slot, "notify::toolbar-menu-sections",
-                                  G_CALLBACK (on_slot_toolbar_menu_sections_changed), self);
         g_signal_connect_swapped (self->window_slot, "notify::extensions-background-menu",
                                   G_CALLBACK (slot_on_extensions_background_menu_changed), self);
         g_signal_connect_swapped (self->window_slot, "notify::templates-menu",
@@ -696,7 +648,6 @@ nautilus_toolbar_set_window_slot (NautilusToolbar    *self,
 
     g_clear_pointer (&self->search_binding, g_binding_unbind);
 
-    disconnect_toolbar_menu_sections_change_handler (self);
     if (self->window_slot != NULL)
     {
         g_signal_handlers_disconnect_by_data (self->window_slot, self);
diff --git a/src/nautilus-view-controls.c b/src/nautilus-view-controls.c
new file mode 100644
index 000000000..bd8e6d761
--- /dev/null
+++ b/src/nautilus-view-controls.c
@@ -0,0 +1,187 @@
+/*
+ * Copyright (C) 2022 The GNOME project contributors
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "nautilus-view-controls.h"
+
+#include "nautilus-toolbar-menu-sections.h"
+#include "nautilus-window.h"
+
+struct _NautilusViewControls
+{
+    AdwBin parent_instance;
+
+    GtkWidget *view_split_button;
+    GMenuModel *view_menu;
+
+    NautilusWindowSlot *window_slot;
+};
+
+G_DEFINE_FINAL_TYPE (NautilusViewControls, nautilus_view_controls, ADW_TYPE_BIN);
+
+
+enum
+{
+    PROP_0,
+    PROP_WINDOW_SLOT,
+    N_PROPS
+};
+
+static GParamSpec *properties[N_PROPS];
+
+static void
+on_slot_toolbar_menu_sections_changed (NautilusViewControls *self,
+                                       GParamSpec           *param,
+                                       NautilusWindowSlot   *slot)
+{
+    NautilusToolbarMenuSections *new_sections;
+    g_autoptr (GMenuItem) zoom_item = NULL;
+    g_autoptr (GMenuItem) sort_item = NULL;
+
+    new_sections = nautilus_window_slot_get_toolbar_menu_sections (slot);
+
+    gtk_widget_set_sensitive (self->view_split_button, (new_sections != NULL));
+    if (new_sections == NULL)
+    {
+        return;
+    }
+
+    /* Let's assume that sort section is the first item
+     * in view_menu, as per nautilus-toolbar.ui. */
+
+    sort_item = g_menu_item_new_from_model (self->view_menu, 0);
+    g_menu_remove (G_MENU (self->view_menu), 0);
+    g_menu_item_set_section (sort_item, new_sections->sort_section);
+    g_menu_insert_item (G_MENU (self->view_menu), 0, sort_item);
+}
+
+static void
+disconnect_toolbar_menu_sections_change_handler (NautilusViewControls *self)
+{
+    if (self->window_slot == NULL)
+    {
+        return;
+    }
+
+    g_signal_handlers_disconnect_by_func (self->window_slot,
+                                          G_CALLBACK (on_slot_toolbar_menu_sections_changed),
+                                          self);
+}
+
+
+static void
+nautilus_view_controls_set_window_slot (NautilusViewControls *self,
+                                        NautilusWindowSlot   *window_slot)
+{
+    g_return_if_fail (NAUTILUS_IS_VIEW_CONTROLS (self));
+    g_return_if_fail (window_slot == NULL || NAUTILUS_IS_WINDOW_SLOT (window_slot));
+
+    if (self->window_slot == window_slot)
+    {
+        return;
+    }
+
+    disconnect_toolbar_menu_sections_change_handler (self);
+
+    self->window_slot = window_slot;
+
+    if (self->window_slot != NULL)
+    {
+        on_slot_toolbar_menu_sections_changed (self, NULL, self->window_slot);
+        g_signal_connect_swapped (self->window_slot, "notify::toolbar-menu-sections",
+                                  G_CALLBACK (on_slot_toolbar_menu_sections_changed), self);
+    }
+
+    g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_WINDOW_SLOT]);
+}
+
+static void
+nautilus_view_controls_get_property (GObject    *object,
+                                     guint       prop_id,
+                                     GValue     *value,
+                                     GParamSpec *pspec)
+{
+    NautilusViewControls *self = NAUTILUS_VIEW_CONTROLS (object);
+
+    switch (prop_id)
+    {
+        case PROP_WINDOW_SLOT:
+        {
+            g_value_set_object (value, G_OBJECT (self->window_slot));
+            break;
+        }
+
+        default:
+        {
+            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+        }
+    }
+}
+
+static void
+nautilus_view_controls_set_property (GObject      *object,
+                                     guint         prop_id,
+                                     const GValue *value,
+                                     GParamSpec   *pspec)
+{
+    NautilusViewControls *self = NAUTILUS_VIEW_CONTROLS (object);
+
+    switch (prop_id)
+    {
+        case PROP_WINDOW_SLOT:
+        {
+            nautilus_view_controls_set_window_slot (self, NAUTILUS_WINDOW_SLOT (g_value_get_object (value)));
+            break;
+        }
+
+        default:
+        {
+            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+        }
+    }
+}
+
+static void
+nautilus_view_controls_finalize (GObject *obj)
+{
+    NautilusViewControls *self = NAUTILUS_VIEW_CONTROLS (obj);
+
+    if (self->window_slot != NULL)
+    {
+        g_signal_handlers_disconnect_by_data (self->window_slot, self);
+        self->window_slot = NULL;
+    }
+
+    G_OBJECT_CLASS (nautilus_view_controls_parent_class)->finalize (obj);
+}
+
+static void
+nautilus_view_controls_class_init (NautilusViewControlsClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS (klass);
+    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+    object_class->finalize = nautilus_view_controls_finalize;
+    object_class->get_property = nautilus_view_controls_get_property;
+    object_class->set_property = nautilus_view_controls_set_property;
+
+    properties[PROP_WINDOW_SLOT] = g_param_spec_object ("window-slot",
+                                                        NULL, NULL,
+                                                        NAUTILUS_TYPE_WINDOW_SLOT,
+                                                        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | 
G_PARAM_EXPLICIT_NOTIFY);
+
+    g_object_class_install_properties (object_class, N_PROPS, properties);
+
+    gtk_widget_class_set_template_from_resource (widget_class,
+                                                 "/org/gnome/nautilus/ui/nautilus-view-controls.ui");
+    gtk_widget_class_bind_template_child (widget_class, NautilusViewControls, view_menu);
+    gtk_widget_class_bind_template_child (widget_class, NautilusViewControls, view_split_button);
+}
+
+static void
+nautilus_view_controls_init (NautilusViewControls *self)
+{
+    gtk_widget_init_template (GTK_WIDGET (self));
+}
diff --git a/src/nautilus-view-controls.h b/src/nautilus-view-controls.h
new file mode 100644
index 000000000..1b54737aa
--- /dev/null
+++ b/src/nautilus-view-controls.h
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2022 The GNOME project contributors
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+#include <libadwaita-1/adwaita.h>
+
+#include "nautilus-window-slot.h"
+
+G_BEGIN_DECLS
+
+#define NAUTILUS_TYPE_VIEW_CONTROLS (nautilus_view_controls_get_type())
+
+G_DECLARE_FINAL_TYPE (NautilusViewControls, nautilus_view_controls, NAUTILUS, VIEW_CONTROLS, AdwBin)
+
+G_END_DECLS
diff --git a/src/resources/nautilus.gresource.xml b/src/resources/nautilus.gresource.xml
index a4273405e..6057d7eac 100644
--- a/src/resources/nautilus.gresource.xml
+++ b/src/resources/nautilus.gresource.xml
@@ -8,6 +8,7 @@
     <file>ui/nautilus-toolbar.ui</file>
     <file>ui/nautilus-history-controls.ui</file>
     <file>ui/nautilus-progress-indicator.ui</file>
+    <file>ui/nautilus-view-controls.ui</file>
     <file>ui/nautilus-toolbar-view-menu.ui</file>
     <file>ui/nautilus-column-chooser.ui</file>
     <file>ui/nautilus-list-view-column-editor.ui</file>
diff --git a/src/resources/ui/nautilus-toolbar.ui b/src/resources/ui/nautilus-toolbar.ui
index aaca5de08..b5cb3918c 100644
--- a/src/resources/ui/nautilus-toolbar.ui
+++ b/src/resources/ui/nautilus-toolbar.ui
@@ -1,24 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <requires lib="gtk" version="4.0"/>
-  <menu id="view_menu">
-    <section>
-      <attribute name="label" translatable="yes" context="menu item" comments="Translators: a menu item in a 
group of sorting options in a toolbar menu, with criterions such as &quot;A-Z&quot; or &quot;Last 
Modified&quot;.">Sort</attribute>
-      <!--
-           Sort section.
-
-           The toolbar code assumes this is the second item of this menu model.
-           Its contents is provided by the view.
-      -->
-    </section>
-    <section>
-      <item>
-        <attribute name="label" translatable="yes">_Visible Columns…</attribute>
-        <attribute name="action">view.visible-columns</attribute>
-        <attribute name="hidden-when">action-missing</attribute>
-      </item>
-    </section>
-  </menu>
   <menu id="app_menu">
     <section>
       <item>
@@ -193,22 +175,8 @@
           </object>
         </child>
         <child type="end">
-          <object class="AdwSplitButton" id="view_split_button">
-            <property name="halign">center</property>
-            <property name="valign">center</property>
-            <property name="tooltip_text" translatable="yes" comments="Translators: This is a noun, meaning 
the options pertaining to the view.">View options</property>
-            <property name="action_name">slot.files-view-mode-toggle</property>
-            <property name="menu-model">view_menu</property>
-            <binding name="icon-name">
-              <lookup name="icon-name">
-                <lookup name="window-slot">NautilusToolbar</lookup>
-              </lookup>
-            </binding>
-            <binding name="tooltip-text">
-              <lookup name="tooltip">
-                <lookup name="window-slot">NautilusToolbar</lookup>
-              </lookup>
-            </binding>
+          <object class="NautilusViewControls">
+            <property name="window-slot" bind-source="NautilusToolbar" bind-property="window-slot" 
bind-flags="sync-create"/>
           </object>
         </child>
         <child type="end">
diff --git a/src/resources/ui/nautilus-view-controls.ui b/src/resources/ui/nautilus-view-controls.ui
new file mode 100644
index 000000000..888f19a57
--- /dev/null
+++ b/src/resources/ui/nautilus-view-controls.ui
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <menu id="view_menu">
+    <section>
+      <attribute name="label" translatable="yes" context="menu item" comments="Translators: a menu item in a 
group of sorting options in a toolbar menu, with criterions such as &quot;A-Z&quot; or &quot;Last 
Modified&quot;.">Sort</attribute>
+      <!--
+           Sort section.
+
+           The toolbar code assumes this is the second item of this menu model.
+           Its contents is provided by the view.
+      -->
+    </section>
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">_Visible Columns…</attribute>
+        <attribute name="action">view.visible-columns</attribute>
+        <attribute name="hidden-when">action-missing</attribute>
+      </item>
+    </section>
+  </menu>
+  <template class="NautilusViewControls" parent="AdwBin">
+    <property name="child">
+      <object class="AdwSplitButton" id="view_split_button">
+        <property name="halign">center</property>
+        <property name="valign">center</property>
+        <property name="tooltip_text" translatable="yes">Toggle view</property>
+        <property name="action_name">slot.files-view-mode-toggle</property>
+        <property name="menu-model">view_menu</property>
+        <binding name="icon-name">
+          <lookup name="icon-name">
+            <lookup name="window-slot">NautilusViewControls</lookup>
+          </lookup>
+        </binding>
+        <binding name="tooltip-text">
+          <lookup name="tooltip">
+            <lookup name="window-slot">NautilusViewControls</lookup>
+          </lookup>
+        </binding>
+      </object>
+    </property>
+  </template>
+</interface>


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