[gnome-builder/gnome-builder-3-18] gb-search-box: handle searchbox button sensitivity



commit 1bd752a1aef71adb744bc4033efea4331b92a168
Author: Sebastien Lafargue <slafargue gnome org>
Date:   Sat Oct 3 13:00:57 2015 +0200

    gb-search-box: handle searchbox button sensitivity
    
    Now, the searchbox button is insensitive when the entry is empty.
    
    When typing something then clearing it, we see an empty results popover.
    
    There's also some refactoring mostly because some signals
    are triggered twice.

 src/search/gb-search-box.c |   99 +++++++++++++++-----------------------------
 1 files changed, 33 insertions(+), 66 deletions(-)
---
diff --git a/src/search/gb-search-box.c b/src/search/gb-search-box.c
index 606e451..39158e3 100644
--- a/src/search/gb-search-box.c
+++ b/src/search/gb-search-box.c
@@ -110,31 +110,29 @@ gb_search_box_delay_cb (gpointer user_data)
 }
 
 static void
-gb_search_box_popover_closed (GbSearchBox *box,
-                              GtkPopover  *popover)
+gb_search_box_popover_set_visible (GbSearchBox *self,
+                                   gboolean     visible)
 {
-  g_return_if_fail (GB_IS_SEARCH_BOX (box));
-  g_return_if_fail (GTK_IS_POPOVER (popover));
+  gboolean entry_has_text;
 
-}
-
-static gboolean
-gb_search_box_entry_focus_in (GbSearchBox   *self,
-                              GdkEventFocus *focus,
-                              GtkWidget     *entry)
-{
-  const gchar *text;
+  g_return_if_fail (GB_IS_SEARCH_BOX (self));
 
-  g_return_val_if_fail (GB_IS_SEARCH_BOX (self), FALSE);
-  g_return_val_if_fail (focus, FALSE);
-  g_return_val_if_fail (GTK_IS_SEARCH_ENTRY (entry), FALSE);
+  entry_has_text = !!(gtk_entry_get_text_length (GTK_ENTRY (self->entry)));
 
-  text = gtk_entry_get_text (GTK_ENTRY (self->entry));
+  if (visible == gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->button)))
+    return;
 
-  if (!ide_str_empty0 (text))
-    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->button), TRUE);
+  if (visible && entry_has_text)
+    {
+      if (!gtk_widget_has_focus (GTK_WIDGET (self->entry)))
+        gtk_widget_grab_focus (GTK_WIDGET (self->entry));
 
-  return GDK_EVENT_PROPAGATE;
+      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->button), TRUE);
+    }
+  else if (!visible)
+    {
+      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->button), FALSE);
+    }
 }
 
 static void
@@ -145,8 +143,6 @@ gb_search_box_entry_activate (GbSearchBox    *self,
   g_return_if_fail (GTK_IS_SEARCH_ENTRY (entry));
 
   gb_search_display_activate (self->display);
-
-  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->button), FALSE);
   gtk_entry_set_text (GTK_ENTRY (self->entry), "");
 }
 
@@ -154,22 +150,23 @@ static void
 gb_search_box_entry_changed (GbSearchBox    *self,
                              GtkSearchEntry *entry)
 {
-  GtkToggleButton *button;
-  const gchar *text;
+  GtkWidget *button;
   gboolean active;
+  gboolean sensitive;
   guint delay_msec = SHORT_DELAY_TIMEOUT_MSEC;
 
   g_return_if_fail (GB_IS_SEARCH_BOX (self));
   g_return_if_fail (GTK_IS_SEARCH_ENTRY (entry));
 
-  button = GTK_TOGGLE_BUTTON (self->button);
-  text = gtk_entry_get_text (GTK_ENTRY (entry));
-  active = !ide_str_empty0 (text) ||
-           (self->delay_timeout != 0) ||
-           gtk_widget_has_focus (GTK_WIDGET (entry));
+  button = GTK_WIDGET (self->button);
+  active = gtk_widget_has_focus (GTK_WIDGET (entry)) || (self->delay_timeout != 0);
+  sensitive = !!(gtk_entry_get_text_length (GTK_ENTRY (self->entry)));
+
+  if (gtk_widget_get_sensitive (button) != sensitive)
+    gtk_widget_set_sensitive (button, sensitive);
 
-  if (gtk_toggle_button_get_active (button) != active)
-    gtk_toggle_button_set_active (button, active);
+  if (active)
+    gb_search_box_popover_set_visible (self, TRUE);
 
   if (!self->delay_timeout)
     {
@@ -200,7 +197,7 @@ gb_search_box_entry_key_press_event (GbSearchBox    *self,
     {
     case GDK_KEY_Escape:
       {
-        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->button), FALSE);
+        gb_search_box_popover_set_visible (self, FALSE);
         gtk_widget_grab_focus (gtk_widget_get_toplevel (GTK_WIDGET (entry)));
 
         return GDK_EVENT_STOP;
@@ -237,29 +234,10 @@ gb_search_box_display_result_activated (GbSearchBox     *self,
   g_return_if_fail (IDE_IS_SEARCH_RESULT (result));
   g_return_if_fail (GB_IS_SEARCH_DISPLAY (display));
 
-  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->button), FALSE);
   gtk_entry_set_text (GTK_ENTRY (self->entry), "");
 }
 
 static void
-gb_search_box_button_toggled (GbSearchBox     *self,
-                              GtkToggleButton *button)
-{
-  g_return_if_fail (GB_IS_SEARCH_BOX (self));
-  g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));
-
-  if (gtk_toggle_button_get_active (button))
-    {
-      if (!gtk_widget_has_focus (GTK_WIDGET (self->entry)))
-        gtk_widget_grab_focus (GTK_WIDGET (self->entry));
-    }
-  else
-    {
-      gtk_widget_hide (GTK_WIDGET (self->popover));
-    }
-}
-
-static void
 gb_search_box_grab_focus (GtkWidget *widget)
 {
   GbSearchBox *self = (GbSearchBox *)widget;
@@ -283,7 +261,10 @@ gb_search_box_workbench_set_focus (GbSearchBox *self,
        !gtk_widget_is_ancestor (focus, GTK_WIDGET (self->popover))))
     {
       gtk_entry_set_text (GTK_ENTRY (self->entry), "");
-      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->button), FALSE);
+    }
+  else
+    {
+      gb_search_box_popover_set_visible (self, TRUE);
     }
 }
 
@@ -297,6 +278,7 @@ gb_search_box_map (GtkWidget *widget)
 
   GTK_WIDGET_CLASS (gb_search_box_parent_class)->map (widget);
 
+  gtk_widget_set_sensitive (GTK_WIDGET (self->button), FALSE);
   toplevel = gtk_widget_get_toplevel (widget);
 
   if (GB_IS_WORKBENCH (toplevel))
@@ -338,16 +320,6 @@ gb_search_box_constructed (GObject *object)
 
   gtk_popover_set_relative_to (self->popover, GTK_WIDGET (self->entry));
 
-  g_signal_connect_object (self->popover,
-                           "closed",
-                           G_CALLBACK (gb_search_box_popover_closed),
-                           self,
-                           G_CONNECT_SWAPPED);
-  g_signal_connect_object (self->entry,
-                           "focus-in-event",
-                           G_CALLBACK (gb_search_box_entry_focus_in),
-                           self,
-                           G_CONNECT_SWAPPED);
   g_signal_connect_object (self->entry,
                            "activate",
                            G_CALLBACK (gb_search_box_entry_activate),
@@ -368,11 +340,6 @@ gb_search_box_constructed (GObject *object)
                            G_CALLBACK (gb_search_box_display_result_activated),
                            self,
                            G_CONNECT_SWAPPED);
-  g_signal_connect_object (self->button,
-                           "toggled",
-                           G_CALLBACK (gb_search_box_button_toggled),
-                           self,
-                           G_CONNECT_SWAPPED);
 }
 
 static void


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