[gnome-builder] libide/tweaks: set button visibility from window property



commit 7faaedc9ca412d176c14b6e3d0c80b0b3a7eb533
Author: Christian Hergert <chergert redhat com>
Date:   Wed Aug 3 10:25:40 2022 -0700

    libide/tweaks: set button visibility from window property
    
     * Add IdeTweaksWindow:can-navigate-back
     * Bind visibility of search button to !can-navigate-back
     * Bind visibility of back button to can-navigate-back
    
    Is there a way to do invert-boolean with property expressions?

 src/libide/tweaks/ide-tweaks-window.c  | 32 +++++++++++++++++++++++++++++---
 src/libide/tweaks/ide-tweaks-window.h  | 16 +++++++++-------
 src/libide/tweaks/ide-tweaks-window.ui |  2 ++
 3 files changed, 40 insertions(+), 10 deletions(-)
---
diff --git a/src/libide/tweaks/ide-tweaks-window.c b/src/libide/tweaks/ide-tweaks-window.c
index 087788bb5..6ed2505db 100644
--- a/src/libide/tweaks/ide-tweaks-window.c
+++ b/src/libide/tweaks/ide-tweaks-window.c
@@ -36,10 +36,13 @@ struct _IdeTweaksWindow
 
   GtkStack  *panel_stack;
   GtkStack  *panel_list_stack;
+
+  guint      can_navigate_back : 1;
 };
 
 enum {
   PROP_0,
+  PROP_CAN_NAVIGATE_BACK,
   PROP_TWEAKS,
   N_PROPS
 };
@@ -66,7 +69,7 @@ static void
 ide_tweaks_window_update_actions (IdeTweaksWindow *self)
 {
   GtkWidget *visible_child;
-  gboolean navigate_back_enabled = FALSE;
+  gboolean can_navigate_back = FALSE;
 
   g_assert (IDE_IS_TWEAKS_WINDOW (self));
 
@@ -75,10 +78,15 @@ ide_tweaks_window_update_actions (IdeTweaksWindow *self)
       IdeTweaksPanelList *list = IDE_TWEAKS_PANEL_LIST (visible_child);
       IdeTweaksItem *item = ide_tweaks_panel_list_get_item (list);
 
-      navigate_back_enabled = !IDE_IS_TWEAKS (item);
+      can_navigate_back = !IDE_IS_TWEAKS (item);
     }
 
-  gtk_widget_action_set_enabled (GTK_WIDGET (self), "navigation.back", navigate_back_enabled);
+  if (can_navigate_back != self->can_navigate_back)
+    {
+      self->can_navigate_back = can_navigate_back;
+      gtk_widget_action_set_enabled (GTK_WIDGET (self), "navigation.back", can_navigate_back);
+      g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_CAN_NAVIGATE_BACK]);
+    }
 }
 
 static void
@@ -236,6 +244,10 @@ ide_tweaks_window_get_property (GObject    *object,
 
   switch (prop_id)
     {
+    case PROP_CAN_NAVIGATE_BACK:
+      g_value_set_boolean (value, ide_tweaks_window_get_can_navigate_back (self));
+      break;
+
     case PROP_TWEAKS:
       g_value_set_object (value, ide_tweaks_window_get_tweaks (self));
       break;
@@ -274,6 +286,12 @@ ide_tweaks_window_class_init (IdeTweaksWindowClass *klass)
   object_class->get_property = ide_tweaks_window_get_property;
   object_class->set_property = ide_tweaks_window_set_property;
 
+  properties[PROP_CAN_NAVIGATE_BACK] =
+    g_param_spec_boolean ("can-navigate-back", NULL, NULL,
+                          FALSE,
+                          (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+
+
   properties [PROP_TWEAKS] =
     g_param_spec_object ("tweaks", NULL, NULL,
                          IDE_TYPE_TWEAKS,
@@ -432,3 +450,11 @@ ide_tweaks_window_navigate_back (IdeTweaksWindow *self)
 
   g_warning ("Failed to lcoate parent panel list");
 }
+
+gboolean
+ide_tweaks_window_get_can_navigate_back (IdeTweaksWindow *self)
+{
+  g_return_val_if_fail (IDE_IS_TWEAKS_WINDOW (self), FALSE);
+
+  return self->can_navigate_back;
+}
diff --git a/src/libide/tweaks/ide-tweaks-window.h b/src/libide/tweaks/ide-tweaks-window.h
index 8fe4b869d..9448ed47e 100644
--- a/src/libide/tweaks/ide-tweaks-window.h
+++ b/src/libide/tweaks/ide-tweaks-window.h
@@ -36,16 +36,18 @@ IDE_AVAILABLE_IN_ALL
 G_DECLARE_FINAL_TYPE (IdeTweaksWindow, ide_tweaks_window, IDE, TWEAKS_WINDOW, AdwWindow)
 
 IDE_AVAILABLE_IN_ALL
-GtkWidget *ide_tweaks_window_new           (void);
+GtkWidget *ide_tweaks_window_new                   (void);
 IDE_AVAILABLE_IN_ALL
-IdeTweaks *ide_tweaks_window_get_tweaks    (IdeTweaksWindow *self);
+gboolean   ide_tweaks_window_get_can_navigate_back (IdeTweaksWindow *self);
 IDE_AVAILABLE_IN_ALL
-void       ide_tweaks_window_set_tweaks    (IdeTweaksWindow *self,
-                                            IdeTweaks       *tweaks);
+IdeTweaks *ide_tweaks_window_get_tweaks            (IdeTweaksWindow *self);
 IDE_AVAILABLE_IN_ALL
-void       ide_tweaks_window_navigate_to   (IdeTweaksWindow *self,
-                                            IdeTweaksItem   *item);
+void       ide_tweaks_window_set_tweaks            (IdeTweaksWindow *self,
+                                                    IdeTweaks       *tweaks);
 IDE_AVAILABLE_IN_ALL
-void       ide_tweaks_window_navigate_back (IdeTweaksWindow *self);
+void       ide_tweaks_window_navigate_to           (IdeTweaksWindow *self,
+                                                    IdeTweaksItem   *item);
+IDE_AVAILABLE_IN_ALL
+void       ide_tweaks_window_navigate_back         (IdeTweaksWindow *self);
 
 G_END_DECLS
diff --git a/src/libide/tweaks/ide-tweaks-window.ui b/src/libide/tweaks/ide-tweaks-window.ui
index 24892850d..c42d39b33 100644
--- a/src/libide/tweaks/ide-tweaks-window.ui
+++ b/src/libide/tweaks/ide-tweaks-window.ui
@@ -26,6 +26,7 @@
                 </property>
                 <child>
                   <object class="GtkButton" id="sidebar_previous_button">
+                    <property name="visible" bind-source="IdeTweaksWindow" bind-property="can-navigate-back" 
bind-flags="sync-create"/>
                     <property name="action-name">navigation.back</property>
                     <property name="icon-name">go-previous-symbolic</property>
                     <accessibility>
@@ -39,6 +40,7 @@
                 <child>
                   <object class="GtkToggleButton" id="sidebar_search_button">
                     <property name="icon-name">edit-find-symbolic</property>
+                    <property name="visible" bind-source="IdeTweaksWindow" bind-property="can-navigate-back" 
bind-flags="sync-create|invert-boolean"/>
                     <accessibility>
                       <property name="label" translatable="yes">Search Preferences</property>
                     </accessibility>


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