From f7432c3261ae4412dcbecb08ffc5848bdae01d81 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 27 Nov 2026 14:51:30 +0000 Subject: [PATCH] panels-w14: net change of 2 commits - panels-w14: written by an agent - panels-w14: review fixes (review-1) --- crates/ui-egui/src/panels/smartart.rs | 186 ++++++++++++-------- crates/ui-egui/src/panels/smartart_tests.rs | 136 +++++++++----- 2 files changed, 204 insertions(+), 118 deletions(-) diff --git a/crates/ui-egui/src/panels/smartart.rs b/crates/ui-egui/src/panels/smartart.rs index a50f95070..869a9cd09 100644 --- a/crates/ui-egui/src/panels/smartart.rs +++ b/crates/ui-egui/src/panels/smartart.rs @@ -1,90 +1,136 @@ -use super::{SlideApp, Tokens, Ui, Value, json, run}; +use craft::behavior::{EditEvent, Mixed}; +use craft::ui::buttons::Button; +use craft::ui::contract::{Bind, Gap, Props as _, Show as _, ShowContainer as _, Sizing}; +use craft::ui::display::{Shade, Text}; +use craft::ui::layout_components::{Row, Stack}; +use craft::ui::runtime::focus::FocusRequest; +use craft::ui::text::TextField; + +use super::{SlideApp, Ui, Value, json}; + +struct LineAction { + index: usize, + text: Option, + structure: Option<(&'static str, usize)>, +} /// SmartArt Text Pane: one line per node, indented by level. Enter adds a line after, Tab and /// Shift+Tab demote and promote, Backspace in an empty line removes it. pub(super) fn smartart(app: &mut SlideApp, ui: &mut Ui) { let Some((gid, d)) = app.session.active().and_then(deckcraft_engine::cmd::smartart::selected) else { - ui.label("Select a Diagram graphic to edit its text."); + Text::new("Select a Diagram graphic to edit its text.").show(ui); return; }; - let texts: Vec = d.nodes.iter().map(|n| n.text.clone()).collect(); - let focused_any = ui.ctx().memory(|m| m.focused().is_some()) || app.ui.smartart_focus.is_some(); - if app.ui.smartart_drafts.0 != gid.0 || app.ui.smartart_drafts.1.len() != texts.len() || !focused_any { - app.ui.smartart_drafts = (gid.0, texts.clone()); - } - let mut drafts = std::mem::take(&mut app.ui.smartart_drafts.1); - let mut action: Option<(&str, Value)> = None; - let mut commit: Option = None; - let mut focus_row: Option = None; - ui.label(egui::RichText::new("Type your text here").color(Tokens::get(ui.ctx()).text_dim)); - ui.add_space(4.0); - for (i, node) in d.nodes.iter().enumerate() { - let Some(draft) = drafts.get_mut(i) else { break }; - let id = egui::Id::unique(("smartart_line", gid.0, i)); - ui.horizontal(|ui| { - ui.add_space(16.0 * node.level as f32); - ui.label("•"); - let te = egui::TextEdit::singleline(draft).id(id).hint_text("Diagram text").desired_width(f32::INFINITY); - let r = ui.add(te); - if app.ui.smartart_focus == Some(i) { - r.request_focus(); - app.ui.smartart_focus = None; - } - if r.gained_focus() || r.has_focus() { - app.ui.smartart_row = i; - } - if r.has_focus() { - let back = ui.input(|inp| inp.key_pressed(egui::Key::Backspace)); - if back && draft.is_empty() && texts.get(i).is_some_and(String::is_empty) && d.nodes.len() > 1 { - action = Some(("smartArt.removeNode", json!({"index": i}))); - focus_row = Some(i.saturating_sub(1)); + let mut texts: Vec = d.nodes.iter().map(|node| node.text.clone()).collect(); + let requested_focus = app.smartart_focus; + let active_row = app.ui.smartart_row; + let mut pane = Stack::new().sizing(Sizing::FillWidth).gap(Gap::Row).show(ui, |ui, _| { + let rows = Stack::new() + .sizing(Sizing::FillWidth) + .gap(Gap::Tight) + .show(ui, |ui, _| { + Text::new("Type your text here").shade(Shade::Muted).show(ui); + let mut rows = Vec::with_capacity(d.nodes.len()); + for (i, node) in d.nodes.iter().enumerate() { + let line = Row::new().indent(node.level).id_salt((gid.0, i)).show(ui, |ui, _| { + Text::new("•").show(ui); + let mut field = TextField::new("Diagram text", Bind::given(node.text.clone())) + .id_salt((gid.0, i)) + .placeholder("Diagram text") + .sizing(Sizing::FillWidth) + .submits() + .tab_reports() + .reports_empty_backspace(); + if requested_focus == Some(i) { + field = field.request_focus(FocusRequest::Take); + } + let out = field.show(ui); + let text = out.shown.as_ref().and_then(Mixed::same).map(|text| text.as_ref().clone()); + (out, text) + }); + if let Some((out, text)) = line.inner { + rows.push((i, out, text)); + } + } + rows + }) + .inner + .unwrap_or_default(); + let focused_row = rows.iter().find(|(_, out, _)| out.response.has_focus()).map(|(i, _, _)| *i); + let index = focused_row.unwrap_or(active_row).min(d.nodes.len().saturating_sub(1)); + let buttons = Row::new() + .show(ui, |ui, _| { + let add = Button::new("+ Add").show(ui).clicked(); + let remove = Button::new("Remove").show(ui).clicked(); + if remove && !d.nodes.is_empty() { + Some(("smartArt.removeNode", index.saturating_sub(1))) + } else if add { + Some(("smartArt.addNode", index + 1)) + } else { + None + } + }) + .inner + .flatten(); + let mut actions = Vec::new(); + for (i, out, text) in &rows { + for event in out.events.iter() { + // A typing run that begins and commits in one pass is folded into Set. + if let EditEvent::Commit { after, .. } | EditEvent::Set { after, .. } = event { + actions.push(LineAction { index: *i, text: Some(after.clone()), structure: None }); } } - if r.lost_focus() { - let (tab, shift) = ui.input(|inp| (inp.key_pressed(egui::Key::Tab), inp.modifiers.shift)); - if tab { - action = Some((if shift { "smartArt.promote" } else { "smartArt.demote" }, json!({"index": i}))); - focus_row = Some(i); - } else if ui.input(|inp| inp.key_pressed(egui::Key::Enter)) { - action = Some(("smartArt.addNode", json!({"index": i, "position": "after"}))); - focus_row = Some(i + 1); - } - commit = Some(i); + let structure = if out.submitted { + Some(("smartArt.addNode", *i + 1)) + } else if let Some(demote) = out.tab { + Some((if demote { "smartArt.demote" } else { "smartArt.promote" }, *i)) + } else if out.empty_backspace && d.nodes.get(*i).is_some_and(|node| node.text.is_empty()) && d.nodes.len() > 1 { + // TextField::update reports only Backspace on an already empty draft, not the deletion that empties it. + Some(("smartArt.removeNode", i.saturating_sub(1))) + } else { + None + }; + if structure.is_some() { + actions.push(LineAction { index: *i, text: text.clone(), structure }); } - }); - } - ui.add_space(6.0); - ui.horizontal(|ui| { - if ui.button("+ Add").clicked() { - action = Some(("smartArt.addNode", json!({"index": app.ui.smartart_row.min(d.nodes.len().saturating_sub(1)), "position": "after"}))); } - if ui.button("Remove").clicked() && !d.nodes.is_empty() { - action = Some(("smartArt.removeNode", json!({"index": app.ui.smartart_row.min(d.nodes.len() - 1)}))); + if buttons.is_some() { + let text = rows.iter().find(|(i, _, _)| *i == index).and_then(|(_, _, text)| text.clone()); + actions.push(LineAction { index, text, structure: buttons }); } + let requested_shown = requested_focus.is_some_and(|i| rows.iter().any(|(index, _, _)| *index == i)); + (actions, focused_row, requested_shown) }); - // Commit a changed line before any structural edit, so the edit sees the new text. - if let Some(i) = commit - && drafts.get(i) != texts.get(i) - { - run(app, "smartArt.setNodes", json!({"id": gid.0, "index": i, "text": drafts.get(i).cloned().unwrap_or_default()})); - } - if action.is_some() { - for (i, t) in drafts.iter().enumerate() { - if Some(i) != commit && texts.get(i) != Some(t) { - run(app, "smartArt.setNodes", json!({"id": gid.0, "index": i, "text": t})); + let actions = pane + .settle(|_, (actions, focused_row, requested_shown)| { + if let Some(row) = *focused_row { + app.ui.smartart_row = row; } + if *requested_shown { + app.smartart_focus = None; + } + std::mem::take(actions) + }) + .unwrap_or_default(); + for action in actions { + if let Some(text) = action.text + && let Some(current) = texts.get_mut(action.index) + && *current != text + { + if app.run("smartArt.setNodes", json!({"id": gid.0, "index": action.index, "text": text})).is_err() { + continue; + } + *current = text; } - } - app.ui.smartart_drafts.1 = drafts; - if let Some((cmd, mut p)) = action { - if let Some(o) = p.as_object_mut() { - o.insert("id".into(), json!(gid.0)); + let Some((command, fallback)) = action.structure else { continue }; + let mut params = json!({"id": gid.0, "index": action.index}); + if command == "smartArt.addNode" { + params["position"] = json!("after"); } - if let Ok(r) = app.run(cmd, p) { - let row = r.get("index").and_then(Value::as_u64).map(|v| v as usize).or(focus_row).unwrap_or(0); + if let Ok(result) = app.run(command, params) { + let row = result.get("index").and_then(Value::as_u64).and_then(|index| usize::try_from(index).ok()).unwrap_or(fallback); app.ui.smartart_row = row; - app.ui.smartart_drafts.0 = 0; - app.ui.smartart_focus = Some(row); + app.smartart_focus = Some(row); } } } diff --git a/crates/ui-egui/src/panels/smartart_tests.rs b/crates/ui-egui/src/panels/smartart_tests.rs index 085146cbd..2c48fcfc4 100644 --- a/crates/ui-egui/src/panels/smartart_tests.rs +++ b/crates/ui-egui/src/panels/smartart_tests.rs @@ -1,11 +1,11 @@ -//! The SmartArt Text Pane driven like a person would: type in a line, Enter for a new line, Tab -//! to demote. +//! Diagram text edits, outline keys and gallery previews driven through the app. use deckcraft_engine::Session; use egui_kittest::Harness; use egui_kittest::kittest::{NodeT, Queryable}; use serde_json::json; +use crate::test_harness::{harness, harness_with}; use crate::{Services, SlideApp}; fn levels(h: &Harness<'_, SlideApp>) -> Vec<(String, u8)> { @@ -23,18 +23,11 @@ fn text_pane_types_adds_and_demotes() { let mut s = Session::with_new(); s.execute("slide.new", &json!({"layout": "blank"})).unwrap(); s.execute("insert.smartArt", &json!({"kind": "list", "items": ["Plan", "Build"]})).unwrap(); - // P3(panels-w14): sh.A2 - let mut app = SlideApp::new_base(s, Services::default()); - app.ui.pane = Some("smartart".into()); - let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 60.0).build_ui_state( - |ui, app: &mut SlideApp| { - app.logic(ui.ctx()); - app.ui(ui); - }, - app, - ); + let mut h = harness(s, Services::default()); + h.state_mut().ui.pane = Some("smartart".into()); h.run_steps(3); assert!(h.query_by_label("Text Pane").is_some()); + let initial = levels(&h); line(&h, "Build").click(); h.run_steps(2); @@ -43,14 +36,66 @@ fn text_pane_types_adds_and_demotes() { h.run_steps(2); h.key_press(egui::Key::Enter); h.run_steps(4); - assert_eq!(levels(&h), [("Plan".into(), 0), ("Build it".into(), 0), (String::new(), 0)]); + let added = levels(&h); + assert_eq!(added.len(), initial.len() + 1, "Enter adds one node"); + assert_eq!(added[0], initial[0]); + assert_eq!(added[1].0, format!("{} it", initial[1].0), "Enter commits the acting line before adding its successor"); + assert!(added[2].0.is_empty()); + assert_eq!(added[2].1, added[1].1); - // The new line has the focus: type, then Tab demotes it under "Build it". - h.event(egui::Event::Text("Test".into())); + let typed = "Test"; + h.event(egui::Event::Text(typed.into())); h.run_steps(2); h.key_press(egui::Key::Tab); h.run_steps(4); - assert_eq!(levels(&h), [("Plan".into(), 0), ("Build it".into(), 0), ("Test".into(), 1)]); + let demoted = levels(&h); + assert_eq!(&demoted[..2], &added[..2]); + assert_eq!(demoted.len(), added.len()); + assert_eq!(demoted[2].0, typed, "the added line takes focus and Tab commits its draft"); + assert!(demoted[2].1 > added[2].1); + + h.key_press_modifiers(egui::Modifiers::SHIFT, egui::Key::Tab); + h.run_steps(4); + let promoted = levels(&h); + assert_eq!(&promoted[..2], &demoted[..2]); + assert_eq!(promoted[2].0, demoted[2].0); + assert_eq!(promoted[2].1, added[2].1, "Tab keeps focus so Shift+Tab promotes the same node"); + + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + h.key_press(egui::Key::Backspace); + h.run_steps(2); + h.key_press(egui::Key::Backspace); + h.run_steps(2); + assert_eq!(levels(&h), promoted, "an empty draft cannot remove a node whose engine text is still nonempty"); + h.key_press(egui::Key::Tab); + h.run_steps(3); + let emptied = levels(&h); + assert_eq!(emptied.len(), promoted.len()); + assert!(emptied[2].0.is_empty(), "Tab commits the empty draft before changing the outline"); + h.key_press(egui::Key::Backspace); + h.run_steps(4); + assert_eq!(levels(&h), added[..2], "empty Backspace removes one node and preserves its neighbours"); + + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + h.key_press(egui::Key::Backspace); + h.run_steps(2); + h.key_press(egui::Key::Tab); + h.run_steps(3); + h.key_press(egui::Key::Backspace); + h.run_steps(4); + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + h.key_press(egui::Key::Backspace); + h.run_steps(2); + h.key_press(egui::Key::Tab); + h.run_steps(3); + let singleton = levels(&h); + assert_eq!(singleton.len() + 1, initial.len()); + assert!(singleton[0].0.is_empty()); + line(&h, "").click(); + h.run_steps(2); + h.key_press(egui::Key::Backspace); + h.run_steps(3); + assert_eq!(levels(&h), singleton, "empty Backspace keeps the last node"); } #[test] @@ -58,23 +103,21 @@ fn home_convert_to_smartart_gallery_converts_the_text() { let mut s = Session::with_new(); s.execute("slide.new", &json!({"layout": "blank"})).unwrap(); s.execute("insert.textBox", &json!({"text": "Goals\nRevenue\nGrowth"})).unwrap(); - // P3(panels-w14): sh.A2 - let app = SlideApp::new_base(s, Services::default()); - let mut h = Harness::builder().with_size(egui::vec2(1600.0, 900.0)).with_step_dt(1.0 / 60.0).build_ui_state( - |ui, app: &mut SlideApp| { - app.logic(ui.ctx()); - app.ui(ui); - }, - app, + let mut h = harness_with( + s, + Services::default(), + Harness::builder().with_size(egui::vec2(1600.0, 900.0)).with_step_dt(1.0 / 60.0), + |_| {}, ); h.run_steps(3); h.get_by_label("Convert to Diagram").click(); h.run_steps(3); - h.get_by_role_and_label(egui::accesskit::Role::Button, "Basic Block List").click(); + let layout = deckcraft_engine::cmd::smartart::LAYOUTS.iter().find(|(kind, _)| *kind == "blockList").unwrap(); + h.get_by_role_and_label(egui::accesskit::Role::Button, layout.1).click(); h.run_steps(3); let st = h.state().session.active().unwrap(); let (_, d) = deckcraft_engine::cmd::smartart::selected(st).expect("the text became a Diagram graphic"); - assert_eq!(d.kind, "blockList"); + assert_eq!(d.kind, layout.0); assert_eq!(d.nodes.len(), 3); } @@ -84,32 +127,29 @@ fn layout_gallery_shows_cached_pictures_and_previews_on_hover() { let mut s = Session::with_new(); s.execute("slide.new", &json!({"layout": "blank"})).unwrap(); s.execute("insert.smartArt", &json!({"kind": "process", "items": ["Plan", "Build", "Ship"]})).unwrap(); - // P3(panels-w14): sh.A2 - let mut app = SlideApp::new_base(s, Services::default()); - app.ui.tab = "SmartArt Design".into(); - let mut h = Harness::builder().with_size(egui::vec2(1600.0, 900.0)).with_step_dt(1.0 / 60.0).build_ui_state( - |ui, app: &mut SlideApp| { - app.logic(ui.ctx()); - app.ui(ui); - }, - app, + let mut h = harness_with( + s, + Services::default(), + Harness::builder().with_size(egui::vec2(1600.0, 900.0)).with_step_dt(1.0 / 60.0), + |_| {}, ); + h.state_mut().ui.tab = "SmartArt Design".into(); h.run_steps(3); h.get_by_label("Layouts").click(); h.run_steps(3); - let first = h.state().textures.smartart_cached(); - assert!(first >= deckcraft_engine::cmd::smartart::LAYOUTS.len().min(8), "gallery pictures were drawn: {first}"); - h.get_by_role_and_label(egui::accesskit::Role::Button, "Vertical Box List").hover(); - h.run_steps(3); - if let Ok(path) = std::env::var("SMARTART_GALLERY_PNG") { - h.render().expect("render").save(path).unwrap(); - } - let hovered = h.state().textures.smartart_cached(); - assert!(hovered > first, "hovering drew a preview of the graphic's own outline"); - h.run_steps(3); - assert_eq!(h.state().textures.smartart_cached(), hovered, "pictures are cached, not redrawn every frame"); - h.get_by_role_and_label(egui::accesskit::Role::Button, "Vertical Box List").click(); + let first = crate::ribbon::diagram_thumbs::thumbnails(&h.ctx).cache().statistics(&h.ctx); + assert!(first.entries > 0, "gallery pictures were drawn"); + let layout = deckcraft_engine::cmd::smartart::LAYOUTS.iter().find(|(kind, _)| *kind == "verticalBoxList").unwrap(); + h.get_by_role_and_label(egui::accesskit::Role::Button, layout.1).hover(); + // The tile's rich tip opens after the pointer rests; each harness step advances 1/60 s. + h.run_steps(60); + let hovered = crate::ribbon::diagram_thumbs::thumbnails(&h.ctx).cache().statistics(&h.ctx); + assert!(hovered.entries > first.entries, "hovering drew a preview of the graphic's own outline"); + h.run_steps(60); + let settled = crate::ribbon::diagram_thumbs::thumbnails(&h.ctx).cache().statistics(&h.ctx); + assert_eq!(settled.entries, hovered.entries, "pictures are cached, not redrawn every frame"); + h.get_by_role_and_label(egui::accesskit::Role::Button, layout.1).click(); h.run_steps(3); let st = h.state().session.active().unwrap(); - assert_eq!(deckcraft_engine::cmd::smartart::selected(st).unwrap().1.kind, "verticalBoxList"); + assert_eq!(deckcraft_engine::cmd::smartart::selected(st).unwrap().1.kind, layout.0); }