diff --git a/crates/ui-egui/src/chrome/tests.rs b/crates/ui-egui/src/chrome/tests.rs index fb3032d235..aeafbc9936 100644 --- a/crates/ui-egui/src/chrome/tests.rs +++ b/crates/ui-egui/src/chrome/tests.rs @@ -3194,6 +3194,36 @@ fn the_control_bar_opacity_drops_an_unfinished_value_when_the_selection_changes( assert_eq!((opacity(&bar, &a), opacity(&bar, &b)), (0.5, 0.01), "the typing gives the other object 1%, not 20+1"); } +/// File › Revert drops an unfinished Opacity expression in the Control bar, though the reverted document has the same +/// object, with the same opacity, selected again before the next frame. +#[test] +fn the_control_bar_opacity_drops_an_unfinished_value_on_file_revert() { + use egui::{Key, Modifiers}; + crate::i18n::set_current(crate::i18n::Lang::EN); + let mut app = crate::VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + let a = app.run("shape.rectangle", json!({"x": 20, "y": 50, "width": 40, "height": 40})).unwrap()["id"].clone(); + app.run("transparency.set", json!({"ids": [a], "opacity": 50})).unwrap(); + let dir = vectorcraft_testkit::TempDir::new("control-bar-opacity-revert"); + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string()))); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))); + app.run("file.saveAs", json!({"path": dir.join("Opacity.vectorcraft")})).unwrap(); + // An unsaved change for Revert to undo, leaving the rectangle as saved. + app.run("shape.rectangle", json!({"x": 200, "y": 50, "width": 40, "height": 40})).unwrap(); + app.run("select.set", json!({"ids": [a]})).unwrap(); + let mut bar = crate::tests::panel_harness::Panel::new(app, crate::chrome::control_bar); + bar.room(egui::vec2(2400.0, 300.0)).frame(); + bar.click_role("Opacity:", Role::SPIN_BUTTON); + bar.key(Key::A, Modifiers::COMMAND); + bar.type_text("20+"); + bar.app().run("file.revert", json!({"confirmed": true})).unwrap(); + bar.app().run("select.set", json!({"ids": [a]})).unwrap(); + bar.type_text("1"); + bar.key(Key::Enter, Modifiers::NONE); + let opacity = bar.app().session.active().unwrap().doc.node(vectorcraft_doc::NodeId(a.as_u64().unwrap())).unwrap().opacity; + assert_eq!(opacity, 0.01, "Revert drops the unfinished value: 1%, not 20+1"); +} + /// With the Artboard tool in use, the Control bar shows the Align buttons after the artboard /// controls while it has room, and the Distribute buttons for two or more selected artboards with /// Align to Artboard and three or more with Align to Selection. A diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs index dcdae93c30..ca610d9067 100644 --- a/crates/ui-egui/src/panels.rs +++ b/crates/ui-egui/src/panels.rs @@ -767,27 +767,33 @@ pub(crate) fn current_transparency(app: &VectorcraftApp) -> Option<(f32, BlendMo Some(picked.unwrap_or((n.opacity, n.blend))) } -/// The objects a selection field edits, as a field's edit target: the document, the selected objects (or the -/// object targeted in the Layers panel) and the selected symbol part. A field with an unfinished value drops it when -/// these change. -pub(crate) type SelectionTarget = (Option, Vec, Option<(NodeId, Vec)>); +/// The objects a selection field edits, as a field's edit target: the document with how many times File › Revert and +/// Undo or Redo replaced its content (after which a node id can name another object), the selected objects (or the +/// object targeted in the Layers panel) in id order, and the selected symbol part. A field with an unfinished value +/// drops it when these change, and keeps it when the same objects are selected in another order. +pub(crate) type SelectionTarget = (Option<(u64, u64, u64)>, Vec, Option<(NodeId, Vec)>); /// The [`SelectionTarget`] of the active document. pub(crate) fn selection_target(app: &VectorcraftApp) -> SelectionTarget { let Some(st) = app.session.active() else { return (None, vec![], None) }; let part = st.selection.symbol_part.as_ref().map(|p| (p.instance, p.path.clone())); - (Some(st.uid), st.selection.subjects().to_vec(), part) + let mut subjects = st.selection.subjects().to_vec(); + subjects.sort_unstable(); + (Some((st.uid, st.reverts, st.history_restores)), subjects, part) } /// What the Transparency panel's and the Control bar's Opacity fields edit, as their edit target: the -/// [`SelectionTarget`], the masked object while its mask is edited, the Appearance panel's active item and the picked -/// gradient mesh points (see [`current_transparency`]). -pub(crate) type TransparencyTarget = (SelectionTarget, Option, Option, Option)>>); +/// [`SelectionTarget`], the masked object while its mask is edited, the Appearance panel's active item with the first +/// selected object, whose stack holds it, and the picked gradient mesh points (see [`current_transparency`]). +pub(crate) type TransparencyTarget = (SelectionTarget, Option, Option<(NodeId, usize)>, Option)>>); /// The [`TransparencyTarget`] of the active document. pub(crate) fn transparency_target(app: &VectorcraftApp) -> TransparencyTarget { - let masked = app.session.active().and_then(|st| st.doc.mask_edit.as_ref()).map(|m| m.object); - (selection_target(app), masked, app.session.appearance_item(), app.session.mesh_points_picked()) + let st = app.session.active(); + let masked = st.and_then(|st| st.doc.mask_edit.as_ref()).map(|m| m.object); + let first = st.and_then(|st| st.selection.subjects().first().copied()); + let item = app.session.appearance_item().zip(first).map(|(index, object)| (object, index)); + (selection_target(app), masked, item, app.session.mesh_points_picked()) } /// Is the active proxy "?" (the selected objects' paints differ)? diff --git a/crates/ui-egui/src/panels/image_trace/tests.rs b/crates/ui-egui/src/panels/image_trace/tests.rs index c97e3e1c27..c01b51b2e4 100644 --- a/crates/ui-egui/src/panels/image_trace/tests.rs +++ b/crates/ui-egui/src/panels/image_trace/tests.rs @@ -657,6 +657,60 @@ fn an_unfinished_divider_value_is_dropped_when_another_object_is_selected() { assert_eq!(first_selected(&panel.app()).unwrap().trace_split(), 0.4, "A keeps its divider"); } +/// Saves the traced object of `app` to a file in `dir`, makes an unsaved change for File › Revert to undo, and selects +/// the traced object again. +fn saved_trace(app: &mut VectorcraftApp, dir: &vectorcraft_testkit::TempDir) -> NodeId { + let id = first_selected(app).unwrap().id; + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string()))); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))); + app.run("file.saveAs", json!({"path": dir.join("Trace.vectorcraft")})).unwrap(); + app.run("shape.rectangle", json!({"x": 60, "y": 60, "width": 10, "height": 10})).unwrap(); + app.run("select.set", json!({"ids": [id.0]})).unwrap(); + id +} + +/// File › Revert drops a value typed into the Divider and not finished, though the reverted document has the same +/// Image Trace object, with the same divider, selected again before the next frame: Enter afterwards leaves the +/// divider as saved. +#[test] +fn an_unfinished_divider_value_is_dropped_by_file_revert() { + crate::i18n::set_current(crate::i18n::Lang::EN); + let mut app = traced_app(); + app.run("imageTrace.setView", json!({"view": "split", "split": 0.4})).unwrap(); + let dir = vectorcraft_testkit::TempDir::new("trace-divider-revert"); + let id = saved_trace(&mut app, &dir); + let mut panel = panel_over(app); + panel.frame(); + type_unfinished(&mut panel, "Divider", "8"); + panel.app().run("file.revert", json!({"confirmed": true})).unwrap(); + panel.app().run("select.set", json!({"ids": [id.0]})).unwrap(); + let steps = undo(&panel.app()); + panel.key(egui::Key::Enter, egui::Modifiers::NONE); + assert_eq!(first_selected(&panel.app()).unwrap().trace_split(), 0.4, "the divider stays as saved"); + assert_eq!(undo(&panel.app()), steps, "no undo step"); +} + +/// File › Revert drops a value typed into a settings slider and not finished, though the reverted document has the +/// same Image Trace object, with the same settings, selected again before the next frame: Enter afterwards leaves +/// the setting as saved. +#[test] +fn an_unfinished_slider_value_is_dropped_by_file_revert() { + crate::i18n::set_current(crate::i18n::Lang::EN); + let mut app = traced_app(); + let dir = vectorcraft_testkit::TempDir::new("trace-slider-revert"); + let id = saved_trace(&mut app, &dir); + let mut panel = panel_over(app); + panel.frame(); + let threshold = param(&panel.app(), "threshold"); + type_unfinished(&mut panel, "Threshold", "3"); + panel.app().run("file.revert", json!({"confirmed": true})).unwrap(); + panel.app().run("select.set", json!({"ids": [id.0]})).unwrap(); + let steps = undo(&panel.app()); + panel.key(egui::Key::Enter, egui::Modifiers::NONE); + assert_eq!(param(&panel.app(), "threshold"), threshold, "the threshold stays as saved"); + assert_eq!(undo(&panel.app()), steps, "nothing was traced"); +} + /// Properties shows the Divider slider of the selected Image Trace object in the Split view only, /// with its name and value for screen readers. #[test] diff --git a/crates/ui-egui/src/panels/properties/tests.rs b/crates/ui-egui/src/panels/properties/tests.rs index cf45300b76..090cac0b4d 100644 --- a/crates/ui-egui/src/panels/properties/tests.rs +++ b/crates/ui-egui/src/panels/properties/tests.rs @@ -1027,6 +1027,34 @@ fn unfinished_opacity_does_not_reach_the_next_selected_object() { assert_eq!((opacity(&panel, &a), opacity(&panel, &b)), (0.5, 0.01), "the typing gives the new object 1%, not 20+1"); } +/// File › Revert drops an unfinished Opacity expression, though the reverted document has the same object, with the +/// same opacity, selected again before the next frame. +#[test] +fn unfinished_opacity_is_dropped_by_file_revert() { + use craft::ui::contract::Role; + let mut app = new_app(); + let a = rect(&mut app, 20.0, 20.0, 40.0, 40.0); + app.run("transparency.set", json!({"ids": [a], "opacity": 50})).unwrap(); + let dir = vectorcraft_testkit::TempDir::new("properties-opacity-revert"); + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string()))); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))); + app.run("file.saveAs", json!({"path": dir.join("Opacity.vectorcraft")})).unwrap(); + // An unsaved change for Revert to undo, leaving the rectangle as saved. + rect(&mut app, 200.0, 20.0, 40.0, 40.0); + app.run("select.set", json!({"ids": [a]})).unwrap(); + let mut panel = Panel::new(app, show); + panel.room(egui::vec2(400.0, 2400.0)).frame(); + panel.click_role("Opacity", Role::SPIN_BUTTON); + panel.key(Key::A, Modifiers::COMMAND); + panel.type_text("20+"); + panel.app().run("file.revert", json!({"confirmed": true})).unwrap(); + panel.app().run("select.set", json!({"ids": [a]})).unwrap(); + panel.type_text("1"); + panel.key(Key::Enter, Modifiers::NONE); + let opacity = panel.app().session.active().unwrap().doc.node(vectorcraft_doc::NodeId(a.as_u64().unwrap())).unwrap().opacity; + assert_eq!(opacity, 0.01, "Revert drops the unfinished value: 1%, not 20+1"); +} + fn undo_depth(app: &VectorcraftApp) -> usize { app.session.doc().unwrap().history.undo.len() } diff --git a/crates/ui-egui/src/panels/transparency.rs b/crates/ui-egui/src/panels/transparency.rs index 565f56da75..05f1c4b940 100644 --- a/crates/ui-egui/src/panels/transparency.rs +++ b/crates/ui-egui/src/panels/transparency.rs @@ -686,6 +686,99 @@ mod tests { assert_eq!(shown(&panel).as_deref(), Some("100%"), "the field shows the other part's value"); } + /// Types `20+` into the panel's Opacity field without committing it. + fn unfinished_opacity(panel: &mut Panel) { + use egui::{Key, Modifiers}; + panel.click_role("Opacity", Role::SPIN_BUTTON); + panel.key(Key::A, Modifiers::COMMAND); + panel.type_text("20+"); + } + + /// Types `1` and Enter into the panel's focused Opacity field. + fn finish_opacity(panel: &mut Panel) { + panel.type_text("1"); + panel.key(egui::Key::Enter, egui::Modifiers::NONE); + } + + /// The opacity of object `id` in the active document. + fn opacity_of(panel: &Panel, id: &Value) -> f32 { + panel.app().session.active().unwrap().doc.node(NodeId(id.as_u64().unwrap())).unwrap().opacity + } + + /// File › Revert drops an unfinished Opacity expression, though the reverted document has the same object, + /// with the same opacity, selected again before the next frame. + #[test] + fn unfinished_opacity_is_dropped_by_file_revert() { + let mut app = new_app(); + let a = run_cmd(&mut app, "shape.rectangle", json!({"x": 0, "y": 0, "width": 30, "height": 30}))["id"].clone(); + run_cmd(&mut app, "transparency.set", json!({"ids": [a], "opacity": 50})); + let dir = vectorcraft_testkit::TempDir::new("opacity-revert"); + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string()))); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))); + app.run("file.saveAs", json!({"path": dir.join("Opacity.vectorcraft")})).unwrap(); + // An unsaved change for Revert to undo, leaving the rectangle as saved. + run_cmd(&mut app, "shape.rectangle", json!({"x": 50, "y": 0, "width": 30, "height": 30})); + run_cmd(&mut app, "select.set", json!({"ids": [a]})); + let mut panel = Panel::new(app, show); + unfinished_opacity(&mut panel); + panel.app().run("file.revert", json!({"confirmed": true})).unwrap(); + run_cmd(&mut panel.app(), "select.set", json!({"ids": [a]})); + finish_opacity(&mut panel); + assert_eq!(opacity_of(&panel, &a), 0.01, "Revert drops the unfinished value: 1%, not 20+1"); + } + + /// Undo drops an unfinished Opacity expression, though the object it takes away and the object made and selected + /// after it have the same id and opacity. + #[test] + fn unfinished_opacity_is_dropped_by_undo() { + let mut app = new_app(); + let a = run_cmd(&mut app, "shape.rectangle", json!({"x": 0, "y": 0, "width": 30, "height": 30}))["id"].clone(); + run_cmd(&mut app, "select.set", json!({"ids": [a]})); + let mut panel = Panel::new(app, show); + unfinished_opacity(&mut panel); + run_cmd(&mut panel.app(), "edit.undo", json!({})); + let b = run_cmd(&mut panel.app(), "shape.rectangle", json!({"x": 50, "y": 0, "width": 30, "height": 30}))["id"].clone(); + assert_eq!(b, a, "the new rectangle has the undone rectangle's id"); + run_cmd(&mut panel.app(), "select.set", json!({"ids": [b]})); + finish_opacity(&mut panel); + assert_eq!(opacity_of(&panel, &b), 0.01, "Undo drops the unfinished value: 1%, not 20+1"); + } + + /// The same objects selected in another order keep an unfinished Opacity expression. + #[test] + fn unfinished_opacity_stays_when_the_same_objects_are_selected_in_another_order() { + let mut app = new_app(); + let a = run_cmd(&mut app, "shape.rectangle", json!({"x": 0, "y": 0, "width": 30, "height": 30}))["id"].clone(); + let b = run_cmd(&mut app, "shape.rectangle", json!({"x": 50, "y": 0, "width": 30, "height": 30}))["id"].clone(); + run_cmd(&mut app, "select.set", json!({"ids": [a, b]})); + let mut panel = Panel::new(app, show); + unfinished_opacity(&mut panel); + run_cmd(&mut panel.app(), "select.set", json!({"ids": [b, a]})); + panel.frame(); + finish_opacity(&mut panel); + assert_eq!((opacity_of(&panel, &a), opacity_of(&panel, &b)), (0.21, 0.21), "both objects take 20+1"); + } + + /// An active Appearance row is a row of the first selected object's stack: the same objects selected in another + /// order, with the same row index active again, drop an unfinished Opacity expression typed for the first one's row. + #[test] + fn unfinished_row_opacity_is_dropped_when_another_object_comes_first() { + let mut app = new_app(); + let a = run_cmd(&mut app, "shape.rectangle", json!({"x": 0, "y": 0, "width": 30, "height": 30}))["id"].clone(); + let b = run_cmd(&mut app, "shape.rectangle", json!({"x": 50, "y": 0, "width": 30, "height": 30}))["id"].clone(); + run_cmd(&mut app, "select.set", json!({"ids": [a, b]})); + run_cmd(&mut app, "appearance.setActiveItem", json!({"index": 0})); + let mut panel = Panel::new(app, show); + unfinished_opacity(&mut panel); + run_cmd(&mut panel.app(), "select.set", json!({"ids": [b, a]})); + run_cmd(&mut panel.app(), "appearance.setActiveItem", json!({"index": 0})); + finish_opacity(&mut panel); + let row = |panel: &Panel, id: &Value| { + panel.app().session.active().unwrap().doc.node(NodeId(id.as_u64().unwrap())).unwrap().appearance.items[0].opacity() + }; + assert_eq!(row(&panel, &b), 0.01, "the first object's row takes 1%, not 20+1"); + } + /// Each knockout state and the state a click on the box moves it to. #[test] fn knockout_states_and_page_group_items() {