GitCorner

Code

ossartcraftdesigncraft🌐
88f5a00e389b
//! Object › Effects (and the Effects panel's fx button): InDesign's Effects dialog. Transparency
//! and every effect in one window: the list on the left (a checkbox turns an effect on, a click on
//! its name shows its settings and turns it on), the settings on the right in the document's
//! units, Preview on the page while it is open, and OK as one undo step (`object.effects`).

use designcraft_geom::{Unit, format_measure, units::parse_measure};
use serde_json::{Value, json};

use crate::DesignApp;
use crate::dialogs::Dialog;
use crate::theme::semibold;

/// The dialog's id.
pub const ID: &str = "effects";
/// The params a previewing dialog shows on the page (its interaction is open while it is set).
const PREVIEWED: &str = "_previewed";

/// The dialog's pages: (key in `Effects` JSON, label). Transparency is the object's own.
pub const PAGES: [(&str, &str); 10] = [
    ("transparency", "Transparency"),
    ("dropShadow", "Drop Shadow"),
    ("innerShadow", "Inner Shadow"),
    ("outerGlow", "Outer Glow"),
    ("innerGlow", "Inner Glow"),
    ("bevel", "Bevel and Emboss"),
    ("satin", "Satin"),
    ("feather", "Basic Feather"),
    ("directionalFeather", "Directional Feather"),
    ("gradientFeather", "Gradient Feather"),
];

/// The page an effect command opens (Object › Effects › Drop Shadow…, its shortcut).
pub fn page_of_command(id: &str) -> Option<&'static str> {
    Some(match id {
        "object.dropShadow" => "dropShadow",
        "object.innerShadow" => "innerShadow",
        "object.outerGlow" => "outerGlow",
        "object.innerGlow" => "innerGlow",
        "object.bevel" => "bevel",
        "object.satin" => "satin",
        "object.feather" => "feather",
        "object.directionalFeather" => "directionalFeather",
        "object.gradientFeather" => "gradientFeather",
        _ => return None,
    })
}

/// Open the dialog on `page` for the selected objects, the page's effect turned on (as InDesign
/// does when an effect is chosen from a menu). Nothing selected: a status message.
pub fn open(app: &mut DesignApp, page: &str) {
    let Some(st) = app.session.active() else { return };
    let ids: Vec<u64> = st.selection.items.iter().map(|i| i.0).collect();
    let Some(it) = st.selection.items.first().and_then(|i| st.doc.item(*i)) else {
        app.status(crate::i18n::tr(&app.ui.language, "Select an object.").to_string());
        return;
    };
    let mut fx = effects_json(&it.effects);
    let fields = json!({"ids": ids, "fx": Value::Null, "opacity": it.opacity, "blend": it.blend.label(), "preview": true, "page": "transparency"});
    let mut d = Dialog::new(ID, fields);
    let page = PAGES.iter().find(|p| p.0 == page).map_or("transparency", |p| p.0);
    turn_on(&mut fx, page);
    d.fields.insert("fx".into(), fx);
    d.fields.insert("page".into(), json!(page));
    app.ui.dialog = Some(d);
}

/// Every effect's settings, those still at their defaults included (the document leaves them out).
pub(crate) fn effects_json(e: &designcraft_doc::Effects) -> Value {
    use designcraft_doc::{Bevel, DirectionalFeather, DropShadow, FeatherOptions, GradientFeather, InnerGlow, InnerShadow, OuterGlow, Satin};
    let mut fx = serde_json::to_value(e).unwrap_or_else(|_| json!({}));
    let defaults = [
        ("dropShadow", serde_json::to_value(DropShadow::default())),
        ("innerShadow", serde_json::to_value(InnerShadow::default())),
        ("outerGlow", serde_json::to_value(OuterGlow::default())),
        ("innerGlow", serde_json::to_value(InnerGlow::default())),
        ("bevel", serde_json::to_value(Bevel::default())),
        ("satin", serde_json::to_value(Satin::default())),
        ("directionalFeather", serde_json::to_value(DirectionalFeather::default())),
        ("gradientFeather", serde_json::to_value(GradientFeather::default())),
        ("featherOptions", serde_json::to_value(FeatherOptions::default())),
    ];
    for (k, v) in defaults {
        if !fx[k].is_object()
            && let Ok(v) = v
        {
            fx[k] = v;
        }
    }
    fx
}

/// Turn the effect of `page` on, as choosing it does (Basic Feather: its default width).
pub(crate) fn turn_on(fx: &mut Value, page: &str) {
    match page {
        "transparency" => {}
        "feather" => {
            if fx["feather"].as_f64().unwrap_or(0.0) <= 0.0 {
                fx["feather"] = json!(9.0);
            }
        }
        _ => {
            if fx[page].is_object() {
                fx[page]["on"] = json!(true);
            }
        }
    }
}

pub(crate) fn is_on(fx: &Value, page: &str) -> bool {
    match page {
        "transparency" => true,
        "feather" => fx["feather"].as_f64().unwrap_or(0.0) > 0.0,
        _ => fx[page]["on"].as_bool().unwrap_or(false),
    }
}

/// The `object.effects` params the dialog stands for.
pub fn params(d: &Dialog) -> Value {
    let mut p = json!({"ids": d.fields.get("ids").cloned().unwrap_or(json!([])), "effects": d.fields.get("fx").cloned().unwrap_or(json!({})),
        "opacity": d.fields.get("opacity").and_then(Value::as_f64).unwrap_or(1.0), "blend": d.fields.get("blend").and_then(Value::as_str).unwrap_or("Normal")});
    if let Some(a) = d.fields.get(GLOBAL_LIGHT).and_then(Value::as_f64) {
        p["globalLight"] = json!(a);
    }
    p
}

/// The global light angle as changed in the dialog (an Angle field with Use Global Light on).
const GLOBAL_LIGHT: &str = "globalLight";

/// Apply: the preview goes and the settings are made once, as one undo step.
pub fn confirm(app: &mut DesignApp, d: &Dialog) -> Result<Value, String> {
    if d.fields.contains_key(PREVIEWED) {
        app.session.cancel_interaction();
    }
    app.run("object.effects", params(d))
}

/// Preview on: the effects show on the page (redone when a value changes); off: they go.
fn preview(app: &mut DesignApp, d: &mut Dialog) {
    let shown = d.fields.get(PREVIEWED).cloned();
    if !d.fields.get("preview").and_then(Value::as_bool).unwrap_or(false) {
        if shown.is_some() {
            app.session.cancel_interaction();
            d.fields.remove(PREVIEWED);
        }
        return;
    }
    let p = params(d);
    if shown.as_ref() == Some(&p) {
        return;
    }
    if shown.is_none() && app.session.begin_interaction("Effects").is_err() {
        return;
    }
    match app.session.preview("object.effects", p.clone()) {
        Ok(()) => d.fields.remove("status"),
        Err(e) => d.fields.insert("status".into(), json!(e.to_string())),
    };
    d.fields.insert(PREVIEWED.into(), p);
}

/// A number of `fx` at `path` (`["dropShadow", "distance"]`).
fn num(d: &Dialog, path: &[&str]) -> f64 {
    let mut v = d.fields.get("fx").unwrap_or(&Value::Null);
    for k in path {
        v = &v[*k];
    }
    v.as_f64().unwrap_or(0.0)
}

fn set(d: &mut Dialog, path: &[&str], value: Value) {
    let Some((last, parents)) = path.split_last() else { return };
    let Some(mut v) = d.fields.get_mut("fx") else { return };
    for k in parents {
        if !v[*k].is_object() {
            v[*k] = json!({});
        }
        v = &mut v[*k];
    }
    v[*last] = value;
}

/// A distance in the document's unit (stored in points).
fn measure(ui: &mut egui::Ui, d: &mut Dialog, path: &[&str], unit: Unit) {
    let mut v = num(d, path);
    let w = egui::DragValue::new(&mut v)
        .range(0.0..=10_000.0)
        .speed(0.25)
        .custom_formatter(move |n, _| format_measure(n, unit))
        .custom_parser(move |s| parse_measure(s, unit).ok());
    if ui.add_sized([90.0, 20.0], w).changed() {
        set(d, path, json!(v));
    }
}

/// An opacity (0–1, shown as %).
fn percent_of_one(ui: &mut egui::Ui, d: &mut Dialog, path: &[&str]) {
    let mut v = num(d, path) * 100.0;
    if ui.add_sized([70.0, 20.0], egui::DragValue::new(&mut v).range(0.0..=100.0).suffix("%").max_decimals(0)).changed() {
        set(d, path, json!(v / 100.0));
    }
}

/// A percentage (0–100).
fn percent(ui: &mut egui::Ui, d: &mut Dialog, path: &[&str]) {
    let mut v = num(d, path);
    if ui.add_sized([70.0, 20.0], egui::DragValue::new(&mut v).range(0.0..=100.0).suffix("%").max_decimals(0)).changed() {
        set(d, path, json!(v));
    }
}

/// The effect's own angle at `path`, or with Use Global Light (`own` false) the document's
/// global light angle: editing it then changes that one, for every effect that uses it. The dial
/// and the field both edit it.
fn angle(ui: &mut egui::Ui, d: &mut Dialog, path: &[&str], own: bool, global: f64) -> egui::Response {
    let mut v = if own { num(d, path) } else { global };
    let put = |d: &mut Dialog, v: f64| {
        if own {
            set(d, path, json!(v));
        } else {
            d.fields.insert(GLOBAL_LIGHT.into(), json!(v));
        }
    };
    ui.horizontal(|ui| {
        if let Some(a) = angle_dial(ui, v, true) {
            v = a;
            put(d, v);
        }
        if ui.add(egui::DragValue::new(&mut v).range(-180.0..=180.0).suffix("°").max_decimals(1)).changed() {
            put(d, v);
        }
    })
    .response
}

/// InDesign's angle dial: a circle with a radius pointing where the light comes from (0° to the
/// right, counter-clockwise). A click or a drag on it points it at the pointer. Focused, the
/// arrow keys turn it a degree (Shift: 10°) — Right and Up counter-clockwise — and assistive
/// technology can step it or set its value, as with a slider.
fn angle_dial(ui: &mut egui::Ui, deg: f64, enabled: bool) -> Option<f64> {
    use egui::accesskit::{Action, ActionData};
    let sense = if enabled { egui::Sense::click_and_drag() } else { egui::Sense::hover() };
    let (r, resp) = ui.allocate_exact_size(egui::vec2(26.0, 26.0), sense);
    resp.widget_info(|| egui::WidgetInfo::slider(enabled, deg, "Angle"));
    let col = if enabled { ui.visuals().text_color() } else { ui.visuals().weak_text_color() };
    let (c, rad) = (r.center(), 11.0);
    ui.painter().circle_stroke(c, rad, egui::Stroke::new(1.2, col));
    let a = deg.to_radians() as f32;
    ui.painter().line_segment([c, c + egui::vec2(a.cos(), -a.sin()) * rad], egui::Stroke::new(1.4, col));
    if resp.has_focus() {
        ui.painter().circle_stroke(c, rad + 2.0, ui.visuals().selection.stroke);
    }
    if !enabled {
        return None;
    }
    ui.ctx().accesskit_node_builder(resp.id, |node| {
        node.set_min_numeric_value(-180.0);
        node.set_max_numeric_value(180.0);
        node.set_numeric_value_step(1.0);
        node.add_action(Action::SetValue);
        node.add_action(Action::Increment);
        node.add_action(Action::Decrement);
    });
    let mut next = None;
    let mut steps = 0i64;
    if resp.has_focus() {
        // The arrows turn the dial instead of moving the focus.
        ui.memory_mut(|m| {
            m.set_focus_lock_filter(resp.id, egui::EventFilter { horizontal_arrows: true, vertical_arrows: true, ..Default::default() })
        });
        ui.input(|i| {
            for e in &i.events {
                if let egui::Event::Key { key, pressed: true, modifiers, .. } = e {
                    let dir = match key {
                        egui::Key::ArrowRight | egui::Key::ArrowUp => 1,
                        egui::Key::ArrowLeft | egui::Key::ArrowDown => -1,
                        _ => 0,
                    };
                    steps += dir * if modifiers.shift { 10 } else { 1 };
                }
            }
        });
    }
    ui.input(|i| {
        steps += i.num_accesskit_action_requests(resp.id, Action::Increment) as i64;
        steps -= i.num_accesskit_action_requests(resp.id, Action::Decrement) as i64;
        for request in i.accesskit_action_requests(resp.id, Action::SetValue) {
            if let Some(ActionData::NumericValue(v)) = request.data {
                next = Some(v);
            }
        }
    });
    if steps != 0 {
        next = Some(deg + steps as f64);
    }
    if (resp.dragged() || resp.clicked())
        && let Some(a) = resp.interact_pointer_pos().and_then(|p| dial_angle(c, p))
    {
        next = Some(a);
    }
    next.filter(|v| v.is_finite()).map(wrap_angle)
}

/// `deg` turned into the dial's range, −180 (exclusive) to 180°.
fn wrap_angle(deg: f64) -> f64 {
    let a = (deg + 180.0).rem_euclid(360.0) - 180.0;
    if a <= -180.0 { 180.0 } else { a }
}

/// The angle a dial at `c` points at for the pointer at `p`, in whole degrees (−180 to 180,
/// counter-clockwise from the right); none too close to the centre to tell.
fn dial_angle(c: egui::Pos2, p: egui::Pos2) -> Option<f64> {
    let d = p - c;
    // Straight left is 180° (not −180°, which a negative zero would give).
    (d.length() > 2.0).then(|| f64::from((-d.y).atan2(d.x)).to_degrees().round()).map(|a| if a <= -180.0 { 180.0 } else { a })
}

fn flag(ui: &mut egui::Ui, d: &mut Dialog, path: &[&str], label: &str) {
    let mut v = {
        let mut cur = d.fields.get("fx").unwrap_or(&Value::Null);
        for k in path {
            cur = &cur[*k];
        }
        cur.as_bool().unwrap_or(false)
    };
    if ui.checkbox(&mut v, crate::rtl::widget(ui, label)).changed() {
        set(d, path, json!(v));
    }
}

/// A swatch of the document.
fn swatch(app: &DesignApp, ui: &mut egui::Ui, d: &mut Dialog, path: &[&str]) {
    let cur = {
        let mut v = d.fields.get("fx").unwrap_or(&Value::Null);
        for k in path {
            v = &v[*k];
        }
        v.as_str().unwrap_or("[Black]").to_string()
    };
    let names: Vec<String> = app
        .session
        .active()
        .map_or_else(Vec::new, |st| st.doc.swatches.iter().filter(|w| !w.hidden || w.name == cur).map(|w| w.name.clone()).collect());
    craft_ui::ComboBox::from_id_salt(("fx_swatch", path.join("."))).selected_text(&cur).width(150.0).show_ui(ui, |ui| {
        for n in names {
            if ui.selectable_label(n == cur, &n).clicked() {
                set(d, path, json!(n));
            }
        }
    });
}

fn heading(ui: &mut egui::Ui, text: &str) {
    ui.add_space(6.0);
    crate::rtl::label(ui, egui::RichText::new(text).font(semibold(12.0)));
}

/// Distance and angle as an X (`y`: false) or Y offset: InDesign's, the shadow falls away from
/// the light. Editing an offset sets the distance and (without Global Light) the angle.
fn offset(ui: &mut egui::Ui, d: &mut Dialog, key: &str, unit: Unit, global: f64, y_axis: bool) {
    let gl = d.fields.get("fx").is_some_and(|f| f[key]["globalLight"].as_bool().unwrap_or(false));
    let a = if gl { global } else { num(d, &[key, "angle"]) }.to_radians();
    let dist = num(d, &[key, "distance"]);
    let (mut x, mut y) = (-dist * a.cos(), dist * a.sin());
    let v = if y_axis { &mut y } else { &mut x };
    let w =
        egui::DragValue::new(v).speed(0.25).custom_formatter(move |n, _| format_measure(n, unit)).custom_parser(move |s| parse_measure(s, unit).ok());
    if ui.add_sized([90.0, 20.0], w).changed() {
        set(d, &[key, "distance"], json!(x.hypot(y)));
        if !gl {
            set(d, &[key, "angle"], json!(y.atan2(-x).to_degrees()));
        }
    }
}

/// The settings of `page`.
pub(crate) fn page_ui(app: &DesignApp, ui: &mut egui::Ui, d: &mut Dialog, page: &str) {
    let lang = app.ui.language.clone();
    let tr = |s: &str| crate::i18n::tr(&lang, s).to_string();
    let unit = app.session.active().map_or(Unit::Points, |st| st.doc.settings.horizontal_units);
    // The global light angle as the dialog will set it (an edit in it shows at once).
    let global =
        d.fields.get(GLOBAL_LIGHT).and_then(Value::as_f64).unwrap_or_else(|| app.session.active().map_or(120.0, |st| st.doc.settings.global_light));
    let row = |ui: &mut egui::Ui, l: &str| crate::rtl::label(ui, tr(l));
    crate::rtl::label(ui, egui::RichText::new(tr(PAGES.iter().find(|p| p.0 == page).map_or("Transparency", |p| p.1))).font(semibold(14.0)));
    let grid = |id: &str| egui::Grid::new(("fx_grid", id.to_string())).num_columns(4).spacing([8.0, 6.0]);
    match page {
        "transparency" => {
            heading(ui, &tr("Basic Blending"));
            grid("t").show(ui, |ui| {
                row(ui, "Mode:");
                let cur = d.fields.get("blend").and_then(Value::as_str).unwrap_or("Normal").to_string();
                craft_ui::ComboBox::from_id_salt("fx_blend").selected_text(tr(&cur)).width(150.0).show_ui(ui, |ui| {
                    for m in designcraft_color::BlendMode::ALL {
                        if ui.selectable_label(m.label() == cur, tr(m.label())).clicked() {
                            d.fields.insert("blend".into(), json!(m.label()));
                        }
                    }
                });
                row(ui, "Opacity:");
                let mut o = d.fields.get("opacity").and_then(Value::as_f64).unwrap_or(1.0) * 100.0;
                if ui.add_sized([70.0, 20.0], egui::DragValue::new(&mut o).range(0.0..=100.0).suffix("%").max_decimals(0)).changed() {
                    d.fields.insert("opacity".into(), json!(o / 100.0));
                }
                ui.end_row();
            });
        }
        "dropShadow" | "innerShadow" => {
            let key = page;
            heading(ui, &tr("Blending"));
            grid("b").show(ui, |ui| {
                mode_row(ui, d, key, &tr);
                row(ui, "Color:");
                swatch(app, ui, d, &[key, "color"]);
                row(ui, "Opacity:");
                percent_of_one(ui, d, &[key, "opacity"]);
                ui.end_row();
            });
            heading(ui, &tr("Position"));
            grid("p").show(ui, |ui| {
                row(ui, "Distance:");
                measure(ui, d, &[key, "distance"], unit);
                row(ui, "X Offset:");
                offset(ui, d, key, unit, global, false);
                ui.end_row();
                row(ui, "Angle:");
                let gl = d.fields.get("fx").is_some_and(|f| f[key]["globalLight"].as_bool().unwrap_or(false));
                angle(ui, d, &[key, "angle"], !gl, global);
                row(ui, "Y Offset:");
                offset(ui, d, key, unit, global, true);
                ui.end_row();
                crate::rtl::label(ui, "");
                flag(ui, d, &[key, "globalLight"], &tr("Use Global Light"));
                ui.end_row();
            });
            heading(ui, &tr("Options"));
            grid("o").show(ui, |ui| {
                row(ui, "Size:");
                measure(ui, d, &[key, "size"], unit);
                if key == "dropShadow" {
                    row(ui, "Spread:");
                    percent(ui, d, &[key, "spread"]);
                } else {
                    row(ui, "Choke:");
                    percent(ui, d, &[key, "choke"]);
                }
                ui.end_row();
            });
        }
        "outerGlow" | "innerGlow" => {
            let key = page;
            heading(ui, &tr("Blending"));
            grid("b").show(ui, |ui| {
                mode_row(ui, d, key, &tr);
                row(ui, "Color:");
                swatch(app, ui, d, &[key, "color"]);
                row(ui, "Opacity:");
                percent_of_one(ui, d, &[key, "opacity"]);
                ui.end_row();
            });
            heading(ui, &tr("Options"));
            grid("o").show(ui, |ui| {
                row(ui, "Size:");
                measure(ui, d, &[key, "size"], unit);
                if key == "outerGlow" {
                    row(ui, "Spread:");
                    percent(ui, d, &[key, "spread"]);
                } else {
                    row(ui, "Choke:");
                    percent(ui, d, &[key, "choke"]);
                    ui.end_row();
                    row(ui, "Technique:");
                    let cur = d.fields.get("fx").and_then(|f| f["innerGlow"]["technique"].as_str()).unwrap_or("softer").to_string();
                    craft_ui::ComboBox::from_id_salt("fx_technique").selected_text(tr(if cur == "precise" { "Precise" } else { "Softer" })).show_ui(
                        ui,
                        |ui| {
                            for (v, l) in [("softer", "Softer"), ("precise", "Precise")] {
                                if ui.selectable_label(cur == v, tr(l)).clicked() {
                                    set(d, &["innerGlow", "technique"], json!(v));
                                }
                            }
                        },
                    );
                    ui.end_row();
                    row(ui, "Source:");
                    let mut center = d.fields.get("fx").is_some_and(|f| f["innerGlow"]["center"].as_bool().unwrap_or(false));
                    let label = |c: bool| tr(if c { "Center" } else { "Edge" });
                    craft_ui::ComboBox::from_id_salt("fx_source").selected_text(label(center)).show_ui(ui, |ui| {
                        for c in [false, true] {
                            if ui.selectable_value(&mut center, c, label(c)).clicked() {
                                set(d, &["innerGlow", "center"], json!(c));
                            }
                        }
                    });
                }
                ui.end_row();
            });
        }
        "bevel" => {
            heading(ui, &tr("Structure"));
            grid("s").show(ui, |ui| {
                row(ui, "Size:");
                measure(ui, d, &["bevel", "size"], unit);
                row(ui, "Depth:");
                let mut v = num(d, &["bevel", "depth"]);
                if ui.add_sized([70.0, 20.0], egui::DragValue::new(&mut v).range(1.0..=1000.0).suffix("%").max_decimals(0)).changed() {
                    set(d, &["bevel", "depth"], json!(v));
                }
                ui.end_row();
            });
            heading(ui, &tr("Shading"));
            grid("sh").show(ui, |ui| {
                row(ui, "Angle:");
                let gl = d.fields.get("fx").is_some_and(|f| f["bevel"]["globalLight"].as_bool().unwrap_or(false));
                angle(ui, d, &["bevel", "angle"], !gl, global);
                flag(ui, d, &["bevel", "globalLight"], &tr("Use Global Light"));
                ui.end_row();
                row(ui, "Highlight:");
                swatch(app, ui, d, &["bevel", "highlight"]);
                row(ui, "Opacity:");
                percent_of_one(ui, d, &["bevel", "highlightOpacity"]);
                ui.end_row();
                row(ui, "Shadow:");
                swatch(app, ui, d, &["bevel", "shadow"]);
                row(ui, "Opacity:");
                percent_of_one(ui, d, &["bevel", "shadowOpacity"]);
                ui.end_row();
            });
        }
        "satin" => {
            heading(ui, &tr("Blending"));
            grid("b").show(ui, |ui| {
                row(ui, "Color:");
                swatch(app, ui, d, &["satin", "color"]);
                row(ui, "Opacity:");
                percent_of_one(ui, d, &["satin", "opacity"]);
                ui.end_row();
            });
            heading(ui, &tr("Options"));
            grid("o").show(ui, |ui| {
                row(ui, "Angle:");
                angle(ui, d, &["satin", "angle"], true, global);
                row(ui, "Distance:");
                measure(ui, d, &["satin", "distance"], unit);
                ui.end_row();
                row(ui, "Size:");
                measure(ui, d, &["satin", "size"], unit);
                crate::rtl::label(ui, "");
                flag(ui, d, &["satin", "invert"], &tr("Invert"));
                ui.end_row();
            });
        }
        "feather" => {
            heading(ui, &tr("Options"));
            grid("o").show(ui, |ui| {
                row(ui, "Feather Width:");
                measure(ui, d, &["feather"], unit);
                ui.end_row();
                row(ui, "Choke:");
                percent(ui, d, &["featherOptions", "choke"]);
                ui.end_row();
                row(ui, "Corners:");
                let cur = d.fields.get("fx").and_then(|f| f["featherOptions"]["corners"].as_str()).unwrap_or("diffused").to_string();
                let shown = match cur.as_str() {
                    "sharp" => "Sharp",
                    "rounded" => "Rounded",
                    _ => "Diffused",
                };
                craft_ui::ComboBox::from_id_salt("fx_corners").selected_text(tr(shown)).show_ui(ui, |ui| {
                    for (v, l) in [("sharp", "Sharp"), ("rounded", "Rounded"), ("diffused", "Diffused")] {
                        if ui.selectable_label(cur == v, tr(l)).clicked() {
                            set(d, &["featherOptions", "corners"], json!(v));
                        }
                    }
                });
                ui.end_row();
            });
        }
        "directionalFeather" => {
            heading(ui, &tr("Feather Widths"));
            grid("w").show(ui, |ui| {
                for (i, l) in ["Top:", "Left:", "Bottom:", "Right:"].into_iter().enumerate() {
                    row(ui, l);
                    let mut v = d.fields.get("fx").and_then(|f| f["directionalFeather"]["widths"][i].as_f64()).unwrap_or(0.0);
                    let w = egui::DragValue::new(&mut v)
                        .range(0.0..=10_000.0)
                        .speed(0.25)
                        .custom_formatter(move |n, _| format_measure(n, unit))
                        .custom_parser(move |s| parse_measure(s, unit).ok());
                    if ui.add_sized([90.0, 20.0], w).changed() {
                        let mut ws: Vec<f64> =
                            (0..4).map(|k| d.fields.get("fx").and_then(|f| f["directionalFeather"]["widths"][k].as_f64()).unwrap_or(0.0)).collect();
                        if let Some(slot) = ws.get_mut(i) {
                            *slot = v;
                        }
                        set(d, &["directionalFeather", "widths"], json!(ws));
                    }
                    if i % 2 == 1 {
                        ui.end_row();
                    }
                }
            });
        }
        "gradientFeather" => {
            heading(ui, &tr("Gradient Stops"));
            grid("g").show(ui, |ui| {
                row(ui, "Start Opacity:");
                percent_of_one(ui, d, &["gradientFeather", "start"]);
                row(ui, "End Opacity:");
                percent_of_one(ui, d, &["gradientFeather", "end"]);
                ui.end_row();
            });
            heading(ui, &tr("Options"));
            grid("o").show(ui, |ui| {
                row(ui, "Type:");
                let mut radial = d.fields.get("fx").is_some_and(|f| f["gradientFeather"]["radial"].as_bool().unwrap_or(false));
                let label = |r: bool| tr(if r { "Radial" } else { "Linear" });
                craft_ui::ComboBox::from_id_salt("fx_gtype").selected_text(label(radial)).show_ui(ui, |ui| {
                    for r in [false, true] {
                        if ui.selectable_value(&mut radial, r, label(r)).clicked() {
                            set(d, &["gradientFeather", "radial"], json!(r));
                        }
                    }
                });
                row(ui, "Angle:");
                angle(ui, d, &["gradientFeather", "angle"], true, global);
                ui.end_row();
            });
        }
        _ => {}
    }
}

/// One line per applied level, as in InDesign's summary: "OBJECT: Normal 100%; Drop Shadow".
fn summary(lang: &str, d: &Dialog) -> String {
    let fx = d.fields.get("fx").cloned().unwrap_or(Value::Null);
    let blend = d.fields.get("blend").and_then(Value::as_str).unwrap_or("Normal");
    let opacity = d.fields.get("opacity").and_then(Value::as_f64).unwrap_or(1.0);
    let on: Vec<String> = PAGES.iter().skip(1).filter(|p| is_on(&fx, p.0)).map(|p| crate::i18n::tr(lang, p.1).to_string()).collect();
    let effects = if on.is_empty() { crate::i18n::tr(lang, "(no effects)").to_string() } else { on.join(", ") };
    format!("{}: {} {:.0}%; {}", crate::i18n::tr(lang, "OBJECT").to_uppercase(), crate::i18n::tr(lang, blend), opacity * 100.0, effects)
}

/// The Mode row of a shadow's or glow's Blending: its blend mode, as InDesign's (shadows start
/// Multiply, glows Screen).
fn mode_row(ui: &mut egui::Ui, d: &mut Dialog, key: &str, tr: &dyn Fn(&str) -> String) {
    crate::rtl::label(ui, tr("Mode:"));
    let fallback = if key.ends_with("Glow") { "Screen" } else { "Multiply" };
    let cur = d.fields.get("fx").and_then(|f| f[key]["mode"].as_str()).unwrap_or(fallback).to_string();
    let name = |m: &designcraft_color::BlendMode| serde_json::to_value(m).ok().and_then(|v| v.as_str().map(str::to_string)).unwrap_or_default();
    let shown = designcraft_color::BlendMode::ALL.iter().find(|m| name(m) == cur).map_or(fallback, |m| m.label());
    let mut picked = None;
    craft_ui::ComboBox::from_id_salt(("fx_mode", key.to_string())).selected_text(tr(shown)).width(150.0).show_ui(ui, |ui| {
        for m in designcraft_color::BlendMode::ALL {
            if ui.selectable_label(name(&m) == cur, tr(m.label())).clicked() {
                picked = Some(name(&m));
            }
        }
    });
    if let Some(m) = picked {
        set(d, &[key, "mode"], json!(m));
    }
    ui.end_row();
}

/// The dialog's body.
pub fn show(app: &mut DesignApp, ui: &mut egui::Ui, d: &mut Dialog) {
    let lang = app.ui.language.clone();
    let tr = |s: &str| crate::i18n::tr(&lang, s).to_string();
    ui.horizontal(|ui| {
        crate::rtl::label(ui, tr("Settings for:"));
        // Stroke, Fill and Text levels aren't in the document model yet: the object only.
        ui.add_enabled_ui(false, |ui| {
            craft_ui::ComboBox::from_id_salt("fx_level").selected_text(tr("Object")).width(110.0).show_ui(ui, |_| {});
        });
    });
    ui.add_space(6.0);
    let page = d.fields.get("page").and_then(Value::as_str).unwrap_or("transparency").to_string();
    ui.horizontal_top(|ui| {
        ui.vertical(|ui| {
            ui.set_width(180.0);
            for (key, label) in PAGES {
                ui.horizontal(|ui| {
                    if key == "transparency" {
                        ui.add_space(24.0);
                    } else {
                        let fx = d.fields.get("fx").cloned().unwrap_or(Value::Null);
                        let mut on = is_on(&fx, key);
                        if ui.checkbox(&mut on, "").changed() {
                            if on {
                                if let Some(f) = d.fields.get_mut("fx") {
                                    turn_on(f, key);
                                }
                            } else if key == "feather" {
                                set(d, &["feather"], json!(0.0));
                            } else {
                                set(d, &[key, "on"], json!(false));
                            }
                        }
                    }
                    if ui.selectable_label(page == key, tr(label)).clicked() {
                        d.fields.insert("page".into(), json!(key));
                        if let Some(f) = d.fields.get_mut("fx") {
                            turn_on(f, key);
                        }
                    }
                });
            }
            ui.add_space(8.0);
            crate::rtl::label(ui, egui::RichText::new(summary(&lang, d)).size(11.0).color(crate::theme::Tokens::get(ui.ctx()).text_dim));
        });
        ui.add_space(16.0);
        ui.vertical(|ui| {
            ui.set_min_width(380.0);
            page_ui(app, ui, d, &page);
        });
    });
    ui.add_space(10.0);
    let mut p = d.fields.get("preview").and_then(Value::as_bool).unwrap_or(false);
    if ui.checkbox(&mut p, crate::rtl::widget(ui, tr("Preview"))).changed() {
        d.fields.insert("preview".into(), json!(p));
    }
    if let Some(st) = d.fields.get("status").and_then(Value::as_str) {
        crate::rtl::label(ui, egui::RichText::new(st).color(crate::theme::Tokens::get(ui.ctx()).text_dim));
    }
    preview(app, d);
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn setting_an_empty_path_changes_nothing() {
        let mut d = Dialog::new(ID, json!({"fx": {"dropShadow": {"distance": 7.0}}}));
        set(&mut d, &[], json!(1));
        assert_eq!(d.fields.get("fx"), Some(&json!({"dropShadow": {"distance": 7.0}})));
        set(&mut d, &["dropShadow", "size"], json!(3.0));
        assert_eq!(num(&d, &["dropShadow", "size"]), 3.0);
    }

    fn app_with_frame() -> (DesignApp, u64) {
        let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
        app.run("file.new", json!({})).unwrap();
        app.run("document.preferences", json!({"horizontalUnits": "inches", "verticalUnits": "inches"})).unwrap();
        let id = app.run("frame.create", json!({"rect": [100, 100, 300, 300]})).unwrap()["id"].as_u64().unwrap();
        app.run("object.fill", json!({"swatch": "[Paper]"})).unwrap();
        app.run("selection.set", json!({"ids": [id]})).unwrap();
        (app, id)
    }

    fn frame(app: &mut DesignApp, ctx: &egui::Context) -> Vec<String> {
        let mut texts = Vec::new();
        for _ in 0..2 {
            let input =
                egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1400.0, 900.0))), ..Default::default() };
            let mut out = ctx.run_ui(input, |ui| crate::dialogs::show(app, ui.ctx()));
            out.textures_delta.clear();
            fn walk(s: egui::epaint::Shape, texts: &mut Vec<String>) {
                match s {
                    egui::epaint::Shape::Text(t) => texts.push(t.galley.text().to_string()),
                    egui::epaint::Shape::Vec(v) => v.into_iter().for_each(|s| walk(s, texts)),
                    _ => {}
                }
            }
            texts.clear();
            for c in out.shapes {
                walk(c.shape, &mut texts);
            }
        }
        texts
    }

    /// With Use Global Light on, the Angle is the document's global light and stays editable:
    /// a change there is applied to the document with the effects (it was greyed out).
    #[test]
    fn the_angle_with_global_light_sets_the_global_light() {
        let (mut app, _) = app_with_frame();
        let ctx = egui::Context::default();
        crate::theme::install_fonts(&ctx, "en");
        crate::menus::activate(&mut app, "object.dropShadow", &Value::Null);
        let d = app.ui.dialog.as_mut().unwrap();
        set(d, &["dropShadow", "globalLight"], json!(true));
        frame(&mut app, &ctx);
        // The field shows the global angle and is enabled: a DragValue that isn't disabled.
        let global = app.session.doc().unwrap().doc.settings.global_light;
        let mut d = app.ui.dialog.clone().unwrap();
        let ui_ctx = egui::Context::default();
        let mut enabled = false;
        let mut out = ui_ctx.run_ui(egui::RawInput::default(), |ui| {
            enabled = angle(ui, &mut d, &["dropShadow", "angle"], false, global).enabled();
        });
        out.textures_delta.clear();
        assert!(enabled, "the Angle stays editable with Use Global Light");
        d.fields.insert(GLOBAL_LIGHT.into(), json!(15.0));
        confirm(&mut app, &d).unwrap();
        assert_eq!(app.session.doc().unwrap().doc.settings.global_light, 15.0);
    }

    #[test]
    fn set_makes_the_path_and_ignores_an_empty_one() {
        let mut d = Dialog::new(ID, json!({"fx": {}}));
        set(&mut d, &["featherOptions", "choke"], json!(20));
        assert_eq!(d.fields["fx"]["featherOptions"]["choke"], json!(20));
        let before = d.fields.clone();
        set(&mut d, &[], json!(1));
        assert_eq!(d.fields, before, "an empty path changes nothing (and doesn't panic)");
    }

    #[test]
    fn the_effects_dialog_opens_on_the_chosen_effect_previews_and_applies_once() {
        let (mut app, id) = app_with_frame();
        let ctx = egui::Context::default();
        crate::theme::install_fonts(&ctx, "en");
        let undo = |app: &DesignApp| app.session.doc().unwrap().history.undo.len();
        let fx = |app: &DesignApp| app.session.doc().unwrap().doc.item(designcraft_doc::ItemId(id)).unwrap().effects.clone();
        let u0 = undo(&app);
        // Object › Effects › Inner Shadow… (as the menu activates it): the dialog, on that page.
        crate::menus::activate(&mut app, "object.innerShadow", &Value::Null);
        assert_eq!(app.ui.dialog.as_ref().map(|d| d.id.as_str()), Some(ID));
        let texts = frame(&mut app, &ctx);
        for t in [
            "Effects",
            "Settings for:",
            "Transparency",
            "Inner Shadow",
            "Drop Shadow",
            "Blending",
            "Position",
            "Distance:",
            "Angle:",
            "Use Global Light",
            "Options",
            "Size:",
            "Choke:",
            "Preview",
            "OK",
            "Cancel",
        ] {
            assert!(texts.iter().any(|x| x.trim() == t), "{t}: {texts:?}");
        }
        assert!(texts.iter().any(|x| x.contains("0.0972 in")), "distance in inches: {texts:?}");
        // Preview: the shadow is on the page while the dialog is open, without an undo step.
        frame(&mut app, &ctx);
        assert!(fx(&app).inner_shadow.on);
        assert_eq!(undo(&app), u0);
        // Cancel: as before.
        crate::dialogs::cancel(&mut app);
        assert!(!fx(&app).inner_shadow.on);
        assert_eq!(undo(&app), u0);
        // Again, with a change, then OK: one undo step.
        crate::menus::activate(&mut app, "object.innerShadow", &Value::Null);
        frame(&mut app, &ctx);
        if let Some(d) = app.ui.dialog.as_mut() {
            set(d, &["innerShadow", "size"], json!(9.0));
            d.fields.insert("opacity".into(), json!(0.8));
        }
        frame(&mut app, &ctx);
        crate::dialogs::confirm(&mut app).unwrap();
        let e = fx(&app);
        assert!(e.inner_shadow.on && e.inner_shadow.size == 9.0);
        assert_eq!(undo(&app), u0 + 1, "one undo step");
        assert!((app.session.doc().unwrap().doc.item(designcraft_doc::ItemId(id)).unwrap().opacity - 0.8).abs() < 1e-6);
        app.run("edit.undo", json!({})).unwrap();
        assert!(!fx(&app).inner_shadow.on);
    }

    /// The angle dial points where the pointer is, as InDesign's: right is 0°, up 90°, left 180°,
    /// down −90°, upper left 135°.
    #[test]
    fn the_angle_dial_points_at_the_pointer() {
        let c = egui::pos2(100.0, 100.0);
        assert_eq!(dial_angle(c, egui::pos2(120.0, 100.0)), Some(0.0));
        assert_eq!(dial_angle(c, egui::pos2(100.0, 80.0)), Some(90.0));
        assert_eq!(dial_angle(c, egui::pos2(80.0, 100.0)), Some(180.0));
        assert_eq!(dial_angle(c, egui::pos2(100.0, 120.0)), Some(-90.0));
        assert_eq!(dial_angle(c, egui::pos2(90.0, 90.0)), Some(135.0));
        assert_eq!(dial_angle(c, egui::pos2(101.0, 100.0)), None, "too close to the centre");
    }

    /// Without a pointer: Tab focuses the dial, the arrows turn it (Shift: 10°), wrapping at 180°.
    #[test]
    fn the_angle_dial_turns_with_the_keyboard() {
        let ctx = egui::Context::default();
        let key = |key, shift| egui::Event::Key {
            key,
            physical_key: None,
            pressed: true,
            repeat: false,
            modifiers: if shift { egui::Modifiers::SHIFT } else { egui::Modifiers::NONE },
        };
        let mut deg = 175.0;
        let frame = |events: Vec<egui::Event>, deg: &mut f64| {
            let mut out = ctx.run_ui(egui::RawInput { events, ..Default::default() }, |ui| {
                if let Some(a) = angle_dial(ui, *deg, true) {
                    *deg = a;
                }
            });
            out.textures_delta.clear();
        };
        frame(vec![], &mut deg);
        frame(vec![key(egui::Key::Tab, false)], &mut deg);
        frame(vec![key(egui::Key::ArrowRight, false)], &mut deg);
        assert_eq!(deg, 176.0);
        frame(vec![key(egui::Key::ArrowUp, true)], &mut deg);
        assert_eq!(deg, -174.0, "past 180° it comes round to −174°");
        frame(vec![key(egui::Key::ArrowLeft, true)], &mut deg);
        frame(vec![key(egui::Key::ArrowDown, false)], &mut deg);
        assert_eq!(deg, 175.0);
        assert_eq!(wrap_angle(-180.0), 180.0);
        assert_eq!(wrap_angle(540.0), 180.0);
    }

    /// Drop Shadow and Outer Glow have InDesign's Mode under Blending (Multiply and Screen to
    /// start); a mode picked there is what OK applies.
    #[test]
    fn shadows_and_glows_have_a_blend_mode() {
        let (mut app, id) = app_with_frame();
        let ctx = egui::Context::default();
        crate::theme::install_fonts(&ctx, "en");
        open(&mut app, "dropShadow");
        let texts = frame(&mut app, &ctx);
        assert!(texts.iter().any(|x| x.trim() == "Mode:") && texts.iter().any(|x| x.trim() == "Multiply"), "{texts:?}");
        crate::dialogs::cancel(&mut app);
        open(&mut app, "outerGlow");
        let texts = frame(&mut app, &ctx);
        assert!(texts.iter().any(|x| x.trim() == "Screen"), "{texts:?}");
        if let Some(d) = app.ui.dialog.as_mut() {
            set(d, &["outerGlow", "mode"], json!("Normal"));
        }
        frame(&mut app, &ctx);
        crate::dialogs::confirm(&mut app).unwrap();
        let fx = app.session.doc().unwrap().doc.item(designcraft_doc::ItemId(id)).unwrap().effects.clone();
        assert!(fx.outer_glow.on);
        assert_eq!(fx.outer_glow.mode, designcraft_color::BlendMode::Normal);
    }

    #[test]
    fn offsets_follow_distance_and_angle_as_in_indesign() {
        let (mut app, _) = app_with_frame();
        open(&mut app, "dropShadow");
        let d = app.ui.dialog.as_mut().unwrap();
        set(d, &["dropShadow", "distance"], json!(7.0));
        set(d, &["dropShadow", "angle"], json!(120.0));
        // InDesign shows 7 pt at 120° as X 0.0486 in, Y 0.0842 in.
        let a = 120f64.to_radians();
        assert!(((-7.0 * a.cos()) / 72.0 - 0.0486).abs() < 1e-4 && ((7.0 * a.sin()) / 72.0 - 0.0842).abs() < 1e-4);
        let p = params(d);
        assert_eq!(p["effects"]["dropShadow"]["on"], true, "chosen from the menu: on");
        assert_eq!(summary("en", d), "OBJECT: Normal 100%; Drop Shadow");
    }
}