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.
This commit is contained in:
parent
f025f6904d
commit
5eb6bb2a6d
5 changed files with 99 additions and 97 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
92
src/client/components/components-search-bar.vala
Normal file
92
src/client/components/components-search-bar.vala
Normal file
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,90 +0,0 @@
|
|||
/* Copyright 2016 Software Freedom Conservancy Inc.
|
||||
*
|
||||
* 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 : Gtk.SearchBar {
|
||||
private const string DEFAULT_SEARCH_TEXT = _("Search");
|
||||
|
||||
public string search_text { get { return search_entry.text; } }
|
||||
public bool search_entry_has_focus { get { return search_entry.has_focus; } }
|
||||
|
||||
private Gtk.SearchEntry search_entry = new Gtk.SearchEntry();
|
||||
private Components.EntryUndo search_undo;
|
||||
private Geary.Account? current_account = null;
|
||||
|
||||
public signal void search_text_changed(string search_text);
|
||||
|
||||
public SearchBar() {
|
||||
// Search entry.
|
||||
search_entry.width_chars = 28;
|
||||
search_entry.tooltip_text = _("Search all mail in account for keywords (Ctrl+S)");
|
||||
search_entry.search_changed.connect(() => {
|
||||
search_text_changed(search_entry.text);
|
||||
});
|
||||
search_entry.activate.connect(() => {
|
||||
search_text_changed(search_entry.text);
|
||||
});
|
||||
search_entry.has_focus = true;
|
||||
|
||||
this.search_undo = new Components.EntryUndo(this.search_entry);
|
||||
|
||||
this.notify["search-mode-enabled"].connect(on_search_mode_changed);
|
||||
|
||||
add(search_entry);
|
||||
|
||||
set_search_placeholder_text(DEFAULT_SEARCH_TEXT);
|
||||
}
|
||||
|
||||
public void set_search_text(string text) {
|
||||
this.search_entry.text = text;
|
||||
}
|
||||
|
||||
public void give_search_focus() {
|
||||
set_search_mode(true);
|
||||
search_entry.grab_focus();
|
||||
}
|
||||
|
||||
public void set_search_placeholder_text(string placeholder) {
|
||||
search_entry.placeholder_text = placeholder;
|
||||
}
|
||||
|
||||
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() {
|
||||
var main = get_toplevel() as Application.MainWindow;
|
||||
if (main != null) {
|
||||
set_search_placeholder_text(
|
||||
current_account == null ||
|
||||
main.application.engine.accounts_count == 1
|
||||
? DEFAULT_SEARCH_TEXT :
|
||||
_("Search %s account").printf(
|
||||
current_account.information.display_name
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void on_search_mode_changed() {
|
||||
if (!this.search_mode_enabled) {
|
||||
this.search_undo.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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',
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue