[gnome-software] Simplify search button handling code



commit c1723f8a1d7c9454f3c0111c7f3dd71fd812fb3e
Author: Kalev Lember <klember redhat com>
Date:   Mon Nov 28 14:46:01 2016 +0100

    Simplify search button handling code
    
    Instead of having two separate search buttons created in the overview
    page and in the search page, just create one in the UI and do signal
    handling in gs-shell.
    
    This fixes various state tracking bugs where the two separate buttons
    got out of sync.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=775083

 src/gnome-software.ui   |   26 ++++++++++++++++++++++++++
 src/gs-common.c         |   47 -----------------------------------------------
 src/gs-common.h         |    1 -
 src/gs-shell-overview.c |   11 ++---------
 src/gs-shell-search.c   |   18 ++----------------
 src/gs-shell.c          |   41 +++++++++++++++++++++++++++++++++++++++++
 6 files changed, 71 insertions(+), 73 deletions(-)
---
diff --git a/src/gnome-software.ui b/src/gnome-software.ui
index 7014390..5333412 100644
--- a/src/gnome-software.ui
+++ b/src/gnome-software.ui
@@ -241,6 +241,32 @@
                 </child>
               </object>
             </child>
+            <child>
+              <object class="GtkToggleButton" id="search_button">
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">False</property>
+                <child internal-child="accessible">
+                  <object class="AtkObject" id="search_button_accessible">
+                    <property name="accessible-name" translatable="yes">Search</property>
+                  </object>
+                </child>
+                <style>
+                  <class name="image-button"/>
+                </style>
+                <child>
+                  <object class="GtkImage" id="search_image">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="icon_name">edit-find-symbolic</property>
+                    <property name="icon_size">1</property>
+                  </object>
+                </child>
+              </object>
+              <packing>
+                <property name="pack-type">end</property>
+              </packing>
+            </child>
           </object>
           <packing>
             <property name="expand">False</property>
diff --git a/src/gs-common.c b/src/gs-common.c
index 86425e2..0567972 100644
--- a/src/gs-common.c
+++ b/src/gs-common.c
@@ -564,53 +564,6 @@ gs_utils_show_error_dialog (GtkWindow *parent,
        gtk_widget_show (dialog);
 }
 
-static void
-gs_search_button_cb (GtkToggleButton *toggle_button, GtkSearchBar *search_bar)
-{
-       gtk_search_bar_set_search_mode (search_bar,
-                                       gtk_toggle_button_get_active (toggle_button));
-}
-
-static void
-gs_search_mode_enabled_cb (GtkSearchBar *search_bar,
-                          GParamSpec *pspec,
-                          GtkToggleButton *toggle_button)
-{
-       gtk_toggle_button_set_active (toggle_button,
-                                     gtk_search_bar_get_search_mode (search_bar));
-}
-
-/**
- * gs_search_button_new:
- *
- * Creates a new search toggle button.
- */
-GtkWidget *
-gs_search_button_new (GtkSearchBar *search_bar)
-{
-       GtkWidget *widget;
-       GtkWidget *im;
-
-       widget = gtk_toggle_button_new ();
-       im = gtk_image_new_from_icon_name ("edit-find-symbolic", GTK_ICON_SIZE_BUTTON);
-       gtk_widget_set_visible (im, TRUE);
-       gtk_container_add (GTK_CONTAINER (widget), im);
-       gtk_widget_set_visible (widget, TRUE);
-
-       if (search_bar != NULL) {
-               /* show the search bar when clicked */
-               g_signal_connect (widget, "clicked",
-                                 G_CALLBACK (gs_search_button_cb),
-                                 search_bar);
-               /* set the button enabled when search appears */
-               g_signal_connect (search_bar, "notify::search-mode-enabled",
-                                 G_CALLBACK (gs_search_mode_enabled_cb),
-                                 widget);
-       }
-
-       return widget;
-}
-
 /**
  * gs_utils_get_error_value:
  * @error: A GError
diff --git a/src/gs-common.h b/src/gs-common.h
index 0ee765c..d0025e2 100644
--- a/src/gs-common.h
+++ b/src/gs-common.h
@@ -64,7 +64,6 @@ gchar         *gs_utils_build_unique_id_kind  (AsAppKind       kind,
                                                 const gchar    *id);
 gboolean        gs_utils_list_has_app_fuzzy    (GsAppList      *list,
                                                 GsApp          *app);
-GtkWidget      *gs_search_button_new           (GtkSearchBar   *search_bar);
 
 G_END_DECLS
 
diff --git a/src/gs-shell-overview.c b/src/gs-shell-overview.c
index 73a4d5b..d7cab5b 100644
--- a/src/gs-shell-overview.c
+++ b/src/gs-shell-overview.c
@@ -51,7 +51,6 @@ typedef struct
        gboolean                 loading_categories;
        gboolean                 empty;
        gchar                   *category_of_day;
-       GtkWidget               *search_button;
        GHashTable              *category_hash;         /* id : GsCategory */
        GSettings               *settings;
 
@@ -584,7 +583,8 @@ gs_shell_overview_switch_to (GsPage *page, gboolean scroll_up)
        }
 
        /* we hid the search bar */
-       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->search_button), FALSE);
+       widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "search_button"));
+       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), FALSE);
 
        widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "buttonbox_main"));
        gtk_widget_show (widget);
@@ -766,7 +766,6 @@ gs_shell_overview_setup (GsShellOverview *self,
                         GCancellable *cancellable)
 {
        GsShellOverviewPrivate *priv = gs_shell_overview_get_instance_private (self);
-       GtkSearchBar *search_bar;
        GtkAdjustment *adj;
        GtkWidget *tile;
        gint i;
@@ -807,12 +806,6 @@ gs_shell_overview_setup (GsShellOverview *self,
        g_signal_connect (priv->categories_expander_button_up, "clicked",
                          G_CALLBACK (gs_shell_overview_categories_expander_up_cb), self);
 
-       /* search button */
-       search_bar = GTK_SEARCH_BAR (gtk_builder_get_object (priv->builder,
-                                                            "search_bar"));
-       priv->search_button = gs_search_button_new (search_bar);
-       gs_page_set_header_end_widget (GS_PAGE (self), priv->search_button);
-
        /* chain up */
        gs_page_setup (GS_PAGE (self),
                       shell,
diff --git a/src/gs-shell-search.c b/src/gs-shell-search.c
index 4d3c967..168e186 100644
--- a/src/gs-shell-search.c
+++ b/src/gs-shell-search.c
@@ -44,7 +44,6 @@ struct _GsShellSearch
        gchar                   *appid_to_show;
        gchar                   *value;
        guint                    waiting_id;
-       GtkWidget               *search_button;
 
        GtkWidget               *list_box_search;
        GtkWidget               *scrolledwindow_search;
@@ -257,7 +256,8 @@ gs_shell_search_switch_to (GsPage *page, gboolean scroll_up)
        gtk_widget_show (widget);
 
        /* hardcode */
-       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->search_button), TRUE);
+       widget = GTK_WIDGET (gtk_builder_get_object (self->builder, "search_button"));
+       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
 
        if (scroll_up) {
                GtkAdjustment *adj;
@@ -384,14 +384,6 @@ gs_shell_search_app_removed (GsPage *page, GsApp *app)
        gs_shell_search_reload (page);
 }
 
-static void
-gs_shell_search_search_button_cb (GtkButton *button, GsShellSearch *self)
-{
-       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
-               return;
-       gs_shell_change_mode (self->shell, GS_SHELL_MODE_OVERVIEW, NULL, TRUE);
-}
-
 void
 gs_shell_search_setup (GsShellSearch *self,
                       GsShell *shell,
@@ -421,12 +413,6 @@ gs_shell_search_setup (GsShellSearch *self,
                                    gs_shell_search_sort_func,
                                    self, NULL);
 
-       /* search button */
-       self->search_button = gs_search_button_new (NULL);
-       gs_page_set_header_end_widget (GS_PAGE (self), self->search_button);
-       g_signal_connect (self->search_button, "clicked",
-                         G_CALLBACK (gs_shell_search_search_button_cb), self);
-
        /* chain up */
        gs_page_setup (GS_PAGE (self),
                       shell,
diff --git a/src/gs-shell.c b/src/gs-shell.c
index abbad94..a05e585 100644
--- a/src/gs-shell.c
+++ b/src/gs-shell.c
@@ -278,6 +278,10 @@ gs_shell_change_mode (GsShell *shell,
        widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "header_selection_menu_button"));
        gtk_widget_hide (widget);
 
+       /* only show the search button in overview and search pages */
+       widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "search_button"));
+       gtk_widget_set_visible (widget, mode == GS_SHELL_MODE_OVERVIEW ||
+                                       mode == GS_SHELL_MODE_SEARCH);
        /* hide unless we're going to search */
        widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "search_bar"));
        gtk_search_bar_set_search_mode (GTK_SEARCH_BAR (widget),
@@ -588,6 +592,32 @@ search_changed_handler (GObject *entry, GsShell *shell)
        }
 }
 
+static void
+search_button_clicked_cb (GtkToggleButton *toggle_button, GsShell *shell)
+{
+       GsShellPrivate *priv = gs_shell_get_instance_private (shell);
+       GtkWidget *search_bar;
+
+       search_bar = GTK_WIDGET (gtk_builder_get_object (priv->builder, "search_bar"));
+       gtk_search_bar_set_search_mode (GTK_SEARCH_BAR (search_bar),
+                                       gtk_toggle_button_get_active (toggle_button));
+
+       /* switch back to overview */
+       if (!gtk_toggle_button_get_active (toggle_button))
+               gs_shell_change_mode (shell, GS_SHELL_MODE_OVERVIEW, NULL, TRUE);
+}
+
+static void
+search_mode_enabled_cb (GtkSearchBar *search_bar, GParamSpec *pspec, GsShell *shell)
+{
+       GsShellPrivate *priv = gs_shell_get_instance_private (shell);
+       GtkWidget *search_button;
+
+       search_button = GTK_WIDGET (gtk_builder_get_object (priv->builder, "search_button"));
+       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (search_button),
+                                     gtk_search_bar_get_search_mode (search_bar));
+}
+
 static gboolean
 window_key_press_event (GtkWidget *win, GdkEventKey *event, GsShell *shell)
 {
@@ -1524,6 +1554,17 @@ gs_shell_setup (GsShell *shell, GsPluginLoader *plugin_loader, GCancellable *can
        g_signal_connect_after (priv->main_window, "button_press_event",
                                G_CALLBACK (window_button_press_event), shell);
 
+       /* show the search bar when clicking on the search button */
+       widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "search_button"));
+       g_signal_connect (widget, "clicked",
+                         G_CALLBACK (search_button_clicked_cb),
+                         shell);
+       /* set the search button enabled when search bar appears */
+       widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "search_bar"));
+       g_signal_connect (widget, "notify::search-mode-enabled",
+                         G_CALLBACK (search_mode_enabled_cb),
+                         shell);
+
        /* setup buttons */
        widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "button_back"));
        g_signal_connect (widget, "clicked",


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