Compass-point synonyms for edge keys in layouts (#628)

This commit is contained in:
Spike 2024-05-08 06:51:11 -04:00 committed by GitHub
parent c7d33356bc
commit 53e04d5784
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -399,24 +399,37 @@ public final class KeyboardData
indication = i; indication = i;
} }
/** Write the parsed key into [ks] at [index]. Doesn't write if the /** Read a key value attribute that have a synonym. Having both synonyms
attribute is not present. Return flags that can be aggregated into the present at the same time is an error.
value for [keysflags]. */ Returns [null] if the attributes are not present. */
static int parse_key_attr(XmlPullParser parser, String attr, KeyValue[] ks, static String get_key_attr(XmlPullParser parser, String syn1, String syn2)
throws Exception
{
String name1 = parser.getAttributeValue(null, syn1);
String name2 = parser.getAttributeValue(null, syn2);
if (name1 != null && name2 != null)
throw error(parser,
"'"+syn1+"' and '"+syn2+"' are synonyms and cannot be passed at the same time.");
return (name1 == null) ? name2 : name1;
}
/** Parse the key description [key_attr] and write into [ks] at [index].
Returns flags that can be aggregated into the value for [keysflags].
[key_attr] can be [null] for convenience. */
static int parse_key_attr(XmlPullParser parser, String key_val, KeyValue[] ks,
int index) int index)
throws Exception throws Exception
{ {
String name = parser.getAttributeValue(null, attr); if (key_val == null)
int flags = 0;
if (name == null)
return 0; return 0;
String name_loc = stripPrefix(name, "loc "); int flags = 0;
String name_loc = stripPrefix(key_val, "loc ");
if (name_loc != null) if (name_loc != null)
{ {
flags |= F_LOC; flags |= F_LOC;
name = name_loc; key_val = name_loc;
} }
ks[index] = KeyValue.getKeyByName(name); ks[index] = KeyValue.getKeyByName(key_val);
return (flags << index); return (flags << index);
} }
@ -429,15 +442,17 @@ public final class KeyboardData
{ {
KeyValue[] ks = new KeyValue[9]; KeyValue[] ks = new KeyValue[9];
int keysflags = 0; int keysflags = 0;
keysflags |= parse_key_attr(parser, "key0", ks, 0); keysflags |= parse_key_attr(parser, parser.getAttributeValue(null, "key0"), ks, 0);
keysflags |= parse_key_attr(parser, "key1", ks, 1); /* Swipe gestures (key1-key8 diagram above), with compass-point synonyms. */
keysflags |= parse_key_attr(parser, "key2", ks, 2); keysflags |= parse_key_attr(parser, get_key_attr(parser, "key1", "nw"), ks, 1);
keysflags |= parse_key_attr(parser, "key3", ks, 3); keysflags |= parse_key_attr(parser, get_key_attr(parser, "key2", "ne"), ks, 2);
keysflags |= parse_key_attr(parser, "key4", ks, 4); keysflags |= parse_key_attr(parser, get_key_attr(parser, "key3", "sw"), ks, 3);
keysflags |= parse_key_attr(parser, "key5", ks, 5); keysflags |= parse_key_attr(parser, get_key_attr(parser, "key4", "se"), ks, 4);
keysflags |= parse_key_attr(parser, "key6", ks, 6); keysflags |= parse_key_attr(parser, get_key_attr(parser, "key5", "w"), ks, 5);
keysflags |= parse_key_attr(parser, "key7", ks, 7); keysflags |= parse_key_attr(parser, get_key_attr(parser, "key6", "e"), ks, 6);
keysflags |= parse_key_attr(parser, "key8", ks, 8); keysflags |= parse_key_attr(parser, get_key_attr(parser, "key7", "n"), ks, 7);
keysflags |= parse_key_attr(parser, get_key_attr(parser, "key8", "s"), ks, 8);
/* Other key attributes */
float width = attribute_float(parser, "width", 1f); float width = attribute_float(parser, "width", 1f);
float shift = attribute_float(parser, "shift", 0.f); float shift = attribute_float(parser, "shift", 0.f);
boolean slider = attribute_bool(parser, "slider", false); boolean slider = attribute_bool(parser, "slider", false);