Skip to content

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

Layout: parent-page margins and guides, Create Guides on the current page, ruler-unit-aware Margins and Columns

Middle Disc2b55f2ed0d9e
diff --git a/crates/doc/src/build.rs b/crates/doc/src/build.rs
index c274f2130..33256d338 100644
--- a/crates/doc/src/build.rs
+++ b/crates/doc/src/build.rs
@@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
use crate::ids::{ItemId, LayerId, PageId, SpreadId};
use crate::item::Item;
-use crate::page::{Columns, Margins, Page, PageSide, ParentInfo, Section, Spread};
+use crate::page::{Columns, Guide, Margins, Orientation, Page, PageSide, ParentInfo, Section, Spread};
use crate::styles::Styles;
use crate::{DataMerge, DocError, DocSettings, Document, Intent, LAYER_COLORS, Layer, ParaFormat, Result, SpreadRef};
@@ -552,6 +552,27 @@ impl Document {
let idx = if ps.pages.len() >= 2 && page.side == PageSide::Left { 0 } else { ps.pages.len().checked_sub(1)? };
Some((pi, idx))
}
+
+ /// Ruler guides document page `abs` inherits from its applied parent page. They are stored on
+ /// the parent page only: positions are moved from the parent page's place in its spread to
+ /// this page's place in its own, so they draw and snap where the page is. Editing them
+ /// happens on the parent page.
+ pub fn inherited_guides(&self, abs: usize) -> Vec<Guide> {
+ let (Some(page), Some((pi, qi))) = (self.page(abs), self.parent_page_for(abs)) else { return Vec::new() };
+ let Some(parent) = self.parents[pi].pages.get(qi) else { return Vec::new() };
+ let dx = page.x - parent.x;
+ parent
+ .guides
+ .iter()
+ .map(|g| {
+ let mut g = g.clone();
+ if g.orientation == Orientation::Vertical {
+ g.position += dx;
+ }
+ g
+ })
+ .collect()
+ }
}
#[cfg(test)]
diff --git a/crates/engine/src/cmd/guides.rs b/crates/engine/src/cmd/guides.rs
index 47aacdc86..94d2319de 100644
--- a/crates/engine/src/cmd/guides.rs
+++ b/crates/engine/src/cmd/guides.rs
@@ -404,4 +404,52 @@ mod layer_tests {
let d = s.doc().unwrap().doc.clone();
assert!(g(&s).visible_in(&d) && !g(&s).editable_in(&d));
}
+
+ #[test]
+ fn create_guides_goes_to_the_named_spread_and_page_only() {
+ use designcraft_doc::SpreadRef;
+ let mut s = Session::new();
+ s.execute("file.new", &json!({"pages": 3})).unwrap();
+ // The UI passes the spread/page being worked on as a serialised SpreadRef (page 3).
+ let spread = serde_json::to_value(SpreadRef::Doc(1)).unwrap();
+ s.execute("layout.createGuides", &json!({"spread": spread, "page": 1, "columns": 2, "columnGutter": 0.0})).unwrap();
+ let l = s.execute("guide.list", &json!({"spread": 1})).unwrap();
+ let l = l.as_array().unwrap();
+ assert!(!l.is_empty() && l.iter().all(|g| g["page"] == 1), "only page 3: {l:?}");
+ let first = s.execute("guide.list", &json!({"spread": 0})).unwrap();
+ assert!(first.as_array().unwrap().is_empty(), "nothing on the first page");
+ }
+
+ #[test]
+ fn guides_on_a_parent_page_show_on_the_pages_that_use_it() {
+ use designcraft_doc::{PageSide, SpreadRef};
+ let mut s = Session::new();
+ s.execute("file.new", &json!({"pages": 4, "facingPages": true})).unwrap();
+ // A vertical guide 88 pt inside the parent's right page, and a horizontal one.
+ let parent = serde_json::to_value(SpreadRef::Parent(0)).unwrap();
+ let right_x = s.doc().unwrap().doc.parents[0].pages.last().unwrap().x;
+ s.execute("guide.add", &json!({"spread": parent, "orientation": "vertical", "position": right_x + 88.0})).unwrap();
+ s.execute("guide.add", &json!({"spread": parent, "orientation": "horizontal", "position": 200.0, "at": right_x + 10.0})).unwrap();
+ let d = &s.doc().unwrap().doc;
+ let mut seen = 0;
+ for abs in 0..d.page_count() {
+ let page = d.page(abs).unwrap();
+ let (pi, qi) = d.parent_page_for(abs).unwrap();
+ let inherited = d.inherited_guides(abs);
+ if d.parents[pi].pages[qi].guides.is_empty() {
+ assert!(inherited.is_empty(), "page {abs} uses the parent's left page, which has no guides");
+ continue;
+ }
+ seen += 1;
+ assert_eq!(page.side, PageSide::Right);
+ let v = inherited.iter().find(|g| g.orientation == designcraft_doc::Orientation::Vertical).unwrap();
+ // Moved to this page's own place in its spread: still 88 pt inside the page.
+ assert!((v.position - (page.x + 88.0)).abs() < 1e-6, "page {abs}: {v:?} vs x {}", page.x);
+ let h = inherited.iter().find(|g| g.orientation == designcraft_doc::Orientation::Horizontal).unwrap();
+ assert!((h.position - 200.0).abs() < 1e-6);
+ }
+ assert!(seen >= 2, "pages on the parent's right side inherit: {seen}");
+ // The guides stay on the parent: nothing is copied onto document pages.
+ assert!((0..d.page_count()).all(|a| d.page(a).unwrap().guides.is_empty()));
+ }
}
diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs
index bd5320799..7df688e5f 100644
--- a/crates/engine/src/cmd/layout.rs
+++ b/crates/engine/src/cmd/layout.rs
@@ -75,6 +75,25 @@ fn dist(r: designcraft_geom::Rect, x: f64) -> f64 {
}
}
+/// Apply the `margins`, `columns` and `gutter` params of `layout.marginsAndColumns` to one page.
+fn set_margins_columns(pg: &mut designcraft_doc::Page, p: &Value) {
+ match p.get("margins") {
+ Some(Value::Number(n)) => pg.margins = Margins::uniform(n.as_f64().unwrap_or(36.0)),
+ Some(v @ Value::Object(_)) => {
+ if let Ok(m) = serde_json::from_value(v.clone()) {
+ pg.margins = m;
+ }
+ }
+ _ => {}
+ }
+ if let Some(c) = p.get("columns").and_then(Value::as_u64) {
+ pg.columns.count = (c as u32).clamp(1, 216);
+ }
+ if let Some(g) = p.get("gutter").and_then(Value::as_f64) {
+ pg.columns.gutter = g.max(0.0);
+ }
+}
+
pub fn specs() -> Vec<CommandSpec> {
vec![
cmd!(
@@ -191,33 +210,48 @@ pub fn specs() -> Vec<CommandSpec> {
"Margins and Columns…",
["Layout"],
None,
- "{pages?: [index] (default all), margins?: number|{top,bottom,inside,outside}, columns?, gutter?, adjustLayout?: bool (objects follow the margins)}",
+ "{pages?: [index] (default all), parent?: index (edit that parent spread instead; `pages` then index its pages), margins?: number|{top,bottom,inside,outside}, columns?, gutter?, adjustLayout?: bool (objects follow the margins)}",
has_doc,
|s, p| {
let pages: Option<Vec<usize>> =
p.get("pages").and_then(Value::as_array).map(|a| a.iter().filter_map(Value::as_u64).map(|v| v as usize).collect());
let p = p.clone();
let adjust = p.get("adjustLayout").and_then(Value::as_bool).unwrap_or(false);
+ let parent = p.get("parent").and_then(Value::as_u64).map(|v| v as usize);
s.edit(|d, _| {
let before = adjust.then(|| margin_boxes(d));
- let all: Vec<usize> = pages.clone().unwrap_or_else(|| (0..d.page_count()).collect());
- for abs in all {
- let Some((si, pi)) = d.page_loc(abs) else { continue };
- let pg = &mut std::sync::Arc::make_mut(&mut d.spreads[si]).pages[pi];
- match p.get("margins") {
- Some(Value::Number(n)) => pg.margins = Margins::uniform(n.as_f64().unwrap_or(36.0)),
- Some(v @ Value::Object(_)) => {
- if let Ok(m) = serde_json::from_value(v.clone()) {
- pg.margins = m;
- }
+ if let Some(pi) = parent {
+ // A parent spread: its own pages change, and so do the document pages based
+ // on it that still have the parent's old margins and columns (not overridden).
+ let Some(ps) = d.parents.get(pi) else {
+ return Err(bad("layout.marginsAndColumns", format!("no parent spread {pi}")));
+ };
+ let idxs: Vec<usize> = pages.clone().unwrap_or_else(|| (0..ps.pages.len()).collect());
+ let old: Vec<(usize, Margins, designcraft_doc::Columns)> =
+ idxs.iter().filter_map(|&qi| ps.pages.get(qi).map(|pg| (qi, pg.margins, pg.columns.clone()))).collect();
+ for &qi in &idxs {
+ if let Some(pg) = std::sync::Arc::make_mut(&mut d.parents[pi]).pages.get_mut(qi) {
+ set_margins_columns(pg, &p);
}
- _ => {}
}
- if let Some(c) = p.get("columns").and_then(Value::as_u64) {
- pg.columns.count = (c as u32).clamp(1, 216);
+ for abs in 0..d.page_count() {
+ let Some((ppi, qi)) = d.parent_page_for(abs) else { continue };
+ if ppi != pi {
+ continue;
+ }
+ let Some((_, om, oc)) = old.iter().find(|o| o.0 == qi) else { continue };
+ let Some((si, pj)) = d.page_loc(abs) else { continue };
+ let pg = &mut std::sync::Arc::make_mut(&mut d.spreads[si]).pages[pj];
+ if pg.margins == *om && pg.columns == *oc {
+ set_margins_columns(pg, &p);
+ }
}
- if let Some(g) = p.get("gutter").and_then(Value::as_f64) {
- pg.columns.gutter = g.max(0.0);
+ } else {
+ let all: Vec<usize> = pages.clone().unwrap_or_else(|| (0..d.page_count()).collect());
+ for abs in all {
+ let Some((si, pi)) = d.page_loc(abs) else { continue };
+ let pg = &mut std::sync::Arc::make_mut(&mut d.spreads[si]).pages[pi];
+ set_margins_columns(pg, &p);
}
}
if let Some(b) = before {
@@ -899,6 +933,7 @@ mod page_numbering_view_tests {
#[cfg(test)]
mod adjust_tests {
+ use designcraft_doc::Margins;
use serde_json::json;
use crate::Session;
@@ -920,4 +955,25 @@ mod adjust_tests {
let b2 = s.doc().unwrap().doc.item(designcraft_doc::ItemId(id)).unwrap().bounds();
assert_eq!(b, b2);
}
+
+ #[test]
+ fn parent_margins_change_the_parent_and_follow_unoverridden_pages() {
+ let mut s = Session::new();
+ s.execute("file.new", &json!({"pages": 3, "facingPages": false})).unwrap();
+ // Page 3 has its own margins (an override); pages 1 and 2 still follow the parent.
+ s.execute("layout.marginsAndColumns", &json!({"pages": [2], "margins": 100})).unwrap();
+ let parent_before = s.doc().unwrap().doc.parents[0].pages[0].margins;
+ assert_eq!(parent_before, Margins::uniform(36.0));
+ s.execute("layout.marginsAndColumns", &json!({"parent": 0, "margins": 60, "columns": 2})).unwrap();
+ let d = &s.doc().unwrap().doc;
+ // The parent page itself changed (this is what was drawn on screen).
+ assert_eq!(d.parents[0].pages[0].margins, Margins::uniform(60.0));
+ assert_eq!(d.parents[0].pages[0].columns.count, 2);
+ // Pages that matched the parent follow; the overridden page keeps its margins.
+ assert_eq!(d.page(0).unwrap().margins, Margins::uniform(60.0));
+ assert_eq!(d.page(1).unwrap().margins, Margins::uniform(60.0));
+ assert_eq!(d.page(2).unwrap().margins, Margins::uniform(100.0));
+ // An unknown parent is an error, not a silent no-op.
+ assert!(s.execute("layout.marginsAndColumns", &json!({"parent": 9, "margins": 10})).is_err());
+ }
}
diff --git a/crates/tools/src/snap.rs b/crates/tools/src/snap.rs
index 6d5c8f6a5..1ff0b4623 100644
--- a/crates/tools/src/snap.rs
+++ b/crates/tools/src/snap.rs
@@ -1083,9 +1083,15 @@ fn pass_guides(cx: &ToolContext, req: &SnapRequest<'_>, axis: Axis, tol: f64) ->
let flags = axis_flags(req, axis);
let (moving_lo, moving_hi) = perp_ends(req.rect, axis);
let mut best: Option<Best> = None;
- for page in &sp.pages {
+ let first_abs = match req.spread {
+ SpreadRef::Doc(si) => Some(cx.doc.first_page_of_spread(si)),
+ _ => None,
+ };
+ for (pidx, page) in sp.pages.iter().enumerate() {
let page_bounds = page.bounds();
- for guide in &page.guides {
+ // Guides set on the applied parent page count as this page's guides.
+ let inherited = first_abs.map(|f| cx.doc.inherited_guides(f + pidx)).unwrap_or_default();
+ for guide in page.guides.iter().chain(inherited.iter()) {
if !guide.visible_in(cx.doc) || !guide.position.is_finite() || guide.orientation != axis_orientation(axis) {
continue;
}
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 4c7313e61..9f2b357f9 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -153,6 +153,38 @@ pub fn current_page(app: &DesignApp) -> Option<usize> {
Some(st.doc.first_page_of_spread(si) + pi)
}
+/// The spread (document or parent) under the view centre and the page index within it: where
+/// commands that act "on the page I am working on" should go.
+pub fn current_spread_page(app: &DesignApp) -> Option<(SpreadRef, usize)> {
+ let st = app.session.active()?;
+ let layout = CanvasLayout::new(&st.doc, st.editing_parents);
+ let i = current_slot(app, &layout)?;
+ let slot = layout.slots.get(i)?;
+ let v = app.view()?;
+ let r = app.canvas_rect?;
+ let c = slot.to_spread(Point::new(v.origin.x + r.width() as f64 / 2.0 / v.zoom, slot.bounds.center().y));
+ let pg = st.doc.spread(slot.spread)?.page_at_x(c.x).unwrap_or(0);
+ Some((slot.spread, pg))
+}
+
+/// While parent spreads are being edited: (parent spread index, page index within it) under the
+/// view centre. `None` when document pages are shown.
+pub fn current_parent_page(app: &DesignApp) -> Option<(usize, usize)> {
+ let st = app.session.active()?;
+ if !st.editing_parents {
+ return None;
+ }
+ let layout = CanvasLayout::new(&st.doc, true);
+ let i = current_slot(app, &layout)?;
+ let slot = layout.slots.get(i)?;
+ let SpreadRef::Parent(pi) = slot.spread else { return None };
+ let v = app.view()?;
+ let r = app.canvas_rect?;
+ let c = slot.to_spread(Point::new(v.origin.x + r.width() as f64 / 2.0 / v.zoom, slot.bounds.center().y));
+ let qi = st.doc.parents.get(pi)?.page_at_x(c.x).unwrap_or(0);
+ Some((pi, qi))
+}
+
/// Scroll so absolute page `abs` is centred (keeps zoom).
pub fn go_to_page(app: &mut DesignApp, abs: usize) {
let Some(st) = app.session.active() else { return };
@@ -603,11 +635,17 @@ fn draw_guides(app: &DesignApp, painter: &egui::Painter, xf: &Xf, doc: &Document
painter.rect_stroke(rect(br), 0.0, Stroke::new(hair(painter), c32(s.bleed_color)), StrokeKind::Middle);
}
let pasteboard = m.inverse().transform_rect_bbox(layout.pasteboard);
- for p in &sp.pages {
+ let first_abs = match slot.spread {
+ SpreadRef::Doc(si) => Some(doc.first_page_of_spread(si)),
+ _ => None,
+ };
+ for (pidx, p) in sp.pages.iter().enumerate() {
let pr = p.bounds();
if !app.ui.guides {
continue;
}
+ // Guides set on the applied parent page show on this page too.
+ let inherited = first_abs.map(|f| doc.inherited_guides(f + pidx)).unwrap_or_default();
// Baseline grid.
if app.ui.baseline_grid && xf.zoom >= s.baseline_grid.view_threshold {
let g = &s.baseline_grid;
@@ -634,7 +672,7 @@ fn draw_guides(app: &DesignApp, painter: &egui::Painter, xf: &Xf, doc: &Document
}
}
}
- for g in p.guides.iter().filter(|g| g.visible_in(doc)) {
+ for g in p.guides.iter().chain(inherited.iter()).filter(|g| g.visible_in(doc)) {
// Spread guides cross the pasteboard; page guides their page.
let span = if g.spread { pasteboard } else { pr };
let (a, bb) = match g.orientation {
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index 2ab7a3bad..ab616504c 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -12,6 +12,10 @@ use crate::theme::semibold;
pub struct Dialog {
pub id: String,
pub fields: Map<String, Value>,
+ /// The document's ruler unit: what a bare number such as `12` means in a measurement field.
+ /// Set from the active document when the dialog is shown or confirmed (picas without one).
+ #[serde(skip)]
+ pub unit: Unit,
}
impl Dialog {
@@ -75,7 +79,7 @@ impl Dialog {
}
}
}
- Dialog { id: id.into(), fields }
+ Dialog { id: id.into(), fields, unit: Unit::Picas }
}
fn s(&self, k: &str) -> String {
match self.fields.get(k) {
@@ -87,7 +91,7 @@ impl Dialog {
fn m(&self, k: &str) -> Option<f64> {
match self.fields.get(k) {
Some(Value::Number(n)) => n.as_f64(),
- Some(Value::String(s)) => parse_measure(s, Unit::Picas).ok(),
+ Some(Value::String(s)) => parse_measure(s, self.unit).ok(),
_ => None,
}
}
@@ -589,6 +593,50 @@ fn confirm_text_frame_options(app: &mut DesignApp, d: &mut Dialog) -> Result<Val
}
/// File › Document Setup, filled from the document.
+/// The active document's ruler unit (picas without a document).
+fn doc_unit(app: &DesignApp) -> Unit {
+ app.session.active().map(|s| s.doc.settings.horizontal_units).unwrap_or(Unit::Picas)
+}
+
+/// Layout › Create Guides…: guides go on the page in view (or the parent page being edited), not
+/// on the first page, and the gutters are typed in the document's ruler unit.
+pub fn open_create_guides(app: &mut DesignApp) {
+ let unit = doc_unit(app);
+ let Some((spread, page)) = crate::canvas::current_spread_page(app) else { return };
+ let gutter = json!(format_measure(12.0, unit));
+ app.ui.dialog = Some(Dialog::new(
+ "createGuides",
+ json!({"rows": 0, "columns": 0, "rowGutter": gutter, "columnGutter": gutter, "fitTo": "margins", "removeExisting": false,
+ "spread": serde_json::to_value(spread).unwrap_or(json!(0)), "page": page}),
+ ));
+}
+
+/// Layout › Margins and Columns…: the margins and columns of the page in view (or of the parent
+/// page while parents are edited), shown and typed in the document's ruler unit.
+pub fn open_margins_and_columns(app: &mut DesignApp) {
+ let unit = doc_unit(app);
+ let parent = crate::canvas::current_parent_page(app);
+ let cur = crate::canvas::current_page(app).unwrap_or(0);
+ let Some(st) = app.session.active() else { return };
+ let page = match parent {
+ Some((pi, qi)) => st.doc.parents.get(pi).and_then(|s| s.pages.get(qi)),
+ None => st.doc.page(cur),
+ };
+ let Some(pg) = page else { return };
+ let fm = |v: f64| json!(format_measure(v, unit));
+ let mut f = json!({
+ "columns": pg.columns.count, "gutter": fm(pg.columns.gutter),
+ "marginTop": fm(pg.margins.top), "marginBottom": fm(pg.margins.bottom),
+ "marginInside": fm(pg.margins.inside), "marginOutside": fm(pg.margins.outside),
+ "adjustLayout": false, "allPages": false, "page": cur,
+ });
+ if let Some((pi, qi)) = parent {
+ f["parent"] = json!(pi);
+ f["parentPage"] = json!(qi);
+ }
+ app.ui.dialog = Some(Dialog::new("marginsAndColumns", f));
+}
+
pub fn open_document_setup(app: &mut DesignApp) {
let Ok(cur) = app.session.execute("layout.documentSetup", &json!({})) else { return };
let units = app.session.active().map(|s| s.doc.settings.horizontal_units).unwrap_or(Unit::Picas);
@@ -1116,6 +1164,7 @@ fn check(ui: &mut egui::Ui, d: &mut Dialog, key: &str, label: &str) {
pub fn show(app: &mut DesignApp, ctx: &egui::Context) {
let Some(mut d) = app.ui.dialog.clone() else { return };
+ d.unit = doc_unit(app);
let mut result: Option<bool> = None;
let alert_title = d.s("title");
let title = match d.id.as_str() {
@@ -1125,6 +1174,8 @@ pub fn show(app: &mut DesignApp, ctx: &egui::Context) {
"frameSize" => crate::i18n::tr(&app.ui.language, "Rectangle"),
"goToPage" => crate::i18n::tr(&app.ui.language, "Go to Page"),
"insertTable" => crate::i18n::tr(&app.ui.language, "Create Table"),
+ "createGuides" => crate::i18n::tr(&app.ui.language, "Create Guides"),
+ "marginsAndColumns" => crate::i18n::tr(&app.ui.language, "Margins and Columns"),
"textFrameOptions" => crate::i18n::tr(&app.ui.language, "Text Frame Options"),
"documentSetup" => crate::i18n::tr(&app.ui.language, "Document Setup"),
"findChange" => crate::i18n::tr(&app.ui.language, "Find/Change"),
@@ -1685,6 +1736,61 @@ pub fn show(app: &mut DesignApp, ctx: &egui::Context) {
ui.end_row();
});
}
+ "createGuides" => {
+ let u = d.unit.suffix();
+ egui::Grid::new("cg").num_columns(2).spacing([8.0, 6.0]).show(ui, |ui| {
+ for (key, label, unit) in [
+ ("rows", "Rows: Number", false),
+ ("rowGutter", "Rows: Gutter", true),
+ ("columns", "Columns: Number", false),
+ ("columnGutter", "Columns: Gutter", true),
+ ] {
+ let text = crate::i18n::tr(&app.ui.language, label);
+ ui.label(crate::rtl::widget(ui, if unit { format!("{text} ({u})") } else { text.to_string() }));
+ text_field(ui, &mut d, key, 80.0);
+ ui.end_row();
+ }
+ ui.label(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Fit Guides to")));
+ combo(ui, &mut d, "fitTo", &[("margins", "Margins"), ("page", "Page")]);
+ ui.end_row();
+ });
+ ui.add_space(4.0);
+ check(ui, &mut d, "removeExisting", crate::i18n::tr(&app.ui.language, "Remove Existing Ruler Guides"));
+ ui.label(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Applies to the page in view.")));
+ if let Some(st) = d.fields.get("status").and_then(Value::as_str) {
+ ui.label(egui::RichText::new(st).color(crate::theme::Tokens::get(ui.ctx()).text_dim));
+ }
+ }
+ "marginsAndColumns" => {
+ let u = d.unit.suffix();
+ egui::Grid::new("mac").num_columns(2).spacing([8.0, 6.0]).show(ui, |ui| {
+ ui.label(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Columns")));
+ text_field(ui, &mut d, "columns", 80.0);
+ ui.end_row();
+ for (key, label) in [
+ ("gutter", "Gutter"),
+ ("marginTop", "Top"),
+ ("marginBottom", "Bottom"),
+ ("marginInside", "Inside"),
+ ("marginOutside", "Outside"),
+ ] {
+ // The unit is shown next to every measurement so it is never a guess.
+ ui.label(crate::rtl::widget(ui, format!("{} ({})", crate::i18n::tr(&app.ui.language, label), u)));
+ text_field(ui, &mut d, key, 80.0);
+ ui.end_row();
+ }
+ });
+ ui.add_space(4.0);
+ if d.fields.contains_key("parent") {
+ ui.label(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Applies to the parent page in view.")));
+ } else {
+ check(ui, &mut d, "allPages", crate::i18n::tr(&app.ui.language, "Apply to all pages"));
+ }
+ check(ui, &mut d, "adjustLayout", crate::i18n::tr(&app.ui.language, "Adjust Layout"));
+ if let Some(st) = d.fields.get("status").and_then(Value::as_str) {
+ ui.label(egui::RichText::new(st).color(crate::theme::Tokens::get(ui.ctx()).text_dim));
+ }
+ }
"textFrameOptions" => {
seed_text_frame_options(app, &mut d);
text_frame_options_dialog(app, ui, &mut d);
@@ -1743,11 +1849,55 @@ pub fn show(app: &mut DesignApp, ctx: &egui::Context) {
/// Apply the open dialog.
pub fn confirm(app: &mut DesignApp) -> Result<Value, String> {
- let Some(d) = app.ui.dialog.take() else { return Err("no dialog open".into()) };
+ let Some(mut d) = app.ui.dialog.take() else { return Err("no dialog open".into()) };
+ d.unit = doc_unit(app);
if d.id != "pdfImport" || !d.b("async") {
app.cancel_pdf_import();
}
match d.id.as_str() {
+ "createGuides" => {
+ let (Some(row_gutter), Some(column_gutter)) =
+ (d.m("rowGutter").filter(|x| *x >= 0.0), d.m("columnGutter").filter(|x| *x >= 0.0))
+ else {
+ let mut d = d.clone();
+ d.fields.insert("status".into(), json!("Enter a valid gutter, e.g. 12 or 12 mm."));
+ app.ui.dialog = Some(d);
+ return Err("invalid gutter".into());
+ };
+ app.run(
+ "layout.createGuides",
+ json!({"rows": d.n("rows").unwrap_or(0.0).max(0.0) as u64, "columns": d.n("columns").unwrap_or(0.0).max(0.0) as u64,
+ "rowGutter": row_gutter, "columnGutter": column_gutter, "fitTo": d.s("fitTo"),
+ "removeExisting": d.b("removeExisting"), "spread": d.fields.get("spread").cloned().unwrap_or(json!(0)),
+ "page": d.fields.get("page").cloned().unwrap_or(json!(0))}),
+ )
+ }
+ "marginsAndColumns" => {
+ // A value that does not parse is an error shown in the dialog, never a silent 0.
+ let mut v = Map::new();
+ for (k, key) in [("top", "marginTop"), ("bottom", "marginBottom"), ("inside", "marginInside"), ("outside", "marginOutside"), ("gutter", "gutter")] {
+ match d.m(key).filter(|x| *x >= 0.0) {
+ Some(x) => {
+ v.insert(k.into(), json!(x));
+ }
+ None => {
+ let mut d = d.clone();
+ d.fields.insert("status".into(), json!(format!("Enter a valid {k} value, e.g. 12 or 12 mm.")));
+ app.ui.dialog = Some(d);
+ return Err(format!("invalid {k}"));
+ }
+ }
+ }
+ let gutter = v.remove("gutter").unwrap_or(json!(12.0));
+ let mut p = json!({"margins": v, "columns": d.n("columns").unwrap_or(1.0).max(1.0) as u64, "gutter": gutter, "adjustLayout": d.b("adjustLayout")});
+ if let Some(pi) = d.fields.get("parent").and_then(Value::as_u64) {
+ p["parent"] = json!(pi);
+ p["pages"] = json!([d.fields.get("parentPage").and_then(Value::as_u64).unwrap_or(0)]);
+ } else if !d.b("allPages") {
+ p["pages"] = json!([d.fields.get("page").and_then(Value::as_u64).unwrap_or(0)]);
+ }
+ app.run("layout.marginsAndColumns", p)
+ }
"newDocument" => {
let margins = json!({"top": d.m("marginTop").unwrap_or(36.0), "bottom": d.m("marginBottom").unwrap_or(36.0), "inside": d.m("marginInside").unwrap_or(36.0), "outside": d.m("marginOutside").unwrap_or(36.0)});
app.run(
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index d123ec4eb..c1fb26cef 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -1844,6 +1844,14 @@ pub fn activate(app: &mut DesignApp, id: &str, params: &Value) {
crate::dialogs::open_print(app);
return;
}
+ if params.is_null() && id == "layout.createGuides" {
+ crate::dialogs::open_create_guides(app);
+ return;
+ }
+ if params.is_null() && id == "layout.marginsAndColumns" {
+ crate::dialogs::open_margins_and_columns(app);
+ return;
+ }
if params.is_null() && id == "object.textFrameOptions" {
app.ui.dialog = Some(crate::dialogs::Dialog::new("textFrameOptions", json!({})));
return;
diff --git a/crates/ui-egui/src/panels/properties.rs b/crates/ui-egui/src/panels/properties.rs
index 49cb920f2..669a00bdf 100644
--- a/crates/ui-egui/src/panels/properties.rs
+++ b/crates/ui-egui/src/panels/properties.rs
@@ -246,7 +246,14 @@ fn document_sections(app: &mut DesignApp, ui: &mut Ui) {
// Margins.
section(ui, crate::i18n::tr(&app.ui.language, "Margins"), true);
let cur = crate::canvas::current_page(app).unwrap_or(0);
- if let Some(p) = d.page(cur).cloned() {
+ // While parent spreads are edited the fields belong to the parent page in view; they used to
+ // show (and change) the document pages instead, so nothing changed on the parent.
+ let parent_target = crate::canvas::current_parent_page(app);
+ let shown = match parent_target {
+ Some((pi, qi)) => d.parents.get(pi).and_then(|s| s.pages.get(qi)).cloned(),
+ None => d.page(cur).cloned(),
+ };
+ if let Some(p) = shown {
let linked = temp_bool(ui, "margins_linked", true);
let b = block(ui, 2.0 * FIELD_H + 5.0);
let mut set: Option<designcraft_doc::Margins> = None;
@@ -289,7 +296,11 @@ fn document_sections(app: &mut DesignApp, ui: &mut Ui) {
set_temp_bool(ui, "margins_linked", !linked);
}
if let Some(m) = set {
- let _ = app.run("layout.marginsAndColumns", json!({"margins": m}));
+ let params = match parent_target {
+ Some((pi, qi)) => json!({"margins": m, "parent": pi, "pages": [qi]}),
+ None => json!({"margins": m}),
+ };
+ let _ = app.run("layout.marginsAndColumns", params);
}
}
ui.add_space(6.0);