[gnome-notes/go-back-mr-64: 5/6] search-toolbar: simplify it
- From: Isaque Galdino de Araujo <igaldino src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-notes/go-back-mr-64: 5/6] search-toolbar: simplify it
- Date: Thu, 27 Aug 2020 22:36:57 +0000 (UTC)
commit 55475dbc01b49f397efb927ca8ed910b591d3a1b
Author: Isaque Galdino <igaldino gmail com>
Date: Thu Aug 20 09:52:30 2020 -0300
search-toolbar: simplify it
src/bjb-search-toolbar.c | 101 +++++++++++++++++------------------------------
src/bjb-search-toolbar.h | 12 +++---
src/bjb-window-base.c | 3 --
3 files changed, 43 insertions(+), 73 deletions(-)
---
diff --git a/src/bjb-search-toolbar.c b/src/bjb-search-toolbar.c
index c1ed11c..9c92368 100644
--- a/src/bjb-search-toolbar.c
+++ b/src/bjb-search-toolbar.c
@@ -47,30 +47,32 @@ static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
struct _BjbSearchToolbar
{
- HdySearchBar parent_instance;
+ HdySearchBar parent_instance;
- GtkWidget *entry;
- gchar *needle;
- BjbController *controller;
-
- /* Signals */
- gulong key_pressed;
- gulong text_id;
-
-
- GtkWidget *window;
+ GtkEntry *entry;
+ gchar *needle;
+ BjbController *controller;
+ BjbWindowBase *window;
};
G_DEFINE_TYPE (BjbSearchToolbar, bjb_search_toolbar, HDY_TYPE_SEARCH_BAR)
static gboolean
-on_key_pressed (GtkWidget *widget,GdkEvent *event,gpointer user_data)
+on_key_press_event_cb (BjbSearchToolbar *self,
+ GdkEvent *event)
{
- HdySearchBar *search_bar = HDY_SEARCH_BAR (user_data);
+ if (bjb_window_base_get_view_type (self->window) == BJB_WINDOW_BASE_NOTE_VIEW)
+ return FALSE;
- return hdy_search_bar_handle_event (search_bar, event);
+ return hdy_search_bar_handle_event (HDY_SEARCH_BAR (self), event);
}
+static void
+on_search_changed_cb (BjbSearchToolbar *self)
+{
+ bjb_controller_set_needle (BJB_CONTROLLER (self->controller),
+ gtk_entry_get_text (self->entry));
+}
static void
bjb_search_toolbar_get_property (GObject *object,
@@ -94,7 +96,6 @@ bjb_search_toolbar_get_property (GObject *object,
}
}
-
static void
bjb_search_toolbar_set_property (GObject *object,
guint property_id,
@@ -117,76 +118,49 @@ bjb_search_toolbar_set_property (GObject *object,
}
}
-static void
-action_entry_text_change_callback (GtkEntry *entry,
- BjbSearchToolbar *self)
-{
- bjb_controller_set_needle (BJB_CONTROLLER (self->controller),
- gtk_entry_get_text (entry));
-}
-
-void
-bjb_search_toolbar_disconnect (BjbSearchToolbar *self)
-{
- if (self->key_pressed)
- g_signal_handler_disconnect (self->window, self->key_pressed);
- if (self->text_id)
- g_signal_handler_disconnect (self->entry, self->text_id);
-
- self->key_pressed = 0;
- self->text_id = 0;
-}
-
-void
-bjb_search_toolbar_connect (BjbSearchToolbar *self)
-{
- /* Connect to set the text */
- if (self->key_pressed == 0)
- self->key_pressed = g_signal_connect(self->window,"key-press-event",
- G_CALLBACK(on_key_pressed), self);
-
-
- if (self->text_id == 0)
- self->text_id = g_signal_connect (self->entry, "search-changed",
- G_CALLBACK (action_entry_text_change_callback), self);
-}
-
static void
bjb_search_toolbar_constructed (GObject *obj)
{
- BjbSearchToolbar *self = BJB_SEARCH_TOOLBAR(obj);
+ BjbSearchToolbar *self = BJB_SEARCH_TOOLBAR (obj);
G_OBJECT_CLASS (bjb_search_toolbar_parent_class)->constructed (obj);
- /* Get the needle from controller */
self->needle = bjb_controller_get_needle (self->controller);
-
if (self->needle && g_strcmp0 (self->needle, "") != 0)
{
- gtk_entry_set_text (GTK_ENTRY (self->entry), self->needle);
+ gtk_entry_set_text (self->entry, self->needle);
gtk_editable_set_position (GTK_EDITABLE (self->entry), -1);
}
-}
+ g_signal_connect_object (self->window,
+ "key-press-event",
+ G_CALLBACK (on_key_press_event_cb),
+ self,
+ G_CONNECT_SWAPPED);
+
+ g_signal_connect_swapped (self->entry,
+ "search-changed",
+ G_CALLBACK (on_search_changed_cb),
+ self);
+}
static void
bjb_search_toolbar_init (BjbSearchToolbar *self)
{
- self->entry = gtk_search_entry_new ();
- hdy_search_bar_connect_entry (HDY_SEARCH_BAR (self), GTK_ENTRY (self->entry));
+ self->entry = GTK_ENTRY (gtk_search_entry_new ());
+ hdy_search_bar_connect_entry (HDY_SEARCH_BAR (self), self->entry);
gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (self->entry));
gtk_widget_show (GTK_WIDGET (self->entry));
}
-
static void
bjb_search_toolbar_class_init (BjbSearchToolbarClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
- object_class->get_property = bjb_search_toolbar_get_property ;
- object_class->set_property = bjb_search_toolbar_set_property ;
- object_class->constructed = bjb_search_toolbar_constructed ;
+ object_class->get_property = bjb_search_toolbar_get_property;
+ object_class->set_property = bjb_search_toolbar_set_property;
+ object_class->constructed = bjb_search_toolbar_constructed;
properties[PROP_WINDOW] = g_param_spec_object ("window",
"Window",
@@ -207,14 +181,13 @@ bjb_search_toolbar_class_init (BjbSearchToolbarClass *class)
g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
}
-
BjbSearchToolbar *
-bjb_search_toolbar_new (GtkWidget *window,
+bjb_search_toolbar_new (BjbWindowBase *window,
BjbController *controller)
{
return g_object_new (BJB_TYPE_SEARCH_TOOLBAR,
- "window", window,
- "controller", controller,
+ "window", window,
+ "controller", controller,
NULL);
}
diff --git a/src/bjb-search-toolbar.h b/src/bjb-search-toolbar.h
index 6113871..ad682fa 100644
--- a/src/bjb-search-toolbar.h
+++ b/src/bjb-search-toolbar.h
@@ -21,17 +21,17 @@
#define HANDY_USE_UNSTABLE_API
#include <handy.h>
+#include "bjb-controller.h"
+#include "bjb-window-base.h"
+
G_BEGIN_DECLS
#define BJB_TYPE_SEARCH_TOOLBAR (bjb_search_toolbar_get_type ())
G_DECLARE_FINAL_TYPE (BjbSearchToolbar, bjb_search_toolbar, BJB, SEARCH_TOOLBAR, HdySearchBar)
-BjbSearchToolbar *bjb_search_toolbar_new (GtkWidget *window,
- BjbController *controller);
-
-void bjb_search_toolbar_disconnect (BjbSearchToolbar *self);
-
-void bjb_search_toolbar_connect (BjbSearchToolbar *self);
+BjbSearchToolbar *bjb_search_toolbar_new (BjbWindowBase *window,
+ BjbController *controller);
G_END_DECLS
+
diff --git a/src/bjb-window-base.c b/src/bjb-window-base.c
index cb2b327..a4be0be 100644
--- a/src/bjb-window-base.c
+++ b/src/bjb-window-base.c
@@ -571,13 +571,11 @@ bjb_window_base_switch_to (BjbWindowBase *self, BjbWindowViewType type)
*/
case BJB_WINDOW_BASE_MAIN_VIEW:
- bjb_search_toolbar_connect (self->search_bar);
gtk_widget_show (GTK_WIDGET (self->search_bar));
gtk_stack_set_visible_child_name (self->stack, "main-view");
break;
case BJB_WINDOW_BASE_ARCHIVE_VIEW:
- bjb_search_toolbar_connect (self->search_bar);
gtk_widget_show (GTK_WIDGET (self->search_bar));
gtk_stack_set_visible_child_name (self->stack, "main-view");
break;
@@ -636,7 +634,6 @@ bjb_window_base_switch_to_item (BjbWindowBase *self, BijiItem *item)
{
GtkWidget *w = GTK_WIDGET (self);
- bjb_search_toolbar_disconnect (self->search_bar);
destroy_note_if_needed (self);
if (BIJI_IS_NOTE_OBJ (item))
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]