[gnome-photos] photos-searchbar: Port to GtkSearchBar



commit 9bdf053a417c31e0be700c4c2716ddf95985bdf9
Author: Alessandro Bono <abono gnome org>
Date:   Fri Apr 7 18:58:49 2017 +0200

    photos-searchbar: Port to GtkSearchBar
    
    Original patch from Cosimo Cecchi for gnome-documents.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=776082

 src/photos-searchbar.c |  160 ++++++------------------------------------------
 src/photos-searchbar.h |    4 +-
 2 files changed, 22 insertions(+), 142 deletions(-)
---
diff --git a/src/photos-searchbar.c b/src/photos-searchbar.c
index dc85dbc..8ed870c 100644
--- a/src/photos-searchbar.c
+++ b/src/photos-searchbar.c
@@ -27,7 +27,6 @@
 #include "config.h"
 
 #include <glib.h>
-#include <libgd/gd.h>
 
 #include "photos-searchbar.h"
 
@@ -37,9 +36,6 @@ struct _PhotosSearchbarPrivate
   GAction *search;
   GtkWidget *search_container;
   GtkWidget *search_entry;
-  GtkWidget *toolbar;
-  gboolean in;
-  gboolean preedit_changed;
   gboolean search_change_blocked;
   gulong search_state_id;
 };
@@ -53,7 +49,7 @@ enum
 static guint signals[LAST_SIGNAL] = { 0 };
 
 
-G_DEFINE_TYPE_WITH_PRIVATE (PhotosSearchbar, photos_searchbar, GTK_TYPE_REVEALER);
+G_DEFINE_TYPE_WITH_PRIVATE (PhotosSearchbar, photos_searchbar, GTK_TYPE_SEARCH_BAR);
 
 
 static void
@@ -79,8 +75,7 @@ photos_searchbar_default_hide (PhotosSearchbar *self)
 
   priv = photos_searchbar_get_instance_private (self);
 
-  priv->in = FALSE;
-  gtk_revealer_set_reveal_child (GTK_REVEALER (self), FALSE);
+  gtk_search_bar_set_search_mode (GTK_SEARCH_BAR (self), FALSE);
 
   /* Clear all the search properties when hiding the entry */
   gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
@@ -90,17 +85,7 @@ photos_searchbar_default_hide (PhotosSearchbar *self)
 static void
 photos_searchbar_default_show (PhotosSearchbar *self)
 {
-  PhotosSearchbarPrivate *priv;
-  GdkDevice *event_device;
-
-  priv = photos_searchbar_get_instance_private (self);
-
-  event_device = gtk_get_current_event_device ();
-  gtk_revealer_set_reveal_child (GTK_REVEALER (self), TRUE);
-  priv->in = TRUE;
-
-  if (event_device != NULL)
-    gd_entry_focus_hack (priv->search_entry, event_device);
+  gtk_search_bar_set_search_mode (GTK_SEARCH_BAR (self), TRUE);
 }
 
 
@@ -117,56 +102,13 @@ photos_searchbar_enable_search (PhotosSearchbar *self, gboolean enable)
 }
 
 
-static gboolean
-photos_is_escape_event (PhotosSearchbar *self, GdkEventKey *event)
-{
-  return event->keyval == GDK_KEY_Escape;
-}
-
-
-static gboolean
-photos_is_keynav_event (PhotosSearchbar *self, GdkEventKey *event)
-{
-  return event->keyval == GDK_KEY_Up
-    || event->keyval == GDK_KEY_KP_Up
-    || event->keyval == GDK_KEY_Down
-    || event->keyval == GDK_KEY_KP_Down
-    || event->keyval == GDK_KEY_Left
-    || event->keyval == GDK_KEY_KP_Left
-    || event->keyval == GDK_KEY_Right
-    || event->keyval == GDK_KEY_KP_Right
-    || event->keyval == GDK_KEY_Home
-    || event->keyval == GDK_KEY_KP_Home
-    || event->keyval == GDK_KEY_End
-    || event->keyval == GDK_KEY_KP_End
-    || event->keyval == GDK_KEY_Page_Up
-    || event->keyval == GDK_KEY_KP_Page_Up
-    || event->keyval == GDK_KEY_Page_Down
-    || event->keyval == GDK_KEY_KP_Page_Down;
-}
-
-
-static gboolean
-photos_is_space_event (PhotosSearchbar *self, GdkEventKey *event)
-{
-  return event->keyval == GDK_KEY_space;
-}
-
-
-static gboolean
-photos_is_tab_event (PhotosSearchbar *self, GdkEventKey *event)
-{
-  return event->keyval == GDK_KEY_Tab || event->keyval == GDK_KEY_KP_Tab;
-}
-
-
 static void
-photos_searchbar_preedit_changed (PhotosSearchbar *self, const gchar *preedit)
+photos_searchbar_notify_search_mode_enabled (PhotosSearchbar *self)
 {
-  PhotosSearchbarPrivate *priv;
+  gboolean search_enabled;
 
-  priv = photos_searchbar_get_instance_private (self);
-  priv->preedit_changed = TRUE;
+  search_enabled = gtk_search_bar_get_search_mode (GTK_SEARCH_BAR (self));
+  photos_searchbar_enable_search (self, search_enabled);
 }
 
 
@@ -190,7 +132,6 @@ photos_searchbar_constructed (GObject *object)
   PhotosSearchbar *self = PHOTOS_SEARCHBAR (object);
   PhotosSearchbarPrivate *priv;
   GApplication *app;
-  GtkToolItem *item;
   GVariant *state;
 
   priv = photos_searchbar_get_instance_private (self);
@@ -199,16 +140,18 @@ photos_searchbar_constructed (GObject *object)
 
   PHOTOS_SEARCHBAR_GET_CLASS (self)->create_search_widgets (self);
 
-  item = gtk_tool_item_new ();
-  gtk_tool_item_set_expand (item, TRUE);
-  gtk_container_add (GTK_CONTAINER (item), priv->search_container);
-  gtk_toolbar_insert (GTK_TOOLBAR (priv->toolbar), item, 0);
+  gtk_container_add (GTK_CONTAINER (self), priv->search_container);
+  gtk_search_bar_connect_entry (GTK_SEARCH_BAR (self), GTK_ENTRY (priv->search_entry));
 
   g_signal_connect_swapped (priv->search_entry,
                             "search-changed",
                             G_CALLBACK (photos_searchbar_search_changed),
                             self);
 
+  g_signal_connect (self,
+                    "notify::search-mode-enabled",
+                    G_CALLBACK (photos_searchbar_notify_search_mode_enabled),
+                    NULL);
 
   app = g_application_get_default ();
   priv->search = g_action_map_lookup_action (G_ACTION_MAP (app), "search");
@@ -252,15 +195,6 @@ photos_searchbar_dispose (GObject *object)
 static void
 photos_searchbar_init (PhotosSearchbar *self)
 {
-  PhotosSearchbarPrivate *priv;
-  GtkStyleContext *context;
-
-  priv = photos_searchbar_get_instance_private (self);
-
-  priv->toolbar = gtk_toolbar_new ();
-  context = gtk_widget_get_style_context (priv->toolbar);
-  gtk_style_context_add_class (context, "search-bar");
-  gtk_container_add (GTK_CONTAINER (self), priv->toolbar);
 }
 
 
@@ -297,83 +231,29 @@ gboolean
 photos_searchbar_handle_event (PhotosSearchbar *self, GdkEventKey *event)
 {
   PhotosSearchbarPrivate *priv;
-  gboolean is_escape;
-  gboolean is_keynav;
-  gboolean is_space;
-  gboolean is_tab;
-  gboolean res;
   gboolean ret_val = GDK_EVENT_PROPAGATE;
-  gchar *new_text = NULL;
-  gchar *old_text = NULL;
+  gboolean search_mode_enabled;
 
   priv = photos_searchbar_get_instance_private (self);
 
-  if (gtk_widget_get_parent (GTK_WIDGET (self)) == NULL)
-    goto out;
+  search_mode_enabled = gtk_search_bar_get_search_mode (GTK_SEARCH_BAR (self));
 
   /* Skip if the search bar is shown and the focus is elsewhere */
-  if (priv->in && !gtk_widget_is_focus (priv->search_entry))
-    goto out;
-
-  is_escape = photos_is_escape_event (self, event);
-  is_keynav = photos_is_keynav_event (self, event);
-  is_space = photos_is_space_event (self, event);
-  is_tab = photos_is_tab_event (self, event);
-
-  /* Skip these if the search bar is hidden. */
-  if (!priv->in && (is_escape || is_keynav || is_space || is_tab))
+  if (search_mode_enabled && !gtk_widget_is_focus (priv->search_entry))
     goto out;
 
-  /* At this point, either the search bar is hidden and the event is
-   * neither escape nor keynav nor space; or the search bar is shown
-   * and has the focus. */
-
-  if (is_escape)
-    {
-      photos_searchbar_enable_search (self, FALSE);
-      ret_val = GDK_EVENT_STOP;
-      goto out;
-    }
-  else if (priv->in && event->keyval == GDK_KEY_Return)
+  if (search_mode_enabled && event->keyval == GDK_KEY_Return)
     {
       g_signal_emit (self, signals[ACTIVATE_RESULT], 0);
       ret_val = GDK_EVENT_STOP;
       goto out;
     }
 
-  if (!gtk_widget_get_realized (priv->search_entry))
-    gtk_widget_realize (priv->search_entry);
-
-  /* Since we can have keynav or space only when the search bar is
-   * show, we want to handle it. Otherwise it will hinder text
-   * input. However, we don't want to handle tabs so that focus can
-   * be shifted to other widgets.
-   */
-  ret_val = is_keynav || is_space;
-
-  priv->preedit_changed = FALSE;
-  g_signal_connect_swapped (priv->search_entry,
-                            "preedit-changed",
-                            G_CALLBACK (photos_searchbar_preedit_changed),
-                            self);
-
-  old_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->search_entry)));
-  res = gtk_widget_event (priv->search_entry, (GdkEvent *) event);
-  new_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->search_entry)));
-
-  g_signal_handlers_disconnect_by_func (priv->search_entry, photos_searchbar_preedit_changed, self);
-
-  if ((res && (g_strcmp0 (new_text, old_text) != 0)) || priv->preedit_changed)
-    {
-      ret_val = GDK_EVENT_STOP;
-
-      if (!priv->in)
-        photos_searchbar_enable_search (self, TRUE);
-    }
+  ret_val = gtk_search_bar_handle_event (GTK_SEARCH_BAR (self), (GdkEvent *) event);
+  if (ret_val == GDK_EVENT_STOP)
+    gtk_entry_grab_focus_without_selecting (GTK_ENTRY (priv->search_entry));
 
  out:
-  g_free (new_text);
-  g_free (old_text);
   return ret_val;
 }
 
diff --git a/src/photos-searchbar.h b/src/photos-searchbar.h
index c6cbbec..bc5eb2c 100644
--- a/src/photos-searchbar.h
+++ b/src/photos-searchbar.h
@@ -30,13 +30,13 @@
 G_BEGIN_DECLS
 
 #define PHOTOS_TYPE_SEARCHBAR (photos_searchbar_get_type ())
-G_DECLARE_DERIVABLE_TYPE (PhotosSearchbar, photos_searchbar, PHOTOS, SEARCHBAR, GtkRevealer);
+G_DECLARE_DERIVABLE_TYPE (PhotosSearchbar, photos_searchbar, PHOTOS, SEARCHBAR, GtkSearchBar);
 
 typedef struct _PhotosSearchbarPrivate PhotosSearchbarPrivate;
 
 struct _PhotosSearchbarClass
 {
-  GtkRevealerClass parent_class;
+  GtkSearchBarClass parent_class;
 
   /* virtual methods */
   void (*create_search_widgets) (PhotosSearchbar *self);


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