Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

#1298: ui.json keeps the map form older versions read; extra keys in their own field

aiongg691eb35a28e2
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 744e7ce8c..957ccd836 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -251,8 +251,9 @@ pub struct UiState {
pub rich_black: bool,
/// View › Overprint Preview.
pub overprint_preview: bool,
- /// Edit › Keyboard Shortcuts: the user's changes to the default shortcuts. Older files store a
- /// map of command id → shortcut ("" = none), which loads unchanged.
+ /// Edit › Keyboard Shortcuts: the user's changes to the default shortcuts. Saved as
+ /// `shortcuts` and `shortcutsMore` (see `prefs::saved_shortcuts`).
+ #[serde(flatten, with = "prefs::saved_shortcuts")]
pub shortcuts: craft_keymap::Overrides,
pub control_bar: bool,
pub tools_double_column: bool,
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index 190a4cdff..4e2392a7b 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -3497,8 +3497,7 @@ mod tests {
assert_eq!(shortcut_of(&app, "app.palette"), None, "a removed default stays removed");
assert_eq!(shortcut_of(&app, "file.close"), None);
assert_eq!(shortcut_of(&app, "app.save").as_deref(), Some("Cmd+S"));
- // Saved again it reads back the same, and with one key per command it keeps the map form
- // an older version reads.
+ // Saved again it reads back the same, in the map form an older version reads.
let resaved = serde_json::to_vec(&app.ui).unwrap();
assert_eq!(crate::UiState::from_preferences_json(&resaved).unwrap().shortcuts, app.ui.shortcuts);
let saved: Value = serde_json::from_slice(&resaved).unwrap();
diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rs
index cb5a0b54c..96fb1288d 100644
--- a/crates/ui-egui/src/prefs.rs
+++ b/crates/ui-egui/src/prefs.rs
@@ -20,6 +20,60 @@ impl UiState {
}
}
+/// `UiState::shortcuts` in ui.json. `shortcuts` maps each changed command to its first
+/// shortcut (`""` for none): the map of one text per command that earlier DesignCraft versions
+/// read, and fail on with anything else, which drops the whole file. A command's further
+/// shortcuts go in `shortcutsMore` (command → texts), which those versions ignore. Loading
+/// appends them to the command's entry in `shortcuts`; extra shortcuts for a command missing
+/// there are ignored, because an earlier version that saved the file left that command at its
+/// defaults. `shortcuts` also loads in the shared keymap's array form, which the first builds
+/// on it wrote.
+pub(crate) mod saved_shortcuts {
+ use std::collections::BTreeMap;
+
+ use craft_keymap::{MAX_BINDINGS, Overrides};
+ use serde::ser::SerializeMap;
+ use serde::{Deserialize, Deserializer, Serializer};
+
+ pub fn serialize<S: Serializer>(overrides: &Overrides, serializer: S) -> Result<S::Ok, S::Error> {
+ let mut first = BTreeMap::new();
+ let mut more = BTreeMap::new();
+ for (command, texts) in overrides.iter() {
+ // A blank text is no shortcut, as `""` is in the map.
+ let mut texts = texts.iter().map(String::as_str).filter(|t| !t.trim().is_empty());
+ first.insert(command, texts.next().unwrap_or_default());
+ let rest: Vec<&str> = texts.collect();
+ if !rest.is_empty() {
+ more.insert(command, rest);
+ }
+ }
+ let mut map = serializer.serialize_map(None)?;
+ map.serialize_entry("shortcuts", &first)?;
+ if !more.is_empty() {
+ map.serialize_entry("shortcutsMore", &more)?;
+ }
+ map.end()
+ }
+
+ // `Overrides` loads leniently (never fails), so neither field can make ui.json unreadable.
+ #[derive(Default, Deserialize)]
+ #[serde(default, rename_all = "camelCase")]
+ struct Saved {
+ shortcuts: Overrides,
+ shortcuts_more: Overrides,
+ }
+
+ pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Overrides, D::Error> {
+ let Saved { mut shortcuts, shortcuts_more } = Saved::deserialize(deserializer)?;
+ for (command, rest) in shortcuts_more.iter() {
+ let Some(first) = shortcuts.get(command) else { continue };
+ let texts: Vec<String> = first.iter().chain(rest).take(MAX_BINDINGS).cloned().collect();
+ shortcuts.insert(command, texts);
+ }
+ Ok(shortcuts)
+ }
+}
+
fn recover_docking(saved: &mut Value) {
let Some(fields) = saved.as_object_mut() else { return };
if fields.get("docking").is_some_and(|value| serde_json::from_value::<Option<Layout<String>>>(value.clone()).is_err()) {
diff --git a/crates/ui-egui/src/shortcut_import.rs b/crates/ui-egui/src/shortcut_import.rs
index a4c57d3c4..015d2bc69 100644
--- a/crates/ui-egui/src/shortcut_import.rs
+++ b/crates/ui-egui/src/shortcut_import.rs
@@ -379,6 +379,32 @@ mod tests {
assert_eq!(menus::shortcut_of(&app, "app.palette").as_deref(), Some("Cmd+Enter"));
}
+ /// Two keys for one command still save a ui.json that earlier versions read: they deserialize
+ /// `shortcuts` as a map of one text per command and drop the whole file otherwise.
+ #[test]
+ fn a_command_with_two_imported_shortcuts_saves_a_ui_json_older_versions_read() {
+ let mut app = app();
+ let set = "Shortcut Set : Two\r\nProduct area : File Menu\r\n\tClose --- Default: Shift+Ctrl+Num 5, Default: Ctrl+F4\r\n";
+ menus::run_ui(&mut app, "window.importShortcuts", &json!({"text": set})).unwrap().unwrap();
+ let keys = app.ui.shortcuts.get("file.close").unwrap().to_vec();
+ assert_eq!(keys.len(), 2, "{keys:?}");
+ let saved = serde_json::to_vec_pretty(&app.ui).unwrap();
+ let fields: Value = serde_json::from_slice(&saved).unwrap();
+ let old: std::collections::BTreeMap<String, String> = serde_json::from_value(fields["shortcuts"].clone()).unwrap();
+ assert_eq!(old.get("file.close"), Some(&keys[0]));
+ assert_eq!(fields["shortcutsMore"], json!({"file.close": [keys[1]]}));
+ let loaded = crate::UiState::from_preferences_json(&saved).unwrap();
+ assert_eq!(loaded.shortcuts, app.ui.shortcuts);
+ // The array form an earlier build of the shared keymap wrote.
+ let draft = json!({"shortcuts": [{"command": "file.close", "keys": keys}, {"command": "app.palette", "keys": []}]});
+ let loaded = crate::UiState::from_preferences_json(&serde_json::to_vec(&draft).unwrap()).unwrap();
+ assert_eq!(loaded.shortcuts.get("file.close"), Some(keys.as_slice()));
+ assert_eq!(loaded.shortcuts.get("app.palette"), Some(&[][..]));
+ // Extra keys for a command an earlier version reset are left out.
+ let reset = json!({"shortcuts": {}, "shortcutsMore": {"file.close": ["Cmd+F4"]}});
+ assert!(crate::UiState::from_preferences_json(&serde_json::to_vec(&reset).unwrap()).unwrap().shortcuts.is_empty());
+ }
+
/// A set's key on a chord the system takes is left out and the summary says so.
#[test]
fn a_reserved_chord_in_a_set_is_left_out() {