From f5eb28b593efd69f05c9302dc2c535324d43f6e4 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Mon, 30 Jun 2025 14:44:00 +0100 Subject: [PATCH 1/3] Lose separate deselected signal, perform selection highight in idle --- .../highlight-word-selection.vala | 48 +++++++++---------- src/Widgets/SourceView.vala | 24 ++++------ 2 files changed, 30 insertions(+), 42 deletions(-) diff --git a/plugins/highlight-word-selection/highlight-word-selection.vala b/plugins/highlight-word-selection/highlight-word-selection.vala index 15e13c9a4..ab6496fde 100644 --- a/plugins/highlight-word-selection/highlight-word-selection.vala +++ b/plugins/highlight-word-selection/highlight-word-selection.vala @@ -34,12 +34,10 @@ public class Scratch.Plugins.HighlightSelectedWords : Peas.ExtensionBase, Scratc plugins = (Scratch.Services.Interface) object; plugins.hook_document.connect ((doc) => { if (current_source != null) { - current_source.deselected.disconnect (on_deselection); current_source.selection_changed.disconnect (on_selection_changed); } current_source = doc.source_view; - current_source.deselected.connect (on_deselection); current_source.selection_changed.connect (on_selection_changed); }); @@ -48,16 +46,22 @@ public class Scratch.Plugins.HighlightSelectedWords : Peas.ExtensionBase, Scratc }); } - 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, - null - ); - current_search_context.settings.search_text = ""; + // A deselection is now treated as a selection change + private void on_selection_changed () requires (main_window != null) { + Idle.add (update_highlight_source_func); + } + + private bool update_highlight_source_func () { + if (current_search_context != null) { + // Cancel existing search current_search_context.set_highlight (false); + current_search_context = null; + } + // Only highlight (extended) selection if non-zero length and search highlighting not happening + Gtk.TextIter start, end; + if (current_source.buffer.get_selection_bounds (out start, out end) && + !main_window.has_successful_search ()) { var original_start = start.copy (); // Ignore leading space @@ -114,29 +118,21 @@ public class Scratch.Plugins.HighlightSelectedWords : Peas.ExtensionBase, Scratc // Ensure no leading or trailing space var selected_text = start.get_text (end).strip (); - if (selected_text.char_count () > SELECTION_HIGHLIGHT_MAX_CHARS) { - return; + if (selected_text.char_count () <= SELECTION_HIGHLIGHT_MAX_CHARS) { + current_search_context = new Gtk.SourceSearchContext ( + (Gtk.SourceBuffer)current_source.buffer, + null + ); + current_search_context.settings.search_text = selected_text; + current_search_context.set_highlight (selected_text.length > 0); } - - current_search_context.settings.search_text = selected_text; - current_search_context.set_highlight (true); - } else if (current_search_context != null) { - // Cancel existing search - current_search_context.set_highlight (false); - current_search_context = null; } - } - public void on_deselection () { - if (current_search_context != null) { - current_search_context.set_highlight (false); - current_search_context = null; - } + return Source.REMOVE; } public void deactivate () { if (current_source != null) { - current_source.deselected.disconnect (on_deselection); current_source.selection_changed.disconnect (on_selection_changed); } } diff --git a/src/Widgets/SourceView.vala b/src/Widgets/SourceView.vala index 85d490e5b..368ecdc00 100644 --- a/src/Widgets/SourceView.vala +++ b/src/Widgets/SourceView.vala @@ -44,9 +44,9 @@ namespace Scratch.Widgets { 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 + // "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 (); //lang can be null, in the case of *No highlight style* aka Normal text public Gtk.SourceLanguage? language { @@ -680,12 +680,7 @@ namespace Scratch.Widgets { } // Fire deselected immediately - if (start.equal (end)) { - deselected (); - // Don't fire signal till we think select movement is done - } else { selection_changed_timer = Timeout.add (THROTTLE_MS, selection_changed_event); - } } private void on_mark_deleted (Gtk.TextMark mark) { @@ -698,15 +693,12 @@ namespace Scratch.Widgets { private bool selection_changed_event () { Gtk.TextIter start, end; - bool selected = buffer.get_selection_bounds (out start, out end); - if (selected) { - 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 (); + buffer.get_selection_bounds (out start, out end); + // No selection now treated as a potential selection change + var prev_selected_text = selected_text; + selected_text = buffer.get_text (start, end, true); + if (selected_text != prev_selected_text) { + selection_changed (start, end); } selection_changed_timer = 0; From eb1364a1fbd37e1e75ae96b35ad94fcc3a8ee575 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Mon, 30 Jun 2025 14:49:06 +0100 Subject: [PATCH 2/3] Idle no longer needed --- .../highlight-word-selection/highlight-word-selection.vala | 6 ------ 1 file changed, 6 deletions(-) diff --git a/plugins/highlight-word-selection/highlight-word-selection.vala b/plugins/highlight-word-selection/highlight-word-selection.vala index ab6496fde..56dc003b5 100644 --- a/plugins/highlight-word-selection/highlight-word-selection.vala +++ b/plugins/highlight-word-selection/highlight-word-selection.vala @@ -48,10 +48,6 @@ public class Scratch.Plugins.HighlightSelectedWords : Peas.ExtensionBase, Scratc // A deselection is now treated as a selection change private void on_selection_changed () requires (main_window != null) { - Idle.add (update_highlight_source_func); - } - - private bool update_highlight_source_func () { if (current_search_context != null) { // Cancel existing search current_search_context.set_highlight (false); @@ -127,8 +123,6 @@ public class Scratch.Plugins.HighlightSelectedWords : Peas.ExtensionBase, Scratc current_search_context.set_highlight (selected_text.length > 0); } } - - return Source.REMOVE; } public void deactivate () { From 5974fa5ff8172c908300a095b6bd3da012c5ff5f Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Mon, 30 Jun 2025 14:51:38 +0100 Subject: [PATCH 3/3] Cleanup --- .../highlight-word-selection/highlight-word-selection.vala | 3 ++- src/Widgets/SourceView.vala | 5 ++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/highlight-word-selection/highlight-word-selection.vala b/plugins/highlight-word-selection/highlight-word-selection.vala index 56dc003b5..c7ad0af48 100644 --- a/plugins/highlight-word-selection/highlight-word-selection.vala +++ b/plugins/highlight-word-selection/highlight-word-selection.vala @@ -114,13 +114,14 @@ public class Scratch.Plugins.HighlightSelectedWords : Peas.ExtensionBase, Scratc // Ensure no leading or trailing space var selected_text = start.get_text (end).strip (); + // We know the selected text is non-zero length, check not too long if (selected_text.char_count () <= SELECTION_HIGHLIGHT_MAX_CHARS) { current_search_context = new Gtk.SourceSearchContext ( (Gtk.SourceBuffer)current_source.buffer, null ); current_search_context.settings.search_text = selected_text; - current_search_context.set_highlight (selected_text.length > 0); + current_search_context.set_highlight (true); } } } diff --git a/src/Widgets/SourceView.vala b/src/Widgets/SourceView.vala index 368ecdc00..c03ddafba 100644 --- a/src/Widgets/SourceView.vala +++ b/src/Widgets/SourceView.vala @@ -44,7 +44,7 @@ namespace Scratch.Widgets { 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). + // "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); @@ -679,8 +679,7 @@ namespace Scratch.Widgets { selection_changed_timer = 0; } - // Fire deselected immediately - selection_changed_timer = Timeout.add (THROTTLE_MS, selection_changed_event); + selection_changed_timer = Timeout.add (THROTTLE_MS, selection_changed_event); } private void on_mark_deleted (Gtk.TextMark mark) {