Skip to content

Commit

Permalink
Per-script numpad
Browse files Browse the repository at this point in the history
The numeric layout and the optional right hand side numpad are modified
to show the digits belonging to the script used in the current layout.

The numpads are still defined as it was before. The digits are changed
in `modify_numpad` if needed.
  • Loading branch information
Julow committed Sep 3, 2023
1 parent b079e5c commit 687d88f
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 13 deletions.
2 changes: 1 addition & 1 deletion res/xml/beng_national.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard name="বাংলা (জাতীয়)">
<keyboard name="বাংলা (জাতীয়)" script="bengali">
<row>
<key key0="" key2="" key3="esc" key4=""/>
<key key0="" key2="" key3="" key4=""/>
Expand Down
2 changes: 1 addition & 1 deletion res/xml/beng_provat.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<keyboard name="বাংলা (প্রভাত)">
<keyboard name="বাংলা (প্রভাত)" script="bengali">
<row>
<key key0="" key1="" key2="" key3="!" key5="esc" />
<key key0="" key1="" key2="" key3="\@" />
Expand Down
17 changes: 11 additions & 6 deletions srcs/juloo.keyboard2/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public KeyValue apply(KeyValue key, boolean localized)
}
});
if (show_numpad)
kw = kw.addNumPad(modify_numpad(KeyboardData.num_pad));
kw = kw.addNumPad(modify_numpad(KeyboardData.num_pad, kw.script));
if (number_row)
kw = kw.addNumberRow();
if (extra_keys.size() > 0)
Expand All @@ -253,9 +253,10 @@ public KeyValue apply(KeyValue key, boolean localized)
/**
* Handle the numpad layout.
*/
public KeyboardData modify_numpad(KeyboardData kw)
public KeyboardData modify_numpad(KeyboardData kw, String script)
{
final KeyValue action_key = action_key();
final KeyModifier.Map_char map_digit = KeyModifier.modify_numpad_script(script);
return kw.mapKeys(new KeyboardData.MapKeyValues() {
public KeyValue apply(KeyValue key, boolean localized)
{
Expand All @@ -277,11 +278,15 @@ public KeyValue apply(KeyValue key, boolean localized)
}
break;
case Char:
char a = key.getChar(), b = a;
char prev_c = key.getChar();
char c = prev_c;
if (inverse_numpad)
b = inverse_numpad_char(a);
if (a != b)
return key.withChar(b);
c = inverse_numpad_char(c);
String modified = map_digit.apply(c);
if (modified != null) // Was modified by script
return key.withSymbol(modified);
if (prev_c != c) // Was inverted
return key.withChar(c);
break;
}
return key;
Expand Down
105 changes: 104 additions & 1 deletion srcs/juloo.keyboard2/KeyModifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ public static KeyValue modify_long_press(KeyValue k)
return k;
}

public static Map_char modify_numpad_script(String script)
{
if (script == null)
return map_char_none;
switch (script)
{
case "arabic": return map_char_numpad_arabic;
case "bengali": return map_char_numpad_bengali;
case "devanagari": return map_char_numpad_devanagari;
case "persian": return map_char_numpad_persian;
default: return map_char_none;
}
}

private static KeyValue apply_map_char(KeyValue k, Map_char map)
{
switch (k.getKind())
Expand Down Expand Up @@ -427,13 +441,18 @@ private static HashMap<Pointers.Modifiers, KeyValue> cacheEntry(KeyValue k)
return ks;
}

private static abstract class Map_char
public static abstract class Map_char
{
/** Modify a char or return [null] if the modifier do not apply. Return a
[String] that can contains combining diacritics. */
public abstract String apply(char c);
}

private static final Map_char map_char_none =
new Map_char() {
public String apply(char _c) { return null; }
};

private static char map_char_shift(char c)
{
switch (c)
Expand Down Expand Up @@ -971,4 +990,88 @@ public String apply(char c)
}
}
};

private static final Map_char map_char_numpad_arabic =
new Map_char() {
public String apply(char c)
{
switch (c)
{
case '0': return "٠";
case '1': return "١";
case '2': return "٢";
case '3': return "٣";
case '4': return "٤";
case '5': return "٥";
case '6': return "٦";
case '7': return "٧";
case '8': return "٨";
case '9': return "٩";
default: return null;
}
}
};

private static final Map_char map_char_numpad_bengali =
new Map_char() {
public String apply(char c)
{
switch (c)
{
case '0': return "০";
case '1': return "১";
case '2': return "২";
case '3': return "৩";
case '4': return "৪";
case '5': return "৫";
case '6': return "৬";
case '7': return "৭";
case '8': return "৮";
case '9': return "৯";
default: return null;
}
}
};

private static final Map_char map_char_numpad_devanagari =
new Map_char() {
public String apply(char c)
{
switch (c)
{
case '0': return "०";
case '1': return "१";
case '2': return "२";
case '3': return "३";
case '4': return "४";
case '5': return "५";
case '6': return "६";
case '7': return "७";
case '8': return "८";
case '9': return "९";
default: return null;
}
}
};

private static final Map_char map_char_numpad_persian =
new Map_char() {
public String apply(char c)
{
switch (c)
{
case '0': return "۰";
case '1': return "۱";
case '2': return "۲";
case '3': return "۳";
case '4': return "۴";
case '5': return "۵";
case '6': return "۶";
case '7': return "۷";
case '8': return "۸";
case '9': return "۹";
default: return null;
}
}
};
}
16 changes: 12 additions & 4 deletions srcs/juloo.keyboard2/Keyboard2.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public class Keyboard2 extends InputMethodService

private Config _config;

/** Layout currently visible. */
KeyboardData current_layout()
/** Layout currently visible before it has been modified. */
KeyboardData current_layout_unmodified()
{
if (_currentSpecialLayout != null)
return _currentSpecialLayout;
Expand All @@ -52,7 +52,13 @@ KeyboardData current_layout()
layout = _config.layouts.get(_currentTextLayout);
if (layout == null)
layout = _localeTextLayout;
return _config.modify_layout(layout);
return layout;
}

/** Layout currently visible. */
KeyboardData current_layout()
{
return _config.modify_layout(current_layout_unmodified());
}

void setTextLayout(int l)
Expand Down Expand Up @@ -84,7 +90,9 @@ KeyboardData loadLayout(int layout_id)
/** Load a layout that contains a numpad (eg. the pin entry). */
KeyboardData loadNumpad(int layout_id)
{
return _config.modify_numpad(KeyboardData.load(getResources(), layout_id));
String current_script = current_layout_unmodified().script;
return _config.modify_numpad(KeyboardData.load(getResources(), layout_id),
current_script);
}

@Override
Expand Down

0 comments on commit 687d88f

Please sign in to comment.