diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 01240d606..924c4b184 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -749,7 +749,13 @@ namespace Scratch { // If selected text covers more than one line return just the first. public void set_selected_text_for_search () { + // Do not overwrite search term if we are editing the entry. + if (search_bar.is_focused) { + return; + } + var doc = get_current_document (); + var selected_text = doc != null ? doc.get_selected_text (false) : ""; var search_term = ""; if (selected_text.contains ("\n")) { @@ -1186,19 +1192,21 @@ namespace Scratch { } private void find (string search_term = "") { + // Set search term before focusing search bar else maybe ineffective + if (search_term != "") { + search_bar.set_search_entry_text (search_term); + } else { + set_selected_text_for_search (); + } + if (!search_bar.is_revealed) { var show_find_action = Utils.action_from_group (ACTION_TOGGLE_SHOW_FIND, actions); if (show_find_action.enabled) { + // This focuses the search bar show_find_action.activate (new Variant ("b", true)); } } - if (search_term != "") { - search_bar.set_search_entry_text (search_term); - } else { - set_selected_text_for_search (); - } - search_bar.search (); } diff --git a/src/Widgets/SearchBar.vala b/src/Widgets/SearchBar.vala index 45ce9d562..d4c1d0f97 100644 --- a/src/Widgets/SearchBar.vala +++ b/src/Widgets/SearchBar.vala @@ -357,8 +357,13 @@ namespace Scratch.Widgets { return false; } - update_search_widgets (); - return false; + Idle.add (() => { + update_search_widgets (); + search_entry.select_region (0, -1); + return Source.REMOVE; + }); + + return Gdk.EVENT_PROPAGATE; } public bool search () {