[geary/mjog/121-search-tokenising: 2/7] Update and simplify SearchBar component



commit 5eb6bb2a6d6b951070b11ddd20bc9b84bf59da7a
Author: Michael Gratton <mike vee net>
Date:   Fri Dec 13 12:21:27 2019 +1100

    Update and simplify SearchBar component
    
    Rename source file name and contents to match code convention, add
    transation comments, remove extra API in favour of simply exposing the
    search entry publically. Extend Hdy.Searchbar so that the width of the
    entry grows as needed.

 po/POTFILES.in                                     |  2 +-
 .../application/application-main-window.vala       | 10 +--
 src/client/components/components-search-bar.vala   | 92 ++++++++++++++++++++++
 src/client/components/search-bar.vala              | 90 ---------------------
 src/client/meson.build                             |  2 +-
 5 files changed, 99 insertions(+), 97 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 91798075..add1424e 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -37,6 +37,7 @@ src/client/components/components-in-app-notification.vala
 src/client/components/components-inspector.vala
 src/client/components/components-placeholder-pane.vala
 src/client/components/components-preferences-window.vala
+src/client/components/components-search-bar.vala
 src/client/components/components-validator.vala
 src/client/components/components-web-view.vala
 src/client/components/count-badge.vala
@@ -46,7 +47,6 @@ src/client/components/main-toolbar.vala
 src/client/components/main-window-info-bar.vala
 src/client/components/monitored-progress-bar.vala
 src/client/components/monitored-spinner.vala
-src/client/components/search-bar.vala
 src/client/components/status-bar.vala
 src/client/components/stock.vala
 src/client/composer/composer-box.vala
diff --git a/src/client/application/application-main-window.vala 
b/src/client/application/application-main-window.vala
index b1c8cdee..d605e027 100644
--- a/src/client/application/application-main-window.vala
+++ b/src/client/application/application-main-window.vala
@@ -282,7 +282,7 @@ public class Application.MainWindow :
     // Widget descendants
     public FolderList.Tree folder_list { get; private set; default = new FolderList.Tree(); }
     public MainToolbar main_toolbar { get; private set; }
-    public SearchBar search_bar { get; private set; default = new SearchBar(); }
+    public SearchBar search_bar { get; private set; }
     public ConversationListView conversation_list_view  { get; private set; }
     public ConversationViewer conversation_viewer { get; private set; }
     public StatusBar status_bar { get; private set; default = new StatusBar(); }
@@ -822,9 +822,9 @@ public class Application.MainWindow :
 
     /** Displays and focuses the search bar for the window. */
     public void show_search_bar(string? text = null) {
-        this.search_bar.give_search_focus();
+        this.search_bar.grab_focus();
         if (text != null) {
-            this.search_bar.set_search_text(text);
+            this.search_bar.entry.text = text;
         }
     }
 
@@ -1012,7 +1012,7 @@ public class Application.MainWindow :
                 yield select_folder(to_select, false);
 
                 if (is_account_search_active) {
-                    this.search_bar.set_search_text("");
+                    this.search_bar.entry.text = "";
                     this.search_bar.search_mode_enabled = false;
                 }
             }
@@ -1178,8 +1178,8 @@ public class Application.MainWindow :
         this.notify["has-toplevel-focus"].connect(on_has_toplevel_focus);
 
         // Search bar
+        this.search_bar = new SearchBar(this.application.engine);
         this.search_bar.search_text_changed.connect(on_search);
-        this.search_bar.show();
         this.search_bar_box.pack_start(this.search_bar, false, false, 0);
 
         // Folder list
diff --git a/src/client/components/components-search-bar.vala 
b/src/client/components/components-search-bar.vala
new file mode 100644
index 00000000..3988a2aa
--- /dev/null
+++ b/src/client/components/components-search-bar.vala
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2016 Software Freedom Conservancy Inc.
+ * Copyright 2019 Michael Gratton <mike vee net>
+ *
+ * This software is licensed under the GNU Lesser General Public License
+ * (version 2.1 or later).  See the COPYING file in this distribution.
+ */
+
+public class SearchBar : Hdy.SearchBar {
+
+    /// Translators: Search entry placeholder text
+    private const string DEFAULT_SEARCH_TEXT = _("Search");
+
+    public Gtk.SearchEntry entry {
+        get; private set; default = new Gtk.SearchEntry();
+    }
+
+    private Components.EntryUndo search_undo;
+    private Geary.Account? current_account = null;
+    private Geary.Engine engine;
+
+    public signal void search_text_changed(string search_text);
+
+
+    public SearchBar(Geary.Engine engine) {
+        this.engine = engine;
+        this.search_undo = new Components.EntryUndo(this.entry);
+
+        this.notify["search-mode-enabled"].connect(on_search_mode_changed);
+
+        /// Translators: Search entry tooltip
+        this.entry.tooltip_text = _("Search all mail in account for keywords");
+        this.entry.search_changed.connect(() => {
+            search_text_changed(this.entry.text);
+        });
+        this.entry.activate.connect(() => {
+            search_text_changed(this.entry.text);
+        });
+        this.entry.placeholder_text = DEFAULT_SEARCH_TEXT;
+        this.entry.has_focus = true;
+
+        var column = new Hdy.Column();
+        column.maximum_width = 450;
+        column.add(this.entry);
+
+        connect_entry(this.entry);
+        add(column);
+
+        show_all();
+    }
+
+    public override void grab_focus() {
+        set_search_mode(true);
+        this.entry.grab_focus();
+    }
+
+    public void set_account(Geary.Account? account) {
+        if (current_account != null) {
+            current_account.information.changed.disconnect(
+                on_information_changed
+            );
+        }
+
+        if (account != null) {
+            account.information.changed.connect(
+                on_information_changed
+            );
+        }
+
+        current_account = account;
+
+        on_information_changed(); // Set new account name.
+    }
+
+    private void on_information_changed() {
+        this.entry.placeholder_text = (
+            this.current_account == null || this.engine.accounts_count == 1
+            ? DEFAULT_SEARCH_TEXT
+            /// Translators: Search entry placeholder, string
+            /// replacement is the name of an account
+            : _("Search %s account").printf(
+                this.current_account.information.display_name
+            )
+        );
+    }
+
+    private void on_search_mode_changed() {
+        if (!this.search_mode_enabled) {
+            this.search_undo.reset();
+        }
+    }
+}
diff --git a/src/client/meson.build b/src/client/meson.build
index 92784c99..ed99bacf 100644
--- a/src/client/meson.build
+++ b/src/client/meson.build
@@ -37,6 +37,7 @@ geary_client_vala_sources = files(
   'components/components-inspector-system-view.vala',
   'components/components-placeholder-pane.vala',
   'components/components-preferences-window.vala',
+  'components/components-search-bar.vala',
   'components/components-validator.vala',
   'components/components-web-view.vala',
   'components/count-badge.vala',
@@ -46,7 +47,6 @@ geary_client_vala_sources = files(
   'components/main-window-info-bar.vala',
   'components/monitored-progress-bar.vala',
   'components/monitored-spinner.vala',
-  'components/search-bar.vala',
   'components/status-bar.vala',
   'components/stock.vala',
 


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