mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 01:34:29 +01:00
Merge pull request #9982 from dotCooCoo/fix/transform-each-copy-refusal
Transform Each: a refused Copy keeps its reason under the rows
This commit is contained in:
1 file changed
+133
-41
@@ -6,19 +6,21 @@
|
||||
//!
|
||||
//! Fields: `scaleH`, `scaleV` (percentages, or with Absolute each object's width and height in pt),
|
||||
//! `relative` (Relative or Absolute scaling), `moveH`, `moveV` (pt), `rotate` (°), `reflectX`,
|
||||
//! `reflectY`, `random`, `reference` (0..8, the 9-point grid), `copy` (set by the Copy button, which
|
||||
//! confirms), `objects`, `patterns`, `corners`, `strokes` (the last two default to the preferences,
|
||||
//! which OK updates) and `preview`; `__constrain` is the link that keeps the two scales in
|
||||
//! proportion. It opens on the values it was last confirmed with, Copy aside (#1732).
|
||||
//! `reflectY`, `random`, `reference` (0..8, the 9-point grid), `objects`, `patterns`, `corners`,
|
||||
//! `strokes` (the last two default to the preferences, which OK updates) and `preview`;
|
||||
//! `__constrain` is the link that keeps the two scales in proportion. It opens on the values it was
|
||||
//! last confirmed with. The Copy button confirms as OK does and transforms a copy of each object
|
||||
//! (#1732); it is not a field.
|
||||
//!
|
||||
//! The dialog is a held-OK [`HostedParamDialog`]: a refused OK keeps it open. The link and mode
|
||||
//! The dialog is a held-OK [`HostedParamDialog`]: a refused OK or Copy keeps it open with the reason
|
||||
//! under the rows. The link and mode
|
||||
//! rules run in its build closure ([`apply_links`]), so a click, a typed value and an automation
|
||||
//! set all follow the same rule.
|
||||
|
||||
use craft::behavior::Mixed;
|
||||
use craft::ui::choice::{Checkbox, Radio, RadioGroup};
|
||||
use craft::ui::contract::{Bind, FieldLabelRole, Props as _, Show as _, ShowContainer as _, SideLabel};
|
||||
use craft::ui::dialog::{DialogHost, DialogId, FieldRefusal};
|
||||
use craft::ui::dialog::{DialogHost, DialogId, FieldRefusal, HeldOk};
|
||||
use craft::ui::display::Text;
|
||||
use craft::ui::form::Field;
|
||||
use craft::ui::layout_components::{Row, Stack};
|
||||
@@ -40,7 +42,7 @@ use crate::VectorcraftApp;
|
||||
|
||||
const CMD: &str = "object.transformEach";
|
||||
const KEY: &str = "transformEach";
|
||||
/// The footer action and field of the Copy button.
|
||||
/// The footer action of the Copy button and the command's `copy` parameter.
|
||||
const COPY: &str = "copy";
|
||||
|
||||
/// Where the values OK confirmed last are kept. Not `.remember`: that drops custom rows.
|
||||
@@ -56,6 +58,9 @@ pub(crate) struct Open {
|
||||
document: (u64, u64),
|
||||
/// The tool reset count when the current preview record was created.
|
||||
preview_tool_resets: u64,
|
||||
/// Whether the OK the dialog holds was filed by the Copy button. Copy is kept here and not in the
|
||||
/// fields: a change to the fields ends the refusal shown under the rows.
|
||||
copying: bool,
|
||||
}
|
||||
|
||||
impl Open {
|
||||
@@ -96,12 +101,11 @@ pub fn fields() -> DialogFields {
|
||||
("reflectY", FieldValue::Bool(false)),
|
||||
("random", FieldValue::Bool(false)),
|
||||
("reference", FieldValue::Int(4)),
|
||||
(COPY, FieldValue::Bool(false)),
|
||||
(LINK, FieldValue::Bool(false)),
|
||||
])
|
||||
}
|
||||
|
||||
/// The fields kept from the last OK or Copy: all but Copy, Preview (it opens on), Transform
|
||||
/// The fields kept from the last OK or Copy: all but Preview (it opens on), Transform
|
||||
/// Patterns (the preference it starts from), the scale options (preferences too) and the Absolute
|
||||
/// targets (`absH`, `absV`), which the dialog sets from the selection every time it opens.
|
||||
const REMEMBERED: [&str; 12] =
|
||||
@@ -144,7 +148,7 @@ fn params(f: &DialogFields) -> Value {
|
||||
"relative": relative(f),
|
||||
"moveH": num(f, "moveH", 0.0), "moveV": num(f, "moveV", 0.0),
|
||||
"rotate": num(f, "rotate", 0.0), "reflectX": flag(f, "reflectX"), "reflectY": flag(f, "reflectY"), "random": flag(f, "random"),
|
||||
"reference": num(f, "reference", 4.0).clamp(0.0, 8.0) as u64, "copy": flag(f, COPY),
|
||||
"reference": num(f, "reference", 4.0).clamp(0.0, 8.0) as u64, "copy": false,
|
||||
});
|
||||
let (h, v) = scale_keys(f);
|
||||
for (key, field) in [("scaleH", h), ("scaleV", v)] {
|
||||
@@ -508,7 +512,8 @@ pub(crate) fn open(app: &mut VectorcraftApp) {
|
||||
let dialog = HostedParamDialog::new(&ctx, KEY, f, move |fields| form(fields, shared, lengths));
|
||||
let options = HeldOptions { cancel: Some(Box::new(cancel)), ..Default::default() };
|
||||
if let Some(id) = open_held_with(app, dialog, move |app, result| ok(app, &ctx, result), options) {
|
||||
app.object_dialogs.each = Some(Open { id, preview: LivePreview::default(), document, preview_tool_resets: app.session.tool_resets() });
|
||||
app.object_dialogs.each =
|
||||
Some(Open { id, preview: LivePreview::default(), document, preview_tool_resets: app.session.tool_resets(), copying: false });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -522,8 +527,8 @@ fn cancel(app: &mut VectorcraftApp) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Applies the confirmed values as one undo step.
|
||||
fn commit(app: &mut VectorcraftApp, f: &DialogFields) -> Result<(), String> {
|
||||
/// Applies the confirmed values as one undo step, to a copy of each object with `copy`.
|
||||
fn commit(app: &mut VectorcraftApp, f: &DialogFields, copy: bool) -> Result<(), String> {
|
||||
let open = app.object_dialogs.each.as_ref().ok_or("the Transform Each dialog is closed")?;
|
||||
let (in_source, reverted, (uid, _), preview_tool_resets) = (open.in_source(app), open.reverted(app), open.document, open.preview_tool_resets);
|
||||
if !in_source {
|
||||
@@ -541,24 +546,19 @@ fn commit(app: &mut VectorcraftApp, f: &DialogFields) -> Result<(), String> {
|
||||
if app.session.tool_resets() != preview_tool_resets && app.session.in_interaction() {
|
||||
return Err("finish the active edit before applying Transform Each".into());
|
||||
}
|
||||
let p = with_scale_options(app, f, command_params(app, f))?;
|
||||
let mut p = command_params(app, f);
|
||||
p[COPY] = json!(copy);
|
||||
let p = with_scale_options(app, f, p)?;
|
||||
let preview = app.object_dialogs.each.as_mut().map(|o| std::mem::take(&mut o.preview)).unwrap_or_default();
|
||||
preview.commit(app, CMD, p).map(|_| ())
|
||||
}
|
||||
|
||||
/// OK: transforms each object and keeps the values for the next open. A refusal turns Copy off, or
|
||||
/// the next plain OK would copy; a refused plain OK keeps its values, so the dialog shows the reason.
|
||||
/// OK and Copy: transform each object, or a copy of each with Copy, and keep the values for the
|
||||
/// next open. A refusal leaves the values as they are, so the dialog shows the reason, and the
|
||||
/// next OK does not copy.
|
||||
fn ok(app: &mut VectorcraftApp, ctx: &egui::Context, result: &ParamResult) -> Result<(), String> {
|
||||
if let Err(reason) = commit(app, &result.fields) {
|
||||
if let (Some(id), Some(dialogs)) = (app.object_dialogs.each.as_ref().map(|o| o.id), app.dialogs()) {
|
||||
dialogs.with(|host| {
|
||||
if let Some(d) = host.get_mut_by_id::<HostedParamDialog>(id) {
|
||||
d.set_field(COPY, FieldValue::Bool(false));
|
||||
}
|
||||
});
|
||||
}
|
||||
return Err(reason);
|
||||
}
|
||||
let copy = app.object_dialogs.each.as_mut().is_some_and(|open| std::mem::take(&mut open.copying));
|
||||
commit(app, &result.fields, copy)?;
|
||||
let mut kept = DialogFields::new();
|
||||
for k in REMEMBERED {
|
||||
if let Some(v) = result.fields.get(k) {
|
||||
@@ -571,7 +571,7 @@ fn ok(app: &mut VectorcraftApp, ctx: &egui::Context, result: &ParamResult) -> Re
|
||||
}
|
||||
|
||||
/// Each frame: previews the dialog's values while Preview is on, and turns a press of Copy into a
|
||||
/// confirm with `copy` set.
|
||||
/// confirm that copies.
|
||||
pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) {
|
||||
let Some((id, in_source)) = app.object_dialogs.each.as_ref().map(|o| (o.id, o.in_source(app))) else { return };
|
||||
let Some(dialogs) = app.dialogs() else { return };
|
||||
@@ -596,13 +596,18 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) {
|
||||
app.object_dialogs.each = Some(open);
|
||||
}
|
||||
if action.as_deref() == Some(COPY) {
|
||||
dialogs.with(|host: &mut DialogHost| {
|
||||
if let Some(d) = host.get_mut_by_id::<HostedParamDialog>(id) {
|
||||
d.set_field(COPY, FieldValue::Bool(true));
|
||||
// A held confirm returns false; the OK it files shows as waiting. An OK already waiting is not
|
||||
// turned into a Copy.
|
||||
let filed = dialogs.with(|host: &mut DialogHost| {
|
||||
let waiting = |host: &DialogHost| host.held_state(id) == Some(HeldOk::Waiting);
|
||||
!waiting(host) && {
|
||||
let _ = host.confirm_by_id(ctx, id);
|
||||
waiting(host)
|
||||
}
|
||||
// A refused confirm leaves the dialog as it is: the held OK reports why.
|
||||
let _ = host.confirm_by_id(ctx, id);
|
||||
});
|
||||
if filed && let Some(open) = app.object_dialogs.each.as_mut() {
|
||||
open.copying = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -788,7 +793,7 @@ mod tests {
|
||||
h.get_by_role_and_label(egui::accesskit::Role::Button, "Copy").click();
|
||||
h.run_steps(4);
|
||||
assert_eq!(h.state().session.doc().unwrap().doc, other, "Copy must leave the other document unchanged");
|
||||
assert_ne!(field(&h, COPY), Some(FieldValue::Bool(true)), "a refused Copy clears its flag");
|
||||
assert!(!h.state().object_dialogs.each.as_ref().unwrap().copying, "a refused Copy is not kept for the next OK");
|
||||
|
||||
h.state_mut().run("document.activate", json!({"index": 0})).unwrap();
|
||||
h.get_by_role_and_label(egui::accesskit::Role::Button, "Copy").click();
|
||||
@@ -928,9 +933,96 @@ mod tests {
|
||||
h.run_steps(4);
|
||||
assert_eq!(h.state().session.doc().unwrap().doc, restored, "Copy must leave the restored document unchanged");
|
||||
assert_eq!(h.state().session.doc().unwrap().history.undo.len(), undo, "Copy must record no undo step");
|
||||
assert_ne!(field(&h, COPY), Some(FieldValue::Bool(true)), "a refused Copy clears its flag");
|
||||
assert_eq!(h.state().ui.status, REVERTED, "the status line reports the revert");
|
||||
assert!(h.state().object_dialogs.each.is_some(), "the dialog stays open");
|
||||
assert!(!h.state().object_dialogs.each.as_ref().unwrap().copying, "a refused Copy is not kept for the next OK");
|
||||
assert_refused_for_the_revert(&h);
|
||||
}
|
||||
|
||||
/// Every open document with its undo depth.
|
||||
fn documents(h: &Harness) -> Vec<(std::sync::Arc<vectorcraft_doc::Document>, usize)> {
|
||||
h.state().session.documents().iter().map(|doc| (doc.doc.clone(), doc.history.undo.len())).collect()
|
||||
}
|
||||
|
||||
/// Presses Copy, then checks over several frames that the dialog stays open with `reason` shown
|
||||
/// under the rows and that no document changed.
|
||||
fn assert_copy_refused(h: &mut Harness, reason: &str) {
|
||||
let before = documents(h);
|
||||
h.get_by_role_and_label(egui::accesskit::Role::Button, "Copy").click();
|
||||
// The press, the refusal and the pass that draws it.
|
||||
h.run_steps(3);
|
||||
for frame in 0..4 {
|
||||
h.run_steps(1);
|
||||
assert!(h.state().object_dialogs.each.is_some(), "the dialog stays open, frame {frame}");
|
||||
let id = id(h);
|
||||
let held = h.state().dialogs().unwrap().with(|host| host.held_state(id));
|
||||
assert!(
|
||||
matches!(&held, Some(craft::ui::dialog::HeldOk::Refused(shown)) if shown.as_str() == reason),
|
||||
"Copy is refused with {reason:?} on frame {frame}, the held state is {held:?}"
|
||||
);
|
||||
assert!(h.query_by_label_contains(reason).is_some(), "the reason shows under the rows on frame {frame}");
|
||||
assert!(documents(h) == before, "a refused Copy changes no document and records no undo step, frame {frame}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Asserts that a press of Copy copies the selection of the active document once, as one undo step.
|
||||
fn assert_copy_applies(h: &mut Harness) {
|
||||
let st = h.state().session.doc().unwrap();
|
||||
let (count, undo) = (st.doc.layers[0].children().unwrap().len(), st.history.undo.len());
|
||||
h.get_by_role_and_label(egui::accesskit::Role::Button, "Copy").click();
|
||||
h.run_steps(4);
|
||||
assert!(h.state().object_dialogs.each.is_none(), "Copy closes the dialog");
|
||||
let st = h.state().session.doc().unwrap();
|
||||
assert_eq!(st.doc.layers[0].children().unwrap().len(), count + 1, "Copy adds a copy of the square");
|
||||
assert_eq!(st.history.undo.len(), undo + 1, "Copy is one undo step");
|
||||
}
|
||||
|
||||
/// Copy from another document is refused with its reason, which stays shown; back on the
|
||||
/// opening document, Copy applies.
|
||||
#[test]
|
||||
fn a_refused_copy_keeps_showing_switch_back() {
|
||||
let (mut h, _) = open_with_two_documents();
|
||||
set(&mut h, "preview", json!(false));
|
||||
set(&mut h, "moveH", json!(100));
|
||||
h.run_steps(2);
|
||||
h.state_mut().run("document.activate", json!({"index": 1})).unwrap();
|
||||
assert_copy_refused(&mut h, "switch back to the document where Transform Each was opened");
|
||||
|
||||
h.state_mut().run("document.activate", json!({"index": 0})).unwrap();
|
||||
h.run_steps(2);
|
||||
assert_copy_applies(&mut h);
|
||||
}
|
||||
|
||||
/// Copy after the opening document closed is refused with its reason, which stays shown.
|
||||
#[test]
|
||||
fn a_refused_copy_keeps_showing_that_the_document_closed() {
|
||||
let (mut h, _) = open_with_two_documents();
|
||||
set(&mut h, "preview", json!(false));
|
||||
set(&mut h, "moveH", json!(100));
|
||||
h.run_steps(2);
|
||||
h.state_mut().session.execute("file.close", &json!({"index": 0})).unwrap();
|
||||
h.run_steps(2);
|
||||
assert_copy_refused(&mut h, "the document where Transform Each was opened has closed");
|
||||
}
|
||||
|
||||
/// Copy during an edit that started after the preview is refused with its reason, which stays
|
||||
/// shown; once the edit ends, Copy applies.
|
||||
#[test]
|
||||
fn a_refused_copy_keeps_showing_finish_the_active_edit() {
|
||||
let (mut h, _) = open_with_two_documents();
|
||||
set(&mut h, "moveH", json!(45));
|
||||
h.run_steps(2);
|
||||
let app = h.state_mut();
|
||||
app.run("document.activate", json!({"index": 1})).unwrap();
|
||||
app.run("document.activate", json!({"index": 0})).unwrap();
|
||||
app.session.begin_interaction("Another gesture").unwrap();
|
||||
app.session.preview("object.move", &json!({"dx": 20, "dy": 0})).unwrap();
|
||||
h.run_steps(2);
|
||||
assert_copy_refused(&mut h, "finish the active edit before applying Transform Each");
|
||||
assert!(h.state().session.in_interaction(), "the other edit goes on");
|
||||
|
||||
h.state_mut().session.cancel_interaction().unwrap();
|
||||
h.run_steps(2);
|
||||
assert_copy_applies(&mut h);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -989,19 +1081,18 @@ mod tests {
|
||||
] {
|
||||
set(&mut h, k, v);
|
||||
}
|
||||
// Copy is a footer button, not a row: the fields hold it as the app's Copy action sets it.
|
||||
let dialog = id(&h);
|
||||
h.state().dialogs().unwrap().with(|host| host.get_mut_by_id::<HostedParamDialog>(dialog).map(|d| d.set_field(COPY, FieldValue::Bool(true))));
|
||||
confirm(&mut h);
|
||||
h.get_by_role_and_label(egui::accesskit::Role::Button, "Copy").click();
|
||||
h.run_steps(4);
|
||||
assert!(h.state().object_dialogs.each.is_none());
|
||||
open(h.state_mut());
|
||||
h.run_steps(2);
|
||||
for (k, v) in [("scaleH", 150.0), ("scaleV", 100.0), ("moveH", 20.0), ("rotate", 15.0), ("reference", 0.0)] {
|
||||
assert_eq!(number(&h, k), v, "{k}");
|
||||
}
|
||||
for (k, v) in [("reflectX", true), ("reflectY", false), ("random", true), ("objects", false), (COPY, false)] {
|
||||
for (k, v) in [("reflectX", true), ("reflectY", false), ("random", true), ("objects", false)] {
|
||||
assert_eq!(field(&h, k), Some(FieldValue::Bool(v)), "{k}");
|
||||
}
|
||||
assert!(!h.state().object_dialogs.each.as_ref().unwrap().copying, "Copy isn't remembered");
|
||||
// Cancel keeps nothing new.
|
||||
set(&mut h, "scaleH", json!(300));
|
||||
let (ctx, id) = (h.ctx.clone(), id(&h));
|
||||
@@ -1041,7 +1132,8 @@ mod tests {
|
||||
assert_eq!(xs(app), vec![20.0, 120.0, 220.0], "Transform Again copies once more");
|
||||
open(h.state_mut());
|
||||
h.run_steps(2);
|
||||
assert_eq!(field(&h, COPY), Some(FieldValue::Bool(false)), "Copy isn't remembered");
|
||||
confirm(&mut h);
|
||||
assert_eq!(h.state().session.doc().unwrap().doc.layers[0].children().unwrap().len(), 3, "a plain OK after Copy does not copy");
|
||||
h.ctx.forget_all_images();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user