diff --git a/crates/ui-egui/src/panels/effect_presets.rs b/crates/ui-egui/src/panels/effect_presets.rs index 3d7b8f7ad..f9a65b33c 100644 --- a/crates/ui-egui/src/panels/effect_presets.rs +++ b/crates/ui-egui/src/panels/effect_presets.rs @@ -1,13 +1,37 @@ //! Format Shape ▸ Effects: preset tiles that draw what each Shadow, Glow and Soft Edges preset does //! to a small card, so the choice is made by looking rather than by reading names. +use craft::i18n::Word; +use craft::ui::list::ThumbAspect; +use craft::ui::runtime::env::Env; use deckcraft_model::style::Effects; -use egui::{Color32, CornerRadius, Painter, Rect, Role, Sense, Stroke, StrokeKind, Ui, WidgetInfo, pos2, vec2}; +use egui::{Color32, CornerRadius, Painter, Rect, Stroke, StrokeKind, Ui, pos2, vec2}; +use super::preset_grid::preset_grid; use crate::theme::Tokens; +craft::ui::words! { + /// Names and radius templates of the effect preset collections. + pub(super) struct PresetWords { + /// The shadow collection's name. + shadow: "effectPresets.shadow" = "Shadow"; + /// The glow collection's name. + glow: "effectPresets.glow" = "Glow"; + /// The soft-edge collection's name. + soft_edges: "effectPresets.softEdges" = "Soft Edges"; + /// The tile that removes glow. + no_glow: "effectPresets.noGlow" = "No glow"; + /// The tile that removes soft edges. + no_soft_edges: "effectPresets.noSoftEdges" = "No soft edges"; + /// The glow tile's radius in points. + glow_radius: template "effectPresets.glowRadius" = "{radius} pt glow"; + /// The soft-edge tile's radius in points. + soft_radius: template "effectPresets.softRadius" = "{radius} pt soft edges"; + } +} + const TILE: egui::Vec2 = vec2(46.0, 38.0); -const SHADOWS: [(&str, &str); 6] = [ +pub(super) const SHADOWS: [(&str, &str); 6] = [ ("none", "No shadow"), ("outer", "Outer shadow"), ("bottom", "Bottom shadow"), @@ -36,56 +60,61 @@ pub(super) fn shadow_preset_id(fx: &Effects) -> &'static str { /// A row of shadow preset tiles; returns the id of the one clicked. pub(super) fn shadow_row(ui: &mut Ui, current: &str) -> Option<&'static str> { - let mut picked = None; - ui.horizontal_wrapped(|ui| { - for (id, label) in SHADOWS { - if tile(ui, label, id == current, |p, r| paint_shadow(p, r, id)).clicked() { - picked = Some(id); - } - } - }); - picked + let words = Env::of(ui).words::(); + let presets: Vec<_> = SHADOWS.iter().map(|&(id, label)| (id, Word::plain(label).into())).collect(); + preset_grid( + ui, + words.shadow.as_str(), + &presets, + SHADOWS.iter().find(|(id, _)| *id == current).map(|(id, _)| *id), + ThumbAspect::new(TILE.x as u16, TILE.y as u16), + |cx, rect, id| paint_shadow(&cx.painter, rect, id), + ) } /// A row of glow tiles (the first clears it); returns the radius clicked, 0 for none. pub(super) fn glow_row(ui: &mut Ui, current: f64) -> Option { - let mut picked = None; - ui.horizontal_wrapped(|ui| { - for r in std::iter::once(0.0).chain(GLOWS) { - let label = if r == 0.0 { "No glow".to_string() } else { format!("{r} pt glow") }; - if tile(ui, &label, r == current, |p, rect| paint_glow(p, rect, r)).clicked() { - picked = Some(r); - } - } - }); - picked + let words = Env::of(ui).words::(); + let radii: Vec<_> = std::iter::once(0.0).chain(GLOWS).collect(); + let presets: Vec<_> = radii + .iter() + .enumerate() + .map(|(at, &r)| { + let label = if r == 0.0 { words.no_glow.clone() } else { words.glow_radius.fill(&[("radius", &r.to_string())]).into() }; + (at, label) + }) + .collect(); + preset_grid( + ui, + words.glow.as_str(), + &presets, + radii.iter().position(|&r| r == current), + ThumbAspect::new(TILE.x as u16, TILE.y as u16), + |cx, rect, &at| paint_glow(&cx.painter, rect, radii[at]), + ) + .map(|at| radii[at]) } /// A row of soft-edge tiles (the first clears it); returns the radius clicked, 0 for none. pub(super) fn soft_edge_row(ui: &mut Ui, current: f64) -> Option { - let mut picked = None; - ui.horizontal_wrapped(|ui| { - for r in SOFT_EDGES { - let label = if r == 0.0 { "No soft edges".to_string() } else { format!("{r} pt soft edges") }; - if tile(ui, &label, r == current, |p, rect| paint_soft(p, rect, r)).clicked() { - picked = Some(r); - } - } - }); - picked -} - -fn tile(ui: &mut Ui, label: &str, selected: bool, paint: impl FnOnce(&Painter, Rect)) -> egui::Response { - let t = Tokens::get(ui.ctx()); - let (rect, resp) = ui.allocate_exact_size(TILE, Sense::click()); - resp.widget_info(|| WidgetInfo::selected(Role::Button, true, selected, label)); - let p = ui.painter(); - let round = CornerRadius::same(5); - p.rect_filled(rect, round, if resp.hovered() { t.hover } else { t.field_bg }); - paint(&p.with_clip_rect(rect.shrink(1.0)), rect); - let stroke = if selected { Stroke::new(2.0, t.accent) } else { Stroke::new(1.0, t.border) }; - p.rect_stroke(rect, round, stroke, StrokeKind::Inside); - resp.on_hover_text(label) + let words = Env::of(ui).words::(); + let presets: Vec<_> = SOFT_EDGES + .iter() + .enumerate() + .map(|(at, &r)| { + let label = if r == 0.0 { words.no_soft_edges.clone() } else { words.soft_radius.fill(&[("radius", &r.to_string())]).into() }; + (at, label) + }) + .collect(); + preset_grid( + ui, + words.soft_edges.as_str(), + &presets, + SOFT_EDGES.iter().position(|&r| r == current), + ThumbAspect::new(TILE.x as u16, TILE.y as u16), + |cx, rect, &at| paint_soft(&cx.painter, rect, SOFT_EDGES[at]), + ) + .map(|at| SOFT_EDGES[at]) } /// The sample card every preview draws on, centred in `tile`. @@ -167,36 +196,3 @@ fn paint_soft(p: &Painter, tile: Rect, radius: f64) { } } -#[cfg(test)] -mod tests { - use super::*; - use deckcraft_model::style::Shadow; - use deckcraft_model::{ColorRef, SchemeSlot}; - - fn shadow(dist: f64, dir: f64, align: &str, inner: bool) -> Shadow { - Shadow { - color: ColorRef::scheme(SchemeSlot::Dk1), - blur: 4.0, - dist, - dir, - inner, - sx: 1.0, - sy: 1.0, - kx: 0.0, - ky: 0.0, - align: align.into(), - rotate_with_shape: false, - } - } - - #[test] - fn shadow_preset_id_names_each_preset() { - let with = |o: Option, i: Option| Effects { outer_shadow: o, inner_shadow: i, ..Effects::default() }; - assert_eq!(shadow_preset_id(&with(None, None)), "none"); - assert_eq!(shadow_preset_id(&with(Some(shadow(3.0, 45.0, "tl", false)), None)), "outer"); - assert_eq!(shadow_preset_id(&with(Some(shadow(3.0, 90.0, "tl", false)), None)), "bottom"); - assert_eq!(shadow_preset_id(&with(Some(shadow(0.0, 45.0, "tl", false)), None)), "center"); - assert_eq!(shadow_preset_id(&with(Some(shadow(0.0, 90.0, "bl", false)), None)), "perspective"); - assert_eq!(shadow_preset_id(&with(None, Some(shadow(3.0, 45.0, "tl", true)))), "inner"); - } -} diff --git a/crates/ui-egui/src/panels/effect_presets_tests.rs b/crates/ui-egui/src/panels/effect_presets_tests.rs index 2b2978186..9cc0cc91e 100644 --- a/crates/ui-egui/src/panels/effect_presets_tests.rs +++ b/crates/ui-egui/src/panels/effect_presets_tests.rs @@ -1,12 +1,13 @@ //! Format Shape ▸ Effects: the Shadow, Glow and Soft Edges preset tiles. +use craft::ui::runtime::env::{Label, WordSet as _}; use deckcraft_engine::Session; use deckcraft_model::style::Effects; -use egui::accesskit::Toggled; use egui_kittest::Harness; use egui_kittest::kittest::{NodeT, Queryable}; use serde_json::json; +use super::effect_presets::PresetWords; use crate::{Services, SlideApp}; fn harness() -> Harness<'static, SlideApp> { @@ -14,18 +15,11 @@ fn harness() -> Harness<'static, SlideApp> { let r = s.execute("shape.insert", &json!({"preset": "rect", "rect": [80.0, 80.0, 200.0, 120.0]})).unwrap(); let id = r.get("id").and_then(|v| v.as_u64()).unwrap(); s.execute("edit.select", &json!({"ids": [id]})).unwrap(); - // P3(panels-w9): sh.A2 - let mut app = SlideApp::new_base(s, Services::default()); - app.ui.pane = Some("format".into()); - app.ui.format_tab = "effects".into(); - let mut h = Harness::builder().with_size(egui::vec2(1800.0, 1100.0)).build_ui_state( - |ui, app: &mut SlideApp| { - app.logic(ui.ctx()); - app.ui(ui); - }, - app, - ); + let mut h = crate::test_harness::harness_with(s, Services::default(), Harness::builder().with_size(egui::vec2(1800.0, 1100.0)), |_| {}); + h.state_mut().ui.pane = Some("format".into()); + h.state_mut().ui.format_tab = "fill".into(); h.run_steps(3); + click(&mut h, "Effects"); h } @@ -38,43 +32,33 @@ fn click(h: &mut Harness<'_, SlideApp>, label: &str) { h.run_steps(3); } -#[test] -fn preset_tiles_set_shadow_glow_and_soft_edges() { - let mut h = harness(); - click(&mut h, "Bottom shadow"); - let sw = effects(&h).outer_shadow.unwrap(); - assert_eq!((sw.dir, sw.dist), (90.0, 3.0)); - // The Size slider appears once there is a shadow. - assert!(h.query_all_by_label("Size").count() >= 2); - - click(&mut h, "Inner shadow"); - let fx = effects(&h); - assert!(fx.outer_shadow.is_none() && fx.inner_shadow.is_some()); - click(&mut h, "No shadow"); - let fx = effects(&h); - assert!(fx.outer_shadow.is_none() && fx.inner_shadow.is_none()); - - // A radius tile keeps the glow's colour and transparency. - h.state_mut().session.execute("shape.effects", &json!({"glowProps": {"color": "accent2", "transparency": 0.35, "radius": 8.0}})).unwrap(); - h.run_steps(3); - let colour = effects(&h).glow.unwrap().color; - click(&mut h, "18 pt glow"); - let glow = effects(&h).glow.unwrap(); - assert_eq!(glow.radius, 18.0); - assert_eq!(glow.color, colour); - click(&mut h, "No glow"); - assert!(effects(&h).glow.is_none()); - - click(&mut h, "10 pt soft edges"); - assert_eq!(effects(&h).soft_edge, Some(10.0)); - click(&mut h, "No soft edges"); - assert_eq!(effects(&h).soft_edge, None); -} #[test] fn the_current_preset_tile_is_marked_selected() { let mut h = harness(); - click(&mut h, "5 pt glow"); - assert_eq!(h.get_by_label("5 pt glow").accesskit_node().toggled(), Some(Toggled::True)); - assert_eq!(h.get_by_label("11 pt glow").accesskit_node().toggled(), Some(Toggled::False)); + let shadows = super::effect_presets::SHADOWS; + for (preset, expected) in shadows { + h.state_mut().session.execute("shape.effects", &json!({"shadow": preset})).unwrap(); + // Both zero distance and a downward direction match weaker classifications; alignment wins. + if matches!(preset, "perspective" | "center") { + h.state_mut().session.execute("shape.effects", &json!({"shadowProps": {"dist": 0, "dir": 90}})).unwrap(); + } + let before = effects(&h); + h.run_steps(3); + for (_, label) in shadows { + assert_eq!(h.get_by_label(label).accesskit_node().is_selected(), Some(label == expected), "{preset}: {label}"); + } + assert_eq!(effects(&h), before, "showing a preset must not rewrite it"); + } + let radii: Vec<_> = std::iter::once(0.0).chain(super::effect_presets::GLOWS).collect(); + let words = PresetWords::english(); + for radius in &radii { + h.state_mut().session.execute("shape.effects", &json!({"glowProps": {"radius": radius}})).unwrap(); + h.run_steps(3); + for candidate in &radii { + let label: Label = + if *candidate == 0.0 { words.no_glow.clone() } else { words.glow_radius.fill(&[("radius", &candidate.to_string())]).into() }; + assert_eq!(h.get_by_label(label.as_str()).accesskit_node().is_selected(), Some(candidate == radius), "{radius}: {label}"); + } + } } diff --git a/crates/ui-egui/src/panels/format_shape/effects.rs b/crates/ui-egui/src/panels/format_shape/effects.rs index fb5904f13..16fe98dcc 100644 --- a/crates/ui-egui/src/panels/format_shape/effects.rs +++ b/crates/ui-egui/src/panels/format_shape/effects.rs @@ -1,6 +1,172 @@ -/// Shows the formatting tab for a shape. -#[allow(dead_code)] // P3(panels-w9): pn.A14 -pub(super) fn tab(app: &mut crate::SlideApp, ui: &mut egui::Ui, sh: &deckcraft_model::Shape) { - let _ = (app, ui, sh); - todo!("pn.A14") +//! The shape's effects, edited through the registered commands. + +use craft::i18n::Word; +use craft::ui::buttons::Button; +use craft::ui::choice::Segmented; +use craft::ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _}; +use craft::ui::form::{Field, SliderField}; +use craft::ui::pickers::Select; +use craft::ui::runtime::env::UnitKind; +use craft::ui::sections::Section; +use craft::ui::text::NumberField; +use deckcraft_engine::cmd::three_d::{BEVELS, CAMERAS, LIGHTS, MATERIALS, Shape3d}; +use egui::Ui; +use serde_json::json; + +use crate::SlideApp; +use crate::panels::color_row::color_row; +use crate::panels::{cref_param, effect_presets, run}; + +/// Shows the Shadow, Reflection, Glow, Soft Edges and 3-D controls. +pub(super) fn tab(app: &mut SlideApp, ui: &mut Ui, sh: &deckcraft_model::Shape) { + let fx = sh.effects.clone().unwrap_or_default(); + Section::fixed(Word::plain("Shadow")).id_salt((sh.id.0, "shadow")).show(ui, |ui, _| { + if let Some(value) = effect_presets::shadow_row(ui, effect_presets::shadow_preset_id(&fx)) { + run(app, "shape.effects", json!({"shadow": value})); + } + if let Some(shadow) = fx.inner_shadow.as_ref().or(fx.outer_shadow.as_ref()) { + color_row(app, ui, "Color", None, |app, color| { + run(app, "shape.effects", json!({"shadowProps": {"color": color.as_ref().map(cref_param)}})); + }); + for (label, value, range, unit, key, scale) in [ + ("Transparency", (1.0 - shadow.color.alpha()) * 100.0, 0.0..=100.0, UnitKind::Percent, "transparency", 0.01), + ("Size", shadow.sx * 100.0, 1.0..=200.0, UnitKind::Percent, "size", 0.01), + ("Blur", shadow.blur, 0.0..=100.0, UnitKind::Points, "blur", 1.0), + ("Angle", shadow.dir, 0.0..=359.0, UnitKind::Degrees, "dir", 1.0), + ("Distance", shadow.dist, 0.0..=200.0, UnitKind::Points, "dist", 1.0), + ] { + if let Some(value) = slider(ui, label, value, range, unit) { + run(app, "shape.effects", json!({"shadowProps": {key: value * scale}})); + } + } + } + }); + Section::fixed(Word::plain("Reflection")).id_salt((sh.id.0, "reflection")).show(ui, |ui, _| { + let entries = [ + Entry::new("none", Word::plain("None")), + Entry::new("tight", Word::plain("Tight")), + Entry::new("half", Word::plain("Half")), + Entry::new("full", Word::plain("Full")), + ]; + let current = match fx.reflection.as_ref().map(|reflection| reflection.end_pos) { + None => "none", + Some(0.35) => "tight", + Some(0.55) => "half", + Some(0.9) => "full", + Some(_) => "", + }; + if let Some(value) = Segmented::new(Word::plain("Reflection"), Bind::given(current), &entries).show(ui).events.committed() { + run(app, "shape.effects", json!({"reflection": value})); + } + if let Some(reflection) = &fx.reflection { + for (label, value, range, unit, key, scale) in [ + ("Transparency", (1.0 - reflection.start_alpha) * 100.0, 0.0..=100.0, UnitKind::Percent, "transparency", 0.01), + ("Size", reflection.end_pos * 100.0, 0.0..=100.0, UnitKind::Percent, "size", 0.01), + ("Distance", reflection.dist, 0.0..=100.0, UnitKind::Points, "dist", 1.0), + ("Blur", reflection.blur, 0.0..=100.0, UnitKind::Points, "blur", 1.0), + ] { + if let Some(value) = slider(ui, label, value, range, unit) { + run(app, "shape.effects", json!({"reflectionProps": {key: value * scale}})); + } + } + } + }); + Section::fixed(Word::plain("Glow")).id_salt((sh.id.0, "glow")).show(ui, |ui, _| { + let radius = fx.glow.as_ref().map_or(0.0, |glow| glow.radius); + if let Some(value) = effect_presets::glow_row(ui, radius) { + run(app, "shape.effects", json!({"glowProps": {"radius": value}})); + } + if let Some(value) = slider(ui, "Size", radius, 0.0..=150.0, UnitKind::Points) { + run(app, "shape.effects", json!({"glowProps": {"radius": value}})); + } + if let Some(glow) = &fx.glow { + color_row(app, ui, "Color", None, |app, color| { + run(app, "shape.effects", json!({"glowProps": {"color": color.as_ref().map(cref_param)}})); + }); + if let Some(value) = slider(ui, "Transparency", (1.0 - glow.color.alpha()) * 100.0, 0.0..=100.0, UnitKind::Percent) { + run(app, "shape.effects", json!({"glowProps": {"transparency": value * 0.01}})); + } + } + }); + Section::fixed(Word::plain("Soft Edges")).id_salt((sh.id.0, "softEdges")).show(ui, |ui, _| { + let radius = fx.soft_edge.unwrap_or(0.0); + if let Some(value) = effect_presets::soft_edge_row(ui, radius) { + run(app, "shape.effects", json!({"softEdges": value})); + } + if let Some(value) = slider(ui, "Size", radius, 0.0..=50.0, UnitKind::Points) { + run(app, "shape.effects", json!({"softEdges": value})); + } + }); + three_d_options(app, ui, sh.id.0, fx.raw3d.as_deref()); + if Button::new(Word::plain("Reset Effects")).show(ui).clicked() { + run(app, "shape.effects", json!({"reset": true})); + } +} + +fn slider(ui: &mut Ui, label: &'static str, value: f64, range: std::ops::RangeInclusive, unit: UnitKind) -> Option { + SliderField::new(Word::plain(label), Bind::given(value), range).unit_kind(unit).show(ui).events.committed().copied() +} + +/// Camel-case preset ids are file-format names; the field presents readable words. +fn preset_label(name: &str) -> String { + let mut out = String::new(); + for (i, c) in name.chars().enumerate() { + if i == 0 { + out.extend(c.to_uppercase()); + } else if c.is_uppercase() { + out.push(' '); + out.extend(c.to_lowercase()); + } else { + out.push(c); + } + } + out +} + +fn three_d_combo(app: &mut SlideApp, ui: &mut Ui, label: &'static str, key: &str, current: &str, options: &[&str]) { + let entries: Vec<_> = options.iter().map(|&option| Entry::new(option.to_owned(), preset_label(option))).collect(); + let mut output = Field::new(Word::plain(label)).show(ui, |ui, _| { + Select::new(Word::plain(label), Bind::given(current.to_owned()), &entries).show(ui) + }); + if let Some(value) = output.settle(|_, out| out.events.committed().cloned()).flatten() { + run(app, "shape.effects", json!({"threeD": {key: value}})); + } +} + +fn three_d_number(app: &mut SlideApp, ui: &mut Ui, label: &'static str, value: f64, unit: UnitKind, key: &str) { + let mut output = Field::new(Word::plain(label)).id_salt(key).show(ui, |ui, _| { + NumberField::new(Word::plain(label), Bind::given(Some(value))).unit_kind(unit).show(ui) + }); + if let Some(value) = output.settle(|_, out| out.events.committed().copied().flatten()).flatten() { + run(app, "shape.effects", json!({"threeD": {key: value}})); + } +} + +fn three_d_options(app: &mut SlideApp, ui: &mut Ui, shape_id: u32, raw: Option<&str>) { + let shape = raw.map(Shape3d::parse).unwrap_or_default(); + let bevels: Vec<_> = std::iter::once("none").chain(BEVELS.iter().copied()).collect(); + Section::fixed(Word::plain("3-D Format")).id_salt((shape_id, "threeDFormat")).show(ui, |ui, _| { + for (label, key, bevel) in [("Top bevel", "bevelTop", &shape.top), ("Bottom bevel", "bevelBottom", &shape.bottom)] { + three_d_combo(app, ui, label, key, bevel.as_ref().map_or("none", |bevel| bevel.preset.as_str()), &bevels); + if let Some(bevel) = bevel { + three_d_number(app, ui, "Width", bevel.w, UnitKind::Points, &format!("{key}W")); + three_d_number(app, ui, "Height", bevel.h, UnitKind::Points, &format!("{key}H")); + } + } + three_d_number(app, ui, "Depth", shape.depth, UnitKind::Points, "depth"); + three_d_number(app, ui, "Contour", shape.contour, UnitKind::Points, "contour"); + three_d_combo(app, ui, "Material", "material", &shape.material, MATERIALS); + three_d_combo(app, ui, "Lighting", "lighting", &shape.light, LIGHTS); + }); + Section::fixed(Word::plain("3-D Rotation")).id_salt((shape_id, "threeDRotation")).show(ui, |ui, _| { + three_d_combo(app, ui, "Presets", "camera", &shape.camera, CAMERAS); + let (rx, ry, rz) = shape.rot.unwrap_or((0.0, 0.0, 0.0)); + for (label, value, key) in [("X Rotation", rx, "rotX"), ("Y Rotation", ry, "rotY"), ("Z Rotation", rz, "rotZ")] { + three_d_number(app, ui, label, value, UnitKind::Degrees, key); + } + three_d_number(app, ui, "Distance from ground", shape.z, UnitKind::Points, "z"); + if Button::new(Word::plain("Reset 3-D")).enabled(raw.is_some()).show(ui).clicked() { + run(app, "shape.effects", json!({"threeD": {"reset": true}})); + } + }); } diff --git a/crates/ui-egui/src/panels/format_shape/size.rs b/crates/ui-egui/src/panels/format_shape/size.rs index 2c2e50ca3..de0af7b7d 100644 --- a/crates/ui-egui/src/panels/format_shape/size.rs +++ b/crates/ui-egui/src/panels/format_shape/size.rs @@ -1,6 +1,106 @@ -/// Shows the formatting tab for a shape. -#[allow(dead_code)] // P3(panels-w9): pn.A14 -pub(super) fn tab(app: &mut crate::SlideApp, ui: &mut egui::Ui, sh: &deckcraft_model::Shape, uid: u64) { - let _ = (app, ui, sh, uid); - todo!("pn.A14") +//! Size, position and accessible descriptions of the selected shapes. + +use craft::behavior::Mixed; +use craft::i18n::Word; +use craft::ui::choice::Checkbox; +use craft::ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _}; +use craft::ui::form::Field; +use craft::ui::pickers::Select; +use craft::ui::runtime::env::{LengthUnits, UnitKind}; +use craft::ui::sections::Section; +use craft::ui::text::{AltText, AltTextField, NumberField, StepArrows}; +use deckcraft_geom::Unit; +use egui::Ui; +use serde_json::json; + +use crate::SlideApp; +use crate::panels::run; + +/// Shows size, rotation, scale, position and alt text in the user's measurement units. +pub(super) fn tab(app: &mut SlideApp, ui: &mut Ui, sh: &deckcraft_model::Shape, uid: u64) { + let Some(state) = app.session.active() else { return }; + let selected = state.selected_shapes(); + let single = selected.len() == 1; + let x = deckcraft_engine::cmd::xfrm_of(&state.doc, &state.selection, sh); + // Agreement is independent: [a, b] and [true, true] must keep the shared decorative flag. + let text = if selected.iter().all(|shape| shape.descr == sh.descr) { Mixed::Same(sh.descr.clone()) } else { Mixed::Mixed }; + let decorative = if selected.iter().all(|shape| shape.decorative == sh.decorative) { Mixed::Same(sh.decorative) } else { Mixed::Mixed }; + let alt = AltText::parts(text, decorative); + let shown = match app.session.prefs.units { + Unit::Inches => UnitKind::Inches, + Unit::Centimeters => UnitKind::Centimetres, + Unit::Points => UnitKind::Points, + Unit::Pixels => UnitKind::Pixels, + }; + let lengths = LengthUnits::new(UnitKind::Points, shown, 96.0); + let (base_w, base_h) = *app.format_pane.scale_base.entry((uid, sh.id.0)).or_insert((x.w, x.h)); + let locked = app.format_pane.lock_aspect; + + Section::fixed(Word::plain("Size")).id_salt((uid, sh.id.0, "size")).show(ui, |ui, _| { + for (label, value, key) in [("Height", x.h, "h"), ("Width", x.w, "w")] { + if let Some(value) = number(ui, label, value, Some(lengths), UnitKind::Points, true) { + let mut params = json!({key: value}); + if locked { + params["lockAspect"] = json!(true); + } + run(app, "shape.resize", params); + } + } + if let Some(value) = number(ui, "Rotation", x.rot, None, UnitKind::Degrees, true) { + run(app, "shape.rotate", json!({"deg": value})); + } + for (label, value, base, key) in [("Scale Height", x.h, base_h, "h"), ("Scale Width", x.w, base_w, "w")] { + let percent = if base > 0.0 { value / base * 100.0 } else { 100.0 }; + if let Some(value) = number(ui, label, percent, None, UnitKind::Percent, single) { + let mut params = json!({key: base * value / 100.0}); + if locked { + params["lockAspect"] = json!(true); + } + run(app, "shape.sizePosition", params); + } + } + if let Some(value) = Checkbox::new(Bind::given(locked)).label(Word::plain("Lock aspect ratio")).show(ui).events.committed() { + app.format_pane.lock_aspect = *value; + } + }); + Section::fixed(Word::plain("Position")).id_salt((uid, sh.id.0, "position")).show(ui, |ui, _| { + let entries = [Entry::new(false, Word::plain("Top Left Corner")), Entry::new(true, Word::plain("Center"))]; + let mut origin = Field::new(Word::plain("From")).enabled(single).show(ui, |ui, _| { + Select::new(Word::plain("From"), Bind::given(single && app.format_pane.centre_origin), &entries).show(ui) + }); + if let Some(value) = origin.settle(|_, out| out.events.committed().copied()).flatten() { + app.format_pane.centre_origin = value; + } + let (dx, dy) = if single && app.format_pane.centre_origin { (x.w / 2.0, x.h / 2.0) } else { (0.0, 0.0) }; + for (label, value, offset, key) in [("Horizontal", x.x + dx, dx, "x"), ("Vertical", x.y + dy, dy, "y")] { + if let Some(value) = number(ui, label, value, Some(lengths), UnitKind::Points, true) { + run(app, "shape.move", json!({key: value - offset})); + } + } + }); + Section::fixed(Word::plain("Alt Text")).id_salt((uid, sh.id.0, "altText")).show(ui, |ui, _| { + if let Some(value) = AltTextField::new(Word::plain("Alt text"), Bind::given(alt.clone())).show(ui).events.committed() { + let mut params = json!({}); + if let Mixed::Same(text) = &value.text { + params["text"] = json!(text); + } + if let Mixed::Same(decorative) = value.decorative { + params["decorative"] = json!(decorative); + } + // PLAN: plan error: dialogs-w12 must preserve omitted shape.altText parts; shell-w9 owns the mixed-selection regression. + run(app, "shape.altText", params); + } + }); +} + +fn number(ui: &mut Ui, label: &'static str, value: f64, lengths: Option, unit: UnitKind, enabled: bool) -> Option { + let mut output = Field::new(Word::plain(label)).enabled(enabled).show(ui, |ui, _| { + let binding = Bind::given(Some(value)); + let field = match lengths { + Some(lengths) => NumberField::length(Word::plain(label), binding, lengths), + None => NumberField::new(Word::plain(label), binding).unit_kind(unit), + }; + field.step_arrows(StepArrows::Trailing).show(ui) + }); + output.settle(|_, out| out.events.committed().copied().flatten()).flatten() } diff --git a/crates/ui-egui/src/panels/preset_grid.rs b/crates/ui-egui/src/panels/preset_grid.rs index 3c39e2d97..661d1a562 100644 --- a/crates/ui-egui/src/panels/preset_grid.rs +++ b/crates/ui-egui/src/panels/preset_grid.rs @@ -1,5 +1,10 @@ -/// Shows a preset grid and returns the selected key. -#[allow(dead_code)] // P3(panels-w9): pn.A9 +use craft::behavior::{Selection, SelectionChange, keyed}; +use craft::ui::contract::{Bind, CollectionEvent, Props as _, SelectMode, ShowCollection as _}; +use craft::ui::list::ThumbSize; +use craft::ui::runtime::env::Env; +use craft::ui::tile_grid::{CaptionPlace, Tile, TileGrid}; + +/// Shows preview tiles and returns only a change of the selected preset. pub(crate) fn preset_grid( ui: &mut egui::Ui, label: &str, @@ -8,6 +13,31 @@ pub(crate) fn preset_grid, egui::Rect, &K), ) -> Option { - let _ = (ui, label, presets, current, aspect, paint); - todo!("pn.A9") + let env = Env::of(ui); + let presets: Vec<_> = presets.iter().map(|(key, label)| (key.clone(), env.view().resolve(label))).collect(); + let selection = current.map(Selection::single).unwrap_or_default(); + let source = keyed(&presets, |(key, _)| key.clone()).with_text(|(_, label)| label.as_str().into()); + + let out = TileGrid::new(label.to_owned()) + .caption(CaptionPlace::None) + .select(SelectMode::Single) + .thumb_size(ThumbSize::Small) + .selection(Bind::given(selection)) + .show_items(ui, &source, &mut |key, item| { + let label = presets[item.at].1.clone(); + let paint = &paint; + let key = key.clone(); + Tile::new(label.clone()).tooltip(label).thumb(aspect, move |cx, rect| paint(cx, rect, &key)) + }); + + out.response + .events + .into_iter() + .filter_map(|event| match event { + CollectionEvent::Selection { change: SelectionChange::Set(keys), .. } => keys.into_iter().next(), + CollectionEvent::Selection { change: SelectionChange::Toggle(key), .. } => Some(key), + CollectionEvent::Selection { change: SelectionChange::Clear, .. } => None, + _ => None, + }) + .next_back() } diff --git a/crates/ui-egui/src/reflection_pane_tests.rs b/crates/ui-egui/src/reflection_pane_tests.rs index 24292fbaa..0f252370e 100644 --- a/crates/ui-egui/src/reflection_pane_tests.rs +++ b/crates/ui-egui/src/reflection_pane_tests.rs @@ -20,56 +20,16 @@ fn reflection_none_clears_after_a_second_preset() { s.execute("slide.new", &json!({"layout": "blank"})).unwrap(); let id = s.execute("shape.insert", &json!({"preset": "rect", "rect": [72.0, 36.0, 144.0, 108.0]})).unwrap()["id"].as_u64().unwrap(); s.execute("edit.select", &json!({"ids": [id]})).unwrap(); - // P3(panels-w9): sh.A2 - let mut app = SlideApp::new_base(s, Services::default()); - app.ui.pane = Some("format".into()); - app.ui.format_tab = "effects".into(); - let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_ui_state( - |ui, app: &mut SlideApp| { - app.logic(ui.ctx()); - app.ui(ui); - }, - app, - ); + let mut h = crate::test_harness::harness_with(s, Services::default(), Harness::builder().with_size(egui::vec2(1400.0, 900.0)), |_| {}); + h.state_mut().ui.pane = Some("format".into()); + h.state_mut().ui.format_tab = "effects".into(); h.run_steps(3); - // The Shadow row has a "None" too; Reflection's is the later one. for preset in ["Tight", "Half", "Full"] { - h.get_by_label(preset).click(); + h.get_by_role_and_label(egui::accesskit::Role::RadioGroup, "Reflection").get_by_label(preset).click(); h.run_steps(4); assert!(has_reflection(&h), "{preset} adds a reflection"); - h.get_all_by_label("None").last().unwrap().click(); + h.get_by_role_and_label(egui::accesskit::Role::RadioGroup, "Reflection").get_by_label("None").click(); h.run_steps(4); assert!(!has_reflection(&h), "None clears the {preset} reflection"); } } - -#[test] -fn reflection_none_stays_cleared_when_a_slider_only_had_focus() { - let mut s = Session::with_new(); - s.execute("slide.new", &json!({"layout": "blank"})).unwrap(); - let id = s.execute("shape.insert", &json!({"preset": "rect", "rect": [72.0, 36.0, 144.0, 108.0]})).unwrap()["id"].as_u64().unwrap(); - s.execute("edit.select", &json!({"ids": [id]})).unwrap(); - // P3(panels-w9): sh.A2 - let mut app = SlideApp::new_base(s, Services::default()); - app.ui.pane = Some("format".into()); - app.ui.format_tab = "effects".into(); - let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_ui_state( - |ui, app: &mut SlideApp| { - app.logic(ui.ctx()); - app.ui(ui); - }, - app, - ); - h.run_steps(3); - h.get_by_label("Tight").click(); - h.run_steps(4); - // Focus a reflection slider without editing it, then click "None": the slider's value must not - // be written back over the cleared reflection. - // The reflection's Size comes before the glow's (the same label). - h.get_all_by_role_and_label(egui::accesskit::Role::Slider, "Size").next().unwrap().focus(); - h.run_steps(4); - assert!(h.get_all_by_role_and_label(egui::accesskit::Role::Slider, "Size").next().unwrap().is_focused(), "the reflection size slider has focus"); - h.get_all_by_label("None").last().unwrap().click(); - h.run_steps(4); - assert!(!has_reflection(&h), "a slider that only had focus does not re-add the reflection"); -}