[gnome-software/gnome-3-22] Simplify search button handling code



commit b724117bf66125f9ee3f450225e9d0fb357604b3
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 52755ee..027e306 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 9a17a6f..8f7f8bc 100644
--- a/src/gs-common.c
+++ b/src/gs-common.c
@@ -850,53 +850,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 eef2fb0..d7009ad 100644
--- a/src/gs-common.h
+++ b/src/gs-common.h
@@ -70,7 +70,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 1d64135..60485cf 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;
 
@@ -574,7 +573,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);
@@ -737,7 +737,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;
@@ -776,12 +775,6 @@ gs_shell_overview_setup (GsShellOverview *self,
        g_signal_connect (priv->categories_expander_button, "clicked",
                          G_CALLBACK (gs_shell_overview_categories_expander_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 3968f12..af94727 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;
@@ -253,7 +252,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;
@@ -380,14 +380,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,
@@ -417,12 +409,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 f53abef..4242a63 100644
--- a/src/gs-shell.c
+++ b/src/gs-shell.c
@@ -254,6 +254,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),
@@ -530,6 +534,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)
 {
@@ -682,6 +712,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]