diff --git a/crates/ui-egui/src/original_editor.rs b/crates/ui-egui/src/original_editor.rs index f9b8f00543..da3bf5d5ed 100644 --- a/crates/ui-egui/src/original_editor.rs +++ b/crates/ui-egui/src/original_editor.rs @@ -14,7 +14,7 @@ use crate::VectorcraftApp; /// Opaque saved choice. Public so hosts can construct `UiState` with a struct update; its path /// has no public setter and is never read from the generic preference or dialog-field maps. -#[derive(Clone, Debug, Default, Serialize, Deserialize)] +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] pub struct Choice { path: Option, } @@ -45,8 +45,11 @@ pub(crate) fn open(app: &mut VectorcraftApp) { app.original_editor.choice = app.ui.original_editor.clone(); } -pub(crate) fn commit(app: &mut VectorcraftApp) { +/// Saves the dialog's choice; returns whether it differs from the saved one. +pub(crate) fn commit(app: &mut VectorcraftApp) -> bool { + let changed = app.ui.original_editor != app.original_editor.choice; app.ui.original_editor = app.original_editor.choice.clone(); + changed } pub(crate) fn reset(app: &mut VectorcraftApp) { diff --git a/crates/ui-egui/src/prefs_dialog.rs b/crates/ui-egui/src/prefs_dialog.rs index cc1a102f26..c6cbd37e50 100644 --- a/crates/ui-egui/src/prefs_dialog.rs +++ b/crates/ui-egui/src/prefs_dialog.rs @@ -1,6 +1,6 @@ //! Edit → Preferences (⌘K): the framework's Preferences window over the engine's preference table //! ([`PREF_SPECS`]). The window edits a draft ([`PrefsWindow::draft`]; one key per preference, so agents can -//! `ui.dialog.set` any of them) and OK applies it through `prefs.set`. Also: persistence of engine prefs inside +//! `ui.dialog.set` any of them) and OK applies the rows changed in it through `prefs.set`. Also: persistence of engine prefs inside //! `UiState` and the per-frame application of the UI-side preferences ([`runtime`]). // PLAN: assumed import paths of the framework items below through the `craft_ui` facade (it re-exports the dialog @@ -44,9 +44,17 @@ pub struct PrefsWindow { draft: SettingsDraft, /// The draft as it was when the window opened or last applied: Apply is enabled while the draft differs. opened: SettingsDraft, + /// Each row's value as the window last read it from the preferences, the document or the UI state. OK and Apply + /// write only the rows whose draft value differs from it ([`Self::edited`]), so a setting changed outside the + /// window while it is open keeps its new value. + source: SettingsDraft, /// The Units ▸ General value Reset Preferences put in the draft, while it is still the row's value: OK then /// leaves the open document's units alone. reset_units: Option, + /// The rows set in the window (by the user, `ui.dialog.set` or a reset) since it last read them. OK writes them + /// even when they hold the value they were read with: a reset of a row a command changed while the window is + /// open, or of Units ▸ General, which shows the document's units, is still written. + touched: std::collections::BTreeSet, /// The colour picker and the Reset question, drawn in the window's own viewport. nested: DialogHost, reset_asked: Option, @@ -70,11 +78,39 @@ impl PrefsWindow { if key == "unitsGeneral" && self.reset_units.as_ref().is_some_and(|u| value.as_str() != Some(u)) { self.reset_units = None; } - self.draft.set(key, value); + self.set_row(key, value); self.sync_strings(); Ok(()) } + /// Whether the row `key` was changed in the window since it was last read: it was set there + /// ([`Self::touched`]), or its draft value differs from [`Self::source`]. Numbers compare by value, so `4` and + /// `4.0` are the same. + fn edited(&self, key: &str) -> bool { + if self.touched.contains(key) { + return true; + } + let (now, read) = (self.draft.get::(key), self.source.get::(key)); + match (now.as_ref().and_then(Value::as_f64), read.as_ref().and_then(Value::as_f64)) { + (Some(a), Some(b)) => a != b, + _ => now != read, + } + } + + /// Writes `value` at `key` in the draft and in [`Self::source`]: the row shows a value read from outside the + /// window, and OK does not write it back. + fn read(&mut self, key: &str, value: &Value) { + self.draft.set(key, value); + self.source.set(key, value); + self.touched.remove(key); + } + + /// Sets the row `key` in the window, as the user or a reset does. + fn set_row(&mut self, key: &str, value: &Value) { + self.draft.set(key, value); + self.touched.insert(key.to_string()); + } + /// Reads the draft's text and choice values again, after any write to the draft. fn sync_strings(&mut self) { use vectorcraft_engine::cmd::prefscmds::{PREF_SPECS, PrefKind}; @@ -90,7 +126,7 @@ impl PrefsWindow { fn refresh(&mut self, prefs: &Value) { for (k, v) in prefs.as_object().into_iter().flatten() { if self.draft.get::(k).is_some() { - self.draft.set(k, v); + self.read(k, v); } } self.reset_units = self.draft.get::("unitsGeneral"); @@ -102,7 +138,7 @@ impl PrefsWindow { let defaults = Prefs::default().to_json(); for key in keys { if let Some(v) = defaults.get(key) { - self.draft.set(key, v); + self.set_row(key, v); } } if self.draft.get::("unitsGeneral").is_some() { @@ -117,7 +153,9 @@ fn is_known(key: &str) -> bool { UI_FIELDS.iter().any(|f| f.0 == key) || spec(key).is_some() || PREF_GROUPS.contains(&key) } -/// Opens the window on `category` (matched ignoring case; General when it names none). +/// Opens the window on `category` (matched ignoring case; General when it names none). OK writes only the rows +/// changed in the window: a setting changed some other way while it is open (a command, a panel menu, a script) +/// keeps its new value. pub fn open(app: &mut VectorcraftApp, category: Option<&str>) { crate::original_editor::open(app); let mut draft = SettingsDraft::default(); @@ -126,7 +164,8 @@ pub fn open(app: &mut VectorcraftApp, category: Option<&str>) { fields.retain(|k, _| is_known(k)); // A restored settings file can hold a Hold Shortcut Delay outside its range: the window shows, // and OK sets, the delay in use. - if fields.contains_key("keyHoldDelay") { + let saved_delay = fields.get("keyHoldDelay").cloned(); + if saved_delay.is_some() { fields.insert("keyHoldDelay".into(), json!(app.session.prefs.key_hold_delay())); } // Units ▸ General shows (and OK sets) the open document's units. @@ -140,11 +179,18 @@ pub fn open(app: &mut VectorcraftApp, category: Option<&str>) { draft.set("__snapToGrid", &app.ui.view.snap_to_grid); draft.set("__taskBar", &app.ui.task_bar); let selected = category.and_then(|c| PREF_CATEGORIES.iter().find(|x| x.eq_ignore_ascii_case(c))).copied().unwrap_or(PREF_CATEGORIES[0]); + // The saved delay, so OK writes the delay in use in its place. + let mut source = draft.clone(); + if let Some(saved) = &saved_delay { + source.set("keyHoldDelay", saved); + } let mut w = PrefsWindow { state: PreferencesState::default(), opened: draft.clone(), + source, draft, reset_units: None, + touched: Default::default(), nested: DialogHost::new(), reset_asked: None, strings: Default::default(), @@ -362,7 +408,7 @@ fn apply_prefs_resets_to(app: &mut VectorcraftApp, mut w: Option<&mut PrefsWindo } // The window shows the reset row, so OK keeps it. if let Some(w) = w.as_deref_mut() { - w.draft.set(key, &value); + w.read(key, &json!(value)); } } // And the reset preferences, the groups with a reset of every category; Units ▸ General then leaves the open @@ -373,7 +419,7 @@ fn apply_prefs_resets_to(app: &mut VectorcraftApp, mut w: Option<&mut PrefsWindo for (k, v) in prefs.as_object().into_iter().flatten() { let reset = spec(k).map_or(every && PREF_GROUPS.contains(&k.as_str()), |s| in_reset(s.category)); if reset && w.draft.get::(k).is_some() { - w.draft.set(k, v); + w.read(k, v); if k == "unitsGeneral" { w.reset_units = v.as_str().map(str::to_string); } diff --git a/crates/ui-egui/src/prefs_dialog/model.rs b/crates/ui-egui/src/prefs_dialog/model.rs index 0d03652289..b1e9bed4e9 100644 --- a/crates/ui-egui/src/prefs_dialog/model.rs +++ b/crates/ui-egui/src/prefs_dialog/model.rs @@ -283,7 +283,7 @@ impl Live<'_> { fn reset_ui_rows(&mut self, reset: impl Fn(&str) -> bool) { for (key, value) in super::default_ui_fields(&self.app.ui) { if UI_FIELDS.iter().any(|&(k, c, _)| k == key && reset(c)) { - self.w.draft.set(key, &value); + self.w.set_row(key, &json!(value)); } } } @@ -424,34 +424,44 @@ impl PreferencesDraft for Live<'_> { fn apply(&mut self) -> Result<(), String> { // A `prefs.reset` run since the last frame shows in the draft before OK reads it. super::apply_prefs_resets_to(self.app, Some(&mut *self.w)); - let values: Map = - self.w.draft.to::>().unwrap_or_default().into_iter().filter(|(k, _)| !k.starts_with("__")).collect(); + // Only the rows changed in the window: a setting changed outside it while it is open keeps its new value. + let edited: Map = + self.w.draft.to::>().unwrap_or_default().into_iter().filter(|(k, _)| self.w.edited(k)).collect(); + let values: Map = edited.iter().filter(|(k, _)| !k.starts_with("__")).map(|(k, v)| (k.clone(), v.clone())).collect(); let touch_ws = values.get("touchWorkspace").and_then(Value::as_bool); let document_units = self.w.reset_units.as_deref().is_none_or(|u| values.get("unitsGeneral").and_then(Value::as_str) != Some(u)); // A refused folder, typed or picked, comes back here as the engine's message. - self.app.run("prefs.set", json!({ "values": values, "documentUnits": document_units }))?; + if !values.is_empty() { + self.app.run("prefs.set", json!({ "values": values, "documentUnits": document_units }))?; + } + let row = |key: &str| edited.get(key).and_then(Value::as_bool); // When OK enters the Touch workspace, the task bar is set before the switch, so it goes to the workspace being // left; otherwise after it, so a return from Touch gives it to the workspace returned to. let before_switch = touch_ws.is_some() && crate::workspaces::enters_touch(self.app, self.app.touch_screen); - if before_switch && let Some(on) = self.w.draft.get::("__taskBar") { + if before_switch && let Some(on) = row("__taskBar") { self.app.ui.task_bar = on; } if touch_ws.is_some() { crate::workspaces::sync_touch_workspace(self.app, self.app.touch_screen); } - let flag = |key: &str, now: bool| self.w.draft.get::(key).unwrap_or(now); - let (smart_guides, snap_to_grid, task_bar) = ( - flag("__smartGuides", self.app.ui.view.smart_guides), - flag("__snapToGrid", self.app.ui.view.snap_to_grid), - if before_switch { self.app.ui.task_bar } else { flag("__taskBar", self.app.ui.task_bar) }, - ); - self.app.ui.view.smart_guides = smart_guides; - self.app.ui.view.snap_to_grid = snap_to_grid; - self.app.ui.task_bar = task_bar; - crate::original_editor::commit(self.app); - super::snapshot(self.app); - self.app.prefs_changed = true; + if let Some(on) = row("__smartGuides") { + self.app.ui.view.smart_guides = on; + } + if let Some(on) = row("__snapToGrid") { + self.app.ui.view.snap_to_grid = on; + } + if !before_switch && let Some(on) = row("__taskBar") { + self.app.ui.task_bar = on; + } + let editor = crate::original_editor::commit(self.app); + // The host saves the settings only when one changed. + if !edited.is_empty() || editor { + super::snapshot(self.app); + self.app.prefs_changed = true; + } self.w.opened = self.w.draft.clone(); + self.w.source = self.w.draft.clone(); + self.w.touched.clear(); Ok(()) } diff --git a/crates/ui-egui/src/prefs_dialog/tests.rs b/crates/ui-egui/src/prefs_dialog/tests.rs index 1b09df5cb0..bf1e2d093e 100644 --- a/crates/ui-egui/src/prefs_dialog/tests.rs +++ b/crates/ui-egui/src/prefs_dialog/tests.rs @@ -296,6 +296,18 @@ fn reset_preferences_keeps_an_edit_of_general_units() { reset_preferences(&mut b, PromptChoice::Proceed); confirm(&mut b).unwrap(); assert_eq!(b.session.prefs.units_general, Prefs::default().units_general); + + // A document in the default unit, with another unit saved: the row opens showing the default, and the reset + // still resets the saved unit. + let mut c = app(); + c.run("file.new", json!({"width": 400, "height": 400})).unwrap(); + c.run("prefs.set", json!({"values": {"unitsGeneral": "inches"}, "documentUnits": false})).unwrap(); + assert_eq!(c.session.active().unwrap().doc.units.key(), Prefs::default().units_general); + open(&mut c, Some("General")); + reset_preferences(&mut c, PromptChoice::Proceed); + confirm(&mut c).unwrap(); + assert_eq!(c.session.prefs.units_general, Prefs::default().units_general, "the saved unit is reset"); + assert_eq!(c.session.active().unwrap().doc.units.key(), Prefs::default().units_general, "the document's units stay"); } /// Smart Guides, Snap to Grid and Enable Contextual Task Bar, the UI-only rows. @@ -538,6 +550,84 @@ fn prefs_reset_outside_run_resets_the_ui_only_rows_in_the_next_frame() { assert!(!left_task_bar(&b, "Essentials Classic"), "the workspace left keeps the reset value"); } +/// OK writes only the rows changed in Preferences: a preference, a UI-only row or the document's units changed by a +/// command while the window is open keeps its new value, and a row changed in the window is still written. +#[test] +fn ok_keeps_settings_changed_by_commands_while_preferences_is_open() { + let mut a = app(); + a.run("file.new", json!({"width": 400, "height": 400})).unwrap(); + let before = ui_rows(&a); + open(&mut a, Some("General")); + a.run("prefs.set", json!({"key": "keyboardIncrement", "value": 9})).unwrap(); + a.run("document.setUnits", json!({"units": "Millimeters"})).unwrap(); + for id in ["view.smartGuides", "view.snapToGrid", "window.taskBar"] { + a.run(id, json!({})).unwrap(); + } + set_field(&mut a, "constrainAngle", json!(30.0)).unwrap(); + confirm(&mut a).unwrap(); + assert_eq!(a.session.prefs.keyboard_increment, 9.0, "the command's preference"); + assert_eq!(a.session.active().unwrap().doc.units, vectorcraft_doc::Unit::Millimeters, "the command's units"); + assert_eq!(ui_rows(&a), (!before.0, !before.1, !before.2), "the commands' UI-only rows"); + assert_eq!(a.session.prefs.constrain_angle, 30.0, "the row changed in the window"); + assert_eq!(a.ui.engine_prefs["keyboardIncrement"], json!(9.0), "saved with the UI state"); + + // A row changed both ways takes the window's value; Cancel writes nothing. + open(&mut a, Some("General")); + set_field(&mut a, "keyboardIncrement", json!(3)).unwrap(); + set_field(&mut a, "constrainAngle", json!(15.0)).unwrap(); + a.run("prefs.set", json!({"key": "keyboardIncrement", "value": 5})).unwrap(); + confirm(&mut a).unwrap(); + assert_eq!(a.session.prefs.keyboard_increment, 3.0, "the window's value"); + open(&mut a, Some("General")); + set_field(&mut a, "constrainAngle", json!(45.0)).unwrap(); + a.run("prefs.set", json!({"key": "keyboardIncrement", "value": 6})).unwrap(); + cancel(&mut a); + assert_eq!((a.session.prefs.keyboard_increment, a.session.prefs.constrain_angle), (6.0, 15.0), "Cancel writes nothing"); + + // A reset in the window is written even where the row already showed the default: a command changed the + // preference after the window opened. + let default = Prefs::default().keyboard_increment; + a.run("prefs.set", json!({"key": "keyboardIncrement", "value": default})).unwrap(); + open(&mut a, Some("General")); + a.run("prefs.set", json!({"key": "keyboardIncrement", "value": 8})).unwrap(); + reset_preferences(&mut a, PromptChoice::Proceed); + confirm(&mut a).unwrap(); + assert_eq!(a.session.prefs.keyboard_increment, default, "the reset is written"); +} + +/// OK with nothing changed in Preferences writes nothing: no preference, no undo step in the document and no save +/// of the settings. +#[test] +fn ok_with_nothing_changed_writes_nothing() { + let mut a = app(); + a.run("file.new", json!({"width": 400, "height": 400})).unwrap(); + let undo = a.session.active().unwrap().history.undo.len(); + let prefs = a.session.prefs.clone(); + a.take_prefs_changed(); + open(&mut a, Some("Units")); + confirm(&mut a).unwrap(); + assert!(!is_open(&a)); + assert_eq!(a.session.prefs, prefs); + assert_eq!(a.session.active().unwrap().history.undo.len(), undo, "no undo step"); + assert!(!a.take_prefs_changed(), "the settings are not saved again"); +} + +/// Enable Contextual Task Bar left alone in Preferences stays with each workspace: a workspace switch while the +/// window is open keeps the bar of the workspace switched to, and the workspace left keeps its own. +#[test] +fn ok_after_a_workspace_switch_keeps_each_workspaces_task_bar() { + let off = crate::workspaces::builtins().into_iter().find(|w| !w.record.task_bar).expect("a workspace saved with the bar off"); + let mut a = app(); + crate::workspaces::switch(&mut a, crate::workspaces::ESSENTIALS).unwrap(); + assert!(a.ui.task_bar, "Essentials starts with the bar on"); + open(&mut a, Some("General")); + a.run("window.workspace", json!({"name": off.name})).unwrap(); + set_field(&mut a, "keyboardIncrement", json!(3)).unwrap(); + confirm(&mut a).unwrap(); + assert!(!a.ui.task_bar, "{} keeps its bar off", off.name); + assert!(left_task_bar(&a, crate::workspaces::ESSENTIALS), "Essentials keeps its bar on"); +} + #[test] fn old_pref_files_keep_their_brightness() { let mut a = app(); diff --git a/docs/mcp.md b/docs/mcp.md index 61663febe2..558c6d648a 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -9054,7 +9054,11 @@ highlighted until another category is shown) and clears `__search`. Cmd/Ctrl+F f field with text clears it, and Escape in the empty field closes the dialog. Its Reset Preferences button opens a `resetPreferences` question (fields `message`, `detail`) over Preferences: `ui.dialog.cancel` shows Preferences again as it was, and `ui.dialog.confirm` shows it again with every preference at its default value, applied by OK in -Preferences. `prefs.reset` resets the preferences at once, without the question. +Preferences. `prefs.reset` resets the preferences at once, without the question. OK in Preferences writes only the +fields changed in the dialog since it opened or since its last Apply, so a preference, Smart Guides, Snap to Grid, +Enable Contextual Task Bar or the document's units changed some other way while it is open (a command, a panel menu, +a script) keeps its new value. A field changed both ways takes the dialog's value. OK with no field changed writes +nothing and adds no undo step. ```json {"name":"run_command","arguments":{"command":"prefs.set","params":{"key":"unitsGeneral","value":"millimeters"}}} diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 09c2f07001..48cf0aef8d 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -895,6 +895,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - **Preferences search:** a Search Preferences field above the category list has the keyboard focus when the dialog opens, and Cmd/Ctrl+F gives it back; typing lists the settings it finds, and choosing one shows its category, as the [help page](https://helpx.adobe.com/illustrator/desktop/get-started/preferences-and-settings/set-app-preferences.html) describes. VectorCraft's own rules: the field matches a setting's label, section or category, ignoring case, in the interface language or in English; the list replaces the categories while the field has text and shows every match, or "No settings found"; the chosen setting's row is highlighted and scrolled into view; Escape in the field with text clears it, and Escape in the empty field closes the dialog. The dialog's `__search` field (`ui.dialog.set`) holds the text. Tests: `crates/ui-egui/src/prefs_dialog/tests.rs`. - **Reset Preferences asks first (#6435):** the Preferences dialog's Reset Preferences button opens a question over the dialog (`resetPreferences`). Cancel or Escape goes back to Preferences unchanged, with its category, search and edits kept; OK goes back with every preference at its default value, which OK in Preferences applies and Cancel there discards. The question and its wording are VectorCraft's own. Tests: `crates/ui-egui/src/prefs_dialog/tests.rs`. - **Reset Preferences resets every setting the dialog shows:** Smart Guides and Snap to Grid also return to their defaults (on and off) when Reset Preferences is confirmed, and General's Enable Contextual Task Bar takes the value the current workspace was saved with. OK in Preferences applies them with the other defaults, and Cancel keeps the values in use. A category's own reset resets the row of that category, and File Handling's also resets the app chosen for Edit Original. `prefs.reset` (the Command Palette's Reset Preferences) resets them too, each only when it resets every category or the setting's own, and with every category or File Handling it also clears the app chosen for Edit Original. The rule is VectorCraft's own. Tests: `crates/ui-egui/src/prefs_dialog/tests.rs`, `crates/ui-egui/src/original_editor.rs`. +- **Preferences OK keeps changes made outside the dialog:** OK writes only the settings changed in the dialog, so a preference, Smart Guides, Snap to Grid, Enable Contextual Task Bar or the document's units changed by a command or a script while Preferences is open keeps its new value, and a workspace switched to while it is open keeps its own Contextual Task Bar. OK with nothing changed writes nothing. The rule is VectorCraft's own. Tests: `crates/ui-egui/src/prefs_dialog/tests.rs`. - **Preferences › General describes its settings:** the line under the Preferences fields shows a sentence for each of the 32 General settings, Enable Contextual Task Bar included, when the pointer is over the setting's row or its control has the keyboard focus, and a screen reader gets the same sentence. The sentences are VectorCraft's own and state what the app does with each setting. Sentences: `crates/ui-egui/src/prefs_dialog/descriptions.rs`. Tests: `crates/ui-egui/src/prefs_dialog/tests.rs`. - **Preferences › Selection & Anchor Display describes its settings:** the line under the Preferences fields shows a sentence for each of the 14 Selection & Anchor Display settings, from Tolerance to the Curvature Tool rubber band, on hover and on keyboard focus, and a screen reader gets the same sentence. The Snap to Point sentence states that the Smart Guides Snapping Tolerance applies while Smart Guides is on. Sentences: `crates/ui-egui/src/prefs_dialog/descriptions.rs`. Tests: `crates/ui-egui/src/prefs_dialog/tests.rs`. - **Preferences › Guides & Grid describes its settings:** the line under the Preferences fields shows a sentence for each of the 12 Guides & Grid settings, from Snap to Grid to Pattern Editing's Tile Edge Color, on hover and on keyboard focus, and a screen reader gets the same sentence. The Show Pixel Grid sentence states that the pixel grid shows from 600% zoom. Sentences: `crates/ui-egui/src/prefs_dialog/descriptions.rs`. Tests: `crates/ui-egui/src/prefs_dialog/tests.rs`.