Apply modify_layout to text layout only

Refactor. Allows to remove the 'extra_keys' and 'num_pad' flags and to
implement more complicated transformations to the layouts.
This commit is contained in:
Jules Aguillon 2023-01-30 22:33:01 +01:00
parent 2539feadcd
commit 90b7944129
7 changed files with 17 additions and 24 deletions

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<keyboard bottom_row="false" extra_keys="false"> <keyboard bottom_row="false">
<row> <row>
<key key0="θ" key2="^"/> <key key0="θ" key2="^"/>
<key key0="δ" key1="⌊" key2="" key3="↔" key4="⊂"/> <key key0="δ" key1="⌊" key2="" key3="↔" key4="⊂"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<keyboard bottom_row="false" extra_keys="false" num_pad="false"> <keyboard bottom_row="false">
<row> <row>
<key width="0.75" key0="esc" key2="~" key4="!"/> <key width="0.75" key0="esc" key2="~" key4="!"/>
<key width="0.75" key0="(" key2="[" key4="{"/> <key width="0.75" key0="(" key2="[" key4="{"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<keyboard width="6.0" bottom_row="false" extra_keys="false" num_pad="false"> <keyboard width="6.0" bottom_row="false">
<row> <row>
<key shift="1.0" key0="1"/> <key shift="1.0" key0="1"/>
<key key0="2" indication="ABC"/> <key key0="2" indication="ABC"/>

View File

@ -170,7 +170,7 @@ final class Config
if (extra_keys_subtype != null) if (extra_keys_subtype != null)
extra_keys.addAll(extra_keys_subtype); extra_keys.addAll(extra_keys_subtype);
extra_keys.addAll(extra_keys_param); extra_keys.addAll(extra_keys_param);
if (kw.num_pad && show_numpad) if (show_numpad)
kw = kw.addNumPad(); kw = kw.addNumPad();
kw = kw.mapKeys(new KeyboardData.MapKeyValues() { kw = kw.mapKeys(new KeyboardData.MapKeyValues() {
/** Apply to the center value only. Partial match, fallback to [apply]. */ /** Apply to the center value only. Partial match, fallback to [apply]. */

View File

@ -47,9 +47,14 @@ public class Keyboard2 extends InputMethodService
{ {
if (_currentSpecialLayout != null) if (_currentSpecialLayout != null)
return _currentSpecialLayout; return _currentSpecialLayout;
KeyboardData layout;
if (_currentTextLayout == Current_text_layout.SECONDARY) if (_currentTextLayout == Current_text_layout.SECONDARY)
return _config.second_layout; layout = _config.second_layout;
return (_config.layout == null) ? _localeTextLayout : _config.layout; else if (_config.layout == null)
layout = _localeTextLayout;
else
layout = _config.layout;
return _config.modify_layout(layout);
} }
void setTextLayout(Current_text_layout layout) void setTextLayout(Current_text_layout layout)

View File

@ -81,7 +81,7 @@ public class Keyboard2View extends View
public void setKeyboard(KeyboardData kw) public void setKeyboard(KeyboardData kw)
{ {
_keyboard = _config.modify_layout(kw); _keyboard = kw;
_shift_kv = KeyValue.getKeyByName("shift"); _shift_kv = KeyValue.getKeyByName("shift");
_shift_key = _keyboard.findKeyWithValue(_shift_kv); _shift_key = _keyboard.findKeyWithValue(_shift_kv);
if (_shift_key == null) if (_shift_key == null)

View File

@ -19,17 +19,13 @@ class KeyboardData
public final float keysWidth; public final float keysWidth;
/** Total height of the keyboard. */ /** Total height of the keyboard. */
public final float keysHeight; public final float keysHeight;
/** Whether to add extra keys. */
public final boolean extra_keys;
/** Whether to possibly add NumPad. */
public final boolean num_pad;
public KeyboardData mapKeys(MapKey f) public KeyboardData mapKeys(MapKey f)
{ {
ArrayList<Row> rows_ = new ArrayList<Row>(); ArrayList<Row> rows_ = new ArrayList<Row>();
for (Row r : rows) for (Row r : rows)
rows_.add(r.mapKeys(f)); rows_.add(r.mapKeys(f));
return new KeyboardData(rows_, keysWidth, extra_keys, num_pad); return new KeyboardData(rows_, keysWidth);
} }
/** Add keys from the given iterator into the keyboard. Extra keys are added /** Add keys from the given iterator into the keyboard. Extra keys are added
@ -38,8 +34,6 @@ class KeyboardData
* third row. */ * third row. */
public KeyboardData addExtraKeys(Iterator<KeyValue> k) public KeyboardData addExtraKeys(Iterator<KeyValue> k)
{ {
if (!extra_keys)
return this;
ArrayList<Row> rows = new ArrayList<Row>(this.rows); ArrayList<Row> rows = new ArrayList<Row>(this.rows);
addExtraKeys_to_row(rows, k, 1, 4); addExtraKeys_to_row(rows, k, 1, 4);
addExtraKeys_to_row(rows, k, 1, 3); addExtraKeys_to_row(rows, k, 1, 3);
@ -51,13 +45,11 @@ class KeyboardData
for (int c = 1; c <= 4; c++) for (int c = 1; c <= 4; c++)
addExtraKeys_to_row(rows, k, r, c); addExtraKeys_to_row(rows, k, r, c);
} }
return new KeyboardData(rows, keysWidth, extra_keys, num_pad); return new KeyboardData(rows, keysWidth);
} }
public KeyboardData addNumPad() public KeyboardData addNumPad()
{ {
if (!num_pad)
return this;
ArrayList<Row> extendedRows = new ArrayList<Row>(); ArrayList<Row> extendedRows = new ArrayList<Row>();
Iterator<Row> iterNumPadRows = _num_pad.rows.iterator(); Iterator<Row> iterNumPadRows = _num_pad.rows.iterator();
for (Row row : rows) for (Row row : rows)
@ -76,7 +68,7 @@ class KeyboardData
} }
extendedRows.add(new Row(keys, row.height, row.shift)); extendedRows.add(new Row(keys, row.height, row.shift));
} }
return new KeyboardData(extendedRows, compute_max_width(extendedRows), extra_keys, num_pad); return new KeyboardData(extendedRows, compute_max_width(extendedRows));
} }
public Key findKeyWithValue(KeyValue kv) public Key findKeyWithValue(KeyValue kv)
@ -170,8 +162,6 @@ class KeyboardData
if (!expect_tag(parser, "keyboard")) if (!expect_tag(parser, "keyboard"))
throw new Exception("Empty layout file"); throw new Exception("Empty layout file");
boolean bottom_row = attribute_bool(parser, "bottom_row", true); boolean bottom_row = attribute_bool(parser, "bottom_row", true);
boolean extra_keys = attribute_bool(parser, "extra_keys", true);
boolean num_pad = attribute_bool(parser, "num_pad", true);
float specified_kw = attribute_float(parser, "width", 0f); float specified_kw = attribute_float(parser, "width", 0f);
ArrayList<Row> rows = new ArrayList<Row>(); ArrayList<Row> rows = new ArrayList<Row>();
while (expect_tag(parser, "row")) while (expect_tag(parser, "row"))
@ -179,7 +169,7 @@ class KeyboardData
float kw = (specified_kw != 0f) ? specified_kw : compute_max_width(rows); float kw = (specified_kw != 0f) ? specified_kw : compute_max_width(rows);
if (bottom_row) if (bottom_row)
rows.add(_bottom_row.updateWidth(kw)); rows.add(_bottom_row.updateWidth(kw));
return new KeyboardData(rows, kw, extra_keys, num_pad); return new KeyboardData(rows, kw);
} }
private static float compute_max_width(List<Row> rows) private static float compute_max_width(List<Row> rows)
@ -197,7 +187,7 @@ class KeyboardData
return Row.parse(parser); return Row.parse(parser);
} }
protected KeyboardData(List<Row> rows_, float kw, boolean xk, boolean np) protected KeyboardData(List<Row> rows_, float kw)
{ {
float kh = 0.f; float kh = 0.f;
for (Row r : rows_) for (Row r : rows_)
@ -205,8 +195,6 @@ class KeyboardData
rows = rows_; rows = rows_;
keysWidth = kw; keysWidth = kw;
keysHeight = kh; keysHeight = kh;
extra_keys = xk;
num_pad = np;
} }
public static class Row public static class Row