DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge origin/main into codex/handle-double-click-fit
moppeeer64c99b18d570
diff --cc crates/tools/src/select.rsindex 6a0356329,b2ac97731..70a6921c5--- a/crates/tools/src/select.rs+++ b/crates/tools/src/select.rs@@@ -2,8 -2,8 +2,9 @@@//!//! Click selects the frontmost item (Shift toggles), drag moves (Alt duplicates, Shift constrains),//! the 8 bounding-box handles resize (Shift keeps proportions, Alt from centre, Cmd scales content),-//! empty-canvas drags make a marquee, double-clicking a text frame switches to the Type tool.+//! empty-canvas drags make a marquee, double-clicking a text frame switches to the Type tool and+//! double-clicking a handle fits the frame to its content (corner: both sides, edge: that one).+ //! A click on a selected text frame's in or out port loads the text cursor (see [`crate::thread`]).use designcraft_doc::{ItemId, SpreadRef};use designcraft_geom::{Affine, Point, Rect, Vec2};@@@ -441,6 -473,19 +494,29 @@@ impl Tool for SelectionToolfn pointer(&mut self, cx: &ToolContext, ev: &PointerEvent) -> Vec<Action> {let p = ev.pos;+ if !self.direct+ && let Some(actions) = self.thread_pointer(cx, ev)+ {+ return actions;+ }++ // The second press of a double click on a selection handle: Fit Frame to Content, along++ // that handle's side (InDesign).++ if !self.direct++ && matches!(ev.kind, PointerKind::Down | PointerKind::DoubleClick)++ && ev.click_count() == 2++ && let Some(h) = handle_at(cx, p)++ {++ self.drag = Drag::None;++ return vec![Action::Exec("object.fit".into(), json!({"mode": "fitFrameToContent", "axis": fitting_axis(cx, h)}))];++ }+ // The second press of a double click on a text frame: edit its text.+ if matches!(ev.kind, PointerKind::Down | PointerKind::DoubleClick)+ && ev.click_count() == 2+ && let Some(actions) = edit_text_at(cx, p)+ {+ self.drag = Drag::None;+ return actions;+ }match ev.kind {PointerKind::Move => {self.hover_handle = handle_at(cx, p);diff --cc crates/ui-egui/src/canvas.rsindex 8374e6324,5d8218b77..e9a3fdfb3--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@@ -1805,49 -1909,79 +1909,122 @@@ mod tests(s.horizontal_units, s.vertical_units)}+ #[test]+ fn mouse_double_clicking_a_frame_handle_fits_with_one_undo_step() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ let id = designcraft_doc::ItemId(+ app.run("frame.create", json!({"rect": [100, 100, 400, 300], "content": "text", "text": "Fit this text"})).unwrap()["id"]+ .as_u64()+ .unwrap(),+ );+ app.run("selection.set", json!({"ids": [id.0]})).unwrap();+ let original = app.session.doc().unwrap().doc.item(id).unwrap().bounds();+ // 60 frames a second: the default 0.25 s step puts two clicks past egui's double-click delay.+ let mut h = Harness::builder().with_size(vec2(1440.0, 900.0)).with_step_dt(1.0 / 60.0).build_ui_state(+ |ui, app: &mut DesignApp| {+ let ctx = ui.ctx().clone();+ app.logic(&ctx);+ app.ui(ui);+ },+ app,+ );+ h.run_steps(4);+ let app = h.state();+ let st = app.session.doc().unwrap();+ let layout = CanvasLayout::new(&st.doc, false);+ let xf = Xf::new(app.canvas_rect.unwrap(), app.view().unwrap());+ let p = xf.to_screen(layout.xf(SpreadRef::Doc(0)) * Point::new(original.x1, original.y1));+ h.hover_at(p);+ h.step();+ // Separate frames model the actual mouse, including egui's release-time double click.+ for _ in 0..2 {+ for pressed in [true, false] {+ h.event(egui::Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() });+ h.step();+ }+ }+ h.run_steps(2);+ let fitted = h.state().session.doc().unwrap().doc.item(id).unwrap().bounds();+ assert!(fitted.width() < original.width() && fitted.height() < original.height(), "{fitted:?}");+ assert_eq!(h.state().session.tool_id(), "selection");+ h.state_mut().run("edit.undo", json!({})).unwrap();+ assert_eq!(h.state().session.doc().unwrap().doc.item(id).unwrap().bounds(), original);+ }++ #[test]+ fn selected_merged_owners_paint_the_same_row_and_column_targets() {+ for column in [true, false] {+ for merged in [false, true] {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({"pages": 1})).unwrap();+ app.run("frame.create", json!({"rect": [36, 36, 300, 300], "content": "text"})).unwrap();+ app.run("table.insert", json!({"rows": 2, "cols": 2})).unwrap();+ if merged {+ let range = if column { json!({"rows": [0, 0], "cols": [0, 1]}) } else { json!({"rows": [0, 1], "cols": [0, 0]}) };+ app.run("table.select", range).unwrap();+ app.run("table.merge", json!({})).unwrap();+ }+ app.run("table.select", json!({"rows": [1, 1], "cols": [1, 1]})).unwrap();+ app.run(if column { "table.selectColumn" } else { "table.selectRow" }, json!({})).unwrap();+ let st = app.session.doc().unwrap();+ let selection = st.selection.clone();+ let ts = selection.text.unwrap();+ let selected = selection.cells.unwrap();+ assert_eq!(+ selected.range,+ if column { designcraft_doc::CellRange::new(0, 1, 1, 1) } else { designcraft_doc::CellRange::new(1, 0, 1, 1) }+ );+ let layout = CanvasLayout::new(&st.doc, st.editing_parents);+ let cs = app.session.cache.get(&st.doc, ts.story, None);+ let ft = &cs.frames[0];+ let table = ft.tables.iter().find(|t| t.table == selected.table).unwrap();+ let (a, _) = item_canvas_xf(&st.doc, &layout, ft.frame).unwrap();+ let m = a * st.doc.text_xf(st.doc.item(ft.frame).unwrap());+ let xf = Xf { min: pos2(0.0, 0.0), origin: Point::new(0.0, 0.0), zoom: 1.0, rot: 0, pivot: pos2(0.0, 0.0) };+ let owners = if merged {+ [(0, 0), (1, 1)]+ } else if column {+ [(0, 1), (1, 1)]+ } else {+ [(1, 0), (1, 1)]+ };+ let expected: Vec<_> = owners+ .into_iter()+ .map(|(r, c)| {+ let rect = table.cells.iter().find(|cell| cell.row == r && cell.col == c).unwrap().rect;+ vec![+ xf.to_screen(m * Point::new(rect.x0, rect.y0)),+ xf.to_screen(m * Point::new(rect.x1, rect.y0)),+ xf.to_screen(m * Point::new(rect.x1, rect.y1)),+ xf.to_screen(m * Point::new(rect.x0, rect.y1)),+ ]+ })+ .collect();+ let ctx = egui::Context::default();+ let mut output = ctx.run_ui(egui::RawInput::default(), |ui| {+ draw_text_selection(&app, ui.painter(), &xf, &st.doc, &layout, ts, Some(selected));+ });+ output.textures_delta.clear();+ let tint = Color32::from_rgba_unmultiplied(80, 140, 255, 90);+ let painted: Vec<_> = output+ .shapes+ .into_iter()+ .filter_map(|s| match s.shape {+ egui::Shape::Path(path) if path.fill == tint => {+ assert!(path.closed);+ assert_eq!(path.stroke, egui::epaint::PathStroke::NONE);+ Some(path.points)+ }+ _ => None,+ })+ .collect();+ assert_eq!(painted, expected, "only the two touched owners are tinted; column={column}, merged={merged}");+ assert_eq!(app.session.doc().unwrap().selection, selection);+ }+ }+ }+#[test]fn right_clicking_a_ruler_sets_that_rulers_units() {let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());