DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge tool-cursors: Canvas: tools show their own cursors (Direct Selection, Pen states)
aionggba6fcbe26a60
diff --cc crates/tools/src/tests.rsindex b04995993,9c5f6bb0a..4c196606c--- a/crates/tools/src/tests.rs+++ b/crates/tools/src/tests.rs@@@ -717,492 -706,52 +717,540 @@@ fn pen_click_snaps_to_a_guide()assert!((x - 100.0).abs() < 1e-6, "anchor x {x}");}+/// A text frame with overset text (selected), an empty frame and a threaded pair x → y, on page 1.+struct ThreadDoc {+ d: Document,+ l: CanvasLayout,+ s: Selection,+ c: Cache,+ /// The selected text frame.+ a: ItemId,+ /// An empty (unassigned) frame.+ e: ItemId,+ x: ItemId,+ y: ItemId,+}++fn thread_doc() -> ThreadDoc {+ let mut d = Document::new(&NewDocument { pages: 2, ..Default::default() });+ let lid = d.default_layer();+ let (a, _) = d+ .add_text_frame(SpreadRef::Doc(0), Rect::new(100.0, 100.0, 250.0, 160.0), lid, &"Words flow on. ".repeat(40), ParaFormat::default())+ .unwrap();+ let e = ItemId(d.alloc());+ d.insert_item(SpreadRef::Doc(0), Item::new(e, lid, Shape::Rectangle, shapes::rectangle(Rect::new(100.0, 400.0, 250.0, 500.0))), None).unwrap();+ let (x, _) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(300.0, 100.0, 450.0, 160.0), lid, "x", ParaFormat::default()).unwrap();+ let (y, _) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(300.0, 400.0, 450.0, 500.0), lid, "", ParaFormat::default()).unwrap();+ d.thread(x, y).unwrap();+ let l = CanvasLayout::new(&d, false);+ ThreadDoc { s: Selection::items(vec![a]), c: Cache::new(), l, d, a, e, x, y }+}++impl ThreadDoc {+ fn cx(&self) -> ToolContext<'_> {+ ctx(&self.d, &self.s, &self.c, &self.l)+ }+ /// Canvas point of a spread-0 point.+ fn at(&self, x: f64, y: f64) -> Point {+ self.l.to_canvas(SpreadRef::Doc(0), Point::new(x, y))+ }+ fn port(&self, id: ItemId, out: bool) -> Point {+ thread::port_center(&self.d, &self.l, id, out, 1.0).unwrap()+ }+}++fn click(t: &mut Box<dyn Tool>, cx: &ToolContext, p: Point, mods: Mods) -> Vec<Action> {+ let mut v = t.pointer(cx, &PointerEvent::new(PointerKind::Down, p.x, p.y).with_mods(mods));+ v.extend(t.pointer(cx, &PointerEvent::new(PointerKind::Up, p.x, p.y).with_mods(mods)));+ v+}++fn thread_exec(p: serde_json::Value) -> Vec<Action> {+ vec![Action::Exec("text.thread".into(), p)]+}++#[test]+fn out_port_loads_text_and_a_click_threads_an_empty_frame() {+ let td = thread_doc();+ let cx = td.cx();+ let mut t = create("selection");+ let port = td.port(td.a, true);+ // The out port sits on the right edge, 12 px above the bottom-right corner.+ assert!((port - td.at(250.0, 148.0)).hypot() < 1e-9, "{port:?}");+ // A press just off the port's centre still loads (any zoom: tolerance is in pixels).+ assert!(click(&mut t, &cx, port + designcraft_geom::Vec2::new(3.0, -3.0), Mods::default()).is_empty());+ assert_eq!(t.cursor(&cx, td.at(500.0, 700.0), Mods::default()), Cursor::LoadedText);+ let over_e = td.at(175.0, 450.0);+ assert_eq!(t.cursor(&cx, over_e, Mods::default()), Cursor::ThreadLink);+ // A frame something already threads into can't join; neither can the loaded frame itself.+ assert_eq!(t.cursor(&cx, td.at(375.0, 450.0), Mods::default()), Cursor::NotAllowed);+ assert_eq!(t.cursor(&cx, td.at(175.0, 130.0), Mods::default()), Cursor::LoadedText);+ assert_eq!(click(&mut t, &cx, over_e, Mods::default()), thread_exec(serde_json::json!({"from": td.a.0, "to": td.e.0})));+ // Unloaded again: the cursor and clicks are back to normal.+ assert_eq!(t.cursor(&cx, over_e, Mods::default()), Cursor::Arrow);+}++#[test]+fn ports_hit_at_any_zoom() {+ let td = thread_doc();+ let mut cx = td.cx();+ cx.zoom = 4.0;+ let port = thread::port_center(&td.d, &td.l, td.a, false, 4.0).unwrap();+ assert!((port - td.at(100.0, 100.0 + 14.5 / 4.0)).hypot() < 1e-9);+ assert_eq!(thread::port_at(&cx, port + designcraft_geom::Vec2::new(1.5, 1.5)), Some((td.a, false)));+ assert_eq!(thread::port_at(&cx, port + designcraft_geom::Vec2::new(4.0, 0.0)), None, "8 screen px away at 4×");+}++#[test]+fn loaded_click_on_empty_page_makes_a_column_frame() {+ let td = thread_doc();+ let cx = td.cx();+ let mut t = create("selection");+ click(&mut t, &cx, td.port(td.a, true), Mods::default());+ let col = td.d.spreads[0].pages[0].column_rects()[0];+ let a = click(&mut t, &cx, td.at(200.0, 600.0), Mods::default());+ assert_eq!(a, thread_exec(serde_json::json!({"from": td.a.0, "rect": [col.x0, 600.0, col.x1, col.y1], "spread": {"kind": "doc", "index": 0}})));+}++#[test]+fn loaded_drag_makes_a_frame_of_that_size_and_shift_autoflows() {+ let td = thread_doc();+ let cx = td.cx();+ let mut t = create("selection");+ click(&mut t, &cx, td.port(td.a, true), Mods::default());+ let (p0, p1) = (td.at(320.0, 560.0), td.at(470.0, 700.0));+ let shift = Mods { shift: true, ..Default::default() };+ assert!(t.pointer(&cx, &PointerEvent::new(PointerKind::Down, p0.x, p0.y).with_mods(shift)).is_empty());+ assert!(t.pointer(&cx, &PointerEvent::new(PointerKind::Drag, p1.x, p1.y).with_mods(shift)).is_empty());+ assert_eq!(t.overlays(&cx), vec![Overlay::Marquee(Rect::from_points(p0, p1))]);+ let a = t.pointer(&cx, &PointerEvent::new(PointerKind::Up, p1.x, p1.y).with_mods(shift));+ assert_eq!(+ a,+ thread_exec(+ serde_json::json!({"from": td.a.0, "rect": [320.0, 560.0, 470.0, 700.0], "spread": {"kind": "doc", "index": 0}, "autoflow": true})+ )+ );+ assert_eq!(t.cursor(&cx, td.at(500.0, 700.0), Mods::default()), Cursor::Arrow, "autoflow unloads");+}++#[test]+fn alt_click_stays_loaded() {+ let td = thread_doc();+ let cx = td.cx();+ let mut t = create("selection");+ click(&mut t, &cx, td.port(td.a, true), Mods::default());+ let alt = Mods { alt: true, ..Default::default() };+ assert_eq!(click(&mut t, &cx, td.at(175.0, 450.0), alt), thread_exec(serde_json::json!({"from": td.a.0, "to": td.e.0})));+ assert_eq!(t.cursor(&cx, td.at(500.0, 700.0), Mods::default()), Cursor::LoadedText);+}++#[test]+fn escape_unloads_and_keeps_the_selection() {+ let td = thread_doc();+ let cx = td.cx();+ let mut t = create("selection");+ click(&mut t, &cx, td.port(td.a, true), Mods::default());+ assert_eq!(t.key(&cx, ToolKey::Escape, Mods::default()), vec![Action::Cancel]);+ assert_eq!(t.cursor(&cx, td.at(500.0, 700.0), Mods::default()), Cursor::Arrow);+ // An ordinary click on empty space deselects again.+ let p = td.at(500.0, 700.0);+ assert_eq!(+ t.pointer(&cx, &PointerEvent::new(PointerKind::Down, p.x, p.y)),+ vec![Action::Exec("selection.set".into(), serde_json::json!({"ids": []}))]+ );+}++#[test]+fn loaded_click_on_the_linked_frame_unthreads() {+ // x is selected; its out port links to y.+ let td = thread_doc();+ let td = ThreadDoc { s: Selection::items(vec![td.x]), ..td };+ let cx = td.cx();+ let mut t = create("selection");+ click(&mut t, &cx, td.port(td.x, true), Mods::default());+ let over_y = td.at(375.0, 450.0);+ assert_eq!(t.cursor(&cx, over_y, Mods::default()), Cursor::Unthread);+ assert_eq!(+ click(&mut t, &cx, over_y, Mods::default()),+ vec![Action::Exec("text.unthread".into(), serde_json::json!({"frame": td.x.0, "side": "after"}))]+ );+ // From y's in port, x is the frame before.+ let td = ThreadDoc { s: Selection::items(vec![td.y]), ..td };+ let cx = td.cx();+ click(&mut t, &cx, td.port(td.y, false), Mods::default());+ assert_eq!(+ click(&mut t, &cx, td.at(375.0, 130.0), Mods::default()),+ vec![Action::Exec("text.unthread".into(), serde_json::json!({"frame": td.y.0, "side": "before"}))]+ );+}++#[test]+fn in_port_threads_in_front() {+ let td = thread_doc();+ let cx = td.cx();+ let mut t = create("selection");+ click(&mut t, &cx, td.port(td.a, false), Mods::default());+ assert_eq!(click(&mut t, &cx, td.at(175.0, 450.0), Mods::default()), thread_exec(serde_json::json!({"from": td.e.0, "to": td.a.0})));+}++#[test]+fn double_click_on_a_port_breaks_its_link() {+ let td = thread_doc();+ let td = ThreadDoc { s: Selection::items(vec![td.x]), ..td };+ let cx = td.cx();+ // A double click sent as such (control channel), and the canvas's second press.+ let second = |p: Point| [PointerEvent::new(PointerKind::DoubleClick, p.x, p.y), PointerEvent::new(PointerKind::Down, p.x, p.y).with_clicks(2)];+ let p = td.port(td.x, true);+ for ev in second(p) {+ let mut t = create("selection");+ click(&mut t, &cx, p, Mods::default());+ let a = t.pointer(&cx, &ev);+ assert_eq!(a, vec![Action::Exec("text.unthread".into(), serde_json::json!({"frame": td.x.0, "side": "after"}))], "{ev:?}");+ assert!(t.pointer(&cx, &PointerEvent::new(PointerKind::Up, p.x, p.y)).is_empty());+ assert_eq!(t.cursor(&cx, td.at(500.0, 700.0), Mods::default()), Cursor::Arrow);+ // The in port of the first frame has no link: nothing to break.+ let q = td.port(td.x, false);+ for ev in second(q) {+ assert!(t.pointer(&cx, &ev).is_empty(), "{ev:?}");+ }+ }+}++#[test]+fn clicks_in_a_row_edit_and_select_text() {+ let mut d = Document::new(&NewDocument::default());+ let lid = d.default_layer();+ let (fid, _) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(100.0, 100.0, 300.0, 200.0), lid, "Two words", ParaFormat::default()).unwrap();+ let (s, c, l) = (Selection::default(), Cache::new(), CanvasLayout::new(&d, false));+ let cx = ctx(&d, &s, &c, &l);+ let off = l.xf(SpreadRef::Doc(0)).translation();+ let (x, y) = (150.0 + off.x, 110.0 + off.y);+ let press = |n: u8| PointerEvent::new(PointerKind::Down, x, y).with_clicks(n);+ // Selection tool: the second press of a double click edits the text there.+ let mut t = create("selection");+ let a = t.pointer(&cx, &press(1));+ assert!(matches!(&a[0], Action::Exec(id, _) if id == "selection.set"), "{a:?}");+ t.pointer(&cx, &PointerEvent::new(PointerKind::Up, x, y));+ let a = t.pointer(&cx, &press(2));+ assert_eq!(a.first(), Some(&Action::SwitchTool("type".into())));+ assert!(matches!(&a[1], Action::Exec(id, p) if id == "text.placeCaret" && p["frame"] == fid.0), "{a:?}");+ // Type tool: word, line, paragraph, then the story.+ let mut t = create("type");+ let unit = |a: Vec<Action>| match a.as_slice() {+ [Action::Exec(id, p)] if id == "text.selectAt" => p["unit"].as_str().map(str::to_string),+ [Action::Exec(id, _)] if id == "text.placeCaret" => None,+ other => panic!("{other:?}"),+ };+ assert_eq!(unit(t.pointer(&cx, &press(1))), None);+ for (n, want) in [(2, "word"), (3, "line"), (4, "paragraph"), (5, "story"), (9, "story")] {+ assert_eq!(unit(t.pointer(&cx, &press(n))).as_deref(), Some(want), "{n} clicks");+ }+ // A double click sent as such (control channel) selects a word too.+ assert_eq!(unit(t.pointer(&cx, &PointerEvent::new(PointerKind::DoubleClick, x, y))).as_deref(), Some("word"));+ // Moving a little while pressed keeps the unit selected.+ assert!(t.pointer(&cx, &PointerEvent::new(PointerKind::Drag, x + 2.0, y)).is_empty());+}++/// A 100×100 frame at (100, 100) holding a 200×100 graphic that overhangs it.+fn graphic_frame(doc: &mut Document) -> ItemId {+ let id = ItemId(doc.alloc());+ let mut item = Item::new(id, doc.default_layer(), Shape::Rectangle, shapes::rectangle(Rect::new(100.0, 100.0, 200.0, 200.0)));+ item.content = designcraft_doc::Content::Graphic(designcraft_doc::Graphic {+ asset: designcraft_doc::AssetId(1),+ size: (200.0, 100.0),+ xf: designcraft_geom::Affine::translate((50.0, 100.0)),+ auto_fit: Default::default(),+ fit_align: 4,+ crop: [0.0; 4],+ wrap: Default::default(),+ });+ doc.insert_item(SpreadRef::Doc(0), item, None).unwrap();+ id+}++fn preview_params(a: &[Action]) -> serde_json::Value {+ match a.last() {+ Some(Action::Preview(cmd, p)) if cmd == "transform.move" => p.clone(),+ other => panic!("{other:?}"),+ }+}++#[test]+fn direct_selection_drags_content_inside_its_frame() {+ let mut d = Document::new(&NewDocument::default());+ let f = graphic_frame(&mut d);+ let (c, l) = (Cache::new(), CanvasLayout::new(&d, false));+ let none = Selection::default();+ let mut t = create("directSelection");+ let at = spread_pt(&l, 120.0, 130.0);+ let a = t.pointer(&ctx(&d, &none, &c, &l), &PointerEvent::new(PointerKind::Down, at.x, at.y));+ assert_eq!(a, vec![Action::Exec("selection.set".into(), serde_json::json!({"ids": [f.0], "content": true}))]);+ let sel = Selection { content: true, ..Selection::items(vec![f]) };+ let cx = ctx(&d, &sel, &c, &l);+ let a = t.pointer(&cx, &PointerEvent::new(PointerKind::Drag, at.x + 15.0, at.y - 4.0));+ assert_eq!(a.first(), Some(&Action::Begin("Move Content".into())));+ let p = preview_params(&a);+ assert_eq!(+ (p["dx"].as_f64(), p["dy"].as_f64(), &p["content"], &p["ids"]),+ (Some(15.0), Some(-4.0), &serde_json::json!(true), &serde_json::json!([f.0]))+ );+ assert_eq!(t.cursor(&cx, at, Mods::default()), Cursor::HandGrab);+ assert!(t.overlays(&cx).contains(&Overlay::ContentGhost(f)));+ // Shift keeps the drag on one axis.+ let a = t.pointer(&cx, &PointerEvent::new(PointerKind::Drag, at.x + 15.0, at.y - 4.0).with_mods(Mods { shift: true, ..Default::default() }));+ let p = preview_params(&a);+ assert_eq!((p["dx"].as_f64(), p["dy"].as_f64()), (Some(15.0), Some(0.0)));+ assert_eq!(t.pointer(&cx, &PointerEvent::new(PointerKind::Up, at.x + 15.0, at.y)), vec![Action::Commit]);+}++#[test]+fn selection_tool_grabber_moves_content_elsewhere_moves_the_frame() {+ let mut d = Document::new(&NewDocument::default());+ let f = graphic_frame(&mut d);+ let (c, l) = (Cache::new(), CanvasLayout::new(&d, false));+ let frame = Selection::items(vec![f]);+ let content = Selection { content: true, ..Selection::items(vec![f]) };+ let centre = spread_pt(&l, 150.0, 150.0);+ let mut t = create("selection");+ // Hovering the grabber shows the hand; it is there on hover before any click, too.+ assert_eq!(t.cursor(&ctx(&d, &Selection::default(), &c, &l), centre, Mods::default()), Cursor::Hand);+ let g = select::grabbers(&ctx(&d, &Selection::default(), &c, &l), Some(centre));+ assert!(matches!(g.as_slice(), [(id, p)] if *id == f && near(*p, centre)), "{g:?}");+ // Press on the grabber: the content is selected; the drag moves it.+ let a = t.pointer(&ctx(&d, &frame, &c, &l), &PointerEvent::new(PointerKind::Down, centre.x, centre.y));+ assert_eq!(a, vec![Action::Exec("selection.set".into(), serde_json::json!({"ids": [f.0], "content": true}))]);+ let a = t.pointer(&ctx(&d, &content, &c, &l), &PointerEvent::new(PointerKind::Drag, centre.x - 20.0, centre.y + 10.0));+ let p = preview_params(&a);+ assert_eq!((p["dx"].as_f64(), p["dy"].as_f64(), &p["content"]), (Some(-20.0), Some(10.0), &serde_json::json!(true)));+ assert_eq!(t.pointer(&ctx(&d, &content, &c, &l), &PointerEvent::new(PointerKind::Up, centre.x - 20.0, centre.y + 10.0)), vec![Action::Commit]);+ // A press elsewhere in the frame keeps the content selection; dragging selects and moves the frame.+ let edge = spread_pt(&l, 115.0, 185.0);+ assert!(t.pointer(&ctx(&d, &content, &c, &l), &PointerEvent::new(PointerKind::Down, edge.x, edge.y)).is_empty());+ let a = t.pointer(&ctx(&d, &content, &c, &l), &PointerEvent::new(PointerKind::Drag, edge.x + 30.0, edge.y));+ assert_eq!(a[0], Action::Exec("selection.set".into(), serde_json::json!({"ids": [f.0]})));+ assert_eq!(a[1], Action::Begin("Move".into()));+ let p = preview_params(&a);+ assert_eq!((p["dx"].as_f64(), p.get("content"), p.get("ids")), (Some(30.0), None, None));+ t.pointer(&ctx(&d, &frame, &c, &l), &PointerEvent::new(PointerKind::Up, edge.x + 30.0, edge.y));+ // With the frame selected, a drag away from the grabber moves the frame.+ t.pointer(&ctx(&d, &frame, &c, &l), &PointerEvent::new(PointerKind::Down, edge.x, edge.y));+ let a = t.pointer(&ctx(&d, &frame, &c, &l), &PointerEvent::new(PointerKind::Drag, edge.x, edge.y + 30.0));+ assert_eq!(a[0], Action::Begin("Move".into()));+ assert_eq!(preview_params(&a).get("content"), None);+}++#[test]+fn double_click_toggles_frame_and_content() {+ let mut d = Document::new(&NewDocument::default());+ let f = graphic_frame(&mut d);+ let (c, l) = (Cache::new(), CanvasLayout::new(&d, false));+ let at = spread_pt(&l, 120.0, 180.0);+ let mut t = create("selection");+ let frame = Selection::items(vec![f]);+ let content = Selection { content: true, ..Selection::items(vec![f]) };+ let set = |content: bool| vec![Action::Exec("selection.set".into(), serde_json::json!({"ids": [f.0], "content": content}))];+ // The canvas's presses in a row: the first selects the frame, the second its content.+ let a = t.pointer(&ctx(&d, &Selection::default(), &c, &l), &PointerEvent::new(PointerKind::Down, at.x, at.y));+ assert_eq!(a, vec![Action::Exec("selection.set".into(), serde_json::json!({"ids": [f.0], "content": false}))]);+ t.pointer(&ctx(&d, &frame, &c, &l), &PointerEvent::new(PointerKind::Up, at.x, at.y));+ assert_eq!(t.pointer(&ctx(&d, &frame, &c, &l), &PointerEvent::new(PointerKind::Down, at.x, at.y).with_clicks(2)), set(true));+ assert!(t.pointer(&ctx(&d, &content, &c, &l), &PointerEvent::new(PointerKind::Up, at.x, at.y)).is_empty());+ // With the content selected, the first press keeps it and the second selects the frame.+ assert!(t.pointer(&ctx(&d, &content, &c, &l), &PointerEvent::new(PointerKind::Down, at.x, at.y)).is_empty());+ t.pointer(&ctx(&d, &content, &c, &l), &PointerEvent::new(PointerKind::Up, at.x, at.y));+ assert_eq!(t.pointer(&ctx(&d, &content, &c, &l), &PointerEvent::new(PointerKind::Down, at.x, at.y).with_clicks(2)), set(false));+ // A double click sent as such (control channel) toggles too.+ let dbl = PointerEvent::new(PointerKind::DoubleClick, at.x, at.y);+ assert_eq!(t.pointer(&ctx(&d, &frame, &c, &l), &dbl), set(true));+ assert_eq!(t.pointer(&ctx(&d, &content, &c, &l), &dbl), set(false));+}++/// A 200 × 150 pt rectangle frame, selected.+fn live_corner_doc() -> (Document, ItemId) {+ let mut d = Document::new(&NewDocument::default());+ let id = add_rect(&mut d, Rect::new(100.0, 100.0, 300.0, 250.0));+ (d, id)+}++fn corner_preview(a: &[Action]) -> Vec<(String, f64)> {+ let p = match a {+ [Action::Preview(cmd, p)] | [Action::Exec(cmd, p)] if cmd == "object.cornerOptions" => p,+ other => panic!("{other:?}"),+ };+ p["corners"].as_array().unwrap().iter().map(|c| (c["shape"].as_str().unwrap().to_string(), c["size"].as_f64().unwrap())).collect()+}++fn diamonds(o: &[Overlay]) -> Vec<Point> {+ o.iter().filter_map(|o| if let Overlay::LiveCornerDiamond(p) = o { Some(*p) } else { None }).collect()+}++#[test]+fn live_corners_click_widget_then_drag_rounds_all_corners() {+ let (d, id) = live_corner_doc();+ let s = Selection { items: vec![id], ..Default::default() };+ let (c, l) = (Cache::new(), CanvasLayout::new(&d, false));+ let cx = ctx(&d, &s, &c, &l);+ let at = |x, y| spread_pt(&l, x, y);+ let mut t = create("selection");+ // The widget sits on the right edge, a little below the top-right corner.+ let w = at(300.0, 111.5);+ assert!(t.overlays(&cx).iter().any(|o| matches!(o, Overlay::LiveCornerWidget(p) if near(*p, w))));+ assert!(diamonds(&t.overlays(&cx)).is_empty());+ assert_eq!(t.pointer(&cx, &PointerEvent::new(PointerKind::Down, w.x, w.y)), vec![Action::Handled]);+ assert!(t.pointer(&cx, &PointerEvent::new(PointerKind::Up, w.x, w.y)).is_empty());+ // Without corner sizes the diamonds sit 12 px along the top and bottom edges.+ let want = [at(112.0, 100.0), at(288.0, 100.0), at(288.0, 250.0), at(112.0, 250.0)];+ let got = diamonds(&t.overlays(&cx));+ assert!(got.len() == 4 && got.iter().zip(want).all(|(g, w)| near(*g, w)), "{got:?}");+ assert!(!t.overlays(&cx).iter().any(|o| matches!(o, Overlay::LiveCornerWidget(_))));+ // Drag the top-left diamond 20 pt toward the centre.+ let a = t.pointer(&cx, &PointerEvent::new(PointerKind::Down, want[0].x, want[0].y));+ assert_eq!(a, vec![Action::Begin("Corner Options".into())]);+ let a = t.pointer(&cx, &PointerEvent::new(PointerKind::Drag, want[0].x + 20.0, want[0].y + 3.0));+ assert_eq!(corner_preview(&a), vec![("rounded".to_string(), 20.0); 4]);+ // The size stops at half the shorter side.+ let a = t.pointer(&cx, &PointerEvent::new(PointerKind::Drag, want[0].x + 500.0, want[0].y));+ assert_eq!(corner_preview(&a), vec![("rounded".to_string(), 75.0); 4]);+ assert_eq!(t.pointer(&cx, &PointerEvent::new(PointerKind::Up, want[0].x + 500.0, want[0].y)), vec![Action::Commit]);+}++#[test]+fn live_corners_shift_drag_and_alt_click_change_one_corner() {+ let (mut d, id) = live_corner_doc();+ d.item_mut(id).unwrap().corners = designcraft_geom::corners::CornerOptions::uniform(designcraft_geom::corners::CornerShape::Rounded, 30.0);+ let s = Selection { items: vec![id], ..Default::default() };+ let (c, l) = (Cache::new(), CanvasLayout::new(&d, false));+ let cx = ctx(&d, &s, &c, &l);+ let at = |x, y| spread_pt(&l, x, y);+ let mut t = create("selection");+ let w = at(300.0, 111.5);+ t.pointer(&cx, &PointerEvent::new(PointerKind::Down, w.x, w.y));+ t.pointer(&cx, &PointerEvent::new(PointerKind::Up, w.x, w.y));+ // With 30 pt corners the diamonds sit 30 pt along the edges.+ let tr = at(270.0, 100.0);+ assert!(diamonds(&t.overlays(&cx)).iter().any(|p| near(*p, tr)));+ let shift = Mods { shift: true, ..Default::default() };+ t.pointer(&cx, &PointerEvent::new(PointerKind::Down, tr.x, tr.y).with_mods(shift));+ let a = t.pointer(&cx, &PointerEvent::new(PointerKind::Drag, tr.x - 10.0, tr.y).with_mods(shift));+ let r = |n: f64| ("rounded".to_string(), n);+ assert_eq!(corner_preview(&a), vec![r(30.0), r(40.0), r(30.0), r(30.0)]);+ t.pointer(&cx, &PointerEvent::new(PointerKind::Up, tr.x - 10.0, tr.y));+ // Alt-click steps the bottom-right corner's shape; Shift+Alt steps every corner.+ let br = at(270.0, 250.0);+ let alt = Mods { alt: true, ..Default::default() };+ let a = t.pointer(&cx, &PointerEvent::new(PointerKind::Down, br.x, br.y).with_mods(alt));+ assert_eq!(corner_preview(&a), vec![r(30.0), r(30.0), ("inverseRounded".to_string(), 30.0), r(30.0)]);+ assert!(t.pointer(&cx, &PointerEvent::new(PointerKind::Drag, br.x - 30.0, br.y).with_mods(alt)).is_empty());+ assert!(t.pointer(&cx, &PointerEvent::new(PointerKind::Up, br.x - 30.0, br.y)).is_empty());+ let a = t.pointer(&cx, &PointerEvent::new(PointerKind::Down, br.x, br.y).with_mods(Mods { alt: true, shift: true, ..Default::default() }));+ assert_eq!(corner_preview(&a), vec![("inverseRounded".to_string(), 30.0); 4]);+}++#[test]+fn live_corners_end_on_escape_empty_click_and_other_selection() {+ let (mut d, id) = live_corner_doc();+ let other = add_rect(&mut d, Rect::new(400.0, 100.0, 500.0, 200.0));+ let s = Selection { items: vec![id], ..Default::default() };+ let (c, l) = (Cache::new(), CanvasLayout::new(&d, false));+ let cx = ctx(&d, &s, &c, &l);+ let w = spread_pt(&l, 300.0, 111.5);+ let mut t = create("selection");+ let enter = |t: &mut Box<dyn Tool>| {+ t.pointer(&cx, &PointerEvent::new(PointerKind::Down, w.x, w.y));+ t.pointer(&cx, &PointerEvent::new(PointerKind::Up, w.x, w.y));+ assert_eq!(diamonds(&t.overlays(&cx)).len(), 4);+ };+ // Escape leaves corner editing and keeps the selection; the next Escape deselects.+ enter(&mut t);+ assert_eq!(t.key(&cx, ToolKey::Escape, Mods::default()), vec![Action::Handled]);+ assert!(diamonds(&t.overlays(&cx)).is_empty());+ assert_eq!(t.key(&cx, ToolKey::Escape, Mods::default()), vec![Action::Exec("selection.set".into(), serde_json::json!({"ids": []}))]);+ // A click on empty canvas.+ enter(&mut t);+ let e = spread_pt(&l, 350.0, 400.0);+ t.pointer(&cx, &PointerEvent::new(PointerKind::Down, e.x, e.y));+ t.pointer(&cx, &PointerEvent::new(PointerKind::Up, e.x, e.y));+ assert!(diamonds(&t.overlays(&cx)).is_empty());+ // Another selection.+ enter(&mut t);+ let s2 = Selection { items: vec![other], ..Default::default() };+ let cx2 = ctx(&d, &s2, &c, &l);+ assert!(diamonds(&t.overlays(&cx2)).is_empty());+ t.pointer(&cx2, &PointerEvent::new(PointerKind::Move, 0.0, 0.0));+ assert!(diamonds(&t.overlays(&cx)).is_empty());+ // A frame too small on screen has no widget.+ let mut far = ctx(&d, &s, &c, &l);+ far.zoom = 0.1;+ assert!(!t.overlays(&far).iter().any(|o| matches!(o, Overlay::LiveCornerWidget(_))));+}++#[test]+fn live_corner_diamonds_follow_a_rotated_frame() {+ let mut d = Document::new(&NewDocument::default());+ let id = add_rotated(&mut d, Rect::new(100.0, 100.0, 300.0, 250.0), 30.0);+ let s = Selection { items: vec![id], ..Default::default() };+ let (c, l) = (Cache::new(), CanvasLayout::new(&d, false));+ let cx = ctx(&d, &s, &c, &l);+ let m = l.xf(SpreadRef::Doc(0)) * d.item(id).unwrap().xf;+ let mut t = create("selection");+ // The widget: 11.5 pt from the top-right corner along the turned right edge.+ let w = m * Point::new(300.0, 111.5);+ t.pointer(&cx, &PointerEvent::new(PointerKind::Down, w.x, w.y));+ t.pointer(&cx, &PointerEvent::new(PointerKind::Up, w.x, w.y));+ let tl = m * Point::new(112.0, 100.0);+ assert!(diamonds(&t.overlays(&cx)).iter().any(|p| near(*p, tl)), "{:?}", t.overlays(&cx));+ // Dragging along the turned top edge sets the size.+ t.pointer(&cx, &PointerEvent::new(PointerKind::Down, tl.x, tl.y));+ let to = m * Point::new(137.0, 100.0);+ let a = t.pointer(&cx, &PointerEvent::new(PointerKind::Drag, to.x, to.y));+ let got = corner_preview(&a);+ assert!(got.iter().all(|(shape, size)| shape == "rounded" && (size - 25.0).abs() < 1e-6), "{got:?}");+ #[test]+ fn pen_cursor_shows_add_delete_and_close() {+ let mut d = Document::new(&NewDocument::default());+ let lid = d.default_layer();+ let (fid, _) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(100.0, 100.0, 200.0, 200.0), lid, "", ParaFormat::default()).unwrap();+ let (c, l) = (Cache::new(), CanvasLayout::new(&d, false));+ let selected = Selection::items(vec![fid]);+ let none = Selection::default();+ let off = l.xf(SpreadRef::Doc(0)).translation();+ let at = |x: f64, y: f64| Point::new(x + off.x, y + off.y);+ let mut t = create("pen");+ let m = Mods::default();++ let cx = ctx(&d, &selected, &c, &l);+ assert_eq!(t.cursor(&cx, at(200.0, 100.0), m), Cursor::PenDelete, "on a selected path's anchor");+ assert_eq!(t.cursor(&cx, at(150.0, 101.0), m), Cursor::PenAdd, "on a selected path's outline");+ assert_eq!(t.cursor(&cx, at(150.0, 150.0), m), Cursor::Pen, "inside, away from the outline");+ let click = |t: &mut Box<dyn Tool>, cx: &ToolContext, p: Point| {+ let down = t.pointer(cx, &PointerEvent::new(PointerKind::Down, p.x, p.y));+ let up = t.pointer(cx, &PointerEvent::new(PointerKind::Up, p.x, p.y));+ (down, up)+ };+ let (down, up) = click(&mut t, &cx, at(200.0, 100.0));+ assert_eq!(down, vec![Action::Exec("path.deleteAnchor".into(), serde_json::json!({"id": fid.0, "subpath": 0, "anchor": 1}))]);+ assert!(up.is_empty() && !t.busy(), "deleting starts no path");+ let (down, _) = click(&mut t, &cx, at(150.0, 101.0));+ assert!(matches!(&down[..], [Action::Exec(cmd, p)] if cmd == "path.addAnchor" && p["id"] == fid.0), "{down:?}");++ // Unselected paths are drawn over; the first anchor of the path being drawn closes it.+ let cx = ctx(&d, &none, &c, &l);+ assert_eq!(t.cursor(&cx, at(200.0, 100.0), m), Cursor::Pen, "unselected path");+ click(&mut t, &cx, at(300.0, 300.0));+ assert_eq!(t.cursor(&cx, at(300.0, 300.0), m), Cursor::Pen, "one anchor cannot close yet");+ click(&mut t, &cx, at(400.0, 300.0));+ assert_eq!(t.cursor(&cx, at(301.0, 301.0), m), Cursor::PenClose, "on the first anchor");+ assert_eq!(t.cursor(&cx, at(350.0, 350.0), m), Cursor::Pen, "elsewhere while drawing");+ }++ #[test]+ fn anchor_tools_show_pen_state_cursors() {+ let d = Document::new(&NewDocument::default());+ let (s, c, l) = (Selection::default(), Cache::new(), CanvasLayout::new(&d, false));+ let cx = ctx(&d, &s, &c, &l);+ let p = Point::new(10.0, 10.0);+ let cursor = |id: &str| create(id).cursor(&cx, p, Mods::default());+ assert_eq!(cursor("addAnchor"), Cursor::PenAdd);+ assert_eq!(cursor("deleteAnchor"), Cursor::PenDelete);+ assert_eq!(cursor("convertDirection"), Cursor::PenConvert);}diff --cc crates/ui-egui/src/canvas.rsindex e62ba9fd5,d8888885f..8067258bd--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@@ -371,37 -337,8 +371,38 @@@ pub fn show(app: &mut DesignApp, ui: &m} else {app.session.cursor(xf.to_canvas(p), ui.input(|i| mods(i, false)), app.view_info())};- ui.ctx().set_cursor_icon(cursor_icon(c));+ let icon = crate::cursors::show(ui.ctx(), &mut app.cursors, c, p);+ ui.ctx().set_cursor_icon(icon);+ draw_cursor_badge(&painter, p, c, &t);+ }+}++/// The loaded text cursor's badge beside the system cursor: lines of text, a closed chain+/// (a click threads) or a broken one (a click unthreads). Drawn in code.+fn draw_cursor_badge(painter: &egui::Painter, p: Pos2, c: Cursor, t: &Tokens) {+ if !matches!(c, Cursor::LoadedText | Cursor::ThreadLink | Cursor::Unthread) {+ return;+ }+ let r = Rect::from_min_size(p + vec2(12.0, 12.0), vec2(20.0, 16.0));+ painter.rect_filled(r, 3.0, t.measure_bg);+ let ink = Stroke::new(1.5, t.measure_text);+ let c0 = r.center();+ match c {+ Cursor::LoadedText => {+ for (dy, w) in [(-4.0, 12.0), (0.0, 12.0), (4.0, 8.0)] {+ painter.line_segment([pos2(c0.x - 6.0, c0.y + dy), pos2(c0.x - 6.0 + w, c0.y + dy)], ink);+ }+ }+ _ => {+ let gap = if c == Cursor::Unthread { 2.5 } else { -2.0 };+ for side in [-1.0, 1.0] {+ let link = Rect::from_center_size(pos2(c0.x + side * (3.5 + gap / 2.0), c0.y), vec2(9.0, 6.0));+ painter.rect_stroke(link, 3.0, ink, StrokeKind::Middle);+ }+ if c == Cursor::Unthread {+ painter.line_segment([pos2(c0.x + 2.0, c0.y - 6.0), pos2(c0.x - 2.0, c0.y + 6.0)], ink);+ }+ }}}@@@ -2447,409 -1776,33 +2423,436 @@@ mod tests}}+ /// The live-corner widget draws on a selected rectangle frame; clicking it draws the diamonds.+ #[test]+ fn live_corner_widget_and_diamonds_draw() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ app.run("frame.create", json!({"rect": [100, 100, 300, 250], "content": "graphic"})).unwrap();+ let id = app.session.doc().unwrap().doc.spreads[0].items[0].id;+ app.run("selection.set", json!({"ids": [id.0]})).unwrap();+ let mut h = Harness::builder().with_size(vec2(1440.0, 900.0)).with_pixels_per_point(1.0).wgpu().build_ui_state(+ |ui, app: &mut DesignApp| {+ let ctx = ui.ctx().clone();+ app.logic(&ctx);+ app.ui(ui);+ },+ app,+ );+ h.run_steps(4);+ let screen = |h: &Harness<'_, DesignApp>, x: f64, y: f64| {+ let app = h.state();+ let layout = CanvasLayout::new(&app.session.doc().unwrap().doc, false);+ let xf = Xf::new(app.canvas_rect.unwrap(), app.view().unwrap());+ xf.to_screen(layout.to_canvas(SpreadRef::Doc(0), Point::new(x, y)))+ };+ let widget = screen(&h, 300.0, 100.0) + vec2(0.0, 11.5);+ let diamond = screen(&h, 100.0, 100.0) + vec2(12.0, 0.0);+ let yellow = |h: &mut Harness<'_, DesignApp>, p: Pos2| -> Option<bool> {+ let img = match h.render() {+ Ok(img) => img,+ Err(e) => {+ eprintln!("skipped: no offscreen renderer ({e})");+ return None;+ }+ };+ let px = img.get_pixel(p.x.round() as u32, p.y.round() as u32).0;+ Some(px[0] > 200 && px[1] > 180 && px[2] < 100)+ };+ let Some(w) = yellow(&mut h, widget) else { return };+ assert!(w, "no widget at {widget:?}");+ assert_eq!(yellow(&mut h, diamond), Some(false));+ h.hover_at(widget);+ h.step();+ for pressed in [true, false] {+ h.event(egui::Event::PointerButton { pos: widget, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() });+ h.step();+ }+ h.run_steps(3);+ assert_eq!(yellow(&mut h, diamond), Some(true), "no diamond at {diamond:?}");+ assert_eq!(yellow(&mut h, widget), Some(false));+ }++ /// A harness whose frames are 1 ms apart, so two clicks make a double-click.+ fn fast_harness(app: DesignApp) -> Harness<'static, DesignApp> {+ let mut h = Harness::builder().with_size(vec2(1440.0, 900.0)).with_step_dt(0.001).build_ui_state(+ |ui, app: &mut DesignApp| {+ let ctx = ui.ctx().clone();+ app.logic(&ctx);+ app.ui(ui);+ },+ app,+ );+ h.run_steps(4);+ h+ }++ fn primary(h: &mut Harness<'_, DesignApp>, pos: Pos2, pressed: bool) {+ h.event(egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() });+ h.run_steps(2);+ }++ fn corner_drag(h: &mut Harness<'_, DesignApp>, from: Pos2, to: Pos2) {+ h.hover_at(from);+ h.step();+ primary(h, from, true);+ h.event(egui::Event::PointerMoved(from + (to - from) / 2.0));+ h.run_steps(2);+ h.event(egui::Event::PointerMoved(to));+ h.run_steps(2);+ primary(h, to, false);+ }++ fn corner_click(h: &mut Harness<'_, DesignApp>, p: Pos2) {+ h.hover_at(p);+ h.step();+ primary(h, p, true);+ primary(h, p, false);+ }++ /// Where the rulers measure from (canvas coordinates): the document's zero point on the+ /// spread in view (its first page, for a page origin).+ fn ruler_zero(app: &DesignApp, doc: &Document, layout: &CanvasLayout) -> Option<Point> {+ ruler_pieces(app, doc, layout, doc.zero_point()).first().map(|p| p.2)+ }++ fn zero_point(h: &Harness<'_, DesignApp>) -> [f64; 2] {+ h.state().session.doc().unwrap().doc.settings.zero_point+ }++ #[test]+ fn the_ruler_corner_drags_resets_and_locks_the_zero_point() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({"pages": 3})).unwrap();+ let mut h = fast_harness(app);+ let rect = h.state().canvas_rect.unwrap();+ let corner = rect.min - vec2(RULER / 2.0, RULER / 2.0);+ // A click that stays in the corner leaves the zero point.+ corner_click(&mut h, corner);+ assert_eq!(zero_point(&h), [0.0, 0.0]);+ // Dragged out, it lands where released, measured from the spread in view.+ let dest = rect.min + vec2(80.0, 50.0);+ let expected = {+ let app = h.state();+ let st = app.session.active().unwrap();+ let layout = CanvasLayout::new(&st.doc, false);+ let slot = layout.slots[current_slot(app, &layout).unwrap()];+ let sp = slot.to_spread(Xf::new(rect, app.view().unwrap()).to_canvas(dest));+ let b = st.doc.spread(slot.spread).unwrap().bounds();+ [sp.x - b.x0, sp.y - b.y0]+ };+ corner_drag(&mut h, corner, dest);+ let zp = zero_point(&h);+ assert!((zp[0] - expected[0]).abs() < 0.5 && (zp[1] - expected[1]).abs() < 0.5, "{zp:?} vs {expected:?}");+ assert!(zp[0].abs() > 1.0 && zp[1].abs() > 1.0, "moved: {zp:?}");+ // The rulers measure from the point it was dropped on.+ {+ let app = h.state();+ let st = app.session.active().unwrap();+ let layout = CanvasLayout::new(&st.doc, false);+ let o = ruler_zero(app, &st.doc, &layout).unwrap();+ let on_screen = Xf::new(rect, app.view().unwrap()).to_screen(o);+ assert!((on_screen - dest).length() < 0.5, "{on_screen:?} vs {dest:?}");+ }+ // A double-click resets it.+ corner_click(&mut h, corner);+ corner_click(&mut h, corner);+ assert_eq!(zero_point(&h), [0.0, 0.0]);+ // Locked from the corner's context menu, the corner ignores drags and double-clicks.+ right_click(&mut h, corner);+ h.get_by_label("Lock Zero Point").click();+ h.run_steps(3);+ assert!(h.state().session.doc().unwrap().doc.settings.zero_point_locked);+ corner_drag(&mut h, corner, dest);+ assert_eq!(zero_point(&h), [0.0, 0.0]);+ // Unlocked again from the same menu.+ right_click(&mut h, corner);+ h.get_by_label("Unlock Zero Point").click();+ h.run_steps(3);+ corner_drag(&mut h, corner, dest);+ assert_ne!(zero_point(&h), [0.0, 0.0]);+ h.state_mut().run("view.lockZeroPoint", json!({"on": true})).unwrap();+ corner_click(&mut h, corner);+ corner_click(&mut h, corner);+ assert_ne!(zero_point(&h), [0.0, 0.0], "a locked zero point isn't reset");+ }++ #[test]+ fn cmd_dragging_the_ruler_corner_makes_a_guide_each_way() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ let mut h = harness(app);+ let rect = h.state().canvas_rect.unwrap();+ let corner = rect.min - vec2(RULER / 2.0, RULER / 2.0);+ let dest = rect.min + vec2(120.0, 90.0);+ let (r, at) = {+ let app = h.state();+ let st = app.session.active().unwrap();+ CanvasLayout::new(&st.doc, false).spread_at(Xf::new(rect, app.view().unwrap()).to_canvas(dest)).unwrap()+ };+ h.event(egui::Event::ModifiersChanged(egui::Modifiers::COMMAND));+ h.step();+ corner_drag(&mut h, corner, dest);+ h.event(egui::Event::ModifiersChanged(egui::Modifiers::NONE));+ h.step();+ let doc = h.state().session.doc().unwrap().doc.clone();+ let guides: Vec<_> = doc.spread(r).unwrap().pages.iter().flat_map(|p| p.guides.clone()).collect();+ assert_eq!(guides.len(), 2, "{guides:?}");+ let pos = |o: designcraft_doc::Orientation| guides.iter().find(|g| g.orientation == o).map(|g| g.position).unwrap();+ assert!((pos(designcraft_doc::Orientation::Horizontal) - at.y).abs() < 0.5);+ assert!((pos(designcraft_doc::Orientation::Vertical) - at.x).abs() < 0.5);+ assert!(guides.iter().all(|g| g.spread));+ assert_eq!(doc.settings.zero_point, [0.0, 0.0], "the zero point stays");+ }++ #[test]+ fn rulers_and_xy_fields_measure_from_the_zero_point_on_every_spread() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ // Spread 0: page 1; spread 1: pages 2 and 3.+ app.run("file.new", json!({"pages": 3})).unwrap();+ app.run("view.zeroPoint", json!({"at": [100, 50]})).unwrap();+ app.session.prefs.dimensions_include_stroke = false;+ let mut h = harness(app);+ let rect = h.state().canvas_rect.unwrap();+ for (spread, x0) in [(0usize, 136.0), (1, 736.0)] {+ let app = h.state_mut();+ let id = app.run("frame.create", json!({"spread": spread, "rect": [x0, 70, x0 + 50.0, 120]})).unwrap()["id"].clone();+ app.run("selection.set", json!({"ids": [id]})).unwrap();+ // X/Y (Control, Properties and Info panels).+ let r = crate::panels::sel_info(app).unwrap().page_rect;+ assert!((r.x0 - (x0 - 100.0)).abs() < 1e-6 && (r.y0 - 20.0).abs() < 1e-6, "spread {spread}: {r:?}");+ // The rulers, with that spread in view.+ let layout = CanvasLayout::new(&app.session.active().unwrap().doc, false);+ let slot = layout.slots[spread];+ let v = app.view_mut().unwrap();+ let zoom = v.zoom;+ let c = slot.bounds.center();+ v.origin = Point::new(c.x - rect.width() as f64 / 2.0 / zoom, c.y - rect.height() as f64 / 2.0 / zoom);+ h.run_steps(2);+ let app = h.state();+ let doc = app.session.active().unwrap().doc.clone();+ assert_eq!(current_slot(app, &layout), Some(spread));+ let o = ruler_zero(app, &doc, &layout).unwrap();+ assert!((o.x - slot.bounds.x0 - 100.0).abs() < 1e-6 && (o.y - slot.bounds.y0 - 50.0).abs() < 1e-6, "spread {spread}: {o:?}");+ }+ }++ /// Screen position of spread 0's point (x, y).+ fn screen_of(h: &Harness<'_, DesignApp>, x: f64, y: f64) -> Pos2 {+ let app = h.state();+ let st = app.session.active().unwrap();+ let xf = Xf::new(app.canvas_rect.unwrap(), app.view().unwrap());+ xf.to_screen(CanvasLayout::new(&st.doc, st.editing_parents).xf(SpreadRef::Doc(0)) * Point::new(x, y))+ }++ /// A menu row: its label, then its shortcut if it has one.+ fn menu_row<'h>(h: &'h Harness<'_, DesignApp>, label: &'h str) -> Option<egui_kittest::Node<'h>> {+ h.query_by(move |n| {+ n.role() == egui::accesskit::Role::Button+ && n.label().is_some_and(|l| l == label || l.strip_prefix(label).and_then(|r| r.strip_prefix(' ')).is_some_and(|r| !r.contains(' ')))+ })+ }++ fn enabled(h: &Harness<'_, DesignApp>, label: &str) -> bool {+ use egui_kittest::kittest::NodeT;+ !menu_row(h, label).unwrap_or_else(|| panic!("no menu row {label}")).accesskit_node().is_disabled()+ }++ fn spread_items(h: &Harness<'_, DesignApp>) -> Vec<u64> {+ h.state().session.doc().unwrap().doc.spreads[0].items.iter().map(|i| i.id.0).collect()+ }++ fn selected(h: &Harness<'_, DesignApp>) -> Vec<u64> {+ h.state().session.doc().unwrap().selection.items.iter().map(|i| i.0).collect()+ }++ #[test]+ fn right_clicking_an_object_selects_it_and_its_menu_arranges_it() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ let a = app.run("frame.create", json!({"rect": [36, 36, 200, 200]})).unwrap()["id"].as_u64().unwrap();+ let b = app.run("frame.create", json!({"rect": [150, 150, 300, 300]})).unwrap()["id"].as_u64().unwrap();+ let mut h = harness(app);+ assert_eq!(selected(&h), [b]);+ let on_a = screen_of(&h, 60.0, 60.0);+ right_click(&mut h, on_a);+ assert_eq!(selected(&h), [a], "the object under the pointer is selected first");+ // An empty frame's menu: Content, but no Fitting (nothing to fit) and no text frame entries.+ assert!(menu_row(&h, "Content").is_some());+ assert!(menu_row(&h, "Fitting").is_none() && menu_row(&h, "Text Frame Options…").is_none());+ assert!(enabled(&h, "Copy") && !enabled(&h, "Paste"), "nothing copied yet: Paste is greyed");+ menu_row(&h, "Arrange").unwrap().click();+ h.run_steps(3);+ menu_row(&h, "Bring to Front").unwrap().click();+ h.run_steps(3);+ assert_eq!(spread_items(&h), [b, a]);+ assert!(menu_row(&h, "Arrange").is_none(), "choosing an entry closes the menu");+ }++ #[test]+ fn right_clicking_empty_canvas_offers_paste_after_a_copy() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ let a = app.run("frame.create", json!({"rect": [36, 36, 200, 200]})).unwrap()["id"].as_u64().unwrap();+ app.run("edit.copy", json!({})).unwrap();+ app.select_tool("rectangleFrame");+ let mut h = harness(app);+ let empty = screen_of(&h, 400.0, 500.0);+ right_click(&mut h, empty);+ assert_eq!(selected(&h), [a], "a right-click on nothing keeps the selection");+ assert!(menu_row(&h, "Arrange").is_none(), "the page menu, not the object menu");+ assert!(enabled(&h, "Paste in Place"));+ assert!(menu_row(&h, "✓ Show/Hide Rulers").is_some(), "toggles show their state");+ // Escape closes the menu and does nothing else: the tool would deselect, the shortcuts+ // would switch to the Selection tool.+ h.key_press(egui::Key::Escape);+ h.run_steps(3);+ assert!(menu_row(&h, "Paste in Place").is_none());+ assert_eq!((h.state().session.tool_id(), selected(&h)), ("rectangleFrame", vec![a]));+ right_click(&mut h, empty);+ menu_row(&h, "Paste").unwrap().click();+ h.run_steps(3);+ assert_eq!(spread_items(&h).len(), 2);+ }++ #[test]+ fn right_clicking_another_object_while_the_menu_is_open_reopens_it_there() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ let a = app.run("frame.create", json!({"rect": [36, 36, 200, 200]})).unwrap()["id"].as_u64().unwrap();+ let b = app.run("frame.create", json!({"rect": [36, 600, 200, 760]})).unwrap()["id"].as_u64().unwrap();+ let mut h = harness(app);+ // The menu opens down from A, with the pointer on its first row (no submenu open); B is below it.+ let (on_a, on_b) = (screen_of(&h, 60.0, 60.0), screen_of(&h, 50.0, 700.0));+ right_click(&mut h, on_a);+ assert_eq!(selected(&h), [a]);+ assert!(menu_row(&h, "Arrange").is_some());+ right_click(&mut h, on_b);+ assert_eq!(selected(&h), [b]);+ assert!(menu_row(&h, "Arrange").is_some(), "the menu reopens at the new click");+ }++ #[test]+ fn a_context_menu_in_one_pane_keeps_the_keys_from_the_other() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ let a = app.run("frame.create", json!({"rect": [36, 36, 100, 100]})).unwrap()["id"].as_u64().unwrap();+ app.select_tool("rectangleFrame");+ app.split = true;+ let mut h = harness(app);+ // The focused pane is the left one; this point is near its right edge, away from the frame.+ let left = h.state().canvas_rect.unwrap();+ right_click(&mut h, pos2(left.max.x - 40.0, left.center().y));+ assert!(menu_row(&h, "Paste in Place").is_some());+ // Escape closes the left pane's menu before the right pane handles the keys.+ h.key_press(egui::Key::Escape);+ h.run_steps(3);+ assert!(menu_row(&h, "Paste in Place").is_none());+ assert_eq!((h.state().session.tool_id(), selected(&h)), ("rectangleFrame", vec![a]));+ }++ #[test]+ fn right_clicking_text_keeps_the_selected_text_or_places_the_caret() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ app.run("frame.create", json!({"rect": [36, 36, 400, 200], "content": "text"})).unwrap();+ app.select_tool("type");+ app.run("text.insert", json!({"text": "Hello world"})).unwrap();+ let sid = *app.session.doc().unwrap().doc.stories.keys().next().unwrap();+ app.run("text.select", json!({"story": sid.0, "anchor": 0, "focus": 5})).unwrap();+ let mut h = harness(app);+ let text_sel = |h: &Harness<'_, DesignApp>| h.state().session.doc().unwrap().selection.text.as_ref().map(|t| (t.anchor, t.focus));+ // Inside "Hello": the selection stays.+ let inside = screen_of(&h, 45.0, 44.0);+ right_click(&mut h, inside);+ assert_eq!(text_sel(&h), Some((0, 5)));+ assert!(menu_row(&h, "Paste without Formatting").is_some(), "the text menu");+ h.key_press(egui::Key::Escape);+ h.run_steps(3);+ // Past the end of the line: the caret goes there.+ let past_end = screen_of(&h, 300.0, 44.0);+ right_click(&mut h, past_end);+ assert_eq!(text_sel(&h), Some((11, 11)));+ assert!(menu_row(&h, "Paste without Formatting").is_some());+ }++ #[test]+ fn rows_hidden_in_the_menu_bar_are_hidden_in_context_menus() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ app.run("frame.create", json!({"rect": [36, 36, 200, 200]})).unwrap();+ app.ui.hidden_menu_items.push(crate::menus::menu_key("Object/Arrange", "Bring to Front"));+ let mut h = harness(app);+ let on_frame = screen_of(&h, 60.0, 60.0);+ right_click(&mut h, on_frame);+ menu_row(&h, "Arrange").unwrap().click();+ h.run_steps(3);+ assert!(menu_row(&h, "Bring to Front").is_none());+ assert!(menu_row(&h, "Send to Back").is_some());+ assert!(h.query_by_label("Show All Menu Items").is_some());+ }++ #[test]+ fn a_secondary_drag_neither_moves_marquees_nor_draws() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ let a = app.run("frame.create", json!({"rect": [36, 36, 200, 200]})).unwrap()["id"].as_u64().unwrap();+ let mut h = harness(app);+ // A secondary-button drag between spread points.+ let drag = |h: &mut Harness<'_, DesignApp>, from: (f64, f64), to: (f64, f64)| {+ let (from, to) = (screen_of(h, from.0, from.1), screen_of(h, to.0, to.1));+ h.hover_at(from);+ h.step();+ h.event(egui::Event::PointerButton { pos: from, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });+ h.step();+ for t in [0.25, 0.5, 0.75, 1.0] {+ h.event(egui::Event::PointerMoved(from + (to - from) * t));+ h.step();+ }+ h.event(egui::Event::PointerButton { pos: to, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });+ h.run_steps(3);+ };+ let xf = |h: &Harness<'_, DesignApp>| h.state().session.doc().unwrap().doc.item(designcraft_doc::ItemId(a)).unwrap().xf;+ let before = xf(&h);+ // Selection tool: on the object (no move), and from empty page across it (no marquee).+ drag(&mut h, (100.0, 100.0), (300.0, 300.0));+ assert_eq!(xf(&h), before);+ h.state_mut().run("edit.deselectAll", json!({})).unwrap();+ drag(&mut h, (400.0, 400.0), (20.0, 20.0));+ assert!(selected(&h).is_empty());+ // Rectangle Frame tool: no frame.+ h.state_mut().select_tool("rectangleFrame");+ drag(&mut h, (300.0, 300.0), (500.0, 500.0));+ assert_eq!(spread_items(&h), [a]);+ }++ #[test]+ fn tools_show_their_own_cursors_over_the_page() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ let mut h = harness(app);+ let p = h.state().canvas_rect.unwrap().center();+ let mut shown = |tool: &str| {+ h.state_mut().run("tool.select", json!({"tool": tool})).unwrap();+ h.hover_at(p);+ h.run_steps(2);+ let out = &h.output().platform_output;+ (out.cursor_icon, out.cursor_image.clone())+ };+ let (pen, direct, text) = (shown("pen"), shown("directSelection"), shown("type"));+ let ppp = h.ctx.pixels_per_point();+ let images = &mut h.state_mut().cursors;+ match crate::cursors::MODE {+ crate::cursors::Mode::Os => {+ assert_eq!(pen.1, images.get(Cursor::Pen, ppp), "Pen");+ assert_eq!(direct.1, images.get(Cursor::ArrowHollow, ppp), "Direct Selection");+ assert_eq!(text, (egui::CursorIcon::Text, None), "the I-beam stays a system cursor");+ }+ crate::cursors::Mode::Painted => assert_eq!((pen.0, direct.0), (egui::CursorIcon::None, egui::CursorIcon::None)),+ crate::cursors::Mode::System => assert_eq!(pen, (egui::CursorIcon::Crosshair, None)),+ }+ }+#[test]fn rulers_open_no_units_menu_without_a_document() {let mut h = harness(DesignApp::new(designcraft_engine::Session::new(), crate::Services::default()));diff --cc crates/ui-egui/src/lib.rsindex 4694b1099,824cbf527..d7d92052b--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@@ -535,22 -511,8 +536,24 @@@ pub struct DesignApplast_time: f64,/// When recovery data was last written (seconds, egui time).pub last_recovery: f64,+ /// The egui context (set by the first frame): copied text goes to the system clipboard through it.+ egui_ctx: Option<egui::Context>,+ /// A control-channel request is being handled: commands it runs never read or write the+ /// user's system clipboard (a control client must not see or replace it).+ pub(crate) in_control: bool,+ /// What the last object copy put on the system clipboard (no native menu): without it the+ /// paste keys produce no paste event. It is never pasted as text.+ object_clip_text: Option<String>,+ /// The marked text the system IME last sent (`None` once it commits or clears). When the+ /// composition ends some other way (a click, a tool switch), the IME is told to drop it.+ pub(crate) ime_marked: Option<String>,+ /// The system IME must drop its marked text (see [`Self::take_ime_discard`]).+ pub(crate) ime_discard: bool,+ /// The canvas asked for the system IME in the last frame (the web build's text input has the+ /// keyboard then).+ pub(crate) canvas_ime: bool,+ /// The tool cursor bitmaps handed to the OS.+ pub cursors: cursors::Images,}impl DesignApp {@@@ -590,12 -550,7 +593,13 @@@native_shortcuts: Default::default(),last_time: 0.0,last_recovery: 0.0,+ egui_ctx: None,+ in_control: false,+ object_clip_text: None,+ ime_marked: None,+ ime_discard: false,+ canvas_ime: false,+ cursors: cursors::Images::default(),}}