Modification step for the special layout

Refactor, follow up of 90b7944. Add a modification step to the "special"
layouts: numpad, greekmath, pin entry.

Remove the apply_key0 function, which is not expressive enough.
Add an enum instead of yet an other "switch_" function.
This commit is contained in:
Jules Aguillon 2023-01-30 23:46:02 +01:00
parent 90b7944129
commit 78a461f740
4 changed files with 98 additions and 62 deletions

View File

@ -153,6 +153,13 @@ final class Config
extra_keys_param = ExtraKeyCheckBoxPreference.get_extra_keys(_prefs);
}
KeyValue action_key()
{
// Update the name to avoid caching in KeyModifier
return (actionLabel == null) ? null :
KeyValue.getKeyByName("action").withSymbol(actionLabel);
}
/** Update the layout according to the configuration.
* - Remove the switching key if it isn't needed
* - Remove "localized" keys from other locales (not in 'extra_keys')
@ -161,9 +168,7 @@ final class Config
*/
public KeyboardData modify_layout(KeyboardData kw)
{
// Update the name to avoid caching in KeyModifier
final KeyValue action_key = (actionLabel == null) ? null :
KeyValue.getKeyByName("action").withSymbol(actionLabel);
final KeyValue action_key = action_key();
// Extra keys are removed from the set as they are encountered during the
// first iteration then automatically added.
final Set<KeyValue> extra_keys = new HashSet<KeyValue>();
@ -173,25 +178,6 @@ final class Config
if (show_numpad)
kw = kw.addNumPad();
kw = kw.mapKeys(new KeyboardData.MapKeyValues() {
/** Apply to the center value only. Partial match, fallback to [apply]. */
public KeyboardData.Corner apply_key0(KeyboardData.Corner corner)
{
if (corner == null)
return null;
KeyValue kv = corner.kv;
switch (kv.getKind())
{
case Char:
char c = kv.getChar();
if (inverse_numpad)
c = inverse_numpad_char(c);
if (c != kv.getChar())
return KeyboardData.Corner.of_kv(kv.withChar(c));
break;
}
return super.apply(corner);
}
public KeyValue apply(KeyValue key, boolean localized)
{
boolean is_extra_key = extra_keys.contains(key);
@ -237,6 +223,45 @@ final class Config
return kw;
}
/**
* Handle the numpad layout.
*/
public KeyboardData modify_numpad(KeyboardData kw)
{
final KeyValue action_key = action_key();
return kw.mapKeys(new KeyboardData.MapKeyValues() {
public KeyValue apply(KeyValue key, boolean localized)
{
switch (key.getKind())
{
case Event:
switch (key.getEvent())
{
case ACTION:
return (swapEnterActionKey && action_key != null) ?
KeyValue.getKeyByName("enter") : action_key;
}
break;
case Keyevent:
switch (key.getKeyevent())
{
case KeyEvent.KEYCODE_ENTER:
return (swapEnterActionKey && action_key != null) ? action_key : key;
}
break;
case Char:
char a = key.getChar(), b = a;
if (inverse_numpad)
b = inverse_numpad_char(a);
if (a != b)
return key.withChar(b);
break;
}
return key;
}
});
}
/** Modify a layout to turn it into a secondary layout by changing the
"switch_second" key. */
KeyboardData tweak_secondary_layout(KeyboardData layout)

View File

@ -44,8 +44,8 @@ class KeyEventHandler implements Config.IKeyEventHandler
switch (key.getEvent())
{
case CONFIG: _recv.showKeyboardConfig(); break;
case SWITCH_TEXT: _recv.switch_text(); break;
case SWITCH_NUMERIC: _recv.switch_layout(R.xml.numeric); break;
case SWITCH_TEXT: _recv.set_layout(Layout.Current); break;
case SWITCH_NUMERIC: _recv.set_layout(Layout.Numeric); break;
case SWITCH_EMOJI: _recv.setPane_emoji(); break;
case SWITCH_BACK_EMOJI: _recv.setPane_normal(); break;
case CHANGE_METHOD: _recv.switchToNextInputMethod(); break;
@ -54,9 +54,9 @@ class KeyEventHandler implements Config.IKeyEventHandler
if (conn != null)
conn.performEditorAction(actionId);
break;
case SWITCH_SECOND: _recv.switch_second(); break;
case SWITCH_SECOND_BACK: _recv.switch_primary(); break;
case SWITCH_GREEKMATH: _recv.switch_layout(R.xml.greekmath); break;
case SWITCH_SECOND: _recv.set_layout(Layout.Secondary); break;
case SWITCH_SECOND_BACK: _recv.set_layout(Layout.Primary); break;
case SWITCH_GREEKMATH: _recv.set_layout(Layout.Greekmath); break;
case CAPS_LOCK: _recv.set_shift_state(true, true); break;
}
break;
@ -168,20 +168,23 @@ class KeyEventHandler implements Config.IKeyEventHandler
conn.performContextMenuAction(id);
}
public enum Layout
{
Current, // The primary or secondary layout
Primary,
Secondary,
Numeric,
Greekmath
}
public static interface IReceiver
{
public void switchToNextInputMethod();
public void setPane_emoji();
public void setPane_normal();
public void showKeyboardConfig();
public void switch_text();
public void switch_primary();
public void switch_second();
public void switch_layout(int layout_id);
public void set_layout(Layout l);
public void set_shift_state(boolean state, boolean lock);
public InputConnection getCurrentInputConnection();
}

View File

@ -64,6 +64,17 @@ public class Keyboard2 extends InputMethodService
_keyboardView.setKeyboard(current_layout());
}
void setSpecialLayout(KeyboardData l)
{
_currentSpecialLayout = l;
_keyboardView.setKeyboard(l);
}
KeyboardData loadLayout(int layout_id)
{
return KeyboardData.load(getResources(), layout_id);
}
@Override
public void onCreate()
{
@ -248,7 +259,8 @@ public class Keyboard2 extends InputMethodService
case InputType.TYPE_CLASS_NUMBER:
case InputType.TYPE_CLASS_PHONE:
case InputType.TYPE_CLASS_DATETIME:
_currentSpecialLayout = KeyboardData.load_pin_entry(getResources());
_currentSpecialLayout =
_config.modify_numpad(KeyboardData.load_pin_entry(getResources()));
break;
default:
_currentSpecialLayout = null;
@ -317,8 +329,7 @@ public class Keyboard2 extends InputMethodService
/** Not static */
public class Receiver implements KeyEventHandler.IReceiver
{
public void switchToNextInputMethod()
{
public void switchToNextInputMethod() {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.showInputMethodPicker();
// deprecated in version 28: imm.switchToNextInputMethod(getConnectionToken(), false);
@ -342,26 +353,28 @@ public class Keyboard2 extends InputMethodService
_keyboardView.set_shift_state(state, lock);
}
public void switch_text()
public void set_layout(KeyEventHandler.Layout l)
{
_currentSpecialLayout = null;
_keyboardView.setKeyboard(current_layout());
}
public void switch_layout(int layout_id)
{
_keyboardView.setKeyboard(KeyboardData.load(getResources(), layout_id));
}
public void switch_second()
{
if (_config.second_layout != null)
setTextLayout(Current_text_layout.SECONDARY);
}
public void switch_primary()
{
setTextLayout(Current_text_layout.PRIMARY);
switch (l)
{
case Current:
_currentSpecialLayout = null;
_keyboardView.setKeyboard(current_layout());
break;
case Primary:
setTextLayout(Current_text_layout.PRIMARY);
break;
case Secondary:
if (_config.second_layout != null)
setTextLayout(Current_text_layout.SECONDARY);
break;
case Numeric:
setSpecialLayout(_config.modify_numpad(loadLayout(R.xml.numeric)));
break;
case Greekmath:
setSpecialLayout(_config.modify_numpad(loadLayout(R.xml.greekmath)));
break;
}
}
public void showKeyboardConfig()

View File

@ -458,16 +458,11 @@ class KeyboardData
public Key apply(Key k)
{
return new Key(apply_key0(k.key0), apply(k.key1), apply(k.key2),
return new Key(apply(k.key0), apply(k.key1), apply(k.key2),
apply(k.key3), apply(k.key4), k.width, k.shift, k.edgekeys,
k.slider, k.indication);
}
protected Corner apply_key0(Corner c)
{
return apply(c);
}
protected Corner apply(Corner c)
{
if (c == null)