Scale the bottom row depending on the host layout

1ff8526 added a bug for layouts that weren't 10 units wide.
This commit is contained in:
Jules Aguillon 2022-02-13 13:20:22 +01:00
parent adb77466f5
commit 94fed03a67

View File

@ -20,7 +20,7 @@ class KeyboardData
ArrayList<Row> rows_ = new ArrayList<Row>(); ArrayList<Row> rows_ = new ArrayList<Row>();
for (Row r : rows) for (Row r : rows)
rows_.add(r.replaceKeys(f)); rows_.add(r.replaceKeys(f));
return new KeyboardData(rows_); return new KeyboardData(rows_, keysWidth);
} }
private static Row _bottomRow = null; private static Row _bottomRow = null;
@ -54,9 +54,18 @@ class KeyboardData
ArrayList<Row> rows = new ArrayList<Row>(); ArrayList<Row> rows = new ArrayList<Row>();
while (expect_tag(parser, "row")) while (expect_tag(parser, "row"))
rows.add(Row.parse(parser)); rows.add(Row.parse(parser));
float kw = compute_max_width(rows);
if (bottom_row) if (bottom_row)
rows.add(_bottomRow); rows.add(_bottomRow.updateWidth(kw));
return new KeyboardData(rows); return new KeyboardData(rows, kw);
}
private static float compute_max_width(List<Row> rows)
{
float w = 0.f;
for (Row r : rows)
w = Math.max(w, r.keysWidth);
return w;
} }
private static Row parse_bottom_row(XmlResourceParser parser) throws Exception private static Row parse_bottom_row(XmlResourceParser parser) throws Exception
@ -66,15 +75,11 @@ class KeyboardData
return Row.parse(parser); return Row.parse(parser);
} }
protected KeyboardData(List<Row> rows_) protected KeyboardData(List<Row> rows_, float kw)
{ {
float kw = 0.f;
float kh = 0.f; float kh = 0.f;
for (Row r : rows_) for (Row r : rows_)
{
kw = Math.max(kw, r.keysWidth);
kh += r.height + r.shift; kh += r.height + r.shift;
}
rows = rows_; rows = rows_;
keysWidth = kw; keysWidth = kw;
keysHeight = kh; keysHeight = kh;
@ -83,11 +88,11 @@ class KeyboardData
public static class Row public static class Row
{ {
public final List<Key> keys; public final List<Key> keys;
/* Height of the row. Unit is abstract. */ /* Height of the row, without 'shift'. Unit is abstract. */
public final float height; public final float height;
/* Extra empty space on the top. */ /* Extra empty space on the top. */
public final float shift; public final float shift;
/* Total width of very keys. Unit is abstract. */ /* Total width of the row. Unit is abstract. */
private final float keysWidth; private final float keysWidth;
protected Row(List<Key> keys_, float h, float s) protected Row(List<Key> keys_, float h, float s)
@ -118,6 +123,16 @@ class KeyboardData
keys_.add(k.replaceKeys(f)); keys_.add(k.replaceKeys(f));
return new Row(keys_, height, shift); return new Row(keys_, height, shift);
} }
/** Change the width of every keys so that the row is 's' units wide. */
public Row updateWidth(float newWidth)
{
float s = newWidth / keysWidth;
ArrayList<Key> keys_ = new ArrayList<Key>();
for (Key k : keys)
keys_.add(k.scaleWidth(s));
return new Row(keys_, height, shift);
}
} }
public static class Key public static class Key
@ -171,6 +186,12 @@ class KeyboardData
{ {
return new Key(f.map(key0), f.map(key1), f.map(key2), f.map(key3), f.map(key4), width, shift, edgekeys); return new Key(f.map(key0), f.map(key1), f.map(key2), f.map(key3), f.map(key4), width, shift, edgekeys);
} }
/** New key with the width multiplied by 's'. */
public Key scaleWidth(float s)
{
return new Key(key0, key1, key2, key3, key4, width * s, shift, edgekeys);
}
} }
public static abstract interface MapKeys public static abstract interface MapKeys