From b4096cee640002e5a63a3d6bb26cb07b7dd7621d Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 12 Jul 2020 18:24:04 -0400 Subject: [PATCH] gtk-demo: Work harder to filter the tree Arrange for an item to be visible if it either matches the filter or has children that do. --- demos/gtk-demo/main.c | 55 ++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/demos/gtk-demo/main.c b/demos/gtk-demo/main.c index 5bad5e80aa..880dcbaf1b 100644 --- a/demos/gtk-demo/main.c +++ b/demos/gtk-demo/main.c @@ -966,21 +966,9 @@ selection_cb (GtkSingleSelection *sel, } static gboolean -demo_filter_by_name (GtkTreeListRow *row, - GtkFilterListModel *model) +filter_demo (GtkDemo *demo) { - GtkDemo *demo; - guint i; - - /* Show all items if search is empty */ - if (!search_needle || !search_needle[0] || !*search_needle[0]) - return TRUE; - - g_assert (GTK_IS_TREE_LIST_ROW (row)); - g_assert (GTK_IS_FILTER_LIST_MODEL (model)); - - demo = gtk_tree_list_row_get_item (row); - g_assert (GTK_IS_DEMO (demo)); + int i; /* Show only if the name maches every needle */ for (i = 0; search_needle[i]; i++) @@ -997,6 +985,45 @@ demo_filter_by_name (GtkTreeListRow *row, return TRUE; } +static gboolean +demo_filter_by_name (GtkTreeListRow *row, + GtkFilterListModel *model) +{ + GListModel *children; + GtkDemo *demo; + guint i, n; + + /* Show all items if search is empty */ + if (!search_needle || !search_needle[0] || !*search_needle[0]) + return TRUE; + + g_assert (GTK_IS_TREE_LIST_ROW (row)); + g_assert (GTK_IS_FILTER_LIST_MODEL (model)); + + children = gtk_tree_list_row_get_children (row); + if (children) + { + n = g_list_model_get_n_items (children); + for (i = 0; i < n; i++) + { + demo = g_list_model_get_item (children, i); + g_assert (GTK_IS_DEMO (demo)); + + if (filter_demo (demo)) + { + g_object_unref (demo); + return TRUE; + } + g_object_unref (demo); + } + } + + demo = gtk_tree_list_row_get_item (row); + g_assert (GTK_IS_DEMO (demo)); + + return filter_demo (demo); +} + static void demo_search_changed_cb (GtkSearchEntry *entry, GtkFilter *filter)