DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge PR #929
Brandon Thomasd47e7a186cf9
diff --cc crates/tools/src/tests.rsindex d2048232e,5884f74a4..aacfb8d3f--- a/crates/tools/src/tests.rs+++ b/crates/tools/src/tests.rs@@@ -1214,53 -1160,52 +1214,103 @@@ fn eyedropper_samples_a_placed_image_anassert_eq!(a, vec![Action::Exec("object.matchAttributes".into(), serde_json::json!({"from": photo.0}))]);}+#[test]+fn eyedropper_ignores_inactive_states_of_a_multi_state_object() {+ use designcraft_doc::{Asset, AssetId, Content, Graphic};+ let mut d = Document::new(&NewDocument::default());+ let lid = d.default_layer();+ d.assets.insert(+ AssetId(1),+ std::sync::Arc::new(Asset {+ id: AssetId(1),+ name: "photo.png".into(),+ mime: "image/png".into(),+ link: None,+ data: std::sync::Arc::new(vec![]),+ pixels: Some((100, 100)),+ page: 0,+ }),+ );+ let area = Rect::new(100.0, 100.0, 200.0, 200.0);+ let raster = {+ let mut it = Item::new(ItemId(d.alloc()), lid, Shape::Rectangle, shapes::rectangle(area));+ it.content = Content::Graphic(Graphic {+ asset: AssetId(1),+ size: (100.0, 100.0),+ xf: designcraft_geom::Affine::translate((0.0, 0.0)),+ auto_fit: Default::default(),+ fit_align: 4,+ crop: [0.0; 4],+ });+ it+ };+ let mut vector = Item::new(ItemId(d.alloc()), lid, Shape::Rectangle, shapes::rectangle(area));+ vector.fill.swatch = "[Black]".into();+ let target = add_rect(&mut d, Rect::new(400.0, 100.0, 450.0, 150.0));+ let s = Selection { items: vec![target], ..Default::default() };+ let sample = Action::Exec("color.sample".into(), serde_json::json!({"at": [120.0, 130.0], "spread": {"kind": "doc", "index": 0}}));+ // Children [vector, raster] with the raster on top: it samples only while it is the active state.+ for (active, samples) in [(1, true), (0, false)] {+ let mut group = Item::new(ItemId(d.alloc()), lid, Shape::Rectangle, shapes::rectangle(area));+ group.content = Content::Group { items: vec![std::sync::Arc::new(vector.clone()), std::sync::Arc::new(raster.clone())] };+ group.states = vec!["a".into(), "b".into()];+ group.active_state = active;+ let mut d2 = d.clone();+ d2.insert_item(SpreadRef::Doc(0), group, None).unwrap();+ let (c, l) = (Cache::new(), CanvasLayout::new(&d2, false));+ let off = l.xf(SpreadRef::Doc(0)).translation();+ let cx = ctx(&d2, &s, &c, &l);+ let a = create("eyedropper").pointer(&cx, &PointerEvent::new(PointerKind::Down, 120.0 + off.x, 130.0 + off.y));+ assert_eq!(a.contains(&sample), samples, "active state {active}: {a:?}");+ }+}+++ #[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/icons.rsindex 2a67de1f7,225ca5172..37a7434c2--- a/crates/ui-egui/src/icons.rs+++ b/crates/ui-egui/src/icons.rs@@@ -963,11 -898,19 +963,27 @@@ mod testsassert_ne!(drawn[1], drawn[2], "Scale and Shear draw alike");}+ #[test]+ fn typography_panel_has_its_own_icon() {+ let unknown = drawing("no-such-icon");+ let typography = drawing("panel-typography");+ assert_ne!(typography, unknown, "panel-typography falls back to the placeholder");+ assert_ne!(typography, drawing("panel-character"), "Typography draws the Character icon");+ }+++ /// Whether the arrow icon `name` paints is filled, from the fill of its one path shape.+ fn arrow_filled(name: &str) -> bool {+ let ctx = egui::Context::default();+ let mut out = ctx.run_ui(egui::RawInput::default(), |ui| {+ paint(ui.painter(), Rect::from_min_size(pos2(0.0, 0.0), vec2(20.0, 20.0)), name, Color32::WHITE);+ });+ out.textures_delta.clear();+ out.shapes.iter().any(|s| matches!(&s.shape, Shape::Path(p) if p.fill != Color32::TRANSPARENT))+ }++ #[test]+ fn selection_arrow_is_filled_and_direct_selection_hollow() {+ assert!(arrow_filled("tool-selection"), "Selection draws a hollow arrow");+ assert!(!arrow_filled("tool-direct"), "Direct Selection draws a filled arrow");+ }}