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




commit 4650ce88fbbe167b0faf1c4918de7623c8db6596
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               | 39 ------------------------------------
 src/resources/ui/nautilus-toolbar.ui | 10 +++++++++
 2 files changed, 10 insertions(+), 39 deletions(-)
---
diff --git a/src/nautilus-toolbar.c b/src/nautilus-toolbar.c
index d80850a42..9a54a4f07 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.
  */
@@ -1345,20 +1321,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);
-
-        /* HACK. We shouldn't be poking at internal children. But alas, no other option. */
-        toggle_button = gtk_widget_get_parent (adw_split_button_get_child (ADW_SPLIT_BUTTON 
(self->view_split_button)));
-        self->tooltip_binding = g_object_bind_property_full (self->window_slot, "tooltip",
-                                                             toggle_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);
@@ -1400,7 +1362,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 b482f7da3..682fff316 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" 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>
         </child>
         <child type="end">


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