From c4d802d873ee404863fa7376651ef59212d57a9f Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Thu, 2 Oct 2025 01:35:32 +0200 Subject: CurrentlyTypedWord: Handle selection When selection is entered, the current word is cleared. --- srcs/juloo.keyboard2/CurrentlyTypedWord.java | 35 ++++++++++++++++++---------- 1 file changed, 23 insertions(+), 12 deletions(-) (limited to 'srcs') diff --git a/srcs/juloo.keyboard2/CurrentlyTypedWord.java b/srcs/juloo.keyboard2/CurrentlyTypedWord.java index 15ffd6c..df7882b 100644 --- a/srcs/juloo.keyboard2/CurrentlyTypedWord.java +++ b/srcs/juloo.keyboard2/CurrentlyTypedWord.java @@ -12,6 +12,7 @@ public final class CurrentlyTypedWord StringBuilder _w = new StringBuilder(); boolean _enabled = false; + boolean _has_selection = false; /** The estimated cursor position. Used to avoid expensive IPC calls when the typed word can be estimated locally with [typed]. When the cursor @@ -32,16 +33,19 @@ public final class CurrentlyTypedWord public void started(Config conf, InputConnection ic) { _ic = ic; + _enabled = true; EditorConfig e = conf.editor_config; - refresh_current_word(e.initial_text_before_cursor, - e.initial_sel_start != e.initial_sel_end); + _has_selection = e.initial_sel_start != e.initial_sel_end; _cursor = e.initial_sel_start; + if (!_has_selection) + set_current_word(e.initial_text_before_cursor); } public void typed(String s) { if (!_enabled) return; + _has_selection = false; type_chars(s); callback(); } @@ -50,10 +54,11 @@ public final class CurrentlyTypedWord { // Avoid the expensive [refresh_current_word] call when [typed] was called // before. - if (!_enabled || newSelStart == _cursor) + boolean new_has_sel = newSelStart != newSelEnd; + if (!_enabled || (newSelStart == _cursor && new_has_sel == _has_selection)) return; - refresh_current_word(_ic.getTextBeforeCursor(10, 0), - newSelStart != newSelEnd); + _has_selection = new_has_sel; + refresh_current_word(); _cursor = newSelStart; } @@ -78,18 +83,24 @@ public final class CurrentlyTypedWord } } - /** Set [_enabled]. */ - private void refresh_current_word(CharSequence text_before_cursor, boolean has_selection) + /** Refresh the current word by immediately querying the editor. */ + private void refresh_current_word() { - _w.setLength(0); - if (_ic == null || text_before_cursor == null) + if (_has_selection) + set_current_word(""); + else + set_current_word(_ic.getTextBeforeCursor(10, 0)); + } + + /** Refresh the current word by immediately querying the editor. */ + private void set_current_word(CharSequence text_before_cursor) + { + if (text_before_cursor == null) { _enabled = false; return; } - _enabled = true; - if (has_selection) - return; + _w.setLength(0); type_chars(text_before_cursor.toString()); callback(); } -- cgit v1.2.3