GitCorner

Code

ossartcraftvectorcraft🌐
2bc41aeb8602
//! Paragraph panel menu › Justification… (or `ui.justification`): Word Spacing, Letter Spacing and
//! Glyph Scaling (each Minimum, Desired and Maximum, in percent), Auto Leading and Single Word
//! Justification of the paragraphs the Paragraph panel acts on. The dialog opens on the values the
//! Paragraph panel shows (blank fields where paragraphs in scope differ) and previews them on the
//! canvas. OK runs `text.setFormat` with the values that changed, as one undo step; paragraphs
//! that differ keep the values left alone. OK is disabled while a row's Minimum exceeds its Desired
//! value or Desired its Maximum.
//!
//! Fields: `wordMin`, `wordDesired`, `wordMax` (0..1000), `letterMin`, `letterDesired`,
//! `letterMax` (-100..500), `glyphMin`, `glyphDesired`, `glyphMax` (50..200), `autoLeading`
//! (0..500), `singleWord` (`fullJustify`, `alignLeft`, `alignCenter`, `alignRight`) and `preview`,
//! with `__initial` (the values it opened with), `__targets` (the type and range it acts on; with
//! the Selection tool, every frame of each story selected when the dialog opened, locked and hidden
//! frames included) and, after Select All in threaded text, `__story` (the edited frame, whose
//! story it acts on).
//!
//! Behaviour reference (public documentation):
//! <https://helpx.adobe.com/illustrator/desktop/design-with-text/edit-format-text/adjust-word-and-letterspacing-in-justified-text.html>.

use serde_json::{Map, Value, json};
use vectorcraft_doc::{JustificationOptions, SingleWordJustify};

use super::swatch_options::label;
use super::{DialogSpec, form};
use crate::state::Dialog;
use crate::{VectorcraftApp, widgets};

/// The dialog kind of Justification.
pub const KIND: &str = "justification";

/// The field holding the values the dialog opened with.
const INITIAL: &str = "__initial";
/// The field holding the `ids`, `start` and `end` the dialog's `text.setFormat` acts on.
const TARGETS: &str = "__targets";
/// The field holding the frame the Type tool edits when the dialog opened after Select All in
/// threaded text: the dialog then acts on that frame's story as the document holds it.
const STORY: &str = "__story";
const CMD: &str = "text.setFormat";

pub(super) const SPEC: DialogSpec = DialogSpec {
    heading: |_| tl!("Justification").into(),
    body,
    confirm,
    preview: true,
    min_width: 460.0,
    ok_enabled: Some(|_, d| problem(d).is_none()),
    // A value the paragraphs don't share opens blank, without its field.
    inputs: |_| {
        &[
            "wordMin",
            "wordDesired",
            "wordMax",
            "letterMin",
            "letterDesired",
            "letterMax",
            "glyphMin",
            "glyphDesired",
            "glyphMax",
            "autoLeading",
            "singleWord",
        ]
    },
    ..DialogSpec::FORM
};

/// The three spacing rows: (`text.setFormat` key, field prefix, range).
const ROWS: [(&str, &str, (f64, f64)); 3] = [
    ("wordSpacing", "word", JustificationOptions::WORD_SPACING),
    ("letterSpacing", "letter", JustificationOptions::LETTER_SPACING),
    ("glyphScaling", "glyph", JustificationOptions::GLYPH_SCALING),
];
/// The columns of a row: (field suffix, `text.setFormat` key).
const COLUMNS: [(&str, &str); 3] = [("Min", "min"), ("Desired", "desired"), ("Max", "max")];

/// Single Word Justification's choices: (`text.setFormat` value, label).
fn single_word_choices() -> [(&'static str, &'static str); 4] {
    [("fullJustify", tl!("Full Justify")), ("alignLeft", tl!("Align left")), ("alignCenter", tl!("Align center")), ("alignRight", tl!("Align right"))]
}

fn single_word_name(s: SingleWordJustify) -> &'static str {
    match s {
        SingleWordJustify::FullJustify => "fullJustify",
        SingleWordJustify::AlignLeft => "alignLeft",
        SingleWordJustify::AlignCenter => "alignCenter",
        SingleWordJustify::AlignRight => "alignRight",
    }
}

/// Open Justification on the paragraph attributes the Paragraph panel shows.
pub fn open(app: &mut VectorcraftApp) -> Result<Value, String> {
    crate::panels::character::text_style(app).ok_or("select some type first")?;
    // While typing, the dialog acts on the paragraphs of the text being edited.
    let mut targets = json!({});
    let mut story = None;
    if let Some((id, a, b)) = crate::panels::character::text_editing_kept(app)? {
        targets = json!({"ids": [id.0], "start": a, "end": b});
        // After Select All in threaded text: every paragraph of every frame ([`params`]).
        story = crate::panels::character::text_story(app).map(|_| id.0);
    } else {
        // With the Selection tool: every frame of each selected story.
        crate::panels::character::target_selected_stories(app, &mut targets);
    }
    let mut values = Map::new();
    for (_, prefix, _) in ROWS {
        for (i, (suffix, _)) in COLUMNS.iter().enumerate() {
            let key = format!("{prefix}{suffix}");
            if let Some(v) = crate::panels::paragraph::shared_justification_f64(app, |j| match prefix {
                "word" => j.word_spacing[i],
                "letter" => j.letter_spacing[i],
                _ => j.glyph_scaling[i],
            }) {
                values.insert(key, json!(v));
            }
        }
    }
    if let Some(v) = crate::panels::paragraph::shared_justification_f64(app, |j| j.auto_leading) {
        values.insert("autoLeading".into(), json!(v));
    }
    if let Some(s) = crate::panels::paragraph::shared_single_word_justify(app) {
        values.insert("singleWord".into(), json!(single_word_name(s)));
    }
    let mut fields = Value::Object(values.clone());
    fields[INITIAL] = Value::Object(values);
    fields[TARGETS] = targets;
    if let Some(id) = story {
        fields[STORY] = json!(id);
    }
    fields["preview"] = json!(false);
    app.ui.dialog = Some(Dialog::new(KIND, fields));
    Ok(Value::Null)
}

/// Why OK can't apply the dialog: a row whose values are out of order.
fn problem(d: &Dialog) -> Option<&'static str> {
    let out_of_order = ROWS.iter().any(|(_, prefix, _)| {
        let keys = COLUMNS.map(|(suffix, _)| format!("{prefix}{suffix}"));
        if !keys.iter().all(|k| d.fields.contains_key(k)) {
            return false;
        }
        let [min, desired, max] = keys.map(|k| d.f64(&k, 0.0));
        !(min <= desired && desired <= max)
    });
    out_of_order.then(|| tl!("Minimum must not exceed Desired, nor Desired Maximum."))
}

fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool {
    // Its own grid: a label column and three value columns.
    egui::Grid::new("justification-spacing").num_columns(4).spacing([10.0, 8.0]).min_col_width(70.0).show(ui, |ui| {
        ui.label("");
        for heading in [tl!("Minimum"), tl!("Desired"), tl!("Maximum")] {
            ui.label(heading);
        }
        ui.end_row();
        for ((_, prefix, (lo, hi)), text) in ROWS.iter().zip([tl!("Word Spacing:"), tl!("Letter Spacing:"), tl!("Glyph Scaling:")]) {
            label(ui, text);
            for (suffix, _) in COLUMNS {
                let key = format!("{prefix}{suffix}");
                let value = d.fields.get(&key).and_then(|v| v.as_f64());
                if let Some(v) = widgets::range_field(ui, ("justification", key.as_str()), value, *lo..=*hi, "%", 0, 70.0) {
                    d.fields.insert(key, json!(v));
                }
            }
            ui.end_row();
        }
    });
    ui.add_space(8.0);
    egui::Grid::new("justification-more").num_columns(2).spacing([10.0, 8.0]).show(ui, |ui| {
        label(ui, tl!("Auto Leading:"));
        let (lo, hi) = JustificationOptions::AUTO_LEADING;
        let leading = d.fields.get("autoLeading").and_then(|v| v.as_f64());
        if let Some(v) = widgets::range_field(ui, ("justification", "autoLeading"), leading, lo..=hi, "%", 0, 64.0) {
            d.fields.insert("autoLeading".into(), json!(v));
        }
        ui.end_row();
        label(ui, tl!("Single Word Justification:"));
        let choices = single_word_choices();
        let current = d.fields.get("singleWord").and_then(|v| v.as_str());
        let shown = current.and_then(|current| choices.iter().find(|(v, _)| *v == current).map(|(_, l)| *l)).unwrap_or("");
        let labels = choices.map(|(_, l)| l);
        if let Some(i) = widgets::dropdown(ui, ("justification", "singleWord"), shown, &labels, 140.0)
            && let Some((v, _)) = choices.get(i)
        {
            d.fields.insert("singleWord".into(), json!(v));
        }
        ui.end_row();
    });
    match problem(d) {
        Some(why) => {
            ui.add_space(6.0);
            ui.colored_label(crate::theme::Tokens::get(ui.ctx()).error, why);
        }
        None => {
            let p = params(app, d);
            form::preview(app, ui, d, tl!("Justification"), CMD, Value::Object(p));
        }
    }
    false
}

/// `text.setFormat` parameters: the fields that differ from the values the dialog opened with,
/// on the dialog's type and range. After Select All in threaded text the type is every frame of
/// the edited frame's thread as the document holds it now; when that frame is no longer threaded,
/// every paragraph in that frame.
fn params(app: &VectorcraftApp, d: &Dialog) -> Map<String, Value> {
    let initial = d.fields.get(INITIAL).and_then(Value::as_object);
    let was = |key: &str| initial.and_then(|m| m.get(key)).and_then(Value::as_f64);
    let mut p = d.fields.get(TARGETS).and_then(Value::as_object).cloned().unwrap_or_default();
    if let (Some(id), Some(st)) = (d.fields.get(STORY).and_then(Value::as_u64), app.session.active()) {
        let story = st.doc.story_of(vectorcraft_doc::NodeId(id));
        p.remove("start");
        p.remove("end");
        if story.len() >= 2 {
            p.insert("ids".into(), json!(story.iter().map(|f| f.0).collect::<Vec<_>>()));
        }
    }
    for (param, prefix, _) in ROWS {
        let mut changed = Map::new();
        for (suffix, name) in COLUMNS {
            let key = format!("{prefix}{suffix}");
            if !d.fields.contains_key(&key) {
                continue;
            }
            let v = d.f64(&key, 0.0);
            if was(&key) != Some(v) {
                changed.insert(name.into(), json!(v));
            }
        }
        if !changed.is_empty() {
            p.insert(param.into(), Value::Object(changed));
        }
    }
    if d.fields.contains_key("autoLeading") {
        let leading = d.f64("autoLeading", 120.0);
        if was("autoLeading") != Some(leading) {
            p.insert("autoLeading".into(), json!(leading));
        }
    }
    if let Some(single) = d.fields.get("singleWord").and_then(|v| v.as_str())
        && initial.and_then(|m| m.get("singleWord")).and_then(Value::as_str) != Some(single)
    {
        p.insert("singleWordJustification".into(), json!(single));
    }
    p
}

/// Does `p` change anything (more than the targets)?
fn changes(p: &Map<String, Value>) -> bool {
    p.keys().any(|k| !matches!(k.as_str(), "ids" | "start" | "end"))
}

fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
    if let Some(why) = problem(d) {
        return Err(why.into());
    }
    let p = params(app, d);
    if !changes(&p) {
        if app.session.in_interaction() {
            let _ = app.session.cancel_interaction();
        }
        app.ui.dialog = None;
        return Ok(Value::Null);
    }
    form::commit_preview(app, CMD, Value::Object(p))
}

#[cfg(test)]
mod tests {
    use super::*;
    use vectorcraft_engine::Session;
    use vectorcraft_engine::doc::{NodeId, NodeKind, ParaStyle};

    /// A selected area type object with two paragraphs.
    fn app() -> (VectorcraftApp, u64) {
        let mut app = VectorcraftApp::new(Session::new(), Default::default());
        app.run("file.new", json!({"width": 400, "height": 400})).unwrap();
        let id = app.run("text.create", json!({"x": 10, "y": 10, "text": "One\nTwo", "area": {"width": 200, "height": 100}})).unwrap()["id"]
            .as_u64()
            .unwrap();
        app.run("select.set", json!({"ids": [id]})).unwrap();
        (app, id)
    }

    fn paras(app: &VectorcraftApp, id: u64) -> (ParaStyle, ParaStyle) {
        match &app.session.active().unwrap().doc.node(NodeId(id)).unwrap().kind {
            NodeKind::Text(t) => (t.para_at(0).clone(), t.para_at(1).clone()),
            _ => panic!("not text"),
        }
    }

    fn frame(app: &mut VectorcraftApp) {
        let ctx = egui::Context::default();
        crate::theme::install_fonts(&ctx);
        let mut out = ctx.run_ui(egui::RawInput::default(), |ui| super::super::show(app, ui.ctx()));
        out.textures_delta.clear();
    }

    fn set(app: &mut VectorcraftApp, key: &str, v: Value) {
        app.ui.dialog.as_mut().unwrap().fields.insert(key.into(), v);
    }

    /// `ui.justification` opens the dialog on the selected type's values; OK sets the paragraphs'
    /// justification options in one undo step.
    #[test]
    fn justification_dialog_sets_the_paragraphs_options() {
        let (mut app, id) = app();
        app.run("ui.justification", json!({})).unwrap();
        let d = app.ui.dialog.clone().expect("Justification opened");
        assert_eq!(d.kind, KIND);
        assert_eq!((d.f64("wordMin", 0.0), d.f64("wordDesired", 0.0), d.f64("wordMax", 0.0)), (80.0, 100.0, 133.0));
        assert_eq!((d.f64("glyphDesired", 0.0), d.f64("autoLeading", 0.0), d.str("singleWord")), (100.0, 120.0, "fullJustify".to_string()));
        set(&mut app, "wordMax", json!(150));
        set(&mut app, "letterMax", json!(5));
        set(&mut app, "glyphMin", json!(97));
        set(&mut app, "autoLeading", json!(140));
        set(&mut app, "singleWord", json!("alignRight"));
        let undo = app.session.active().unwrap().history.undo.len();
        super::super::confirm(&mut app).unwrap();
        assert!(app.ui.dialog.is_none());
        let want = JustificationOptions {
            word_spacing: [80.0, 100.0, 150.0],
            letter_spacing: [0.0, 0.0, 5.0],
            glyph_scaling: [97.0, 100.0, 100.0],
            auto_leading: 140.0,
            single_word: SingleWordJustify::AlignRight,
        };
        for p in [paras(&app, id).0, paras(&app, id).1] {
            assert_eq!(p.justification, want);
        }
        assert_eq!(app.session.active().unwrap().history.undo.len(), undo + 1);
    }

    /// OK sends only the values that changed: paragraphs whose other values differ keep them. Values
    /// out of order keep the dialog open and change nothing.
    #[test]
    fn justification_dialog_leaves_unchanged_values_alone_and_refuses_values_out_of_order() {
        let (mut app, id) = app();
        app.run("text.setFormat", json!({"ids": [id], "wordSpacing": [70, 100, 200], "start": 5, "end": 5})).unwrap();
        app.run("ui.justification", json!({})).unwrap();
        set(&mut app, "letterMax", json!(20));
        // Desired above Maximum: refused.
        set(&mut app, "glyphDesired", json!(150));
        let d = app.ui.dialog.clone().unwrap();
        assert!(problem(&d).is_some());
        assert!(super::super::confirm(&mut app).is_err());
        assert!(app.ui.dialog.is_some(), "the dialog stays open");
        assert_eq!(paras(&app, id).0.justification, JustificationOptions::default());
        set(&mut app, "glyphDesired", json!(100.0));
        super::super::confirm(&mut app).unwrap();
        let (first, second) = paras(&app, id);
        assert_eq!((first.justification.word_spacing, first.justification.letter_spacing), ([80.0, 100.0, 133.0], [0.0, 0.0, 20.0]));
        assert_eq!((second.justification.word_spacing, second.justification.letter_spacing), ([70.0, 100.0, 200.0], [0.0, 0.0, 20.0]));
        // Nothing changed: no undo step.
        let undo = app.session.active().unwrap().history.undo.len();
        app.run("ui.justification", json!({})).unwrap();
        super::super::confirm(&mut app).unwrap();
        assert_eq!(app.session.active().unwrap().history.undo.len(), undo);
        // With no type selected the dialog does not open.
        app.run("select.set", json!({"ids": []})).unwrap();
        assert!(app.run("ui.justification", json!({})).is_err());
        assert!(app.ui.dialog.is_none());
    }

    /// Preview shows the values on the canvas; Cancel rolls them back.
    #[test]
    fn justification_dialog_previews_and_cancel_restores() {
        let (mut app, id) = app();
        app.run("ui.justification", json!({})).unwrap();
        set(&mut app, "autoLeading", json!(200));
        set(&mut app, "preview", json!(true));
        frame(&mut app);
        assert!(app.session.in_interaction(), "the preview runs");
        assert_eq!(paras(&app, id).0.justification.auto_leading, 200.0);
        super::super::cancel(&mut app);
        assert!(app.ui.dialog.is_none());
        assert_eq!(paras(&app, id).0.justification.auto_leading, 120.0);
    }

    /// After Select All in threaded text, OK changes the frames in the edited frame's thread when OK
    /// is pressed: not a frame that left the thread while the dialog was open, and a frame that
    /// joined it.
    #[test]
    fn justification_dialog_follows_the_story_thread_at_ok() {
        let mut app = VectorcraftApp::new(Session::new(), Default::default());
        app.run("file.new", json!({"width": 900, "height": 300})).unwrap();
        let story = "The quick brown fox jumps over the lazy dog and keeps running far beyond the first frame.\n".repeat(6);
        let area = json!({"x": 10, "y": 10, "text": story, "size": 12, "area": {"width": 150, "height": 60}});
        let a = app.run("text.create", area).unwrap()["id"].as_u64().unwrap();
        let b = app.run("text.thread.add", json!({"from": a, "rect": [200, 10, 150, 60]})).unwrap()["id"].as_u64().unwrap();
        let c = app.run("text.thread.add", json!({"from": b, "rect": [400, 10, 150, 200]})).unwrap()["id"].as_u64().unwrap();
        app.run("select.set", json!({"ids": [a]})).unwrap();
        app.session.select_tool("type", Default::default()).unwrap();
        app.session.set_tool_option("textSelection", &json!({"editing": a, "anchor": 0, "caret": 0}));
        app.run("select.all", json!({})).unwrap();
        app.run("ui.justification", json!({})).unwrap();
        app.run("text.thread.break", json!({"id": b, "port": "out"})).unwrap();
        let d = app.run("text.thread.add", json!({"from": a, "rect": [600, 10, 150, 60]})).unwrap()["id"].as_u64().unwrap();
        set(&mut app, "autoLeading", json!(140));
        super::super::confirm(&mut app).unwrap();
        for id in [a, d, b] {
            assert_eq!(paras(&app, id).0.justification.auto_leading, 140.0, "frame {id} takes the change");
        }
        assert_eq!(paras(&app, c).0.justification.auto_leading, 120.0, "the frame that left keeps its paragraphs");
    }

    /// After Select All in threaded text, a thread broken after the edited frame while the dialog is
    /// open leaves the whole story in that frame, and OK changes every paragraph it now holds.
    #[test]
    fn justification_dialog_takes_the_story_into_a_frame_that_left_its_thread() {
        let mut app = VectorcraftApp::new(Session::new(), Default::default());
        app.run("file.new", json!({"width": 900, "height": 300})).unwrap();
        let story = "The quick brown fox jumps over the lazy dog and keeps running far beyond the first frame.\n".repeat(6);
        let area = json!({"x": 10, "y": 10, "text": story, "size": 12, "area": {"width": 150, "height": 60}});
        let a = app.run("text.create", area).unwrap()["id"].as_u64().unwrap();
        app.run("text.thread.add", json!({"from": a, "rect": [200, 10, 150, 400]})).unwrap();
        app.run("select.set", json!({"ids": [a]})).unwrap();
        app.session.select_tool("type", Default::default()).unwrap();
        app.session.set_tool_option("textSelection", &json!({"editing": a, "anchor": 0, "caret": 0}));
        app.run("select.all", json!({})).unwrap();
        app.run("ui.justification", json!({})).unwrap();
        app.run("text.thread.break", json!({"id": a, "port": "out"})).unwrap();
        set(&mut app, "autoLeading", json!(140));
        super::super::confirm(&mut app).unwrap();
        let NodeKind::Text(t) = &app.session.active().unwrap().doc.node(NodeId(a)).unwrap().kind else { panic!("not text") };
        assert!(t.paragraph_count() >= 6, "the story flows into the frame: {} paragraphs", t.paragraph_count());
        for i in 0..t.paragraph_count() {
            assert_eq!(t.para_at(i).justification.auto_leading, 140.0, "paragraph {i}");
        }
    }

    /// A threaded story of three frames, its second frame locked and its third hidden, a type object
    /// of its own, and the first frame selected with the Selection tool → the app, the frames and
    /// the other type object.
    fn story_selected_in_its_first_frame() -> (VectorcraftApp, [u64; 3], u64) {
        let mut app = VectorcraftApp::new(Session::new(), Default::default());
        app.run("file.new", json!({"width": 900, "height": 300})).unwrap();
        let story = "The quick brown fox jumps over the lazy dog.\n".repeat(12);
        let area = json!({"x": 10, "y": 10, "text": story, "size": 12, "area": {"width": 150, "height": 60}});
        let a = app.run("text.create", area).unwrap()["id"].as_u64().unwrap();
        let b = app.run("text.thread.add", json!({"from": a, "rect": [200, 10, 150, 60]})).unwrap()["id"].as_u64().unwrap();
        let c = app.run("text.thread.add", json!({"from": b, "rect": [400, 10, 150, 200]})).unwrap()["id"].as_u64().unwrap();
        let other = app.run("text.create", json!({"x": 10, "y": 250, "text": "Other", "size": 12})).unwrap()["id"].as_u64().unwrap();
        app.run("select.set", json!({"ids": [b]})).unwrap();
        app.run("object.lock", json!({})).unwrap();
        app.run("select.set", json!({"ids": [c]})).unwrap();
        app.run("object.hide", json!({})).unwrap();
        app.run("select.set", json!({"ids": [a]})).unwrap();
        (app, [a, b, c], other)
    }

    /// Every paragraph's Auto Leading in `id`.
    fn auto_leadings(app: &VectorcraftApp, id: u64) -> Vec<f64> {
        match &app.session.active().unwrap().doc.node(NodeId(id)).unwrap().kind {
            NodeKind::Text(t) => (0..t.paragraph_count()).map(|i| t.para_at(i).justification.auto_leading).collect(),
            _ => panic!("not text"),
        }
    }

    /// With the Selection tool a selected frame of threaded text stands for its whole story: the
    /// dialog opens blank where a locked or hidden frame differs, previews on every frame, Cancel
    /// restores them, and OK changes every frame of the story as it was when the dialog opened, in
    /// one undo step, leaving other type and a selection made later alone.
    #[test]
    fn selection_tool_justification_reaches_every_frame_of_the_story() {
        let (mut app, frames, other) = story_selected_in_its_first_frame();
        let [a, _, c] = frames;
        for id in frames {
            assert!(!auto_leadings(&app, id).is_empty(), "frame {id} holds text");
        }
        app.run("text.setFormat", json!({"ids": [c], "autoLeading": 150})).unwrap();
        app.run("select.set", json!({"ids": [a]})).unwrap();
        app.run("ui.justification", json!({})).unwrap();
        assert!(!app.ui.dialog.as_ref().unwrap().fields.contains_key("autoLeading"), "the hidden frame differs");
        set(&mut app, "autoLeading", json!(200));
        set(&mut app, "preview", json!(true));
        frame(&mut app);
        for id in frames {
            assert!(auto_leadings(&app, id).iter().all(|l| *l == 200.0), "the preview shows on frame {id}");
        }
        super::super::cancel(&mut app);
        assert!(auto_leadings(&app, a).iter().all(|l| *l == 120.0));
        assert!(auto_leadings(&app, c).iter().all(|l| *l == 150.0), "Cancel restores the hidden frame");

        app.run("ui.justification", json!({})).unwrap();
        // A selection made while the dialog is open does not change what it acts on.
        app.run("select.set", json!({"ids": [other]})).unwrap();
        set(&mut app, "autoLeading", json!(140));
        let undo = app.session.active().unwrap().history.undo.len();
        super::super::confirm(&mut app).unwrap();
        assert_eq!(app.session.active().unwrap().history.undo.len(), undo + 1, "one undo step");
        for id in frames {
            assert!(auto_leadings(&app, id).iter().all(|l| *l == 140.0), "frame {id} takes the change");
        }
        assert!(auto_leadings(&app, other).iter().all(|l| *l == 120.0), "other type keeps its paragraphs");
        app.run("edit.undo", json!({})).unwrap();
        assert!(auto_leadings(&app, c).iter().all(|l| *l == 150.0), "undo restores the hidden frame");
    }

    /// When paragraphs in scope disagree, only shared justification values are filled in.
    #[test]
    fn justification_dialog_mixed_paragraph_fields_show_blank() {
        let (mut app, id) = app();
        app.run("text.setFormat", json!({"ids": [id], "wordSpacing": [70, 100, 200], "start": 5, "end": 5})).unwrap();
        app.run("ui.justification", json!({})).unwrap();
        let d = app.ui.dialog.clone().unwrap();
        assert!(!d.fields.contains_key("wordMin"));
        assert!(!d.fields.contains_key("wordMax"));
        assert_eq!(d.f64("wordDesired", 0.0), 100.0);
        set(&mut app, "wordMin", json!(75.0));
        super::super::confirm(&mut app).unwrap();
        let (first, second) = paras(&app, id);
        assert_eq!(first.justification.word_spacing, [75.0, 100.0, 133.0]);
        assert_eq!(second.justification.word_spacing, [75.0, 100.0, 200.0]);
    }
}