[nautilus/wip/cdavis/toolbar-refactor: 3/7] toolbar: Use expressions for icon and tooltip bindings




commit f91a5e03baee55d61b937ab932a0a7e73eb91052
Author: Christopher Davis <christopherdavis gnome org>
Date:   Mon Jul 18 18:37:04 2022 -0400

    toolbar: Use expressions for icon and tooltip bindings
    
    GTK4 provides expressions that we can use to automatically
    look up and set properties through a chain of other properties.
    This means that instead of manually binding the tooltip
    and the icon name in the C files, we can use the `<binding>`
    tag and chain `<lookup>` tags.

 src/nautilus-toolbar.c               | 37 ------------------------------------
 src/resources/ui/nautilus-toolbar.ui | 10 ++++++++++
 2 files changed, 10 insertions(+), 37 deletions(-)
---
diff --git a/src/nautilus-toolbar.c b/src/nautilus-toolbar.c
index 63989ce4b..f7bf7ec62 100644
--- a/src/nautilus-toolbar.c
+++ b/src/nautilus-toolbar.c
@@ -95,9 +95,7 @@ struct _NautilusToolbar
 
     /* active slot & bindings */
     NautilusWindowSlot *window_slot;
-    GBinding *icon_binding;
     GBinding *search_binding;
-    GBinding *tooltip_binding;
 };
 
 enum
@@ -971,7 +969,6 @@ on_window_slot_destroyed (gpointer  data,
     /* The window slot was finalized, and the binding has already been removed.
      * Null it here, so that dispose() does not trip over itself when removing it.
      */
-    self->icon_binding = NULL;
     self->search_binding = NULL;
 
     nautilus_toolbar_set_window_slot_real (self, NULL);
@@ -1085,7 +1082,6 @@ nautilus_toolbar_dispose (GObject *object)
 
     self = NAUTILUS_TOOLBAR (object);
 
-    g_clear_pointer (&self->icon_binding, g_binding_unbind);
     g_clear_pointer (&self->search_binding, g_binding_unbind);
     g_clear_pointer (&self->back_menu, gtk_widget_unparent);
     g_clear_pointer (&self->forward_menu, gtk_widget_unparent);
@@ -1307,26 +1303,6 @@ disconnect_toolbar_menu_sections_change_handler (NautilusToolbar *self)
                                           self);
 }
 
-static gboolean
-nautilus_toolbar_view_toggle_tooltip_transform_to (GBinding     *binding,
-                                                   const GValue *from_value,
-                                                   GValue       *to_value,
-                                                   gpointer      user_data)
-{
-    const gchar *tooltip;
-
-    tooltip = g_value_get_string (from_value);
-
-    /* As per design decision, we let the previous used tooltip if no
-     * view menu is available */
-    if (tooltip)
-    {
-        g_value_set_string (to_value, tooltip);
-    }
-
-    return TRUE;
-}
-
 /* Called from on_window_slot_destroyed(), since bindings and signal handlers
  * are automatically removed once the slot goes away.
  */
@@ -1344,18 +1320,6 @@ nautilus_toolbar_set_window_slot_real (NautilusToolbar    *self,
                            on_window_slot_destroyed,
                            self);
 
-        self->icon_binding = g_object_bind_property (self->window_slot, "icon-name",
-                                                     self->view_split_button, "icon-name",
-                                                     G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
-
-        self->tooltip_binding = g_object_bind_property_full (self->window_slot, "tooltip",
-                                                             self->view_split_button, "tooltip-text",
-                                                             G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE,
-                                                             (GBindingTransformFunc) 
nautilus_toolbar_view_toggle_tooltip_transform_to,
-                                                             NULL,
-                                                             self,
-                                                             NULL);
-
         self->search_binding = g_object_bind_property (self->window_slot, "searching",
                                                        self, "searching",
                                                        G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
@@ -1397,7 +1361,6 @@ nautilus_toolbar_set_window_slot (NautilusToolbar    *self,
         return;
     }
 
-    g_clear_pointer (&self->icon_binding, g_binding_unbind);
     g_clear_pointer (&self->search_binding, g_binding_unbind);
 
     disconnect_toolbar_menu_sections_change_handler (self);
diff --git a/src/resources/ui/nautilus-toolbar.ui b/src/resources/ui/nautilus-toolbar.ui
index a37ad2059..ccf4ee8fe 100644
--- a/src/resources/ui/nautilus-toolbar.ui
+++ b/src/resources/ui/nautilus-toolbar.ui
@@ -232,6 +232,16 @@
             <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">NautilusToolbar</lookup>
+              </lookup>
+            </binding>
+            <binding name="tooltip-text">
+              <lookup name="tooltip">
+                <lookup name="window-slot">NautilusToolbar</lookup>
+              </lookup>
+            </binding>
           </object>
         </child>
         <child type="end">


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