Skip to content

Commit

Permalink
Fix searchterm reverts (#1336)
Browse files Browse the repository at this point in the history
* Always search for search entry text if present

* Add action to clear search entry

* Remove temporary comment

* Searchbar listens directly for selection change and clears itself

* Remove commented out code

---------

Co-authored-by: Ryan Kornheisl <[email protected]>
  • Loading branch information
Jeremy Wootten and zeebok authored Jul 11, 2023
1 parent 380240e commit b58e753
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,10 @@ namespace Scratch {
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.
Expand Down
11 changes: 10 additions & 1 deletion src/Widgets/SearchBar.vala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ namespace Scratch.Widgets {
MIXED,
ALWAYS
}

public weak MainWindow window { get; construct; }

private Gtk.Button tool_arrow_up;
Expand Down Expand Up @@ -235,8 +234,11 @@ namespace Scratch.Widgets {
return;
} else if (this.text_buffer != null) {
this.text_buffer.changed.disconnect (on_text_buffer_changed);
this.text_view.selection_changed.disconnect (on_selection_changed);
}

this.text_view = text_view;
this.text_view.selection_changed.connect (on_selection_changed);
this.text_buffer = text_view.get_buffer ();
this.text_buffer.changed.connect (on_text_buffer_changed);
this.search_context = new Gtk.SourceSearchContext (text_buffer as Gtk.SourceBuffer, null);
Expand All @@ -250,6 +252,13 @@ namespace Scratch.Widgets {
update_search_widgets ();
}

private void on_selection_changed () {
var selected_text = text_view.get_selected_text ();
if (selected_text != search_entry.text) {
search_entry.text = "";
}
}

private void on_replace_entry_activate () {
if (text_buffer == null) {
warning ("No valid buffer to replace");
Expand Down
9 changes: 7 additions & 2 deletions src/Widgets/SourceView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ namespace Scratch.Widgets {
private uint size_allocate_timer = 0;
private Gtk.TextIter last_select_start_iter;
private Gtk.TextIter last_select_end_iter;
private string selected_text = "";
private SourceGutterRenderer git_diff_gutter_renderer;

private const uint THROTTLE_MS = 400;
private double total_delta = 0;
private const double SCROLL_THRESHOLD = 1.0;

public signal void style_changed (Gtk.SourceStyleScheme style);
// "selection_changed" signal now only emitted when the selected text changes (position ignored). Listened to by searchbar and highlight word selection plugin
public signal void selection_changed (Gtk.TextIter start_iter, Gtk.TextIter end_iter);
public signal void deselected ();

Expand Down Expand Up @@ -570,7 +572,6 @@ namespace Scratch.Widgets {

last_select_start_iter.assign (start);
last_select_end_iter.assign (end);

update_draw_spaces ();

if (selection_changed_timer != 0) {
Expand All @@ -592,7 +593,11 @@ namespace Scratch.Widgets {
Gtk.TextIter start, end;
bool selected = buffer.get_selection_bounds (out start, out end);
if (selected) {
selection_changed (start, end);
var prev_selected_text = selected_text;
selected_text = buffer.get_text (start, end, true);
if (selected_text != prev_selected_text) {
selection_changed (start, end);
}
} else {
deselected ();
}
Expand Down

0 comments on commit b58e753

Please sign in to comment.