[gtk/matthiasc/for-master: 6/6] demo: Clear filter when search is stopped



commit a888e1cb654c721ec5e8a2a7322703a5c6d23a48
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Jul 12 16:47:51 2020 -0400

    demo: Clear filter when search is stopped
    
    When the search entry disappears on Escape,
    reset the search string to "", so we don't
    end up with a filtered list and no obvious
    way to remove the filtering.

 demos/gtk-demo/main.c  | 14 +++++++++++++-
 demos/gtk-demo/main.ui |  2 +-
 2 files changed, 14 insertions(+), 2 deletions(-)
---
diff --git a/demos/gtk-demo/main.c b/demos/gtk-demo/main.c
index 11e7f0f1b6..5bad5e80aa 100644
--- a/demos/gtk-demo/main.c
+++ b/demos/gtk-demo/main.c
@@ -1070,13 +1070,23 @@ get_child_model (gpointer item,
   return NULL;
 }
 
+static void
+clear_search (GtkSearchBar *bar)
+{
+  if (!gtk_search_bar_get_search_mode (bar))
+    {
+      GtkWidget *entry = gtk_search_bar_get_child (GTK_SEARCH_BAR (bar));
+      gtk_editable_set_text (GTK_EDITABLE (entry), "");
+    }
+}
+
 static void
 activate (GApplication *app)
 {
   GtkBuilder *builder;
   GListModel *listmodel;
   GtkTreeListModel *treemodel;
-  GtkWidget *window, *listview, *search_entry;
+  GtkWidget *window, *listview, *search_entry, *search_bar;
   GtkFilterListModel *filter_model;
   GtkFilter *filter;
 
@@ -1099,6 +1109,8 @@ activate (GApplication *app)
   toplevel = GTK_WIDGET (window);
   listview = GTK_WIDGET (gtk_builder_get_object (builder, "listview"));
   g_signal_connect (listview, "activate", G_CALLBACK (activate_cb), window);
+  search_bar = GTK_WIDGET (gtk_builder_get_object (builder, "searchbar"));
+  g_signal_connect (search_bar, "notify::search-mode-enabled", G_CALLBACK (clear_search), NULL);
 
   listmodel = create_demo_model ();
   treemodel = gtk_tree_list_model_new (FALSE,
diff --git a/demos/gtk-demo/main.ui b/demos/gtk-demo/main.ui
index 975e3ea1bc..5d97687ab8 100644
--- a/demos/gtk-demo/main.ui
+++ b/demos/gtk-demo/main.ui
@@ -60,7 +60,7 @@
                 <property name="orientation">vertical</property>
 
                 <child>
-                  <object class="GtkSearchBar">
+                  <object class="GtkSearchBar" id="searchbar">
                     <property name="key-capture-widget">window</property>
                     <child>
                       <object class="GtkSearchEntry" id="search-entry"/>


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