[gtk/stack-fixes: 4/13] sidebar: Use a model
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/stack-fixes: 4/13] sidebar: Use a model
- Date: Sat, 9 Feb 2019 18:10:34 +0000 (UTC)
commit e11373e7b8e546dbb8c3871a1f6c5063565d0f44
Author: Matthias Clasen <mclasen redhat com>
Date: Fri Feb 8 18:27:10 2019 -0500
sidebar: Use a model
Make GtkStackSidebar and GtkStack communicate via the
selection model that GtkStack now exposes. This is parallel
to the GtkStackSwitcher changes in the previous commit.
gtk/gtkstacksidebar.c | 180 +++++++++++++++++++++-----------------------------
1 file changed, 77 insertions(+), 103 deletions(-)
---
diff --git a/gtk/gtkstacksidebar.c b/gtk/gtkstacksidebar.c
index bf1b13759d..74a92ac002 100644
--- a/gtk/gtkstacksidebar.c
+++ b/gtk/gtkstacksidebar.c
@@ -28,6 +28,7 @@
#include "gtkscrolledwindow.h"
#include "gtkseparator.h"
#include "gtkstylecontext.h"
+#include "gtksingleselection.h"
#include "gtkprivate.h"
#include "gtkintl.h"
@@ -58,6 +59,7 @@ struct _GtkStackSidebarPrivate
{
GtkListBox *list;
GtkStack *stack;
+ GtkSelectionModel *pages;
GHashTable *rows;
gboolean in_child_changed;
};
@@ -129,30 +131,14 @@ sort_list (GtkListBoxRow *row1,
GtkListBoxRow *row2,
gpointer userdata)
{
- GtkStackSidebar *sidebar = GTK_STACK_SIDEBAR (userdata);
- GtkStackSidebarPrivate *priv = gtk_stack_sidebar_get_instance_private (sidebar);
- GtkWidget *item;
- GtkWidget *widget;
gint left = 0; gint right = 0;
if (row1)
- {
- item = gtk_bin_get_child (GTK_BIN (row1));
- widget = g_object_get_data (G_OBJECT (item), "stack-child");
- g_object_get (gtk_stack_get_page (GTK_STACK (priv->stack), widget),
- "position", &left,
- NULL);
- }
+ left = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (row1), "child-index"));
if (row2)
- {
- item = gtk_bin_get_child (GTK_BIN (row2));
- widget = g_object_get_data (G_OBJECT (item), "stack-child");
- g_object_get (gtk_stack_get_page (GTK_STACK (priv->stack), widget),
- "position", &right,
- NULL);
- }
+ right = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (row2), "child-index"));
if (left < right)
return -1;
@@ -170,8 +156,7 @@ gtk_stack_sidebar_row_selected (GtkListBox *box,
{
GtkStackSidebar *sidebar = GTK_STACK_SIDEBAR (userdata);
GtkStackSidebarPrivate *priv = gtk_stack_sidebar_get_instance_private (sidebar);
- GtkWidget *item;
- GtkWidget *widget;
+ guint index;
if (priv->in_child_changed)
return;
@@ -179,9 +164,8 @@ gtk_stack_sidebar_row_selected (GtkListBox *box,
if (!row)
return;
- item = gtk_bin_get_child (GTK_BIN (row));
- widget = g_object_get_data (G_OBJECT (item), "stack-child");
- gtk_stack_set_visible_child (priv->stack, widget);
+ index = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (row), "child-index"));
+ gtk_single_selection_set_selected (GTK_SINGLE_SELECTION (priv->pages), index);
}
static void
@@ -247,29 +231,34 @@ update_row (GtkStackSidebar *sidebar,
}
static void
-on_position_updated (GtkWidget *widget,
- GParamSpec *pspec,
- GtkStackSidebar *sidebar)
+on_visible_updated (GtkWidget *widget,
+ GParamSpec *pspec,
+ GtkStackSidebar *sidebar)
{
GtkStackSidebarPrivate *priv = gtk_stack_sidebar_get_instance_private (sidebar);
+ GtkWidget *row;
- gtk_list_box_invalidate_sort (priv->list);
+ row = g_hash_table_lookup (priv->rows, widget);
+ update_row (sidebar, widget, row);
}
static void
-on_child_updated (GtkWidget *widget,
- GParamSpec *pspec,
- GtkStackSidebar *sidebar)
+on_page_updated (GtkStackPage *page,
+ GParamSpec *pspec,
+ GtkStackSidebar *sidebar)
{
GtkStackSidebarPrivate *priv = gtk_stack_sidebar_get_instance_private (sidebar);
+ GtkWidget *widget;
GtkWidget *row;
+ widget = gtk_stack_page_get_child (page);
row = g_hash_table_lookup (priv->rows, widget);
update_row (sidebar, widget, row);
}
static void
add_child (GtkWidget *widget,
+ guint position,
GtkStackSidebar *sidebar)
{
GtkStackSidebarPrivate *priv = gtk_stack_sidebar_get_instance_private (sidebar);
@@ -277,10 +266,6 @@ add_child (GtkWidget *widget,
GtkWidget *row;
GtkStackPage *page;
- /* Check we don't actually already know about this widget */
- if (g_hash_table_lookup (priv->rows, widget))
- return;
-
/* Make a pretty item when we add kids */
item = gtk_label_new ("");
gtk_widget_set_halign (item, GTK_ALIGN_START);
@@ -293,79 +278,88 @@ add_child (GtkWidget *widget,
/* Hook up for events */
page = gtk_stack_get_page (GTK_STACK (priv->stack), widget);
- g_signal_connect (widget, "notify::visible",
- G_CALLBACK (on_child_updated), sidebar);
- g_signal_connect (page, "notify::title",
- G_CALLBACK (on_child_updated), sidebar);
- g_signal_connect (page, "notify::needs-attention",
- G_CALLBACK (on_child_updated), sidebar);
- g_signal_connect (page, "notify::position",
- G_CALLBACK (on_position_updated), sidebar);
-
- g_object_set_data (G_OBJECT (item), I_("stack-child"), widget);
+ g_signal_connect (widget, "notify::visible", G_CALLBACK (on_visible_updated), sidebar);
+ g_signal_connect (page, "notify", G_CALLBACK (on_page_updated), sidebar);
+
+ g_object_set_data (G_OBJECT (row), "child-index", GUINT_TO_POINTER (position));
g_hash_table_insert (priv->rows, widget, row);
gtk_container_add (GTK_CONTAINER (priv->list), row);
}
static void
-remove_child (GtkWidget *widget,
- GtkStackSidebar *sidebar)
+populate_sidebar (GtkStackSidebar *sidebar)
{
GtkStackSidebarPrivate *priv = gtk_stack_sidebar_get_instance_private (sidebar);
- GtkWidget *row;
-
- row = g_hash_table_lookup (priv->rows, widget);
- if (!row)
- return;
+ GtkWidget *widget, *row;
+ guint i, selected;
- 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 (GTK_STACK (priv->stack), widget);
- if (page)
- {
- g_signal_handlers_disconnect_by_func (page, on_child_updated, sidebar);
- g_signal_handlers_disconnect_by_func (page, on_position_updated, sidebar);
- }
+ widget = g_list_model_get_item (G_LIST_MODEL (priv->pages), i);
+ add_child (widget, i, sidebar);
}
- gtk_container_remove (GTK_CONTAINER (priv->list), row);
- g_hash_table_remove (priv->rows, 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);
+ row = g_hash_table_lookup (priv->rows, widget);
+ priv->in_child_changed = TRUE;
+ gtk_list_box_select_row (priv->list, GTK_LIST_BOX_ROW (row));
+ priv->in_child_changed = FALSE;
+ }
}
static void
-populate_sidebar (GtkStackSidebar *sidebar)
+clear_sidebar (GtkStackSidebar *sidebar)
{
GtkStackSidebarPrivate *priv = gtk_stack_sidebar_get_instance_private (sidebar);
- GtkWidget *widget, *row;
-
- gtk_container_foreach (GTK_CONTAINER (priv->stack), (GtkCallback)add_child, sidebar);
+ GHashTableIter iter;
+ GtkWidget *widget;
+ GtkWidget *row;
- widget = gtk_stack_get_visible_child (priv->stack);
- if (widget)
+ g_hash_table_iter_init (&iter, priv->rows);
+ while (g_hash_table_iter_next (&iter, (gpointer *)&widget, (gpointer *)&row))
{
- row = g_hash_table_lookup (priv->rows, widget);
- gtk_list_box_select_row (priv->list, GTK_LIST_BOX_ROW (row));
+ gtk_container_remove (GTK_CONTAINER (priv->list), row);
+ 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_updated, sidebar);
+ g_signal_handlers_disconnect_by_func (widget, on_visible_updated, sidebar);
+ }
}
}
static void
-clear_sidebar (GtkStackSidebar *sidebar)
+items_changed_cb (GListModel *model,
+ guint position,
+ guint removed,
+ guint added,
+ GtkStackSidebar *sidebar)
{
- GtkStackSidebarPrivate *priv = gtk_stack_sidebar_get_instance_private (sidebar);
-
- gtk_container_foreach (GTK_CONTAINER (priv->stack), (GtkCallback)remove_child, sidebar);
+ /* FIXME: we can do better */
+ clear_sidebar (sidebar);
+ populate_sidebar (sidebar);
}
static void
-on_child_changed (GtkWidget *widget,
- GParamSpec *pspec,
- GtkStackSidebar *sidebar)
+selection_changed_cb (GtkSelectionModel *model,
+ GParamSpec *pspec,
+ GtkStackSidebar *sidebar)
{
GtkStackSidebarPrivate *priv = gtk_stack_sidebar_get_instance_private (sidebar);
+ guint selected;
GtkWidget *child;
GtkWidget *row;
- child = gtk_stack_get_visible_child (GTK_STACK (widget));
+ selected = gtk_single_selection_get_selected (GTK_SINGLE_SELECTION (model));
+ if (selected == GTK_INVALID_LIST_POSITION)
+ return;
+
+ child = g_list_model_get_item (G_LIST_MODEL (model), selected);
row = g_hash_table_lookup (priv->rows, child);
if (row != NULL)
{
@@ -375,30 +369,13 @@ on_child_changed (GtkWidget *widget,
}
}
-static void
-on_stack_child_added (GtkContainer *container,
- GtkWidget *widget,
- GtkStackSidebar *sidebar)
-{
- add_child (widget, sidebar);
-}
-
-static void
-on_stack_child_removed (GtkContainer *container,
- GtkWidget *widget,
- GtkStackSidebar *sidebar)
-{
- remove_child (widget, sidebar);
-}
-
static void
disconnect_stack_signals (GtkStackSidebar *sidebar)
{
GtkStackSidebarPrivate *priv = gtk_stack_sidebar_get_instance_private (sidebar);
- g_signal_handlers_disconnect_by_func (priv->stack, on_stack_child_added, sidebar);
- g_signal_handlers_disconnect_by_func (priv->stack, on_stack_child_removed, sidebar);
- g_signal_handlers_disconnect_by_func (priv->stack, on_child_changed, sidebar);
+ g_signal_handlers_disconnect_by_func (priv->pages, items_changed_cb, sidebar);
+ g_signal_handlers_disconnect_by_func (priv->pages, selection_changed_cb, sidebar);
g_signal_handlers_disconnect_by_func (priv->stack, disconnect_stack_signals, sidebar);
}
@@ -407,14 +384,9 @@ connect_stack_signals (GtkStackSidebar *sidebar)
{
GtkStackSidebarPrivate *priv = gtk_stack_sidebar_get_instance_private (sidebar);
- g_signal_connect_after (priv->stack, "add",
- G_CALLBACK (on_stack_child_added), sidebar);
- g_signal_connect_after (priv->stack, "remove",
- G_CALLBACK (on_stack_child_removed), sidebar);
- g_signal_connect (priv->stack, "notify::visible-child",
- G_CALLBACK (on_child_changed), sidebar);
- g_signal_connect_swapped (priv->stack, "destroy",
- G_CALLBACK (disconnect_stack_signals), sidebar);
+ g_signal_connect (priv->pages, "items-changed", G_CALLBACK (items_changed_cb), sidebar);
+ g_signal_connect (priv->pages, "notify::selected", G_CALLBACK (selection_changed_cb), sidebar);
+ g_signal_connect_swapped (priv->stack, "destroy", G_CALLBACK (disconnect_stack_signals), sidebar);
}
static void
@@ -502,10 +474,12 @@ gtk_stack_sidebar_set_stack (GtkStackSidebar *sidebar,
disconnect_stack_signals (sidebar);
clear_sidebar (sidebar);
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_sidebar (sidebar);
connect_stack_signals (sidebar);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]