[network-manager-applet/lr/ui-improvements: 22/23] editor: use custom search bar for filtering



commit b1e1e3fe34bc7b06dac2b03b1aaebd2072361474
Author: Lubomir Rintel <lkundrak v3 sk>
Date:   Mon Jul 3 12:12:45 2017 +0200

    editor: use custom search bar for filtering
    
    We need our own search bar, because we also want to do filtering an
    refilter on earch search entry change.

 src/connection-editor/nm-connection-list.c  |   58 ++++++++++-
 src/connection-editor/nm-connection-list.ui |  135 +++++++++++++++++----------
 2 files changed, 137 insertions(+), 56 deletions(-)
---
diff --git a/src/connection-editor/nm-connection-list.c b/src/connection-editor/nm-connection-list.c
index 7caa515..ce4d0f2 100644
--- a/src/connection-editor/nm-connection-list.c
+++ b/src/connection-editor/nm-connection-list.c
@@ -50,6 +50,8 @@ struct _NMConnectionListPrivate {
        GtkWidget *connection_del;
        GtkWidget *connection_edit;
        GtkTreeView *connection_list;
+       GtkSearchBar *search_bar;
+       GtkEntry *search_entry;
        GtkTreeModel *model;
        GtkTreeModelFilter *filter;
        GtkTreeSortable *sortable;
@@ -459,14 +461,45 @@ list_close_cb (GtkDialog *dialog, gpointer user_data)
 }
 
 static gboolean
-key_press_cb (GtkWidget *widget, GdkEventKey *event, gpointer user_data)
+key_press_cb (GtkWidget *widget, GdkEvent *event, gpointer user_data)
 {
-       if (event->keyval == GDK_KEY_Escape) {
-               gtk_window_close (GTK_WINDOW (user_data));
-               return TRUE;
+       NMConnectionList *list = user_data;
+       NMConnectionListPrivate *priv = NM_CONNECTION_LIST_GET_PRIVATE (list);
+       GdkEventKey *key_event = (GdkEventKey *) event;
+
+       if (gtk_search_bar_handle_event (priv->search_bar, event) == GDK_EVENT_STOP)
+               return GDK_EVENT_STOP;
+
+       if (key_event->keyval == GDK_KEY_Escape) {
+               if (gtk_search_bar_get_search_mode (priv->search_bar))
+                       gtk_search_bar_set_search_mode (priv->search_bar, FALSE);
+               else
+                       gtk_window_close (GTK_WINDOW (user_data));
+               return GDK_EVENT_STOP;
        }
 
-       return FALSE;
+       return GDK_EVENT_PROPAGATE;
+}
+
+static gboolean
+start_search (GtkTreeView *treeview, gpointer user_data)
+{
+       NMConnectionList *list = user_data;
+       NMConnectionListPrivate *priv = NM_CONNECTION_LIST_GET_PRIVATE (list);
+
+       gtk_search_bar_set_search_mode (priv->search_bar, TRUE);
+
+       return TRUE;
+}
+
+static void
+search_changed (GtkSearchEntry *entry, gpointer user_data)
+{
+       NMConnectionList *list = user_data;
+       NMConnectionListPrivate *priv = NM_CONNECTION_LIST_GET_PRIVATE (list);
+
+       gtk_tree_model_filter_refilter (priv->filter);
+       gtk_tree_view_expand_all (priv->connection_list);
 }
 
 static void
@@ -520,6 +553,8 @@ nm_connection_list_class_init (NMConnectionListClass *klass)
         gtk_widget_class_bind_template_child_private (widget_class, NMConnectionList, connection_add);
         gtk_widget_class_bind_template_child_private (widget_class, NMConnectionList, connection_del);
         gtk_widget_class_bind_template_child_private (widget_class, NMConnectionList, connection_edit);
+        gtk_widget_class_bind_template_child_private (widget_class, NMConnectionList, search_bar);
+        gtk_widget_class_bind_template_child_private (widget_class, NMConnectionList, search_entry);
 
         gtk_widget_class_bind_template_callback (widget_class, add_clicked);
         gtk_widget_class_bind_template_callback (widget_class, do_edit);
@@ -527,6 +562,8 @@ nm_connection_list_class_init (NMConnectionListClass *klass)
         gtk_widget_class_bind_template_callback (widget_class, list_close_cb);
         gtk_widget_class_bind_template_callback (widget_class, selection_changed_cb);
         gtk_widget_class_bind_template_callback (widget_class, key_press_cb);
+        gtk_widget_class_bind_template_callback (widget_class, start_search);
+        gtk_widget_class_bind_template_callback (widget_class, search_changed);
 }
 
 static void
@@ -615,13 +652,21 @@ tree_model_visible_func (GtkTreeModel *model,
        NMSettingConnection *s_con;
        const char *master;
        const char *slave_type;
+       gs_free char *id = NULL;
 
-       gtk_tree_model_get (model, iter, COL_CONNECTION, &connection, -1);
+       gtk_tree_model_get (model, iter,
+                           COL_ID, &id,
+                           COL_CONNECTION, &connection,
+                           -1);
        if (!connection) {
                /* Top-level type nodes are visible iff they have children */
                return gtk_tree_model_iter_has_child  (model, iter);
        }
 
+       if (   gtk_search_bar_get_search_mode (priv->search_bar)
+           && strcasestr (id, gtk_entry_get_text (GTK_ENTRY (priv->search_entry))) == NULL)
+               return FALSE;
+
        /* A connection node is visible unless it is a slave to a known
         * bond or team or bridge.
         */
@@ -706,6 +751,7 @@ initialize_treeview (NMConnectionList *self)
 
        gtk_tree_view_set_model (priv->connection_list, GTK_TREE_MODEL (priv->sortable));
        gtk_tree_view_set_search_equal_func (priv->connection_list, connection_list_equal, NULL, NULL);
+       gtk_tree_view_set_search_entry (priv->connection_list, priv->search_entry);
 
        /* Name column */
        renderer = gtk_cell_renderer_text_new ();
diff --git a/src/connection-editor/nm-connection-list.ui b/src/connection-editor/nm-connection-list.ui
index 37d014f..cb77528 100644
--- a/src/connection-editor/nm-connection-list.ui
+++ b/src/connection-editor/nm-connection-list.ui
@@ -9,93 +9,128 @@
     <property name="default_width">600</property>
     <property name="default_height">400</property>
     <property name="gravity">north-east</property>
-    <signal name="key-press-event" handler="key_press_cb" swapped="no"/>
     <signal name="destroy" handler="list_close_cb" swapped="no"/>
+    <signal name="key-press-event" handler="key_press_cb" swapped="no"/>
     <child>
-      <object class="GtkBox" id="connection_box">
+      <object class="GtkBox" id="box">
         <property name="visible">True</property>
         <property name="can_focus">False</property>
-        <property name="border_width">6</property>
         <property name="orientation">vertical</property>
         <child>
-          <object class="GtkScrolledWindow" id="scrolledwindow1">
+          <object class="GtkSearchBar" id="search_bar">
             <property name="visible">True</property>
-            <property name="can_focus">True</property>
-            <property name="hscrollbar_policy">never</property>
-            <property name="shadow_type">in</property>
+            <property name="can_focus">False</property>
             <child>
-              <object class="GtkTreeView" id="connection_list">
+              <object class="GtkSearchEntry" id="search_entry">
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
-                <child internal-child="selection">
-                  <object class="GtkTreeSelection">
-                    <signal name="changed" handler="selection_changed_cb" swapped="no"/>
-                  </object>
-                </child>
+                <property name="primary_icon_name">edit-find-symbolic</property>
+                <property name="primary_icon_activatable">False</property>
+                <property name="primary_icon_sensitive">False</property>
+                <signal name="activate" handler="do_edit" swapped="yes"/>
+                <signal name="search-changed" handler="search_changed" swapped="no"/>
               </object>
             </child>
           </object>
           <packing>
-            <property name="expand">True</property>
+            <property name="expand">False</property>
             <property name="fill">True</property>
             <property name="position">0</property>
           </packing>
         </child>
         <child>
-          <object class="GtkToolbar" id="connection_toolbar">
+          <object class="GtkBox" id="connection_box">
             <property name="visible">True</property>
             <property name="can_focus">False</property>
-            <property name="toolbar_style">icons</property>
-            <property name="icon_size">2</property>
-            <child>
-              <object class="GtkToolButton" id="connection_add">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="tooltip_text" translatable="yes">Add a new connection</property>
-                <property name="label" translatable="yes">_Add</property>
-                <property name="use_underline">True</property>
-                <property name="icon_name">list-add-symbolic</property>
-                <signal name="clicked" handler="add_clicked" swapped="no"/>
-              </object>
-              <packing>
-                <property name="expand">False</property>
-                <property name="homogeneous">True</property>
-              </packing>
-            </child>
+            <property name="border_width">6</property>
+            <property name="orientation">vertical</property>
             <child>
-              <object class="GtkToolButton" id="connection_del">
+              <object class="GtkScrolledWindow" id="scrolledwindow1">
                 <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="label" translatable="yes">_Delete</property>
-                <property name="use_underline">True</property>
-                <property name="icon_name">list-remove-symbolic</property>
-                <signal name="clicked" handler="delete_clicked" swapped="no"/>
+                <property name="can_focus">True</property>
+                <property name="hscrollbar_policy">never</property>
+                <property name="shadow_type">in</property>
+                <child>
+                  <object class="GtkTreeView" id="connection_list">
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <signal name="start-interactive-search" handler="start_search" swapped="no"/>
+                    <child internal-child="selection">
+                      <object class="GtkTreeSelection">
+                        <signal name="changed" handler="selection_changed_cb" swapped="no"/>
+                      </object>
+                    </child>
+                  </object>
+                </child>
               </object>
               <packing>
-                <property name="expand">False</property>
-                <property name="homogeneous">True</property>
+                <property name="expand">True</property>
+                <property name="fill">True</property>
+                <property name="position">1</property>
               </packing>
             </child>
             <child>
-              <object class="GtkToolButton" id="connection_edit">
+              <object class="GtkToolbar" id="connection_toolbar">
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
-                <property name="label" translatable="yes">_Edit</property>
-                <property name="use_underline">True</property>
-                <property name="icon_name">emblem-system-symbolic</property>
-                <signal name="clicked" handler="do_edit" swapped="yes"/>
+                <property name="toolbar_style">icons</property>
+                <property name="icon_size">2</property>
+                <child>
+                  <object class="GtkToolButton" id="connection_add">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="tooltip_text" translatable="yes">Add a new connection</property>
+                    <property name="label" translatable="yes">_Add</property>
+                    <property name="use_underline">True</property>
+                    <property name="icon_name">list-add-symbolic</property>
+                    <signal name="clicked" handler="add_clicked" swapped="no"/>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="homogeneous">True</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkToolButton" id="connection_del">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="label" translatable="yes">_Delete</property>
+                    <property name="use_underline">True</property>
+                    <property name="icon_name">list-remove-symbolic</property>
+                    <signal name="clicked" handler="delete_clicked" swapped="no"/>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="homogeneous">True</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkToolButton" id="connection_edit">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="label" translatable="yes">_Edit</property>
+                    <property name="use_underline">True</property>
+                    <property name="icon_name">emblem-system-symbolic</property>
+                    <signal name="clicked" handler="do_edit" swapped="yes"/>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="homogeneous">True</property>
+                  </packing>
+                </child>
+                <style>
+                  <class name="inline-toolbar"/>
+                </style>
               </object>
               <packing>
                 <property name="expand">False</property>
-                <property name="homogeneous">True</property>
+                <property name="fill">True</property>
+                <property name="position">2</property>
               </packing>
             </child>
-            <style>
-              <class name="inline-toolbar"/>
-            </style>
           </object>
           <packing>
-            <property name="expand">False</property>
+            <property name="expand">True</property>
             <property name="fill">True</property>
             <property name="position">1</property>
           </packing>


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