From f32239df1f44ff73cfb81cec4bf631520677a65b Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 12 Jul 2020 16:25:24 -0400 Subject: [PATCH 1/3] searchbar: Add a key-capture-widget property This is handy to set up this relationship in ui files, and also makes it accessible in the inspector. --- gtk/gtksearchbar.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/gtk/gtksearchbar.c b/gtk/gtksearchbar.c index 5609f24088..c0624268c5 100644 --- a/gtk/gtksearchbar.c +++ b/gtk/gtksearchbar.c @@ -119,6 +119,7 @@ enum { PROP_SEARCH_MODE_ENABLED, PROP_SHOW_CLOSE_BUTTON, PROP_CHILD, + PROP_KEY_CAPTURE_WIDGET, LAST_PROPERTY }; @@ -206,6 +207,9 @@ gtk_search_bar_set_property (GObject *object, case PROP_CHILD: gtk_search_bar_set_child (bar, g_value_get_object (value)); break; + case PROP_KEY_CAPTURE_WIDGET: + gtk_search_bar_set_key_capture_widget (bar, g_value_get_object (value)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -231,6 +235,9 @@ gtk_search_bar_get_property (GObject *object, case PROP_CHILD: g_value_set_object (value, gtk_search_bar_get_child (bar)); break; + case PROP_KEY_CAPTURE_WIDGET: + g_value_set_object (value, gtk_search_bar_get_key_capture_widget (bar)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -318,6 +325,13 @@ gtk_search_bar_class_init (GtkSearchBarClass *klass) GTK_TYPE_WIDGET, GTK_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_EXPLICIT_NOTIFY); + widget_props[PROP_KEY_CAPTURE_WIDGET] + = g_param_spec_object ("key-capture-widget", + P_("Key Capture Widget"), + P_("Key Capture Widget"), + GTK_TYPE_WIDGET, + GTK_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_EXPLICIT_NOTIFY); + g_object_class_install_properties (object_class, LAST_PROPERTY, widget_props); gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT); @@ -610,6 +624,8 @@ gtk_search_bar_set_key_capture_widget (GtkSearchBar *bar, G_CALLBACK (capture_widget_key_handled), bar); gtk_widget_add_controller (widget, bar->capture_widget_controller); } + + g_object_notify_by_pspec (G_OBJECT (bar), widget_props[PROP_KEY_CAPTURE_WIDGET]); } /** From c80b33b2ad39d7f02414c4827b7dcf820dc741f4 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 12 Jul 2020 16:26:17 -0400 Subject: [PATCH 2/3] gtk-demo: Make the search bar appear for typing Having the search bar present from the start is a bit jarring. Make it appear when called for. --- demos/gtk-demo/main.ui | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demos/gtk-demo/main.ui b/demos/gtk-demo/main.ui index 3d18e18bf3..975e3ea1bc 100644 --- a/demos/gtk-demo/main.ui +++ b/demos/gtk-demo/main.ui @@ -61,7 +61,7 @@ - 1 + window From a888e1cb654c721ec5e8a2a7322703a5c6d23a48 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 12 Jul 2020 16:47:51 -0400 Subject: [PATCH 3/3] 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 @@ vertical - + window