Skip to content

Commit

Permalink
Update auto-capitalisation state when input starts
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Julow committed Sep 9, 2023
1 parent 687d88f commit 92a8db5
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions srcs/juloo.keyboard2/Autocapitalisation.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public void started(EditorInfo info, InputConnection ic)
}
_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)
Expand Down Expand Up @@ -81,7 +82,7 @@ public void stop()
{
_should_enable_shift = false;
_should_update_caps_mode = false;
callback(true);
callback_now(true);
}

public static interface Callback
Expand Down Expand Up @@ -119,14 +120,25 @@ public void run()
}
};

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
// be called before the editor finished handling the previous event.
_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++;
Expand All @@ -146,4 +158,26 @@ boolean is_trigger_character(char c)
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;
}
}
}

0 comments on commit 92a8db5

Please sign in to comment.