From 92a8db5e9374082000f9c79becd09661bea84dd9 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Sat, 9 Sep 2023 14:15:44 +0200 Subject: Update auto-capitalisation state when input starts The initial capitalisation state given by the editor (`info.initialCapsMode`) is always 0 in many editors. For some text input types, update the state when typing starts, disregarding the value given by `info.initialCapsMode`. --- srcs/juloo.keyboard2/Autocapitalisation.java | 40 +++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'srcs/juloo.keyboard2/Autocapitalisation.java') diff --git a/srcs/juloo.keyboard2/Autocapitalisation.java b/srcs/juloo.keyboard2/Autocapitalisation.java index 3dcdefc..b73f2e3 100644 --- a/srcs/juloo.keyboard2/Autocapitalisation.java +++ b/srcs/juloo.keyboard2/Autocapitalisation.java @@ -49,7 +49,8 @@ final class Autocapitalisation } _enabled = true; _should_enable_shift = (info.initialCapsMode != 0); - _callback.update_shift_state(_should_enable_shift, true); + _should_update_caps_mode = started_should_update_state(info.inputType); + callback_now(true); } public void typed(CharSequence c) @@ -81,7 +82,7 @@ final class Autocapitalisation { _should_enable_shift = false; _should_update_caps_mode = false; - callback(true); + callback_now(true); } public static interface Callback @@ -119,7 +120,11 @@ final class Autocapitalisation } }; - void callback(final boolean might_disable) + /** Update the shift state if [_should_update_caps_mode] is true, then call + [_callback.update_shift_state]. This is done after a short delay to wait + for the editor to handle the events, as this might be called before the + corresponding event is sent. */ + void callback(boolean might_disable) { _should_disable_shift = might_disable; // The callback must be delayed because [getCursorCapsMode] would sometimes @@ -127,6 +132,13 @@ final class Autocapitalisation _handler.postDelayed(delayed_callback, 1); } + /** Like [callback] but runs immediately. */ + void callback_now(boolean might_disable) + { + _should_disable_shift = might_disable; + delayed_callback.run(); + } + void type_one_char(char c) { _cursor++; @@ -146,4 +158,26 @@ final class Autocapitalisation return false; } } + + /** Whether the caps state should be updated when input starts. [inputType] + is the field from the editor info object. */ + boolean started_should_update_state(int inputType) + { + int class_ = inputType & InputType.TYPE_MASK_CLASS; + int variation = inputType & InputType.TYPE_MASK_VARIATION; + if (class_ != InputType.TYPE_CLASS_TEXT) + return false; + switch (variation) + { + case InputType.TYPE_TEXT_VARIATION_LONG_MESSAGE: + case InputType.TYPE_TEXT_VARIATION_NORMAL: + case InputType.TYPE_TEXT_VARIATION_PERSON_NAME: + case InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE: + case InputType.TYPE_TEXT_VARIATION_EMAIL_SUBJECT: + case InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT: + return true; + default: + return false; + } + } } -- cgit v1.2.3