[gtk/stack-fixes: 2/2] wip: stack: use a selection model



commit 2c7a55c068ca2055c649638af771e2260a02c53d
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Feb 8 17:01:30 2019 -0500

    wip: stack: use a selection model
    
    This is an attempt to make the stack and stack switcher
    communicate via a selection model. It works, but some
    aspects of this are suboptimal.

 gtk/gtkstack.c         |  70 ++++++++++++++++++
 gtk/gtkstack.h         |   5 ++
 gtk/gtkstackswitcher.c | 195 +++++++++++++++++--------------------------------
 3 files changed, 144 insertions(+), 126 deletions(-)
---
diff --git a/gtk/gtkstack.c b/gtk/gtkstack.c
index 338199c319..edc1d44b85 100644
--- a/gtk/gtkstack.c
+++ b/gtk/gtkstack.c
@@ -30,6 +30,7 @@
 #include "gtksettingsprivate.h"
 #include "gtksnapshot.h"
 #include "gtkwidgetprivate.h"
+#include "gtksingleselectionprivate.h"
 #include "a11y/gtkstackaccessible.h"
 #include "a11y/gtkstackaccessibleprivate.h"
 #include <math.h>
@@ -138,6 +139,8 @@ typedef struct {
 
   GtkStackTransitionType active_transition_type;
 
+  GtkSelectionModel *pages;
+
 } GtkStackPrivate;
 
 static void gtk_stack_buildable_interface_init (GtkBuildableIface *iface);
@@ -2294,3 +2297,70 @@ gtk_stack_init (GtkStack *stack)
   priv->transition_duration = 200;
   priv->transition_type = GTK_STACK_TRANSITION_TYPE_NONE;
 }
+
+static gboolean
+transform_to (GBinding *binding,
+              const GValue *from_value,
+              GValue *to_value,
+              gpointer user_data)
+{
+  GtkStack *stack = user_data;
+  GtkStackPrivate *priv = gtk_stack_get_instance_private (stack);
+  GtkWidget *child;
+  guint i;
+
+  child = g_value_get_object (from_value);
+  for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (priv->pages)); i++)
+    {
+      if (g_list_model_get_item (G_LIST_MODEL (priv->pages), i) == child)
+        {
+          g_value_set_uint (to_value, i);
+          return TRUE;
+        }
+    }
+
+  g_value_set_uint (to_value, GTK_INVALID_LIST_POSITION);
+  return TRUE;
+}
+
+static gboolean
+transform_from (GBinding *binding,
+              const GValue *from_value,
+              GValue *to_value,
+              gpointer user_data)
+{
+  GtkStack *stack = user_data;
+  GtkStackPrivate *priv = gtk_stack_get_instance_private (stack);
+  guint selected;
+
+  selected = g_value_get_uint (from_value);
+  if (selected == GTK_INVALID_LIST_POSITION)
+    g_value_set_object (to_value, NULL);
+  else
+    {
+      GtkWidget *child;
+      child = g_list_model_get_item (G_LIST_MODEL (priv->pages), selected);
+      g_value_set_object (to_value, child);
+    }
+  return TRUE;
+}
+
+GtkSelectionModel *
+gtk_stack_get_pages (GtkStack *stack)
+{
+  GtkStackPrivate *priv = gtk_stack_get_instance_private (stack);
+
+  g_return_val_if_fail (GTK_IS_STACK (stack), NULL);
+
+  if (priv->pages)
+    return g_object_ref (priv->pages);
+
+  priv->pages = GTK_SELECTION_MODEL (gtk_single_selection_new (gtk_widget_observe_children (GTK_WIDGET 
(stack))));
+  g_object_add_weak_pointer (G_OBJECT (priv->pages), (gpointer *)&priv->pages);
+  g_object_bind_property_full (stack, "visible-child",
+                               priv->pages, "selected",
+                               G_BINDING_BIDIRECTIONAL|G_BINDING_SYNC_CREATE,
+                               transform_to, transform_from, stack, NULL);
+
+  return priv->pages;
+}
diff --git a/gtk/gtkstack.h b/gtk/gtkstack.h
index 6379328041..658ef8bb6a 100644
--- a/gtk/gtkstack.h
+++ b/gtk/gtkstack.h
@@ -27,6 +27,7 @@
 #endif
 
 #include <gtk/gtkcontainer.h>
+#include <gtk/gtkselectionmodel.h>
 
 G_BEGIN_DECLS
 
@@ -155,6 +156,10 @@ void                   gtk_stack_set_interpolate_size    (GtkStack *stack,
                                                           gboolean  interpolate_size);
 GDK_AVAILABLE_IN_ALL
 gboolean               gtk_stack_get_interpolate_size    (GtkStack *stack);
+
+GDK_AVAILABLE_IN_ALL
+GtkSelectionModel *    gtk_stack_get_pages               (GtkStack *stack);
+
 G_END_DECLS
 
 #endif
diff --git a/gtk/gtkstackswitcher.c b/gtk/gtkstackswitcher.c
index ab9d91b27d..dfe44501cf 100644
--- a/gtk/gtkstackswitcher.c
+++ b/gtk/gtkstackswitcher.c
@@ -29,6 +29,7 @@
 #include "gtkwidgetprivate.h"
 #include "gtktypebuiltins.h"
 #include "gtkimage.h"
+#include "gtksingleselectionprivate.h"
 
 /**
  * SECTION:gtkstackswitcher
@@ -66,6 +67,7 @@ typedef struct _GtkStackSwitcherPrivate GtkStackSwitcherPrivate;
 struct _GtkStackSwitcherPrivate
 {
   GtkStack *stack;
+  GtkSelectionModel *pages;
   GHashTable *buttons;
   gboolean in_child_changed;
   GtkWidget *switch_button;
@@ -89,7 +91,6 @@ gtk_stack_switcher_init (GtkStackSwitcher *switcher)
 
   priv = gtk_stack_switcher_get_instance_private (switcher);
 
-  priv->stack = NULL;
   priv->buttons = g_hash_table_new (g_direct_hash, g_direct_equal);
 
   context = gtk_widget_get_style_context (GTK_WIDGET (switcher));
@@ -106,15 +107,16 @@ static void
 on_button_clicked (GtkWidget        *widget,
                    GtkStackSwitcher *self)
 {
-  GtkWidget *child;
   GtkStackSwitcherPrivate *priv;
 
   priv = gtk_stack_switcher_get_instance_private (self);
 
   if (!priv->in_child_changed)
     {
-      child = g_object_get_data (G_OBJECT (widget), "stack-child");
-      gtk_stack_set_visible_child (priv->stack, child);
+      guint index;
+
+      index = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (widget), "child-index"));
+      gtk_single_selection_set_selected (GTK_SINGLE_SELECTION (priv->pages), index);
     }
 }
 
@@ -169,6 +171,7 @@ update_button (GtkStackSwitcher *self,
   gboolean needs_attention;
   GtkStackSwitcherPrivate *priv;
   GtkStyleContext *context;
+
   priv = gtk_stack_switcher_get_instance_private (self);
 
   g_object_get (gtk_stack_get_page (priv->stack, widget),
@@ -206,7 +209,7 @@ on_visible_updated (GtkWidget        *widget,
 }
 
 static void
-on_title_icon_updated (GtkStackPage     *page,
+on_page_props_updated (GtkStackPage     *page,
                        GParamSpec       *pspec,
                        GtkStackSwitcher *self)
 {
@@ -221,53 +224,6 @@ on_title_icon_updated (GtkStackPage     *page,
   update_button (self, widget, button);
 }
 
-static void
-on_position_updated (GtkStackPage     *page,
-                     GParamSpec       *pspec,
-                     GtkStackSwitcher *self)
-{
-  GtkWidget *widget;
-  GtkWidget *button;
-  gint position;
-  GtkStackSwitcherPrivate *priv;
-
-  priv = gtk_stack_switcher_get_instance_private (self);
-
-  widget = gtk_stack_page_get_child (page);
-  button = g_hash_table_lookup (priv->buttons, widget);
-
-  gtk_container_child_get (GTK_CONTAINER (priv->stack), widget,
-                           "position", &position,
-                           NULL);
-
-  if (position == 0)
-    gtk_box_reorder_child_after (GTK_BOX (self), button, NULL);
-  else
-    {
-      GtkWidget *sibling = gtk_widget_get_first_child (GTK_WIDGET (self));
-      int i;
-      for (i = 1; i < position; i++)
-        sibling = gtk_widget_get_next_sibling (sibling);
-      gtk_box_reorder_child_after (GTK_BOX (self), button, sibling);
-    }
-}
-
-static void
-on_needs_attention_updated (GtkStackPage     *page,
-                            GParamSpec       *pspec,
-                            GtkStackSwitcher *self)
-{
-  GtkWidget *widget;
-  GtkWidget *button;
-  GtkStackSwitcherPrivate *priv;
-
-  priv = gtk_stack_switcher_get_instance_private (self);
-
-  widget = gtk_stack_page_get_child (page);
-  button = g_hash_table_lookup (priv->buttons, widget);
-  update_button (self, widget, button);
-}
-
 static void
 remove_switch_timer (GtkStackSwitcher *self)
 {
@@ -356,6 +312,7 @@ gtk_stack_switcher_drag_leave (GtkWidget *widget,
 
 static void
 add_child (GtkWidget        *widget,
+           guint             position,
            GtkStackSwitcher *self)
 {
   GtkWidget *button;
@@ -382,54 +339,33 @@ add_child (GtkWidget        *widget,
 
   gtk_container_add (GTK_CONTAINER (self), button);
 
-  g_object_set_data (G_OBJECT (button), "stack-child", widget);
+  g_object_set_data (G_OBJECT (button), "child-index", GUINT_TO_POINTER (position));
   g_signal_connect (button, "clicked", G_CALLBACK (on_button_clicked), self);
   g_signal_connect (widget, "notify::visible", G_CALLBACK (on_visible_updated), self);
-  g_signal_connect (page, "notify::title", G_CALLBACK (on_title_icon_updated), self);
-  g_signal_connect (page, "notify::icon-name", G_CALLBACK (on_title_icon_updated), self);
-  g_signal_connect (page, "notify::position", G_CALLBACK (on_position_updated), self);
-  g_signal_connect (page, "notify::needs-attention", G_CALLBACK (on_needs_attention_updated), self);
+  g_signal_connect (page, "notify", G_CALLBACK (on_page_props_updated), self);
 
   g_hash_table_insert (priv->buttons, widget, button);
 }
 
 static void
-remove_child (GtkWidget        *widget,
-              GtkStackSwitcher *self)
+populate_switcher (GtkStackSwitcher *self)
 {
+  GtkStackSwitcherPrivate *priv = gtk_stack_switcher_get_instance_private (self);
+  guint i;
+  guint selected;
   GtkWidget *button;
-  GtkStackSwitcherPrivate *priv;
-
-  priv = gtk_stack_switcher_get_instance_private (self);
+  GtkWidget *widget;
 
-  if (priv->stack)
+  for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (priv->pages)); i++)
     {
-      GtkStackPage *page = gtk_stack_get_page (priv->stack, widget);
-      if (page)
-        {
-          g_signal_handlers_disconnect_by_func (page, on_title_icon_updated, self);
-          g_signal_handlers_disconnect_by_func (page, on_position_updated, self);
-          g_signal_handlers_disconnect_by_func (page, on_needs_attention_updated, self);
-        }
-      g_signal_handlers_disconnect_by_func (widget, on_visible_updated, self);
+      widget = g_list_model_get_item (G_LIST_MODEL (priv->pages), i);
+      add_child (widget, i, self);
     }
-  button = g_hash_table_lookup (priv->buttons, widget);
-  gtk_container_remove (GTK_CONTAINER (self), button);
-  g_hash_table_remove (priv->buttons, widget);
-}
-
-static void
-populate_switcher (GtkStackSwitcher *self)
-{
-  GtkStackSwitcherPrivate *priv;
-  GtkWidget *widget, *button;
-
-  priv = gtk_stack_switcher_get_instance_private (self);
-  gtk_container_foreach (GTK_CONTAINER (priv->stack), (GtkCallback)add_child, self);
 
-  widget = gtk_stack_get_visible_child (priv->stack);
-  if (widget)
+  selected = gtk_single_selection_get_selected (GTK_SINGLE_SELECTION (priv->pages));
+  if (selected != GTK_INVALID_LIST_POSITION)
     {
+      widget = g_list_model_get_item (G_LIST_MODEL (priv->pages), selected);
       button = g_hash_table_lookup (priv->buttons, widget);
       priv->in_child_changed = TRUE;
       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
@@ -440,24 +376,53 @@ populate_switcher (GtkStackSwitcher *self)
 static void
 clear_switcher (GtkStackSwitcher *self)
 {
-  GtkStackSwitcherPrivate *priv;
+  GtkStackSwitcherPrivate *priv = gtk_stack_switcher_get_instance_private (self);
+  GHashTableIter iter;
+  GtkWidget *widget;
+  GtkWidget *button;
 
-  priv = gtk_stack_switcher_get_instance_private (self);
-  gtk_container_foreach (GTK_CONTAINER (priv->stack), (GtkCallback)remove_child, self);
+  g_hash_table_iter_init (&iter, priv->buttons);
+  while (g_hash_table_iter_next (&iter, (gpointer *)&widget, (gpointer *)&button))
+    {
+      gtk_container_remove (GTK_CONTAINER (self), button);
+      g_hash_table_iter_remove (&iter);
+      if (priv->stack)
+        {
+          GtkStackPage *page = gtk_stack_get_page (priv->stack, widget);
+          if (page)
+            g_signal_handlers_disconnect_by_func (page, on_page_props_updated, self);
+          g_signal_handlers_disconnect_by_func (widget, on_visible_updated, self);
+        }
+    }
 }
 
 static void
-on_child_changed (GtkWidget        *widget,
-                  GParamSpec       *pspec,
-                  GtkStackSwitcher *self)
+items_changed_cb (GListModel       *model,
+                  guint             position,
+                  guint             removed,
+                  guint             added,
+                  GtkStackSwitcher *switcher)
 {
+  /* FIXME: we can do better */
+  clear_switcher (switcher);
+  populate_switcher (switcher);
+}
+
+static void
+selection_changed_cb (GtkSelectionModel *model,
+                      GParamSpec        *pspec,
+                      GtkStackSwitcher  *switcher)
+{
+  GtkStackSwitcherPrivate *priv = gtk_stack_switcher_get_instance_private (switcher);
+  guint selected;
   GtkWidget *child;
   GtkWidget *button;
-  GtkStackSwitcherPrivate *priv;
 
-  priv = gtk_stack_switcher_get_instance_private (self);
+  selected = gtk_single_selection_get_selected (GTK_SINGLE_SELECTION (model));
+  if (selected == GTK_INVALID_LIST_POSITION)
+    return;
 
-  child = gtk_stack_get_visible_child (GTK_STACK (widget));
+  child = g_list_model_get_item (G_LIST_MODEL (model), selected);
   button = g_hash_table_lookup (priv->buttons, child);
   if (button != NULL)
     {
@@ -467,48 +432,24 @@ on_child_changed (GtkWidget        *widget,
     }
 }
 
-static void
-on_stack_child_added (GtkContainer     *container,
-                      GtkWidget        *widget,
-                      GtkStackSwitcher *self)
-{
-  add_child (widget, self);
-}
-
-static void
-on_stack_child_removed (GtkContainer     *container,
-                        GtkWidget        *widget,
-                        GtkStackSwitcher *self)
-{
-  remove_child (widget, self);
-}
-
 static void
 disconnect_stack_signals (GtkStackSwitcher *switcher)
 {
-  GtkStackSwitcherPrivate *priv;
+  GtkStackSwitcherPrivate *priv = gtk_stack_switcher_get_instance_private (switcher);
 
-  priv = gtk_stack_switcher_get_instance_private (switcher);
-  g_signal_handlers_disconnect_by_func (priv->stack, on_stack_child_added, switcher);
-  g_signal_handlers_disconnect_by_func (priv->stack, on_stack_child_removed, switcher);
-  g_signal_handlers_disconnect_by_func (priv->stack, on_child_changed, switcher);
+  g_signal_handlers_disconnect_by_func (priv->pages, items_changed_cb, switcher);
+  g_signal_handlers_disconnect_by_func (priv->pages, selection_changed_cb, switcher);
   g_signal_handlers_disconnect_by_func (priv->stack, disconnect_stack_signals, switcher);
 }
 
 static void
 connect_stack_signals (GtkStackSwitcher *switcher)
 {
-  GtkStackSwitcherPrivate *priv;
+  GtkStackSwitcherPrivate *priv = gtk_stack_switcher_get_instance_private (switcher);
 
-  priv = gtk_stack_switcher_get_instance_private (switcher);
-  g_signal_connect_after (priv->stack, "add",
-                          G_CALLBACK (on_stack_child_added), switcher);
-  g_signal_connect_after (priv->stack, "remove",
-                          G_CALLBACK (on_stack_child_removed), switcher);
-  g_signal_connect (priv->stack, "notify::visible-child",
-                    G_CALLBACK (on_child_changed), switcher);
-  g_signal_connect_swapped (priv->stack, "destroy",
-                            G_CALLBACK (disconnect_stack_signals), switcher);
+  g_signal_connect (priv->pages, "items-changed", G_CALLBACK (items_changed_cb), switcher);
+  g_signal_connect (priv->pages, "notify::selected", G_CALLBACK (selection_changed_cb), switcher);
+  g_signal_connect_swapped (priv->stack, "destroy", G_CALLBACK (disconnect_stack_signals), switcher);
 }
 
 /**
@@ -537,10 +478,12 @@ gtk_stack_switcher_set_stack (GtkStackSwitcher *switcher,
       disconnect_stack_signals (switcher);
       clear_switcher (switcher);
       g_clear_object (&priv->stack);
+      g_clear_object (&priv->pages);
     }
   if (stack)
     {
       priv->stack = g_object_ref (stack);
+      priv->pages = gtk_stack_get_pages (stack);
       populate_switcher (switcher);
       connect_stack_signals (switcher);
     }


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