abouttreesummaryrefslogcommitdiff
diff options
context:
space:
mode:
authorJules Aguillon2022-07-30 17:42:58 +0200
committerJules Aguillon2022-07-30 17:56:31 +0200
commit85d798299eaee3affc9e548975c9f3dac390e6e3 (patch)
treeab26cbb81471547418a2dad9ac63d88a2f6f73b0
parentdec0a7471b20f8fd84a59f25b5c00043cc872865 (diff)
downloadunexpected-keyboard-85d798299eaee3affc9e548975c9f3dac390e6e3.tar.gz
unexpected-keyboard-85d798299eaee3affc9e548975c9f3dac390e6e3.zip
Standard auto-capitalisation only after space
Change the capitalisation algorithm to use Android's 'getCursorCapsMode'. This requires a bit of cursor calculations but should feel more standard. The auto completion only triggers after a space is typed or backspace is pressed.
-rw-r--r--srcs/juloo.keyboard2/Autocapitalisation.java116
-rw-r--r--srcs/juloo.keyboard2/Keyboard2.java3
2 files changed, 32 insertions, 87 deletions
diff --git a/srcs/juloo.keyboard2/Autocapitalisation.java b/srcs/juloo.keyboard2/Autocapitalisation.java
index 99333e0..c23509c 100644
--- a/srcs/juloo.keyboard2/Autocapitalisation.java
+++ b/srcs/juloo.keyboard2/Autocapitalisation.java
@@ -9,32 +9,36 @@ import android.view.KeyEvent;
final class Autocapitalisation
{
private boolean _enabled = false;
- private boolean _beginning_of_sentence = false;
- /** Used to avoid enabling shift after an arrow key is pressed. */
- private boolean _skip_next_selection_update = false;
- /** Keep track of the cursor to differentiate 'selection_updated' events
- corresponding to typing from cursor movement. */
- private int _cursor = 0;
+ private boolean _should_enable_shift = false;
+
+ private InputConnection _ic;
+ private int _caps_mode;
+
+ /** Keep track of the cursor to recognize cursor movements from typing. */
+ private int _cursor;
+
+ static private int SUPPORTED_CAPS_MODES =
+ InputType.TYPE_TEXT_FLAG_CAP_SENTENCES |
+ InputType.TYPE_TEXT_FLAG_CAP_WORDS;
public boolean should_enable_shift()
{
- return _enabled && _beginning_of_sentence;
+ return _should_enable_shift;
}
/** Returns [true] if shift should be on initially. The input connection
isn't stored. */
public void started(EditorInfo info, InputConnection ic)
{
- if (!Config.globalConfig().autocapitalisation
- || (info.inputType & InputType.TYPE_TEXT_FLAG_CAP_SENTENCES) == 0)
+ _ic = ic;
+ _caps_mode = info.inputType & TextUtils.CAP_MODE_SENTENCES;
+ if (!Config.globalConfig().autocapitalisation || _caps_mode == 0)
{
_enabled = false;
return;
}
_enabled = true;
- _beginning_of_sentence = ((info.initialCapsMode & TextUtils.CAP_MODE_SENTENCES) != 0);
- _cursor = 0; // Just a guess
- scan_text_before_cursor(10, ic);
+ _should_enable_shift = (info.initialCapsMode != 0);
}
public void typed(CharSequence c)
@@ -46,101 +50,43 @@ final class Autocapitalisation
public void typed(char c)
{
_cursor++;
- if (is_beginning_of_sentence(c))
- _beginning_of_sentence = true;
- else if (!ignore_at_beginning_of_sentence(c))
- _beginning_of_sentence = false;
+ if (is_trigger_character(c))
+ update_caps_mode();
+ else
+ _should_enable_shift = false;
}
public void event_sent(int code)
{
switch (code)
{
- // Disable temporarily after a keyboard cursor movement
- case KeyEvent.KEYCODE_DPAD_UP:
- case KeyEvent.KEYCODE_DPAD_RIGHT:
- case KeyEvent.KEYCODE_DPAD_DOWN:
- case KeyEvent.KEYCODE_DPAD_LEFT:
- case KeyEvent.KEYCODE_PAGE_UP:
- case KeyEvent.KEYCODE_PAGE_DOWN:
- case KeyEvent.KEYCODE_MOVE_HOME:
- case KeyEvent.KEYCODE_MOVE_END:
- _skip_next_selection_update = true;
- _beginning_of_sentence = false;
+ case KeyEvent.KEYCODE_DEL:
+ _cursor--;
+ update_caps_mode();
break;
}
}
/** Returns [true] if shift might be disabled. */
- public boolean selection_updated(int old_cursor, int new_cursor, InputConnection ic)
+ public boolean selection_updated(int old_cursor, int new_cursor)
{
- if (_skip_next_selection_update)
- {
- _cursor = new_cursor;
- _skip_next_selection_update = false;
- return false;
- }
- if (new_cursor == _cursor)
+ if (new_cursor == _cursor) // Just typing
return false;
- // Text has been inserted or cursor moved forward
- if (old_cursor == _cursor && new_cursor > old_cursor)
- {
- scan_text_before_cursor(Math.min(new_cursor - old_cursor, 10), ic);
- return true;
- }
- else
- {
- // Cursor has moved backward or text deleted
- _beginning_of_sentence = false;
- scan_text_before_cursor(10, ic);
- _cursor = new_cursor;
- return true;
- }
+ _cursor = new_cursor;
+ _should_enable_shift = false;
+ return true;
}
- /** Updates [_cursor]. */
- private void scan_text_before_cursor(int range, InputConnection ic)
+ private void update_caps_mode()
{
- if (!_enabled) // Don't query characters if disabled
- return;
- CharSequence text_before = ic.getTextBeforeCursor(range, 0);
- if (text_before == null)
- {
- _beginning_of_sentence = false;
- }
- else
- {
- _beginning_of_sentence = true;
- typed(text_before);
- }
+ _should_enable_shift = _enabled && (_ic.getCursorCapsMode(_caps_mode) != 0);
}
- private boolean ignore_at_beginning_of_sentence(char c)
+ private boolean is_trigger_character(char c)
{
switch (c)
{
case ' ':
- case '"':
- case '\'':
- case '(':
- case '«':
- return true;
- default:
- return false;
- }
- }
-
- private boolean is_beginning_of_sentence(char c)
- {
- switch (c)
- {
- case '.':
- case ';':
- case '\n':
- case '!':
- case '?':
- case '¿':
- case '¡':
return true;
default:
return false;
diff --git a/srcs/juloo.keyboard2/Keyboard2.java b/srcs/juloo.keyboard2/Keyboard2.java
index 756295e..b2197a1 100644
--- a/srcs/juloo.keyboard2/Keyboard2.java
+++ b/srcs/juloo.keyboard2/Keyboard2.java
@@ -251,8 +251,7 @@ public class Keyboard2 extends InputMethodService
public void onUpdateSelection(int oldSelStart, int oldSelEnd, int newSelStart, int newSelEnd, int candidatesStart, int candidatesEnd)
{
super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd, candidatesStart, candidatesEnd);
- update_shift_state(
- _autocap.selection_updated(oldSelStart, newSelStart, getCurrentInputConnection()));
+ update_shift_state(_autocap.selection_updated(oldSelStart, newSelStart));
}
@Override