prefs: Show custom layout names if provided

Show the name of custom layouts in the list if it's provided using the
`name` attribute.
This should make managing several custom layouts easier.
This commit is contained in:
Jules Aguillon 2024-01-10 23:39:19 +01:00
parent 6725d3057b
commit 148f3dfc05
3 changed files with 32 additions and 9 deletions

View File

@ -430,7 +430,7 @@ final class Config
l.add(migrate_layout(snd_layout)); l.add(migrate_layout(snd_layout));
String custom_layout = prefs.getString("custom_layout", ""); String custom_layout = prefs.getString("custom_layout", "");
if (custom_layout != null && !custom_layout.equals("")) if (custom_layout != null && !custom_layout.equals(""))
l.add(new LayoutsPreference.CustomLayout(custom_layout)); l.add(LayoutsPreference.CustomLayout.parse(custom_layout));
LayoutsPreference.save_to_preferences(e, l); LayoutsPreference.save_to_preferences(e, l);
case 1: case 1:
default: break; default: break;

View File

@ -27,6 +27,8 @@ class KeyboardData
public final String script; public final String script;
/** Might be different from [script]. Might be null. */ /** Might be different from [script]. Might be null. */
public final String numpad_script; public final String numpad_script;
/** The [name] attribute. Might be null. */
public final String name;
/** Position of every keys on the layout, see [getKeys()]. */ /** Position of every keys on the layout, see [getKeys()]. */
private Map<KeyValue, KeyPos> _key_pos = null; private Map<KeyValue, KeyPos> _key_pos = null;
@ -229,6 +231,7 @@ class KeyboardData
String numpad_script = parser.getAttributeValue(null, "numpad_script"); String numpad_script = parser.getAttributeValue(null, "numpad_script");
if (numpad_script == null) if (numpad_script == null)
numpad_script = script; numpad_script = script;
String name = parser.getAttributeValue(null, "name");
ArrayList<Row> rows = new ArrayList<Row>(); ArrayList<Row> rows = new ArrayList<Row>();
Modmap modmap = null; Modmap modmap = null;
while (next_tag(parser)) while (next_tag(parser))
@ -248,7 +251,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 (add_bottom_row) if (add_bottom_row)
rows.add(bottom_row.updateWidth(kw)); rows.add(bottom_row.updateWidth(kw));
return new KeyboardData(rows, kw, modmap, script, numpad_script); return new KeyboardData(rows, kw, modmap, script, numpad_script, name);
} }
private static float compute_max_width(List<Row> rows) private static float compute_max_width(List<Row> rows)
@ -266,7 +269,8 @@ class KeyboardData
return Row.parse(parser); return Row.parse(parser);
} }
protected KeyboardData(List<Row> rows_, float kw, Modmap mm, String sc, String npsc) protected KeyboardData(List<Row> rows_, float kw, Modmap mm, String sc,
String npsc, String name_)
{ {
float kh = 0.f; float kh = 0.f;
for (Row r : rows_) for (Row r : rows_)
@ -275,6 +279,7 @@ class KeyboardData
modmap = mm; modmap = mm;
script = sc; script = sc;
numpad_script = npsc; numpad_script = npsc;
name = name_;
keysWidth = kw; keysWidth = kw;
keysHeight = kh; keysHeight = kh;
} }
@ -282,7 +287,8 @@ class KeyboardData
/** Copies the fields of an other keyboard, with rows changed. */ /** Copies the fields of an other keyboard, with rows changed. */
protected KeyboardData(KeyboardData src, List<Row> rows) protected KeyboardData(KeyboardData src, List<Row> rows)
{ {
this(rows, compute_max_width(rows), src.modmap, src.script, src.numpad_script); this(rows, compute_max_width(rows), src.modmap, src.script,
src.numpad_script, src.name);
} }
public static class Row public static class Row

View File

@ -67,7 +67,7 @@ public class LayoutsPreference extends ListGroupPreference<LayoutsPreference.Lay
if (l instanceof NamedLayout) if (l instanceof NamedLayout)
layouts.add(layout_of_string(res, ((NamedLayout)l).name)); layouts.add(layout_of_string(res, ((NamedLayout)l).name));
else if (l instanceof CustomLayout) else if (l instanceof CustomLayout)
layouts.add(KeyboardData.load_string(((CustomLayout)l).xml)); layouts.add(((CustomLayout)l).parsed);
else // instanceof SystemLayout else // instanceof SystemLayout
layouts.add(null); layouts.add(null);
} }
@ -106,7 +106,15 @@ public class LayoutsPreference extends ListGroupPreference<LayoutsPreference.Lay
return value_i < 0 ? lname : _layout_display_names[value_i]; return value_i < 0 ? lname : _layout_display_names[value_i];
} }
else if (l instanceof CustomLayout) else if (l instanceof CustomLayout)
return getContext().getString(R.string.pref_layout_e_custom); {
// Use the layout's name if possible
CustomLayout cl = (CustomLayout)l;
if (cl.parsed != null && cl.parsed.name != null
&& !cl.parsed.name.equals(""))
return cl.parsed.name;
else
return getContext().getString(R.string.pref_layout_e_custom);
}
else // instanceof SystemLayout else // instanceof SystemLayout
return getContext().getString(R.string.pref_layout_e_system); return getContext().getString(R.string.pref_layout_e_system);
} }
@ -175,7 +183,7 @@ public class LayoutsPreference extends ListGroupPreference<LayoutsPreference.Lay
if (text == null) if (text == null)
callback.select(null); callback.select(null);
else else
callback.select(new CustomLayout(text)); callback.select(CustomLayout.parse(text));
} }
public String validate(String text) public String validate(String text)
@ -247,7 +255,16 @@ public class LayoutsPreference extends ListGroupPreference<LayoutsPreference.Lay
static final class CustomLayout implements Layout static final class CustomLayout implements Layout
{ {
public final String xml; public final String xml;
public CustomLayout(String c) { xml = c; } /** Might be null. */
public final KeyboardData parsed;
public CustomLayout(String xml_, KeyboardData k) { xml = xml_; parsed = k; }
public static CustomLayout parse(String xml)
{
KeyboardData parsed = null;
try { parsed = KeyboardData.load_string_exn(xml); }
catch (Exception e) {}
return new CustomLayout(xml, parsed);
}
} }
/** Named layouts are serialized to strings and custom layouts to JSON /** Named layouts are serialized to strings and custom layouts to JSON
@ -266,7 +283,7 @@ public class LayoutsPreference extends ListGroupPreference<LayoutsPreference.Lay
JSONObject obj_ = (JSONObject)obj; JSONObject obj_ = (JSONObject)obj;
switch (obj_.getString("kind")) switch (obj_.getString("kind"))
{ {
case "custom": return new CustomLayout(obj_.getString("xml")); case "custom": return CustomLayout.parse(obj_.getString("xml"));
case "system": default: return new SystemLayout(); case "system": default: return new SystemLayout();
} }
} }