DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge PR #393
Brandon Thomas32ff0fc786b2
diff --cc crates/ui-egui/src/canvas.rsindex 24150ede7,a52569490..ef5ed0ef4--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@@ -2238,79 -1778,38 +2244,111 @@@ mod tests(s.horizontal_units, s.vertical_units)}+ /// A viewport wider than the GPU's largest texture: the canvas texture is sampled down, and an+ /// edit repaints it whole. A region repainted in view pixels would land in the wrong place,+ /// or outside the texture (a wgpu validation error). A view that fits is still patched.+ #[test]+ fn an_edit_repaints_a_canvas_texture_sampled_down_to_the_gpu_limit_whole() {+ for (width, sampled_down) in [(1440.0, false), (2600.0, true)] {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ let frame = app.run("frame.create", json!({"rect": [72, 72, 400, 300], "content": "text", "text": "Text"})).unwrap();+ let mut h = Harness::builder().with_size(vec2(width, 900.0)).build_ui_state(+ |ui, app: &mut DesignApp| {+ let ctx = ui.ctx().clone();+ app.logic(&ctx);+ app.ui(ui);+ },+ app,+ );+ h.run_steps(6);+ let limit = h.ctx.input(|i| i.max_texture_side);+ let size = h.state().canvas.texture.as_ref().map(|t| t.size()).unwrap();+ assert!(size[0] <= limit && size[1] <= limit, "{size:?}");+ let shown = h.state().canvas.shown.unwrap().size;+ assert_eq!(shown.0 > limit as f32, sampled_down, "view {shown:?}, limit {limit}");+ let patches = h.state().canvas.patches;+ // A small edit: one more character in the frame.+ h.state_mut().run("text.insert", json!({"story": frame["story"], "at": 4, "text": "s"})).unwrap();+ h.run_steps(6);+ assert_eq!(h.state().canvas.patches == patches, sampled_down, "width {width}");+ assert_eq!(h.state().canvas.texture.as_ref().map(|t| t.size()), Some(size));+ }+ }++ #[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());diff --cc crates/ui-egui/src/panels/glyphs.rsindex 63f02e9ac,f8fd8fdf4..61c9cb42d--- a/crates/ui-egui/src/panels/glyphs.rs+++ b/crates/ui-egui/src/panels/glyphs.rs@@@ -71,12 -71,13 +71,16 @@@ pub fn show(app: &mut DesignApp, ui: &m}});}- let cols = ((ui.available_width() / CELL).floor() as u32).max(1);+ // The scroll bar floats over the grid's right edge: leave it room.+ let scroll = &ui.spacing().scroll;+ let bar = scroll.bar_inner_margin + scroll.bar_width + scroll.bar_outer_margin;+ let cols = (((ui.available_width() - bar) / CELL).floor() as u32).max(1);let ppp = ui.ctx().pixels_per_point();- let cell_px = (CELL * ppp).round() as u32;+ let cell_px = (CELL * ppp).round().max(1.0) as u32;+ // The grid is one texture: no more rows than the GPU's largest texture holds (a narrow+ // panel of a large CJK font ran past it).+ let max_rows = (ui.ctx().input(|i| i.max_texture_side) as u32 / cell_px).max(1);+ chars.truncate((max_rows * cols) as usize);let ink = t.text_strong;let key = egui::Id::new(("glyph_grid", &st.family, &st.style, scope, &q, cols, cell_px, ink.to_array()));let tex: egui::TextureHandle = match ui.data(|d| d.get_temp::<egui::TextureHandle>(key)) {