From d4b6a82e4bbbfcdd8bc9537367bda4d1bd4e3cf2 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 27 Nov 2026 14:51:29 +0000 Subject: [PATCH] canvas-w9: net change of 3 commits - canvas-w9: written by an agent - canvas-w9: finished by an agent - canvas-w9: review fixes (review-1) --- crates/ui-egui/src/canvas/handles.rs | 145 ++++++++++++- crates/ui-egui/src/canvas/overlays.rs | 293 -------------------------- crates/ui-egui/src/canvas/previews.rs | 136 +++++++++++- 3 files changed, 271 insertions(+), 303 deletions(-) delete mode 100644 crates/ui-egui/src/canvas/overlays.rs diff --git a/crates/ui-egui/src/canvas/handles.rs b/crates/ui-egui/src/canvas/handles.rs index b166332e3..8015f19c0 100644 --- a/crates/ui-egui/src/canvas/handles.rs +++ b/crates/ui-egui/src/canvas/handles.rs @@ -1,6 +1,141 @@ -/// Paints or hosts this canvas part over the slide picture. -#[allow(dead_code)] // P3(canvas-w9): cv.A3 -pub(crate) fn show(app: &mut crate::SlideApp, ui: &mut egui::Ui, space: &craft::ui::canvas::CanvasSpace) { - let _ = (app, ui, space); - todo!("cv.A3") +//! Selection decoration; engine pointer gestures own every document edit. + +use craft::behavior::geometry::quad::{BoxAllow, BoxRules, BoxState}; +use craft::behavior::kurbo::{BezPath, Point, Rect}; +use craft::draw::ui::transform::TransformReach; +use craft::draw::ui::{AdjustHandles, RotateZone, TransformBox}; +use craft::ui::canvas::CanvasSpace; +use craft::ui::canvas_controls::SelectionOutline; +use craft::ui::contract::{Bind, Props as _, Show as _}; +use craft::ui::primitives::{Draw, HandleShape, PaintShape, StrokeWidth}; +use craft::ui::style::{Flags, State, StateSet, StyleCx}; + +use crate::SlideApp; + +/// Shows selection controls and table-cell highlights in the canvas's current view. +pub(crate) fn show(app: &mut SlideApp, ui: &mut egui::Ui, space: &CanvasSpace) { + if app.anim_preview_active() { + return; + } + let Some(st) = app.session.active() else { return }; + let editing = st.selection.text.as_ref(); + for id in &st.selection.shapes { + let Some(sh) = st.shape(*id) else { continue }; + let x = deckcraft_engine::cmd::xfrm_of(&st.doc, &st.selection, sh); + let a = x.affine(); + let start = Rect::new(0.0, 0.0, x.w, x.h); + let quad = BoxState::from_rect(start).quad.map(|q| a * q); + if sh.is_line() { + let glued = match &sh.kind { + deckcraft_model::ShapeKind::Connector { start, end, .. } => [start.is_some(), end.is_some()], + _ => [false, false], + }; + space.paint_overlay(ui, |draw, style| { + for (point, glued) in [Point::ZERO, Point::new(x.w, x.h)].into_iter().zip(glued) { + paint_handle(draw, style, HandleShape::Circle, space.view.to_screen(a * point), glued, sh.locked); + } + }); + continue; + } + if app.session.tool.editing_points(st) == Some(sh.id) { + space.paint_overlay(ui, |draw, style| { + let line = draw.role(|c| c.overlay.line); + if let Some((path, index)) = app.session.tool.edit_vertex + && let Some((hin, hout, vertex)) = deckcraft_engine::cmd::points::slide_handles(&st.doc, &st.selection, sh, path, index) + { + for h in [hin, hout].into_iter().flatten() { + let at = space.view.to_screen(h); + draw.stroke(&PaintShape::Path(vec![space.view.to_screen(vertex), at]), StrokeWidth::Thin, line); + paint_handle(draw, style, HandleShape::Square, at, false, sh.locked); + } + } + for (path, vertices) in deckcraft_engine::cmd::points::slide_vertices(&st.doc, &st.selection, sh).into_iter().enumerate() { + for (index, point) in vertices.into_iter().enumerate() { + let selected = app.session.tool.edit_vertex == Some((path, index)); + paint_handle(draw, style, HandleShape::Square, space.view.to_screen(point), selected, sh.locked); + } + } + }); + continue; + } + let editing_this = editing_shape(st, sh); + if editing_this { + let mut path = BezPath::new(); + path.move_to(quad[0]); + for point in &quad[1..] { + path.line_to(*point); + } + path.close_path(); + SelectionOutline::dashed("Text frame", &[path]).id(ui.id().with(("text_frame", *id))).show_at(ui, space.view.rect); + } + let Some((_, control)) = selection_box(st, sh) else { continue }; + control.id(ui.id().with(("selection", *id))).show_at(ui, space.view.rect); + if !sh.locked { + let geometry = deckcraft_render::shape_geometry(sh, x.w, x.h); + let mut points: Vec = geometry.handles.iter().map(|h| a * h.pos).collect(); + points.extend(deckcraft_engine::tools::wordart_handles(&st.doc, &st.selection, sh, x.w, x.h).into_iter().map(|h| a * h.pos)); + AdjustHandles::new("Adjustment points", &points).id(ui.id().with(("adjustment_points", *id))).show_at(ui, space.view.rect); + } + } + space.paint_overlay(ui, |draw, _| { + if let Some(ts) = editing + && let (Some((row, column)), Some(sh)) = (ts.cell, st.shape(ts.shape)) + && let Some(frame) = deckcraft_engine::cmd::table::table_frame(st, sh) + && let Some(cell) = frame.cell_rect(row, column) + { + let points = BoxState::from_rect(cell).quad.map(|p| space.view.to_screen(frame.affine * p)); + draw.stroke(&PaintShape::Polygon(points.to_vec()), StrokeWidth::Medium, draw.role(|c| c.accent.solid)); + } + if let Some((id, ..)) = st.selection.cells + && editing.is_none() + && let Some(sh) = st.shape(id) + && let deckcraft_model::ShapeKind::Table(table) = &sh.kind + && let Some(frame) = deckcraft_engine::cmd::table::table_frame(st, sh) + { + use deckcraft_engine::cmd::table::{in_range, selected_range}; + let range = selected_range(st.selection.cells, id, table); + for (_, _, cell) in frame.cells.iter().filter(|(r, c, _)| range.is_some() && in_range(range, *r, *c)) { + let points = BoxState::from_rect(*cell).quad.map(|p| space.view.to_screen(frame.affine * p)); + draw.fill(&PaintShape::Polygon(points.to_vec()), draw.role(|c| c.state.text_selection)); + } + } + }); +} + +fn editing_shape(st: &deckcraft_engine::DocState, sh: &deckcraft_model::Shape) -> bool { + st.selection.text.as_ref().is_some_and(|e| e.shape == sh.id && !e.notes) +} + +/// Builds the displayed selection box and its state for canonical transform hit queries. +pub(super) fn selection_box(st: &deckcraft_engine::DocState, sh: &deckcraft_model::Shape) -> Option<(BoxState, TransformBox<'static>)> { + let editing = editing_shape(st, sh); + if sh.is_line() || (editing && st.selection.shapes.len() != 1) { + return None; + } + let x = deckcraft_engine::cmd::xfrm_of(&st.doc, &st.selection, sh); + let a = x.affine(); + let start = Rect::new(0.0, 0.0, x.w, x.h); + let state = BoxState::new(BoxState::from_rect(start).quad.map(|q| a * q), a * start.center()); + let rules = BoxRules::default().with_allow(BoxAllow::new(!sh.locked && !editing, false, false)); + // Shown boxes declare no gesture parts: the engine is the sole owner of transforms. + let control = TransformBox::new("Selection", Bind::given(state), start, 0.0) + .reach(TransformReach::Shown) + .rotate_zone(if editing { RotateZone::Band } else { RotateZone::Handle }) + .round_handles(true) + .reference_point(false) + .rules(rules) + .enabled(!sh.locked); + Some((state, control)) +} + +/// Paints engine-owned points with the framework handle look and state. +pub(super) fn paint_handle(draw: &Draw<'_>, style: &StyleCx<'_>, shape: HandleShape, at: egui::Pos2, selected: bool, locked: bool) { + let look = style.handle(); + let mut state = StateSet::default(); + state.flags.set(Flags::SELECTED, selected); + if locked { + state.state = State::Disabled; + } + // PLAN: assumed Draw::handle(&self, look: &HandleLook, shape: HandleShape, center: egui::Pos2, state: &StateSet) (canvas-w4) + draw.handle(&look, shape, at, &state); } diff --git a/crates/ui-egui/src/canvas/overlays.rs b/crates/ui-egui/src/canvas/overlays.rs deleted file mode 100644 index ff59a8e31..000000000 --- a/crates/ui-egui/src/canvas/overlays.rs +++ /dev/null @@ -1,293 +0,0 @@ -use super::{Color32, CornerRadius, Point, Pos2, Rect, SlideApp, Stroke, Tokens, ToolKind, Ui, Xf, Xfrm, box_handles, pos2, theme}; -use deckcraft_engine::DocState; - -fn poly(p: &egui::Painter, pts: [Pos2; 4], stroke: Stroke) { - for i in 0..4 { - p.line_segment([pts[i], pts[(i + 1) % 4]], stroke); - } -} - -fn selection_handles(app: &SlideApp, st: &DocState, painter: &egui::Painter, xf: Xf, t: &Tokens) { - let sel_stroke = Stroke::new(1.0, t.selection); - let editing = st.selection.text.clone(); - for id in &st.selection.shapes { - let Some(sh) = st.shape(*id) else { continue }; - let x = deckcraft_engine::cmd::xfrm_of(&st.doc, &st.selection, sh); - let a = x.affine(); - let corners = [Point::new(0.0, 0.0), Point::new(x.w, 0.0), Point::new(x.w, x.h), Point::new(0.0, x.h)].map(|q| xf.to_screen(a * q)); - if sh.is_line() { - let (p0, p1) = (xf.to_screen(a * Point::new(0.0, 0.0)), xf.to_screen(a * Point::new(x.w, x.h))); - let glued = match sh.kind { - deckcraft_model::ShapeKind::Connector { start, end } => [start.is_some(), end.is_some()], - _ => [false, false], - }; - for (q, g) in [p0, p1].into_iter().zip(glued) { - // Glued ends are filled with the accent, like attached connector ends. - let fill = if g { t.accent } else { t.handle_fill }; - painter.circle(q, 4.5, fill, Stroke::new(1.0, if g { t.handle_fill } else { t.handle_stroke })); - } - continue; - } - let editing_this = editing.as_ref().is_some_and(|e| e.shape == *id && !e.notes); - if app.session.tool.editing_points(st) == Some(sh.id) { - // Edit Points: the outline's vertices are the handles. - // No selection box: the outline and its vertices are what's edited. - if let Some((path, index)) = app.session.tool.edit_vertex - && let Some((hin, hout, v)) = deckcraft_engine::cmd::points::slide_handles(&st.doc, &st.selection, sh, path, index) - { - for h in [hin, hout].into_iter().flatten() { - painter.line_segment([xf.to_screen(v), xf.to_screen(h)], sel_stroke); - painter.rect( - egui::Rect::from_center_size(xf.to_screen(h), egui::vec2(6.0, 6.0)), - 0.0, - Color32::WHITE, - Stroke::new(1.0, t.selection), - egui::StrokeKind::Middle, - ); - } - } - for q in deckcraft_engine::cmd::points::slide_vertices(&st.doc, &st.selection, sh).into_iter().flatten() { - let c = xf.to_screen(q); - painter.rect( - egui::Rect::from_center_size(c, egui::vec2(7.0, 7.0)), - 0.0, - Color32::BLACK, - Stroke::new(1.0, Color32::WHITE), - egui::StrokeKind::Middle, - ); - } - continue; - } - if editing_this { - // Dashed box while editing text. - for i in 0..4 { - let (s, e) = (corners[i], corners[(i + 1) % 4]); - painter.add(egui::Shape::dashed_line(&[s, e], Stroke::new(1.0, t.selection), 4.0, 3.0)); - } - } else { - poly(painter, corners, sel_stroke); - } - if sh.locked && !editing_this { - // Locked: greyed handles that don't respond, no rotate or adjust handles. - for h in box_handles(&x).0 { - painter.circle(xf.to_screen(h), 4.0, t.text_faint, Stroke::new(1.0, t.handle_fill)); - } - continue; - } - if st.selection.shapes.len() == 1 || !editing_this { - let (hs, rot) = box_handles(&x); - let rot_s = xf.to_screen(rot); - if !editing_this && sh.ph.as_ref().is_none_or(|_| true) { - let top_mid = xf.to_screen(hs[1]); - painter.line_segment([top_mid, rot_s], sel_stroke); - rotate_handle(painter, rot_s, t); - } - for h in hs { - painter.circle(xf.to_screen(h), 4.5, t.handle_fill, Stroke::new(1.0, t.handle_stroke)); - } - let geo = deckcraft_render::shape_geometry(sh, x.w, x.h); - for hd in &geo.handles { - let q = xf.to_screen(a * hd.pos); - painter.circle(q, 4.5, Color32::from_rgb(0xFF, 0xD2, 0x3F), Stroke::new(1.0, Color32::from_rgb(0x8A, 0x6A, 0x00))); - } - for hd in deckcraft_engine::tools::wordart_handles(&st.doc, &st.selection, sh, x.w, x.h) { - let q = xf.to_screen(a * hd.pos); - painter.circle(q, 4.5, Color32::from_rgb(0xFF, 0xD2, 0x3F), Stroke::new(1.0, Color32::from_rgb(0x8A, 0x6A, 0x00))); - } - } - } -} - -fn text_overlays(st: &DocState, ui: &Ui, painter: &egui::Painter, xf: Xf, t: &Tokens) { - let editing = st.selection.text.clone(); - // Caret / text selection, in a shape or a table cell. - if let Some(ts) = &editing { - for quad in text_marks(st, ts).iter().flat_map(|m| &m.selection) { - painter.add(egui::Shape::convex_polygon(quad.iter().map(|q| xf.to_screen(*q)).collect(), t.text_selection, Stroke::NONE)); - } - // An input method's composition: underlined in place, thicker under the part being converted. - for (a, b, thick) in deckcraft_engine::cmd::text::composition_underlines(st) { - painter.line_segment([xf.to_screen(a), xf.to_screen(b)], Stroke::new(if thick { 2.0 } else { 1.0 }, t.caret)); - } - if !ts.is_range() { - let blink = (ui.input(|i| i.time) * 1.6).fract() < 0.6; - ui.ctx().request_repaint_after(std::time::Duration::from_millis(300)); - // An empty placeholder shows the caret where typing begins too. - if blink && let Some((top, bot)) = deckcraft_engine::cmd::text::caret_line(st, ts) { - // In the colour of the text being typed: a black caret is lost on a dark slide. - let color = deckcraft_engine::cmd::text::caret_color(st, ts).map_or(t.caret, theme::to_color32); - painter.line_segment([xf.to_screen(top), xf.to_screen(bot)], Stroke::new(1.5, color)); - } - } - } - // Table cell editing: outline the cell (merged cells as drawn). - if let Some(ts) = &editing - && let (Some((r, c)), Some(sh)) = (ts.cell, st.shape(ts.shape)) - && let Some(f) = deckcraft_engine::cmd::table::table_frame(st, sh) - && let Some(cr) = f.cell_rect(r, c) - { - let pts = [Point::new(cr.x0, cr.y0), Point::new(cr.x1, cr.y0), Point::new(cr.x1, cr.y1), Point::new(cr.x0, cr.y1)] - .map(|q| xf.to_screen(f.affine * q)); - poly(painter, pts, Stroke::new(2.0, t.accent)); - } - // Shade the selected table cells in the text-selection color, each merged cell as it is drawn. - if let Some((id, ..)) = st.selection.cells - && st.selection.text.is_none() - && let Some(sh) = st.shape(id) - && let deckcraft_model::ShapeKind::Table(table) = &sh.kind - && let Some(f) = deckcraft_engine::cmd::table::table_frame(st, sh) - { - use deckcraft_engine::cmd::table::{in_range, selected_range}; - let range = selected_range(st.selection.cells, id, table); - for (_, _, cr) in f.cells.iter().filter(|(r, c, _)| range.is_some() && in_range(range, *r, *c)) { - let pts = [Point::new(cr.x0, cr.y0), Point::new(cr.x1, cr.y0), Point::new(cr.x1, cr.y1), Point::new(cr.x0, cr.y1)]; - painter.add(egui::Shape::convex_polygon(pts.iter().map(|q| xf.to_screen(f.affine * *q)).collect(), t.text_selection, Stroke::NONE)); - } - } -} - -pub(super) fn overlays(app: &mut SlideApp, ui: &Ui, painter: &egui::Painter, xf: Xf, t: &Tokens) { - // Spelling squiggles under misspelt words (checked in the background). - if let Some(st) = app.session.active() { - let lines = app.squiggles.update(st, Some(ui.ctx())); - let scale = (xf.to_screen(Point::new(1.0, 0.0)) - xf.to_screen(Point::new(0.0, 0.0))).length(); - for (a, b) in lines { - crate::squiggles::paint(painter, xf.to_screen(a), xf.to_screen(b), scale); - } - } - let Some(st) = app.session.active() else { return }; - selection_handles(app, st, painter, xf, t); - text_overlays(st, ui, painter, xf, t); - // Smart guides, marquee, previews. - for g in &app.session.tool.guides { - let (a, b) = - if g.vertical { (Point::new(g.pos, g.from), Point::new(g.pos, g.to)) } else { (Point::new(g.from, g.pos), Point::new(g.to, g.pos)) }; - painter.add(egui::Shape::dashed_line(&[xf.to_screen(a), xf.to_screen(b)], Stroke::new(1.0, t.guide), 5.0, 3.0)); - } - if let Some(m) = app.session.tool.marquee { - let r = Rect::from_two_pos(xf.to_screen(Point::new(m.x0, m.y0)), xf.to_screen(Point::new(m.x1, m.y1))); - painter.rect( - r, - CornerRadius::ZERO, - Color32::from_rgba_unmultiplied(80, 130, 230, 30), - Stroke::new(1.0, Color32::from_rgb(80, 130, 230)), - egui::StrokeKind::Inside, - ); - } - if let Some(pv) = app.session.tool.preview { - draw_preview(app, painter, xf, pv, t); - } - // Freeform being drawn. - let path = &app.session.tool.path_preview; - if path.len() > 1 { - let pts: Vec = if app.session.tool.path_smooth && path.len() > 2 { - let d = deckcraft_engine::cmd::shape::freeform_path(path, false, true); - let mut v = vec![]; - let bp = deckcraft_render::parse_path(&d); - deckcraft_geom::preset_flatten(&bp, &mut |el| match el { - deckcraft_geom::PathEl::MoveTo(q) | deckcraft_geom::PathEl::LineTo(q) => v.push(q), - _ => {} - }); - v - } else { - path.clone() - }; - painter.add(egui::Shape::line(pts.iter().map(|q| xf.to_screen(*q)).collect(), Stroke::new(1.5, t.accent))); - if app.session.tool.path_closing - && let Some(s0) = path.first() - { - painter.circle(xf.to_screen(*s0), 5.0, t.accent, Stroke::new(1.5, t.handle_fill)); - } - } - // Connection sites of the shape under a connector end, and the site it would glue to. - for q in &app.session.tool.sites { - let c = xf.to_screen(*q); - painter.circle(c, 3.5, t.handle_fill, Stroke::new(1.0, t.handle_stroke)); - } - if let Some(g) = app.session.tool.glue { - painter.circle(xf.to_screen(g), 5.0, t.accent, Stroke::new(1.5, t.handle_fill)); - } - if app.session.tool.ink_preview.len() > 1 - && let ToolKind::Ink { color, width, mode } = &app.session.tool.kind - { - let pts = &app.session.tool.ink_preview; - let legacy = deckcraft_engine::model::ink_pressure_is_legacy(pts); - let c = theme::to_color32(*color); - let c = if mode == "highlighter" { c.gamma_multiply(0.45) } else { c }; - if legacy || mode == "highlighter" { - // One line, so see-through highlighter ink doesn't darken where segments meet. - let line: Vec = pts.iter().map(|(x, y, _)| xf.to_screen(Point::new(*x, *y))).collect(); - painter.add(egui::Shape::line(line, Stroke::new((*width as f32 * xf.scale).max(1.0), c))); - } else { - for pair in pts.windows(2) { - let [q0, q1] = pair else { continue }; - let a = deckcraft_engine::model::ink_sample_width(*width, q0.2, legacy); - let b = deckcraft_engine::model::ink_sample_width(*width, q1.2, legacy); - let sw = ((a + b) * 0.5 * f64::from(xf.scale)).max(1.0) as f32; - let p0 = xf.to_screen(Point::new(q0.0, q0.1)); - let p1 = xf.to_screen(Point::new(q1.0, q1.1)); - painter.add(egui::Shape::line(vec![p0, p1], Stroke::new(sw, c))); - } - } - } -} - -/// The text selection (quads) of the text being edited, in slide space: in a shape or a table -/// cell, wherever its text is drawn. The caret is `cmd::text::caret_line`. -pub(crate) struct TextMarks { - pub selection: Vec<[Point; 4]>, -} - -pub(crate) fn text_marks(st: &deckcraft_engine::DocState, ts: &deckcraft_engine::TextSel) -> Option { - let (l, m) = deckcraft_engine::cmd::text::text_frame(st, ts)?; - let pos = |p: (usize, usize)| deckcraft_text::Pos { para: p.0, ch: p.1 }; - let (s0, s1) = ts.ordered(); - let selection = l - .selection_rects(pos(s0), pos(s1)) - .into_iter() - .map(|r| [Point::new(r.x0, r.y0), Point::new(r.x1, r.y0), Point::new(r.x1, r.y1), Point::new(r.x0, r.y1)].map(|q| m * q)) - .collect(); - Some(TextMarks { selection }) -} - -fn rotate_handle(p: &egui::Painter, c: Pos2, t: &Tokens) { - p.circle_filled(c, 6.0, t.handle_fill); - p.add(egui::Shape::Path(egui::epaint::PathShape { - points: (0..=20) - .map(|i| { - let a = -0.4 + i as f32 / 20.0 * 5.2; - pos2(c.x + 4.0 * a.cos(), c.y + 4.0 * a.sin()) - }) - .collect(), - closed: false, - fill: Color32::TRANSPARENT, - stroke: Stroke::new(1.3, t.handle_stroke).into(), - })); -} - -fn draw_preview(app: &SlideApp, p: &egui::Painter, xf: Xf, x: Xfrm, t: &Tokens) { - let preset = match &app.session.tool.kind { - ToolKind::Shape { preset } => preset.clone(), - _ => "rect".into(), - }; - if preset == deckcraft_engine::tools::DRAW_TABLE { - // A border inside a table (flat), or the outline of a new table. - let (a, b) = (xf.to_screen(Point::new(x.x, x.y)), xf.to_screen(Point::new(x.x + x.w, x.y + x.h))); - if x.w < 0.5 || x.h < 0.5 { - p.line_segment([a, b], Stroke::new(1.5, t.accent)); - } else { - p.rect_stroke(Rect::from_two_pos(a, b), 0.0, Stroke::new(1.0, t.accent), egui::StrokeKind::Inside); - } - return; - } - if deckcraft_geom::preset::is_line_like(&preset) { - let a = x.affine(); - p.line_segment([xf.to_screen(a * Point::new(0.0, 0.0)), xf.to_screen(a * Point::new(x.w, x.h))], Stroke::new(1.5, t.accent)); - return; - } - let r = Rect::from_two_pos(xf.to_screen(Point::new(x.x, x.y)), xf.to_screen(Point::new(x.x + x.w, x.y + x.h))); - if r.width() < 1.0 || r.height() < 1.0 { - return; - } - crate::ribbon::paint_preset(p, r, &preset, t.accent.gamma_multiply(0.25), t.accent); -} diff --git a/crates/ui-egui/src/canvas/previews.rs b/crates/ui-egui/src/canvas/previews.rs index 9b02ae098..429a85fcd 100644 --- a/crates/ui-egui/src/canvas/previews.rs +++ b/crates/ui-egui/src/canvas/previews.rs @@ -1,6 +1,132 @@ -/// Paints animation preview overlays. -#[allow(dead_code)] // P3(canvas-w9): cv.A3 -pub(crate) fn paint(draw: &craft::ui::primitives::Draw<'_>, cx: &craft::ui::canvas::ToolContext<'_>, app: &crate::SlideApp) { - let _ = (draw, cx, app); - todo!("cv.A3") +//! Tool-owned drawing previews, mapped through the current canvas space. + +use craft::behavior::CanvasView; +use craft::behavior::kurbo::Point; +use craft::ui::canvas::ToolContext; +use craft::ui::canvas_controls::{Marquee, MarqueeLook, SmartGuides, SmartLine}; +use craft::ui::primitives::{DataWidth, Draw, HandleShape, PaintShape, StrokeWidth}; +use craft::ui::style::DataColor; +use deckcraft_engine::ToolKind; + +use super::handles::paint_handle; +use crate::SlideApp; + +/// Paints the active tool's held preview without consuming input or producing edits. +pub(crate) fn paint(draw: &Draw<'_>, cx: &ToolContext<'_>, app: &SlideApp) { + if app.anim_preview_active() { + return; + } + let Some(space) = cx.space() else { return }; + let Some(style) = cx.style() else { return }; + let view = &space.view; + let tool = &app.session.tool; + let accent = draw.role(|c| c.accent.solid); + let lines: Vec = tool + .guides + .iter() + .map(|g| { + let (a, b) = if g.vertical { + (Point::new(g.pos, g.from), Point::new(g.pos, g.to)) + } else { + (Point::new(g.from, g.pos), Point::new(g.to, g.pos)) + }; + SmartLine::new(a, b).dashed() + }) + .collect(); + cx.paint_smart_guides(&SmartGuides::new(&lines, &[])); + if let Some(rect) = tool.marquee { + cx.paint_marquee(&Marquee::rect("Selection band", rect).look(MarqueeLook::Band)); + } + if let Some(preview) = tool.preview { + paint_shape(draw, view, &tool.kind, preview); + } + let path = &tool.path_preview; + if path.len() > 1 { + let points = if tool.path_smooth && path.len() > 2 { + let data = deckcraft_engine::cmd::shape::freeform_path(path, false, true); + let outline = deckcraft_render::parse_path(&data); + let mut points = Vec::new(); + deckcraft_geom::preset_flatten(&outline, &mut |element| match element { + deckcraft_geom::PathEl::MoveTo(p) | deckcraft_geom::PathEl::LineTo(p) => points.push(view.to_screen(p)), + _ => {} + }); + points + } else { + path.iter().map(|p| view.to_screen(*p)).collect() + }; + draw.stroke(&PaintShape::Path(points), StrokeWidth::Medium, accent); + if tool.path_closing + && let Some(first) = path.first() + { + paint_handle(draw, style, HandleShape::Circle, view.to_screen(*first), true, false); + } + } + for site in &tool.sites { + paint_handle(draw, style, HandleShape::Circle, view.to_screen(*site), false, false); + } + if let Some(glue) = tool.glue { + paint_handle(draw, style, HandleShape::Circle, view.to_screen(glue), true, false); + } + if tool.ink_preview.len() > 1 + && let ToolKind::Ink { color, width, mode } = &tool.kind + { + let points = &tool.ink_preview; + let legacy = deckcraft_engine::model::ink_pressure_is_legacy(points); + let color = DataColor::new(egui::Color32::from_rgba_unmultiplied(color.r, color.g, color.b, color.a)); + let stroke = |draw: &Draw<'_>| { + if legacy || mode == "highlighter" { + // One translucent path avoids dark joints where highlighter segments meet. + let path = points.iter().map(|(x, y, _)| view.to_screen(Point::new(*x, *y))).collect(); + draw.stroke(&PaintShape::Path(path), DataWidth::points((*width * view.zoom.abs()).max(1.0) as f32), color); + } else { + for pair in points.windows(2) { + let [a, b] = pair else { continue }; + let wa = deckcraft_engine::model::ink_sample_width(*width, a.2, legacy); + let wb = deckcraft_engine::model::ink_sample_width(*width, b.2, legacy); + let width = ((wa + wb) * 0.5 * view.zoom.abs()).max(1.0) as f32; + let path = vec![view.to_screen(Point::new(a.0, a.1)), view.to_screen(Point::new(b.0, b.1))]; + draw.stroke(&PaintShape::Path(path), DataWidth::points(width), color); + } + } + }; + if mode == "highlighter" { + draw.faded(0.45, stroke); + } else { + stroke(draw); + } + } +} + +fn paint_shape(draw: &Draw<'_>, view: &CanvasView, tool: &ToolKind, x: deckcraft_geom::Xfrm) { + let preset = match tool { + ToolKind::Shape { preset } => preset.as_str(), + _ => "rect", + }; + let accent = draw.role(|c| c.accent.solid); + let diagonal = || { + let a = x.affine(); + PaintShape::Path(vec![view.to_screen(a * Point::ZERO), view.to_screen(a * Point::new(x.w, x.h))]) + }; + if preset == deckcraft_engine::tools::DRAW_TABLE { + if x.w < 0.5 || x.h < 0.5 { + draw.stroke(&diagonal(), StrokeWidth::Medium, accent); + } else { + let rect = craft::behavior::kurbo::Rect::new(x.x, x.y, x.x + x.w, x.y + x.h); + draw.stroke(&PaintShape::Polygon(view.quad(rect).to_vec()), StrokeWidth::Thin, accent); + } + return; + } + if deckcraft_geom::preset::is_line_like(preset) { + draw.stroke(&diagonal(), StrokeWidth::Medium, accent); + return; + } + let rect = egui::Rect::from_two_pos(view.to_screen(Point::new(x.x, x.y)), view.to_screen(Point::new(x.x + x.w, x.y + x.h))); + if rect.width() < 1.0 || rect.height() < 1.0 { + return; + } + for points in crate::ribbon::preset_outline(rect, preset) { + let shape = PaintShape::Polygon(points); + draw.faded(0.25, |draw| draw.fill(&shape, accent)); + draw.stroke(&shape, StrokeWidth::Medium, accent); + } }