diff options
| author | Jules Aguillon | 2026-04-16 15:33:28 +0200 |
|---|---|---|
| committer | GitHub | 2026-04-16 15:33:28 +0200 |
| commit | af5f6df02c68a4ba9c06c4c69f49425b8eaabee0 (patch) | |
| tree | 4c8240c2924a303f280b10645a86bccb05a783d5 /srcs | |
| parent | 08eb3a04a6b711cc3e88cf0d59a35626a8562afa (diff) | |
| download | unexpected-keyboard-af5f6df02c68a4ba9c06c4c69f49425b8eaabee0.tar.gz unexpected-keyboard-af5f6df02c68a4ba9c06c4c69f49425b8eaabee0.zip | |
Autocorrect middle word (#1242)
* Fix crash when typing space in the middle of a word
* Don't trigger autocorrect when the cursor is not at the end of a word
This prevented from editing a word that was already typed.
* Don't undo autocorrect at the wrong time
Backspace was trying to undo autocorrect even after some other action
were done after the last autocorrect. This ensures that the keyboard
remembers accurately the last action.
Diffstat (limited to 'srcs')
| -rw-r--r-- | srcs/juloo.keyboard2/CurrentlyTypedWord.java | 2 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/KeyEventHandler.java | 33 |
2 files changed, 20 insertions, 15 deletions
diff --git a/srcs/juloo.keyboard2/CurrentlyTypedWord.java b/srcs/juloo.keyboard2/CurrentlyTypedWord.java index acfce9e..674a90d 100644 --- a/srcs/juloo.keyboard2/CurrentlyTypedWord.java +++ b/srcs/juloo.keyboard2/CurrentlyTypedWord.java @@ -132,7 +132,7 @@ public final class CurrentlyTypedWord } if (insert_start > 0) _w.setLength(0); - _w.insert(_w.length() + _w_cursor, s, insert_start, end); + _w.insert(Math.max(_w.length() + _w_cursor, 0), s, insert_start, end); } void type_chars(CharSequence s) diff --git a/srcs/juloo.keyboard2/KeyEventHandler.java b/srcs/juloo.keyboard2/KeyEventHandler.java index 8691cda..66a10df 100644 --- a/srcs/juloo.keyboard2/KeyEventHandler.java +++ b/srcs/juloo.keyboard2/KeyEventHandler.java @@ -32,6 +32,9 @@ public final class KeyEventHandler boolean _move_cursor_force_fallback = false; /** Whether the space bar automatically enters the best suggestion. */ boolean _space_bar_auto_complete = false; + /** Remember the action that was handled. This is used by autocorrect. */ + LastAction _last_action = null; + LastAction _next_last_action = null; public KeyEventHandler(IReceiver recv, Config config) { @@ -54,7 +57,7 @@ public final class KeyEventHandler _move_cursor_force_fallback = conf.editor_config.should_move_cursor_force_fallback; _space_bar_auto_complete = conf.space_bar_auto_complete; - clear_space_bar_state(); + _last_action = null; } /** Selection has been updated. */ @@ -102,6 +105,7 @@ public final class KeyEventHandler { if (key == null) return; + _next_last_action = LastAction.OTHER; Pointers.Modifiers old_mods = _mods; update_meta_state(mods); switch (key.getKind()) @@ -117,6 +121,7 @@ public final class KeyEventHandler case Macro: evaluate_macro(key.getMacro()); break; } update_meta_state(old_mods); + _last_action = _next_last_action; } @Override @@ -133,6 +138,7 @@ public final class KeyEventHandler replace_surrounding_text(old.length() + cur_rel, -cur_rel, text + " "); last_replaced_word = old; last_replacement_word_len = text.length() + 1; + _next_last_action = LastAction.SUGGESTION_ENTERED; } @Override @@ -242,7 +248,6 @@ public final class KeyEventHandler { _autocap.event_sent(eventCode, metaState); _typedword.event_sent(eventCode, metaState); - clear_space_bar_state(); } } @@ -254,7 +259,6 @@ public final class KeyEventHandler _autocap.typed(text); _typedword.typed(text); conn.commitText(text, 1); - clear_space_bar_state(); } void replace_surrounding_text(int remove_before, int remove_after, @@ -525,22 +529,22 @@ public final class KeyEventHandler backspace. */ int last_replacement_word_len = 0; + /** Implement autocorrect when enabled in the settings. */ void handle_space_bar() { if (_space_bar_auto_complete && _suggestions.best_suggestion != null - && !_typedword.is_selection_not_empty()) - { + && !_typedword.is_selection_not_empty() + && _typedword.cursor_relative() == 0) suggestion_entered(_suggestions.best_suggestion); - } else - { send_text(" "); - } } + /** Undo the last autocorrect. */ void handle_backspace() { - if (last_replaced_word != null) + if (_last_action == LastAction.SUGGESTION_ENTERED + && last_replaced_word != null) { replace_surrounding_text(last_replacement_word_len, 0, last_replaced_word + " "); @@ -552,11 +556,6 @@ public final class KeyEventHandler } } - void clear_space_bar_state() - { - last_replaced_word = null; - } - public static interface IReceiver extends Suggestions.Callback { public void handle_event_key(KeyValue.Event ev); @@ -578,4 +577,10 @@ public final class KeyEventHandler _recv.set_shift_state(false, false); } } + + public static enum LastAction + { + SUGGESTION_ENTERED, + OTHER + } } |
