diff options
| author | Jules Aguillon | 2025-10-02 01:59:07 +0200 |
|---|---|---|
| committer | Jules Aguillon | 2025-12-28 18:13:17 +0100 |
| commit | 2208079e489ef7652e28294130825d7451e01571 (patch) | |
| tree | eaf08fad2d28585b858e2248b9cb0133a1237755 | |
| parent | c4d802d873ee404863fa7376651ef59212d57a9f (diff) | |
| download | unexpected-keyboard-2208079e489ef7652e28294130825d7451e01571.tar.gz unexpected-keyboard-2208079e489ef7652e28294130825d7451e01571.zip | |
CurrentlyTypedWord: Handle key events
Currently, refreshing the current word on each key event.
Refreshing is done after a short delay, to ensure the editor has handled
the event.
| -rw-r--r-- | srcs/juloo.keyboard2/CurrentlyTypedWord.java | 45 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/KeyEventHandler.java | 8 |
2 files changed, 46 insertions, 7 deletions
diff --git a/srcs/juloo.keyboard2/CurrentlyTypedWord.java b/srcs/juloo.keyboard2/CurrentlyTypedWord.java index df7882b..5a0733b 100644 --- a/srcs/juloo.keyboard2/CurrentlyTypedWord.java +++ b/srcs/juloo.keyboard2/CurrentlyTypedWord.java @@ -1,5 +1,7 @@ package juloo.keyboard2; +import android.os.Handler; +import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import java.util.List; @@ -8,11 +10,18 @@ import java.util.List; public final class CurrentlyTypedWord { InputConnection _ic = null; + Handler _handler; Callback _callback; + /** The currently typed word. */ StringBuilder _w = new StringBuilder(); + /** This can be disabled if the editor doesn't support looking at the text + before the cursor. */ boolean _enabled = false; + /** The current word is empty while the selection is ongoing. */ boolean _has_selection = false; + /** Used to avoid concurrent refreshes in [delayed_refresh()]. */ + boolean _refresh_pending = false; /** The estimated cursor position. Used to avoid expensive IPC calls when the typed word can be estimated locally with [typed]. When the cursor @@ -20,8 +29,9 @@ public final class CurrentlyTypedWord the editor. */ int _cursor; - public CurrentlyTypedWord(Callback cb) + public CurrentlyTypedWord(Handler h, Callback cb) { + _handler = h; _callback = cb; } @@ -62,13 +72,20 @@ public final class CurrentlyTypedWord _cursor = newSelStart; } - private void callback() + public void event_sent(int code, int meta) + { + if (!_enabled) + return; + delayed_refresh(); + } + + void callback() { _callback.currently_typed_word(_w.toString()); } /** Estimate the currently typed word after [chars] has been typed. */ - private void type_chars(String s) + void type_chars(String s) { int len = s.length(); for (int i = 0; i < len;) @@ -84,8 +101,9 @@ public final class CurrentlyTypedWord } /** Refresh the current word by immediately querying the editor. */ - private void refresh_current_word() + void refresh_current_word() { + _refresh_pending = false; if (_has_selection) set_current_word(""); else @@ -93,7 +111,7 @@ public final class CurrentlyTypedWord } /** Refresh the current word by immediately querying the editor. */ - private void set_current_word(CharSequence text_before_cursor) + void set_current_word(CharSequence text_before_cursor) { if (text_before_cursor == null) { @@ -105,6 +123,23 @@ public final class CurrentlyTypedWord callback(); } + /** Wait some time to let the editor finishes reacting to changes and call + [refresh_current_word]. */ + void delayed_refresh() + { + _refresh_pending = true; + _handler.postDelayed(delayed_refresh_run, 50); + } + + Runnable delayed_refresh_run = new Runnable() + { + public void run() + { + if (_refresh_pending) + refresh_current_word(); + } + }; + public static interface Callback { public void currently_typed_word(String word); diff --git a/srcs/juloo.keyboard2/KeyEventHandler.java b/srcs/juloo.keyboard2/KeyEventHandler.java index c6b1730..057033b 100644 --- a/srcs/juloo.keyboard2/KeyEventHandler.java +++ b/srcs/juloo.keyboard2/KeyEventHandler.java @@ -33,11 +33,12 @@ public final class KeyEventHandler public KeyEventHandler(IReceiver recv) { _recv = recv; - _autocap = new Autocapitalisation(recv.getHandler(), + Handler handler = recv.getHandler(); + _autocap = new Autocapitalisation(handler, this.new Autocapitalisation_callback()); _mods = Pointers.Modifiers.EMPTY; _suggestions = new Suggestions(recv); - _typedword = new CurrentlyTypedWord(this); + _typedword = new CurrentlyTypedWord(handler, this); } /** Editing just started. */ @@ -222,7 +223,10 @@ public final class KeyEventHandler metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE)); if (eventAction == KeyEvent.ACTION_UP) + { _autocap.event_sent(eventCode, metaState); + _typedword.event_sent(eventCode, metaState); + } } void send_text(String text) |
