Skip to content

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

Rectangle dialog: the height keeps the unit it was shown in (#1964)

Zinadin Zidan486dcd53a2d1
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index 63619da5e..b976887cd 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -22,6 +22,10 @@ pub struct Dialog {
/// Set from the active document when the dialog is shown or confirmed (picas without one).
#[serde(skip)]
pub unit: Unit,
+ /// The vertical ruler unit, for a dialog with a height field (the Rectangle dialog): kept
+ /// from when it opened, so a bare `50` means what it did then. `None` falls back to `unit`.
+ #[serde(skip)]
+ pub vertical_unit: Option<Unit>,
}
impl Dialog {
@@ -79,7 +83,7 @@ impl Dialog {
if id == "ruby" {
fields.entry("section".to_string()).or_insert(json!("cjk.rubyPlacement"));
}
- Dialog { id: id.into(), fields, unit: Unit::Picas }
+ Dialog { id: id.into(), fields, unit: Unit::Picas, vertical_unit: None }
}
/// Set a field and report a genuine Find domain/mode transition to the caller.
pub(crate) fn set_field(&mut self, key: &str, value: Value) -> bool {
@@ -947,6 +951,7 @@ pub fn frame_size_dialog(app: &DesignApp, params: Value) -> Dialog {
}
}
d.unit = doc_unit(app);
+ d.vertical_unit = Some(doc_vertical_unit(app));
d
}
@@ -3293,7 +3298,8 @@ fn confirm_part1(app: &mut DesignApp, d: Dialog) -> Result<Value, String> {
let x = d.fields.get("x").and_then(Value::as_f64).unwrap_or(0.0);
let y = d.fields.get("y").and_then(Value::as_f64).unwrap_or(0.0);
let w = d.m("width").unwrap_or(72.0);
- let h = d.m_in("height", doc_vertical_unit(app)).unwrap_or(72.0);
+ // In the units the dialog showed, not ones changed while it was open.
+ let h = d.m_in("height", d.vertical_unit.unwrap_or(d.unit)).unwrap_or(72.0);
app.run(
"frame.create",
json!({"spread": d.fields.get("spread").cloned().unwrap_or(json!(0)), "shape": d.s("shape"), "content": d.s("content"), "rect": [x, y, x + w, y + h]}),
diff --git a/crates/ui-egui/src/dialogs/tests.rs b/crates/ui-egui/src/dialogs/tests.rs
index 5b5516c77..e195a8c23 100644
--- a/crates/ui-egui/src/dialogs/tests.rs
+++ b/crates/ui-egui/src/dialogs/tests.rs
@@ -3566,3 +3566,41 @@ fn frame_size_dialog_follows_the_ruler_units() {
}
}
}
+
+/// #2055 review: the Rectangle dialog reads the height in the vertical unit it showed when it
+/// opened. Rulers switched while it is open (the control channel) leave a typed 50 at 50 mm.
+#[test]
+fn frame_size_dialog_keeps_its_units_while_open() {
+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({})).unwrap();
+ app.run("document.preferences", json!({"horizontalUnits": "millimeters", "verticalUnits": "millimeters"})).unwrap();
+ app.session.ui_requests.push(designcraft_engine::UiRequest::Dialog {
+ id: "frameSize".into(),
+ params: json!({"spread": 0, "shape": "rectangle", "content": "graphic", "x": 36.0, "y": 36.0, "width": 72.0, "height": 72.0}),
+ });
+ app.after_engine();
+ let d = app.ui.dialog.as_mut().expect("the Rectangle dialog");
+ d.fields.insert("width".into(), json!("50"));
+ d.fields.insert("height".into(), json!("50"));
+ app.run("document.preferences", json!({"horizontalUnits": "inches", "verticalUnits": "inches"})).unwrap();
+ let before = app.session.doc().unwrap().doc.clone();
+ confirm(&mut app).unwrap();
+ let after = &app.session.doc().unwrap().doc;
+ let added: Vec<_> = after.spreads[0].items.iter().filter(|i| !before.spreads[0].items.iter().any(|b| b.id == i.id)).collect();
+ assert_eq!(added.len(), 1, "one frame");
+ let b = added[0].bounds();
+ let mm50 = 50.0 / 25.4 * 72.0;
+ assert!((b.width() - mm50).abs() < 0.01 && (b.height() - mm50).abs() < 0.01, "50 mm by 50 mm (about 141.73 pt): {b:?}");
+ // A dialog made without the vertical unit (`Dialog::new`) reads the height in its own unit.
+ let mut d = Dialog::new("frameSize", json!({"width": "2", "height": "3"}));
+ d.unit = Unit::Inches;
+ assert_eq!(d.vertical_unit, None);
+ app.ui.dialog = Some(d);
+ let before = app.session.doc().unwrap().doc.clone();
+ confirm(&mut app).unwrap();
+ let after = &app.session.doc().unwrap().doc;
+ let added: Vec<_> = after.spreads[0].items.iter().filter(|i| !before.spreads[0].items.iter().any(|b| b.id == i.id)).collect();
+ assert_eq!(added.len(), 1, "one frame");
+ let b = added[0].bounds();
+ assert!((b.width() - 144.0).abs() < 0.01 && (b.height() - 216.0).abs() < 0.01, "{b:?}");
+}