Skip to content

Commit 09c844e

Browse files
jeremypwJeremy Woottenzeebok
authored
Persist current search term until intentionally changed (#1509)
* Do not replace search term with doc selection unless action-fetch activated * Prioritise non empty current search entry for when setting search text * Make SearchBar more self-contained; simplify; cleanup * Focus search after showing searchbar * Ensure searchbar visible and shows expected search term on searches * Focus search entry on search; Ensure non-null search term * Always search for any selected text after Control + F * Set searchbar entry after global search --------- Co-authored-by: Jeremy Wootten <jeremy@Proteus-EL07R6-9b3c42bb.localdomain> Co-authored-by: Ryan Kornheisl <ryan@skarva.tech>
1 parent 17bc74a commit 09c844e

4 files changed

Lines changed: 158 additions & 159 deletions

File tree

plugins/highlight-word-selection/highlight-word-selection.vala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,8 @@ public class Scratch.Plugins.HighlightSelectedWords : Peas.ExtensionBase, Peas.A
4848
});
4949
}
5050

51-
public void on_selection_changed (ref Gtk.TextIter start, ref Gtk.TextIter end) {
52-
var window_search_context = main_window != null ? main_window.search_bar.search_context : null;
53-
54-
if (window_search_context == null ||
55-
window_search_context.settings.search_text == "" ||
56-
window_search_context.get_occurrences_count () == 0) {
51+
public void on_selection_changed (ref Gtk.TextIter start, ref Gtk.TextIter end) requires (main_window != null) {
52+
if (!main_window.has_successful_search ()) {
5753
// Perform plugin selection when there is no ongoing and successful search
5854
current_search_context = new Gtk.SourceSearchContext (
5955
(Gtk.SourceBuffer)current_source.buffer,

src/MainWindow.vala

Lines changed: 71 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ namespace Scratch {
4343

4444
// Widgets
4545
public Scratch.HeaderBar toolbar;
46-
private Gtk.Revealer search_revealer;
4746
public Scratch.Widgets.SearchBar search_bar;
4847
private Code.WelcomeView welcome_view;
4948
private Code.Terminal terminal;
@@ -89,7 +88,6 @@ namespace Scratch {
8988
public const string ACTION_REVERT = "action-revert";
9089
public const string ACTION_SAVE = "action-save";
9190
public const string ACTION_SAVE_AS = "action-save-as";
92-
public const string ACTION_SHOW_FIND = "action-show-find";
9391
public const string ACTION_TEMPLATES = "action-templates";
9492
public const string ACTION_SHOW_REPLACE = "action-show-replace";
9593
public const string ACTION_TO_LOWER_CASE = "action-to-lower-case";
@@ -101,6 +99,7 @@ namespace Scratch {
10199
public const string ACTION_ZOOM_IN = "action-zoom-in";
102100
public const string ACTION_ZOOM_OUT = "action-zoom-out";
103101
public const string ACTION_TOGGLE_COMMENT = "action-toggle-comment";
102+
public const string ACTION_TOGGLE_SHOW_FIND = "action-toggle_show-find";
104103
public const string ACTION_TOGGLE_SIDEBAR = "action-toggle-sidebar";
105104
public const string ACTION_TOGGLE_OUTLINE = "action-toggle-outline";
106105
public const string ACTION_TOGGLE_TERMINAL = "action-toggle-terminal";
@@ -127,7 +126,7 @@ namespace Scratch {
127126
private Services.GitManager git_manager;
128127

129128
private const ActionEntry[] ACTION_ENTRIES = {
130-
{ ACTION_FIND, action_fetch, "s" },
129+
{ ACTION_FIND, action_find, "s"},
131130
{ ACTION_FIND_NEXT, action_find_next },
132131
{ ACTION_FIND_PREVIOUS, action_find_previous },
133132
{ ACTION_FIND_GLOBAL, action_find_global, "s" },
@@ -139,7 +138,7 @@ namespace Scratch {
139138
{ ACTION_REVERT, action_revert },
140139
{ ACTION_SAVE, action_save },
141140
{ ACTION_SAVE_AS, action_save_as },
142-
{ ACTION_SHOW_FIND, action_show_fetch, null, "false" },
141+
{ ACTION_TOGGLE_SHOW_FIND, action_toggle_show_find, null, "false" },
143142
{ ACTION_TEMPLATES, action_templates },
144143
{ ACTION_GO_TO, action_go_to },
145144
{ ACTION_SORT_LINES, action_sort_lines },
@@ -378,7 +377,7 @@ namespace Scratch {
378377

379378
private void update_toolbar_button (string name, bool new_state) {
380379
switch (name) {
381-
case ACTION_SHOW_FIND:
380+
case ACTION_TOGGLE_SHOW_FIND:
382381
if (new_state) {
383382
toolbar.find_button.tooltip_markup = Granite.markup_accel_tooltip (
384383
{"Escape"},
@@ -391,7 +390,7 @@ namespace Scratch {
391390
);
392391
}
393392

394-
search_revealer.set_reveal_child (new_state);
393+
search_bar.reveal (new_state);
395394

396395
break;
397396
case ACTION_TOGGLE_SIDEBAR:
@@ -444,20 +443,6 @@ namespace Scratch {
444443

445444
// SearchBar
446445
search_bar = new Scratch.Widgets.SearchBar (this);
447-
search_revealer = new Gtk.Revealer ();
448-
search_revealer.add (search_bar);
449-
450-
search_bar.map.connect_after ((w) => { /* signalled when reveal child */
451-
set_search_text ();
452-
});
453-
search_bar.search_entry.unmap.connect_after (() => { /* signalled when reveal child */
454-
search_bar.search_entry.text = "";
455-
search_bar.highlight_none ();
456-
});
457-
search_bar.search_empty.connect (() => {
458-
folder_manager_view.clear_badges ();
459-
});
460-
461446
welcome_view = new Code.WelcomeView (this);
462447
document_view = new Scratch.Widgets.DocumentView (this);
463448
// Handle Drag-and-drop for files functionality on welcome screen
@@ -524,7 +509,7 @@ namespace Scratch {
524509
var view_grid = new Gtk.Grid () {
525510
orientation = Gtk.Orientation.VERTICAL
526511
};
527-
view_grid.add (search_revealer);
512+
view_grid.add (search_bar);
528513
view_grid.add (document_view);
529514

530515
content_stack = new Gtk.Stack () {
@@ -563,8 +548,6 @@ namespace Scratch {
563548
size_group.add_widget (sidebar.headerbar);
564549
size_group.add_widget (toolbar);
565550

566-
search_revealer.set_reveal_child (false);
567-
568551
realize.connect (() => {
569552
Scratch.saved_state.bind ("sidebar-visible", sidebar, "visible", SettingsBindFlags.DEFAULT);
570553
Scratch.saved_state.bind ("outline-visible", document_view , "outline_visible", SettingsBindFlags.DEFAULT);
@@ -707,13 +690,12 @@ namespace Scratch {
707690
});
708691
}
709692

710-
// private bool on_key_pressed (Gdk.EventKey event) {
711693
private bool on_key_pressed (uint keyval, uint keycode, Gdk.ModifierType state) {
712694
switch (Gdk.keyval_name (keyval)) {
713695
case "Escape":
714-
if (search_revealer.get_child_revealed ()) {
715-
var fetch_action = Utils.action_from_group (ACTION_SHOW_FIND, actions);
716-
fetch_action.set_state (false);
696+
if (search_bar.is_revealed) {
697+
var action = Utils.action_from_group (ACTION_TOGGLE_SHOW_FIND, actions);
698+
action.set_state (false);
717699
document_view.current_document.source_view.grab_focus ();
718700
}
719701

@@ -732,7 +714,7 @@ namespace Scratch {
732714
// Set sensitive property for 'delicate' Widgets/GtkActions while
733715
private void set_widgets_sensitive (bool val) {
734716
// SearchManager's stuffs
735-
Utils.action_from_group (ACTION_SHOW_FIND, actions).set_enabled (val);
717+
Utils.action_from_group (ACTION_TOGGLE_SHOW_FIND, actions).set_enabled (val);
736718
Utils.action_from_group (ACTION_GO_TO, actions).set_enabled (val);
737719
Utils.action_from_group (ACTION_SHOW_REPLACE, actions).set_enabled (val);
738720
// Toolbar Actions
@@ -755,6 +737,26 @@ namespace Scratch {
755737
return document_view.current_document;
756738
}
757739

740+
// If selected text covers more than one line return just the first.
741+
public void set_selected_text_for_search () {
742+
var doc = get_current_document ();
743+
var selected_text = doc != null ? doc.get_selected_text (false) : "";
744+
var search_term = "";
745+
if (selected_text.contains ("\n")) {
746+
search_term = selected_text.split ("\n", 2)[0];
747+
} else {
748+
search_term = selected_text;
749+
}
750+
751+
if (search_term != "") {
752+
search_bar.set_search_entry_text (search_term);
753+
}
754+
}
755+
756+
public bool has_successful_search () {
757+
return search_bar.search_occurrences > 0;
758+
}
759+
758760
public void open_folder (File folder) {
759761
var foldermanager_file = new FolderManager.File (folder.get_path ());
760762
folder_manager_view.open_folder (foldermanager_file);
@@ -1168,32 +1170,38 @@ namespace Scratch {
11681170
}
11691171

11701172
/** Not a toggle action - linked to keyboard short cut (Ctrl-f). **/
1171-
private string current_search_term = "";
1172-
private void action_fetch (SimpleAction action, Variant? param) {
1173-
current_search_term = param != null ? param.get_string () : "";
1174-
if (!search_revealer.child_revealed) {
1175-
var show_find_action = Utils.action_from_group (ACTION_SHOW_FIND, actions);
1173+
private void action_find (SimpleAction action, Variant? param) {
1174+
find (param != null ? param.get_string () : "");
1175+
}
1176+
1177+
private void find (string search_term = "") {
1178+
if (!search_bar.is_revealed) {
1179+
var show_find_action = Utils.action_from_group (ACTION_TOGGLE_SHOW_FIND, actions);
11761180
if (show_find_action.enabled) {
1177-
/* Toggling the fetch action causes this function to be called again but the search_revealer child
1178-
* is still not revealed so nothing more happens. We use the map signal on the search entry
1179-
* to set it up once it has been revealed. */
1180-
show_find_action.set_state (true);
1181+
show_find_action.activate (new Variant ("b", true));
11811182
}
1183+
}
1184+
1185+
if (search_term != "") {
1186+
search_bar.set_search_entry_text (search_term);
11821187
} else {
1183-
set_search_text ();
1188+
set_selected_text_for_search ();
11841189
}
1190+
1191+
search_bar.search ();
11851192
}
11861193

11871194
private void action_show_replace (SimpleAction action) {
1188-
action_fetch (action, null);
1195+
find ();
11891196
// May have to wait for the search bar to be revealed before we can grab focus
1190-
if (search_revealer.child_revealed) {
1191-
search_bar.replace_entry.grab_focus ();
1197+
1198+
if (search_bar.is_revealed) {
1199+
search_bar.focus_replace_entry ();
11921200
} else {
1193-
ulong map_handler = 0;
1194-
map_handler = search_bar.map.connect_after (() => {
1195-
search_bar.replace_entry.grab_focus ();
1196-
search_bar.disconnect (map_handler);
1201+
search_bar.reveal (true);
1202+
Idle.add (() => {
1203+
search_bar.focus_replace_entry ();
1204+
return Source.REMOVE;
11971205
});
11981206
}
11991207
}
@@ -1207,50 +1215,34 @@ namespace Scratch {
12071215
}
12081216

12091217
private void action_find_global (SimpleAction action, Variant? param) {
1210-
var selected_text = "";
1211-
var search_path = "";
1212-
1213-
var current_doc = get_current_document ();
1214-
if (current_doc != null) {
1215-
selected_text = current_doc.get_selected_text (false);
1216-
}
1217-
1218-
if (selected_text != "") {
1219-
selected_text = selected_text.split ("\n", 2)[0];
1220-
}
1221-
// If search entry focused use its text for search term, else use selected text
1222-
var term = search_bar.search_entry.has_focus ?
1223-
search_bar.search_entry.text : selected_text;
1224-
1225-
// If no focused selected text fallback to search entry text if visible
1226-
if (term == "" &&
1227-
!search_bar.search_entry.has_focus &&
1228-
search_revealer.reveal_child) {
1229-
1230-
term = search_bar.search_entry.text;
1218+
if (!search_bar.is_focused || search_bar.entry_text == "") {
1219+
set_selected_text_for_search ();
12311220
}
12321221

1222+
var search_path = "";
12331223
if (param != null && param.get_string () != "") {
12341224
search_path = param.get_string ();
12351225
} else {
12361226
search_path = default_globalsearch_path;
12371227
}
12381228

12391229
if (search_path != "") {
1240-
folder_manager_view.search_global (search_path, term);
1230+
folder_manager_view.search_global (search_path, search_bar.entry_text);
12411231
} else {
12421232
// Fallback to standard search
12431233
warning ("Unable to perform global search - search document instead");
1244-
action_fetch (action, param);
1234+
find ();
12451235
}
1236+
1237+
// No need to reveal searchbar - handled by subsequent find action.
12461238
}
12471239

12481240
private void update_find_actions () {
12491241
// Idle needed to ensure that existence of current_doc is up to date
12501242
Idle.add (() => {
12511243
var is_current_doc = get_current_document () != null;
12521244
Utils.action_from_group (ACTION_FIND, actions).set_enabled (is_current_doc);
1253-
Utils.action_from_group (ACTION_SHOW_FIND, actions).set_enabled (is_current_doc);
1245+
Utils.action_from_group (ACTION_TOGGLE_SHOW_FIND, actions).set_enabled (is_current_doc);
12541246
Utils.action_from_group (ACTION_FIND_NEXT, actions).set_enabled (is_current_doc);
12551247
Utils.action_from_group (ACTION_FIND_PREVIOUS, actions).set_enabled (is_current_doc);
12561248
var can_global_search = is_current_doc || git_manager.active_project_path != null;
@@ -1260,38 +1252,18 @@ namespace Scratch {
12601252
});
12611253
}
12621254

1263-
private void set_search_text () {
1264-
if (current_search_term != "") {
1265-
search_bar.search_entry.text = current_search_term;
1266-
search_bar.search_entry.grab_focus ();
1267-
search_bar.search_next ();
1268-
} else if (search_bar.search_entry.text != "") {
1269-
// Always search on what is showing in search entry
1270-
current_search_term = search_bar.search_entry.text;
1271-
search_bar.search_entry.grab_focus ();
1272-
} else {
1273-
var current_doc = get_current_document ();
1274-
// This is also called when all documents are closed.
1275-
if (current_doc != null) {
1276-
var selected_text = current_doc.get_selected_text (false);
1277-
if (selected_text != "" && selected_text.length < MAX_SEARCH_TEXT_LENGTH) {
1278-
current_search_term = selected_text.split ("\n", 2)[0];
1279-
search_bar.search_entry.text = current_search_term;
1280-
}
1281-
1282-
search_bar.search_entry.grab_focus (); /* causes loss of document selection */
1255+
/** Toggle action - linked to toolbar togglebutton. **/
1256+
private void action_toggle_show_find () {
1257+
var action = Utils.action_from_group (ACTION_TOGGLE_SHOW_FIND, actions);
1258+
var to_show = !action.get_state ().get_boolean ();
1259+
action.set_state (to_show);
1260+
search_bar.reveal (to_show);
1261+
if (to_show) {
1262+
search_bar.focus_search_entry ();
1263+
if (search_bar.entry_text == "") {
1264+
set_selected_text_for_search ();
12831265
}
12841266
}
1285-
1286-
if (current_search_term != "") {
1287-
search_bar.search_next (); /* this selects the next match (if any) */
1288-
}
1289-
}
1290-
1291-
/** Toggle action - linked to toolbar togglebutton. **/
1292-
private void action_show_fetch () {
1293-
var fetch_action = Utils.action_from_group (ACTION_SHOW_FIND, actions);
1294-
fetch_action.set_state (!fetch_action.get_state ().get_boolean ());
12951267
}
12961268

12971269
private void action_go_to () {

src/Widgets/HeaderBar.vala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public class Scratch.HeaderBar : Hdy.HeaderBar {
114114
font_size_box.add (zoom_in_button);
115115

116116
find_button = new Gtk.ToggleButton () {
117-
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_SHOW_FIND,
117+
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_TOGGLE_SHOW_FIND,
118118
image = new Gtk.Image.from_icon_name ("edit-find-on-page-symbolic", Gtk.IconSize.MENU)
119119
};
120120
find_button.tooltip_markup = Granite.markup_accel_tooltip (

0 commit comments

Comments
 (0)