forked from extern/Unexpected-Keyboard
Fix localized key not in predefined position
The "loc " prefix for predefining a place for an "extra key" was broken
since 31d6a70
.
The FLAG_LOCALIZED flag cannot be used anymore, as adding it to any key
would turn it into a different key that wouldn't be recognized by parts
of the code comparing the keys (placing the extra keys).
Add an other layer in KeyboardData to store such informations.
This commit is contained in:
parent
6e5be63e25
commit
ab987c776c
@ -160,15 +160,12 @@ final class Config
|
||||
// first iteration then automatically added.
|
||||
final Set<KeyValue> extra_keys = new HashSet<KeyValue>(this.extra_keys);
|
||||
KeyboardData kw = original_kw.mapKeys(new KeyboardData.MapKeyValues() {
|
||||
public KeyValue apply(KeyValue key)
|
||||
public KeyValue apply(KeyValue key, boolean localized)
|
||||
{
|
||||
if (key == null)
|
||||
return null;
|
||||
boolean is_extra_key = extra_keys.contains(key);
|
||||
if (is_extra_key)
|
||||
extra_keys.remove(key);
|
||||
int flags = key.getFlags();
|
||||
if ((flags & KeyValue.FLAG_LOCALIZED) != 0 && !is_extra_key)
|
||||
if (localized && !is_extra_key)
|
||||
return null;
|
||||
switch (key.getKind())
|
||||
{
|
||||
@ -193,7 +190,7 @@ final class Config
|
||||
break;
|
||||
case Modifier:
|
||||
if (lockable_modifiers.contains(key.getModifier()))
|
||||
return key.withFlags(flags | KeyValue.FLAG_LOCK);
|
||||
return key.withFlags(key.getFlags() | KeyValue.FLAG_LOCK);
|
||||
break;
|
||||
}
|
||||
return key;
|
||||
|
@ -14,7 +14,8 @@ public class EmojiKeyButton extends Button
|
||||
{
|
||||
super(context, attrs);
|
||||
setOnClickListener(this);
|
||||
_key = KeyValue.getKeyByName(attrs.getAttributeValue(null, "key"));
|
||||
String key_name = attrs.getAttributeValue(null, "key");
|
||||
_key = (key_name == null) ? null : KeyValue.getKeyByName(key_name);
|
||||
setText(_key.getString());
|
||||
if (_key.hasFlags(KeyValue.FLAG_KEY_FONT))
|
||||
setTypeface(Theme.getSpecialKeyFont(context));
|
||||
|
@ -58,8 +58,6 @@ final class KeyValue
|
||||
public static final int FLAG_SMALLER_FONT = (1 << 25);
|
||||
// Used by [Pointers].
|
||||
public static final int FLAG_LOCKED = (1 << 26);
|
||||
// Language specific keys that are removed from the keyboard by default.
|
||||
public static final int FLAG_LOCALIZED = (1 << 27);
|
||||
|
||||
// Kinds
|
||||
public static final int KIND_CHAR = (0 << 29);
|
||||
@ -196,27 +194,11 @@ final class KeyValue
|
||||
_code = kind | flags | value;
|
||||
}
|
||||
|
||||
private static String stripPrefix(String s, String prefix)
|
||||
{
|
||||
if (s.startsWith(prefix))
|
||||
return s.substring(prefix.length());
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
public static KeyValue getKeyByName(String name)
|
||||
{
|
||||
if (name == null)
|
||||
return null;
|
||||
KeyValue kv = KeyValue.keys.get(name);
|
||||
KeyValue kv = keys.get(name);
|
||||
if (kv != null)
|
||||
return kv;
|
||||
String localized = stripPrefix(name, "loc ");
|
||||
if (localized != null)
|
||||
{
|
||||
kv = getKeyByName(localized);
|
||||
return kv.withFlags(kv.getFlags() | FLAG_LOCALIZED);
|
||||
}
|
||||
if (name.length() == 1)
|
||||
return new KeyValue(name, KIND_CHAR, name.charAt(0), 0);
|
||||
else
|
||||
|
@ -296,10 +296,9 @@ public class Keyboard2 extends InputMethodService
|
||||
return;
|
||||
KeyboardData layout =
|
||||
getLayout(_config.programming_layout).mapKeys(new KeyboardData.MapKeyValues() {
|
||||
public KeyValue apply(KeyValue key)
|
||||
public KeyValue apply(KeyValue key, boolean localized)
|
||||
{
|
||||
if (key != null
|
||||
&& key.getKind() == KeyValue.Kind.Event
|
||||
if (key.getKind() == KeyValue.Kind.Event
|
||||
&& key.getEvent() == KeyValue.Event.SWITCH_PROGRAMMING)
|
||||
return KeyValue.getKeyByName("switch_text");
|
||||
return key;
|
||||
|
@ -266,26 +266,30 @@ public class Keyboard2View extends View
|
||||
return defaultColor;
|
||||
}
|
||||
|
||||
private void drawLabel(Canvas canvas, KeyValue k, float x, float y, float keyH, boolean isKeyDown)
|
||||
private void drawLabel(Canvas canvas, KeyboardData.Corner k, float x, float y, float keyH, boolean isKeyDown)
|
||||
{
|
||||
k = KeyModifier.modify(k, _mods);
|
||||
if (k == null)
|
||||
return;
|
||||
float textSize = scaleTextSize(k, _config.labelTextSize, keyH);
|
||||
Paint p = _theme.labelPaint(k.hasFlags(KeyValue.FLAG_KEY_FONT));
|
||||
p.setColor(labelColor(k, isKeyDown, _theme.labelColor));
|
||||
KeyValue kv = KeyModifier.modify(k.kv, _mods);
|
||||
if (kv == null)
|
||||
return;
|
||||
float textSize = scaleTextSize(kv, _config.labelTextSize, keyH);
|
||||
Paint p = _theme.labelPaint(kv.hasFlags(KeyValue.FLAG_KEY_FONT));
|
||||
p.setColor(labelColor(kv, isKeyDown, _theme.labelColor));
|
||||
p.setTextSize(textSize);
|
||||
canvas.drawText(k.getString(), x, (keyH - p.ascent() - p.descent()) / 2f + y, p);
|
||||
canvas.drawText(kv.getString(), x, (keyH - p.ascent() - p.descent()) / 2f + y, p);
|
||||
}
|
||||
|
||||
private void drawSubLabel(Canvas canvas, KeyValue k, float x, float y, float keyW, float keyH, Paint.Align a, Vertical v, boolean isKeyDown)
|
||||
private void drawSubLabel(Canvas canvas, KeyboardData.Corner k, float x, float y, float keyW, float keyH, Paint.Align a, Vertical v, boolean isKeyDown)
|
||||
{
|
||||
k = KeyModifier.modify(k, _mods);
|
||||
if (k == null)
|
||||
return;
|
||||
float textSize = scaleTextSize(k, _config.sublabelTextSize, keyH);
|
||||
Paint p = _theme.subLabelPaint(k.hasFlags(KeyValue.FLAG_KEY_FONT), a);
|
||||
p.setColor(labelColor(k, isKeyDown, _theme.subLabelColor));
|
||||
KeyValue kv = KeyModifier.modify(k.kv, _mods);
|
||||
if (kv == null)
|
||||
return;
|
||||
float textSize = scaleTextSize(kv, _config.sublabelTextSize, keyH);
|
||||
Paint p = _theme.subLabelPaint(kv.hasFlags(KeyValue.FLAG_KEY_FONT), a);
|
||||
p.setColor(labelColor(kv, isKeyDown, _theme.subLabelColor));
|
||||
p.setTextSize(textSize);
|
||||
float subPadding = _config.keyPadding;
|
||||
if (v == Vertical.CENTER)
|
||||
@ -296,7 +300,7 @@ public class Keyboard2View extends View
|
||||
x += keyW / 2f;
|
||||
else
|
||||
x += (a == Paint.Align.LEFT) ? subPadding : keyW - subPadding;
|
||||
canvas.drawText(k.getString(), x, y, p);
|
||||
canvas.drawText(kv.getString(), x, y, p);
|
||||
}
|
||||
|
||||
private float scaleTextSize(KeyValue k, float rel_size, float keyH)
|
||||
|
@ -177,11 +177,11 @@ class KeyboardData
|
||||
** 0
|
||||
** 3 4
|
||||
*/
|
||||
public final KeyValue key0;
|
||||
public final KeyValue key1;
|
||||
public final KeyValue key2;
|
||||
public final KeyValue key3;
|
||||
public final KeyValue key4;
|
||||
public final Corner key0;
|
||||
public final Corner key1;
|
||||
public final Corner key2;
|
||||
public final Corner key3;
|
||||
public final Corner key4;
|
||||
|
||||
/** Key width in relative unit. */
|
||||
public final float width;
|
||||
@ -190,7 +190,7 @@ class KeyboardData
|
||||
/** Put keys 1 to 4 on the edges instead of the corners. */
|
||||
public final boolean edgekeys;
|
||||
|
||||
protected Key(KeyValue k0, KeyValue k1, KeyValue k2, KeyValue k3, KeyValue k4, float w, float s, boolean e)
|
||||
protected Key(Corner k0, Corner k1, Corner k2, Corner k3, Corner k4, float w, float s, boolean e)
|
||||
{
|
||||
key0 = k0;
|
||||
key1 = k1;
|
||||
@ -204,11 +204,11 @@ class KeyboardData
|
||||
|
||||
public static Key parse(XmlResourceParser parser) throws Exception
|
||||
{
|
||||
KeyValue k0 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key0"));
|
||||
KeyValue k1 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key1"));
|
||||
KeyValue k2 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key2"));
|
||||
KeyValue k3 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key3"));
|
||||
KeyValue k4 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key4"));
|
||||
Corner k0 = Corner.parse_of_attr(parser, "key0");
|
||||
Corner k1 = Corner.parse_of_attr(parser, "key1");
|
||||
Corner k2 = Corner.parse_of_attr(parser, "key2");
|
||||
Corner k3 = Corner.parse_of_attr(parser, "key3");
|
||||
Corner k4 = Corner.parse_of_attr(parser, "key4");
|
||||
float width = parser.getAttributeFloatValue(null, "width", 1f);
|
||||
float shift = parser.getAttributeFloatValue(null, "shift", 0.f);
|
||||
boolean edgekeys = parser.getAttributeBooleanValue(null, "edgekeys", false);
|
||||
@ -225,27 +225,30 @@ class KeyboardData
|
||||
|
||||
public KeyValue getKeyValue(int i)
|
||||
{
|
||||
Corner c;
|
||||
switch (i)
|
||||
{
|
||||
case 0: return key0;
|
||||
case 1: return key1;
|
||||
case 2: return key2;
|
||||
case 3: return key3;
|
||||
case 4: return key4;
|
||||
default: return null;
|
||||
case 0: c = key0; break;
|
||||
case 1: c = key1; break;
|
||||
case 2: c = key2; break;
|
||||
case 3: c = key3; break;
|
||||
case 4: c = key4; break;
|
||||
default: c = null; break;
|
||||
}
|
||||
return (c == null) ? null : c.kv;
|
||||
}
|
||||
|
||||
public Key withKeyValue(int i, KeyValue kv)
|
||||
{
|
||||
KeyValue k0 = key0, k1 = key1, k2 = key2, k3 = key3, k4 = key4;
|
||||
Corner k0 = key0, k1 = key1, k2 = key2, k3 = key3, k4 = key4;
|
||||
Corner k = Corner.of_kv(kv);
|
||||
switch (i)
|
||||
{
|
||||
case 0: k0 = kv; break;
|
||||
case 1: k1 = kv; break;
|
||||
case 2: k2 = kv; break;
|
||||
case 3: k3 = kv; break;
|
||||
case 4: k4 = kv; break;
|
||||
case 0: k0 = k; break;
|
||||
case 1: k1 = k; break;
|
||||
case 2: k2 = k; break;
|
||||
case 3: k3 = k; break;
|
||||
case 4: k4 = k; break;
|
||||
}
|
||||
return new Key(k0, k1, k2, k3, k4, width, shift, edgekeys);
|
||||
}
|
||||
@ -255,6 +258,7 @@ class KeyboardData
|
||||
*/
|
||||
public KeyValue getAtDirection(int direction)
|
||||
{
|
||||
Corner c = null;
|
||||
if (edgekeys)
|
||||
{
|
||||
// \ 1 /
|
||||
@ -264,10 +268,10 @@ class KeyboardData
|
||||
// / 4 \
|
||||
switch (direction)
|
||||
{
|
||||
case 2: case 3: return key1;
|
||||
case 4: case 5: return key2;
|
||||
case 6: case 7: return key4;
|
||||
case 8: case 1: return key3;
|
||||
case 2: case 3: c = key1; break;
|
||||
case 4: case 5: c = key2; break;
|
||||
case 6: case 7: c = key4; break;
|
||||
case 8: case 1: c = key3; break;
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -279,13 +283,56 @@ class KeyboardData
|
||||
// 3 | 4
|
||||
switch (direction)
|
||||
{
|
||||
case 1: case 2: return key1;
|
||||
case 3: case 4: return key2;
|
||||
case 5: case 6: return key4;
|
||||
case 7: case 8: return key3;
|
||||
case 1: case 2: c = key1; break;
|
||||
case 3: case 4: c = key2; break;
|
||||
case 5: case 6: c = key4; break;
|
||||
case 7: case 8: c = key3; break;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return (c == null) ? null : c.kv;
|
||||
}
|
||||
}
|
||||
|
||||
public static final class Corner
|
||||
{
|
||||
public final KeyValue kv;
|
||||
/** Whether the kv is marked with the "loc " prefix. To be removed if not
|
||||
specified in the [extra_keys]. */
|
||||
public final boolean localized;
|
||||
|
||||
protected Corner(KeyValue k, boolean l)
|
||||
{
|
||||
kv = k;
|
||||
localized = l;
|
||||
}
|
||||
|
||||
public static Corner parse_of_attr(XmlResourceParser parser, String attr) throws Exception
|
||||
{
|
||||
String name = parser.getAttributeValue(null, attr);
|
||||
boolean localized = false;
|
||||
|
||||
if (name == null)
|
||||
return null;
|
||||
String name_loc = stripPrefix(name, "loc ");
|
||||
if (name_loc != null)
|
||||
{
|
||||
localized = true;
|
||||
name = name_loc;
|
||||
}
|
||||
return new Corner(KeyValue.getKeyByName(name), localized);
|
||||
}
|
||||
|
||||
public static Corner of_kv(KeyValue kv)
|
||||
{
|
||||
return new Corner(kv, false);
|
||||
}
|
||||
|
||||
private static String stripPrefix(String s, String prefix)
|
||||
{
|
||||
if (s.startsWith(prefix))
|
||||
return s.substring(prefix.length());
|
||||
else
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -295,13 +342,23 @@ class KeyboardData
|
||||
}
|
||||
|
||||
public static abstract class MapKeyValues implements MapKey {
|
||||
abstract public KeyValue apply(KeyValue kv);
|
||||
abstract public KeyValue apply(KeyValue c, boolean localized);
|
||||
|
||||
public Key apply(Key k)
|
||||
{
|
||||
return new Key(apply(k.key0), apply(k.key1), apply(k.key2),
|
||||
apply(k.key3), apply(k.key4), k.width, k.shift, k.edgekeys);
|
||||
}
|
||||
|
||||
private Corner apply(Corner c)
|
||||
{
|
||||
if (c == null)
|
||||
return null;
|
||||
KeyValue kv = apply(c.kv, c.localized);
|
||||
if (kv == null)
|
||||
return null;
|
||||
return Corner.of_kv(kv);
|
||||
}
|
||||
}
|
||||
|
||||
/** Parsing utils */
|
||||
|
@ -140,7 +140,7 @@ public final class Pointers implements Handler.Callback
|
||||
// Don't take latched modifiers into account if an other key is pressed.
|
||||
// The other key already "own" the latched modifiers and will clear them.
|
||||
Modifiers mods = getModifiers(isOtherPointerDown());
|
||||
KeyValue value = _handler.modifyKey(key.key0, mods);
|
||||
KeyValue value = handleKV(key.key0, mods);
|
||||
Pointer ptr = new Pointer(pointerId, key, value, x, y, mods);
|
||||
_ptrs.add(ptr);
|
||||
if (value != null && !value.hasFlags(KeyValue.FLAG_SPECIAL))
|
||||
@ -159,7 +159,7 @@ public final class Pointers implements Handler.Callback
|
||||
private KeyValue getKeyAtDirection(Pointer ptr, int direction)
|
||||
{
|
||||
if (direction == 0)
|
||||
return _handler.modifyKey(ptr.key.key0, ptr.modifiers);
|
||||
return handleKV(ptr.key.key0, ptr.modifiers);
|
||||
KeyValue k;
|
||||
for (int i = 0; i > -3; i = (~i>>31) - i)
|
||||
{
|
||||
@ -173,6 +173,13 @@ public final class Pointers implements Handler.Callback
|
||||
return null;
|
||||
}
|
||||
|
||||
private KeyValue handleKV(KeyboardData.Corner c, Modifiers modifiers)
|
||||
{
|
||||
if (c == null)
|
||||
return null;
|
||||
return _handler.modifyKey(c.kv, modifiers);
|
||||
}
|
||||
|
||||
public void onTouchMove(float x, float y, int pointerId)
|
||||
{
|
||||
Pointer ptr = getPtr(pointerId);
|
||||
|
Loading…
Reference in New Issue
Block a user