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));
String custom_layout = prefs.getString("custom_layout", "");
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);
case 1:
default: break;

View File

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

View File

@ -67,7 +67,7 @@ public class LayoutsPreference extends ListGroupPreference<LayoutsPreference.Lay
if (l instanceof NamedLayout)
layouts.add(layout_of_string(res, ((NamedLayout)l).name));
else if (l instanceof CustomLayout)
layouts.add(KeyboardData.load_string(((CustomLayout)l).xml));
layouts.add(((CustomLayout)l).parsed);
else // instanceof SystemLayout
layouts.add(null);
}
@ -106,7 +106,15 @@ public class LayoutsPreference extends ListGroupPreference<LayoutsPreference.Lay
return value_i < 0 ? lname : _layout_display_names[value_i];
}
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
return getContext().getString(R.string.pref_layout_e_system);
}
@ -175,7 +183,7 @@ public class LayoutsPreference extends ListGroupPreference<LayoutsPreference.Lay
if (text == null)
callback.select(null);
else
callback.select(new CustomLayout(text));
callback.select(CustomLayout.parse(text));
}
public String validate(String text)
@ -247,7 +255,16 @@ public class LayoutsPreference extends ListGroupPreference<LayoutsPreference.Lay
static final class CustomLayout implements Layout
{
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
@ -266,7 +283,7 @@ public class LayoutsPreference extends ListGroupPreference<LayoutsPreference.Lay
JSONObject obj_ = (JSONObject)obj;
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();
}
}