Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist current search term until intentionally changed #1509

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,8 @@ public class Scratch.Plugins.HighlightSelectedWords : Peas.ExtensionBase, Peas.A
});
}

public void on_selection_changed (ref Gtk.TextIter start, ref Gtk.TextIter end) {
var window_search_context = main_window != null ? main_window.search_bar.search_context : null;

if (window_search_context == null ||
window_search_context.settings.search_text == "" ||
window_search_context.get_occurrences_count () == 0) {
public void on_selection_changed (ref Gtk.TextIter start, ref Gtk.TextIter end) requires (main_window != null) {
if (!main_window.has_successful_search ()) {
// Perform plugin selection when there is no ongoing and successful search
current_search_context = new Gtk.SourceSearchContext (
(Gtk.SourceBuffer)current_source.buffer,
Expand Down
170 changes: 71 additions & 99 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ namespace Scratch {

// Widgets
public Scratch.HeaderBar toolbar;
private Gtk.Revealer search_revealer;
public Scratch.Widgets.SearchBar search_bar;
private Code.WelcomeView welcome_view;
private Code.Terminal terminal;
Expand Down Expand Up @@ -89,7 +88,6 @@ namespace Scratch {
public const string ACTION_REVERT = "action-revert";
public const string ACTION_SAVE = "action-save";
public const string ACTION_SAVE_AS = "action-save-as";
public const string ACTION_SHOW_FIND = "action-show-find";
public const string ACTION_TEMPLATES = "action-templates";
public const string ACTION_SHOW_REPLACE = "action-show-replace";
public const string ACTION_TO_LOWER_CASE = "action-to-lower-case";
Expand All @@ -101,6 +99,7 @@ namespace Scratch {
public const string ACTION_ZOOM_IN = "action-zoom-in";
public const string ACTION_ZOOM_OUT = "action-zoom-out";
public const string ACTION_TOGGLE_COMMENT = "action-toggle-comment";
public const string ACTION_TOGGLE_SHOW_FIND = "action-toggle_show-find";
public const string ACTION_TOGGLE_SIDEBAR = "action-toggle-sidebar";
public const string ACTION_TOGGLE_OUTLINE = "action-toggle-outline";
public const string ACTION_TOGGLE_TERMINAL = "action-toggle-terminal";
Expand All @@ -127,7 +126,7 @@ namespace Scratch {
private Services.GitManager git_manager;

private const ActionEntry[] ACTION_ENTRIES = {
{ ACTION_FIND, action_fetch, "s" },
{ ACTION_FIND, action_find, "s"},
{ ACTION_FIND_NEXT, action_find_next },
{ ACTION_FIND_PREVIOUS, action_find_previous },
{ ACTION_FIND_GLOBAL, action_find_global, "s" },
Expand All @@ -139,7 +138,7 @@ namespace Scratch {
{ ACTION_REVERT, action_revert },
{ ACTION_SAVE, action_save },
{ ACTION_SAVE_AS, action_save_as },
{ ACTION_SHOW_FIND, action_show_fetch, null, "false" },
{ ACTION_TOGGLE_SHOW_FIND, action_toggle_show_find, null, "false" },
{ ACTION_TEMPLATES, action_templates },
{ ACTION_GO_TO, action_go_to },
{ ACTION_SORT_LINES, action_sort_lines },
Expand Down Expand Up @@ -378,7 +377,7 @@ namespace Scratch {

private void update_toolbar_button (string name, bool new_state) {
switch (name) {
case ACTION_SHOW_FIND:
case ACTION_TOGGLE_SHOW_FIND:
if (new_state) {
toolbar.find_button.tooltip_markup = Granite.markup_accel_tooltip (
{"Escape"},
Expand All @@ -391,7 +390,7 @@ namespace Scratch {
);
}

search_revealer.set_reveal_child (new_state);
search_bar.reveal (new_state);

break;
case ACTION_TOGGLE_SIDEBAR:
Expand Down Expand Up @@ -444,20 +443,6 @@ namespace Scratch {

// SearchBar
search_bar = new Scratch.Widgets.SearchBar (this);
search_revealer = new Gtk.Revealer ();
search_revealer.add (search_bar);

search_bar.map.connect_after ((w) => { /* signalled when reveal child */
set_search_text ();
});
search_bar.search_entry.unmap.connect_after (() => { /* signalled when reveal child */
search_bar.search_entry.text = "";
search_bar.highlight_none ();
});
search_bar.search_empty.connect (() => {
folder_manager_view.clear_badges ();
});

welcome_view = new Code.WelcomeView (this);
document_view = new Scratch.Widgets.DocumentView (this);
// Handle Drag-and-drop for files functionality on welcome screen
Expand Down Expand Up @@ -524,7 +509,7 @@ namespace Scratch {
var view_grid = new Gtk.Grid () {
orientation = Gtk.Orientation.VERTICAL
};
view_grid.add (search_revealer);
view_grid.add (search_bar);
view_grid.add (document_view);

content_stack = new Gtk.Stack () {
Expand Down Expand Up @@ -563,8 +548,6 @@ namespace Scratch {
size_group.add_widget (sidebar.headerbar);
size_group.add_widget (toolbar);

search_revealer.set_reveal_child (false);

realize.connect (() => {
Scratch.saved_state.bind ("sidebar-visible", sidebar, "visible", SettingsBindFlags.DEFAULT);
Scratch.saved_state.bind ("outline-visible", document_view , "outline_visible", SettingsBindFlags.DEFAULT);
Expand Down Expand Up @@ -707,13 +690,12 @@ namespace Scratch {
});
}

// private bool on_key_pressed (Gdk.EventKey event) {
private bool on_key_pressed (uint keyval, uint keycode, Gdk.ModifierType state) {
switch (Gdk.keyval_name (keyval)) {
case "Escape":
if (search_revealer.get_child_revealed ()) {
var fetch_action = Utils.action_from_group (ACTION_SHOW_FIND, actions);
fetch_action.set_state (false);
if (search_bar.is_revealed) {
var action = Utils.action_from_group (ACTION_TOGGLE_SHOW_FIND, actions);
action.set_state (false);
document_view.current_document.source_view.grab_focus ();
}

Expand All @@ -732,7 +714,7 @@ namespace Scratch {
// Set sensitive property for 'delicate' Widgets/GtkActions while
private void set_widgets_sensitive (bool val) {
// SearchManager's stuffs
Utils.action_from_group (ACTION_SHOW_FIND, actions).set_enabled (val);
Utils.action_from_group (ACTION_TOGGLE_SHOW_FIND, actions).set_enabled (val);
Utils.action_from_group (ACTION_GO_TO, actions).set_enabled (val);
Utils.action_from_group (ACTION_SHOW_REPLACE, actions).set_enabled (val);
// Toolbar Actions
Expand All @@ -755,6 +737,26 @@ namespace Scratch {
return document_view.current_document;
}

// If selected text covers more than one line return just the first.
public void set_selected_text_for_search () {
var doc = get_current_document ();
var selected_text = doc != null ? doc.get_selected_text (false) : "";
var search_term = "";
if (selected_text.contains ("\n")) {
search_term = selected_text.split ("\n", 2)[0];
} else {
search_term = selected_text;
}

if (search_term != "") {
search_bar.set_search_entry_text (search_term);
}
}

public bool has_successful_search () {
return search_bar.search_occurrences > 0;
}

public void open_folder (File folder) {
var foldermanager_file = new FolderManager.File (folder.get_path ());
folder_manager_view.open_folder (foldermanager_file);
Expand Down Expand Up @@ -1175,32 +1177,38 @@ namespace Scratch {
}

/** Not a toggle action - linked to keyboard short cut (Ctrl-f). **/
private string current_search_term = "";
private void action_fetch (SimpleAction action, Variant? param) {
current_search_term = param != null ? param.get_string () : "";
if (!search_revealer.child_revealed) {
var show_find_action = Utils.action_from_group (ACTION_SHOW_FIND, actions);
private void action_find (SimpleAction action, Variant? param) {
find (param != null ? param.get_string () : "");
}

private void find (string search_term = "") {
if (!search_bar.is_revealed) {
var show_find_action = Utils.action_from_group (ACTION_TOGGLE_SHOW_FIND, actions);
if (show_find_action.enabled) {
/* Toggling the fetch action causes this function to be called again but the search_revealer child
* is still not revealed so nothing more happens. We use the map signal on the search entry
* to set it up once it has been revealed. */
show_find_action.set_state (true);
show_find_action.activate (new Variant ("b", true));
}
}

if (search_term != "") {
search_bar.set_search_entry_text (search_term);
} else {
set_search_text ();
set_selected_text_for_search ();
}

search_bar.search ();
}

private void action_show_replace (SimpleAction action) {
action_fetch (action, null);
find ();
// May have to wait for the search bar to be revealed before we can grab focus
if (search_revealer.child_revealed) {
search_bar.replace_entry.grab_focus ();

if (search_bar.is_revealed) {
search_bar.focus_replace_entry ();
} else {
ulong map_handler = 0;
map_handler = search_bar.map.connect_after (() => {
search_bar.replace_entry.grab_focus ();
search_bar.disconnect (map_handler);
search_bar.reveal (true);
Idle.add (() => {
search_bar.focus_replace_entry ();
return Source.REMOVE;
});
}
}
Expand All @@ -1214,50 +1222,34 @@ namespace Scratch {
}

private void action_find_global (SimpleAction action, Variant? param) {
var selected_text = "";
var search_path = "";

var current_doc = get_current_document ();
if (current_doc != null) {
selected_text = current_doc.get_selected_text (false);
}

if (selected_text != "") {
selected_text = selected_text.split ("\n", 2)[0];
}
// If search entry focused use its text for search term, else use selected text
var term = search_bar.search_entry.has_focus ?
search_bar.search_entry.text : selected_text;

// If no focused selected text fallback to search entry text if visible
if (term == "" &&
!search_bar.search_entry.has_focus &&
search_revealer.reveal_child) {

term = search_bar.search_entry.text;
if (!search_bar.is_focused || search_bar.entry_text == "") {
set_selected_text_for_search ();
}

var search_path = "";
if (param != null && param.get_string () != "") {
search_path = param.get_string ();
} else {
search_path = default_globalsearch_path;
}

if (search_path != "") {
folder_manager_view.search_global (search_path, term);
folder_manager_view.search_global (search_path, search_bar.entry_text);
} else {
// Fallback to standard search
warning ("Unable to perform global search - search document instead");
action_fetch (action, param);
find ();
}

// No need to reveal searchbar - handled by subsequent find action.
}

private void update_find_actions () {
// Idle needed to ensure that existence of current_doc is up to date
Idle.add (() => {
var is_current_doc = get_current_document () != null;
Utils.action_from_group (ACTION_FIND, actions).set_enabled (is_current_doc);
Utils.action_from_group (ACTION_SHOW_FIND, actions).set_enabled (is_current_doc);
Utils.action_from_group (ACTION_TOGGLE_SHOW_FIND, actions).set_enabled (is_current_doc);
Utils.action_from_group (ACTION_FIND_NEXT, actions).set_enabled (is_current_doc);
Utils.action_from_group (ACTION_FIND_PREVIOUS, actions).set_enabled (is_current_doc);
var can_global_search = is_current_doc || git_manager.active_project_path != null;
Expand All @@ -1267,38 +1259,18 @@ namespace Scratch {
});
}

private void set_search_text () {
if (current_search_term != "") {
search_bar.search_entry.text = current_search_term;
search_bar.search_entry.grab_focus ();
search_bar.search_next ();
} else if (search_bar.search_entry.text != "") {
// Always search on what is showing in search entry
current_search_term = search_bar.search_entry.text;
search_bar.search_entry.grab_focus ();
} else {
var current_doc = get_current_document ();
// This is also called when all documents are closed.
if (current_doc != null) {
var selected_text = current_doc.get_selected_text (false);
if (selected_text != "" && selected_text.length < MAX_SEARCH_TEXT_LENGTH) {
current_search_term = selected_text.split ("\n", 2)[0];
search_bar.search_entry.text = current_search_term;
}

search_bar.search_entry.grab_focus (); /* causes loss of document selection */
/** Toggle action - linked to toolbar togglebutton. **/
private void action_toggle_show_find () {
var action = Utils.action_from_group (ACTION_TOGGLE_SHOW_FIND, actions);
var to_show = !action.get_state ().get_boolean ();
action.set_state (to_show);
search_bar.reveal (to_show);
if (to_show) {
search_bar.focus_search_entry ();
if (search_bar.entry_text == "") {
set_selected_text_for_search ();
}
}

if (current_search_term != "") {
search_bar.search_next (); /* this selects the next match (if any) */
}
}

/** Toggle action - linked to toolbar togglebutton. **/
private void action_show_fetch () {
var fetch_action = Utils.action_from_group (ACTION_SHOW_FIND, actions);
fetch_action.set_state (!fetch_action.get_state ().get_boolean ());
}

private void action_go_to () {
Expand Down
2 changes: 1 addition & 1 deletion src/Widgets/HeaderBar.vala
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public class Scratch.HeaderBar : Hdy.HeaderBar {
font_size_box.add (zoom_in_button);

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