diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 465d4567..b2d7a939 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -6050,14 +6050,14 @@ impl NoteCraftApp { /// Letters going into the block the caret was already in fold into the step before /// them, so Ctrl+Z takes back a run of writing rather than one character. fn remember_step(ed: &mut Editor) { - Self::remember(ed, false); + Self::remember(ed, false, None); } /// Remembers a change made from the ribbon rather than by writing, such as Insert Space /// or a row put into a table, as a step of its own. It never folds into the writing /// before it, even when the caret stays in the same block (issue #153). fn remember_edit(ed: &mut Editor) { - Self::remember(ed, true); + Self::remember(ed, true, None); } /// Remembers a change to how a table is laid out that may leave its words as they were, @@ -6073,7 +6073,9 @@ impl NoteCraftApp { ed.history.record(step); } - fn remember(ed: &mut Editor, edit: bool) { + /// `moving`: boxes moved by the press of the pointer that went down then, and nothing else + /// changed. The frames of one drag fold into one step. + fn remember(ed: &mut Editor, edit: bool, moving: Option) { let same = ed.history.here().is_some_and(|s| s.body == ed.body && s.blocks == ed.blocks && s.boxes == ed.boxes); if same || { // Only the cursor's place on open paper came or went: the page is the one remembered. @@ -6091,8 +6093,9 @@ impl NoteCraftApp { let here = ed.caret.as_ref().map(|c| c.block); let was = ed.history.here().and_then(|s| s.caret.as_ref().map(|c| c.block)); let shape = ed.history.here().is_some_and(|s| s.blocks.len() == ed.blocks.len()); - let kind = match (shape, here, was) { - (true, Some(a), Some(b)) if a == b && !edit => crate::history::Kind::Typing(a), + let kind = match (moving, shape, here, was) { + (Some(press), ..) => crate::history::Kind::Moving(press), + (None, true, Some(a), Some(b)) if a == b && !edit => crate::history::Kind::Typing(a), _ => crate::history::Kind::Edit, }; let mut step = crate::history::Step::new(ed.body.clone(), ed.blocks.clone(), ed.boxes.clone(), ed.caret.clone(), kind); @@ -9365,16 +9368,20 @@ impl NoteCraftApp { if (changed || from_blocks) && Self::name_ink(ed) { from_blocks = true; } - if changed || from_blocks || boxes_moved { - Self::remember_step(ed); - } // A container was dragged or resized: the arrangement is the page's own, so it - // is written into the page's file rather than kept in the window. + // is written into the page's file rather than kept in the window. Done before the + // step is taken, so the step's text holds the boxes where they now are. if boxes_moved { ed.arranged = true; ed.rebuild_body(); Self::mark_dirty_from_blocks(ed); } + if changed || from_blocks || boxes_moved { + // Boxes moved while the pointer is held: one drag, one step. + let press = ui.input(|i| (i.pointer.any_down() || i.pointer.any_released()).then(|| i.pointer.press_start_time()).flatten()); + let moving = (boxes_moved && !changed && !from_blocks).then_some(press).flatten().map(f64::to_bits); + Self::remember(ed, false, moving); + } // A column was dragged wider or narrower. Widths are layout, like the boxes, // so they go into the page's own file beside them. if cols_moved { diff --git a/crates/ui-egui/src/containers.rs b/crates/ui-egui/src/containers.rs index 33e5f4ba..9607ddb5 100644 --- a/crates/ui-egui/src/containers.rs +++ b/crates/ui-egui/src/containers.rs @@ -596,8 +596,23 @@ pub fn show( out.changed.caret = true; log::debug!("picture in a shared box hit: container={i} block={block}"); } - if body.drag_started() && lift_picture.is_none() { - lift_picture = Some(PictureLift { container: i, block: *block, rect: *rect }); + // A printout page brings the rest of its printout along. When the printout is + // all the box holds, the box itself is what moves. + let printout = printout_blocks(blocks, &range, *block); + if printout.as_ref() == Some(&range) { + if body.dragged() { + let pull = scroll_at_edges(ui, area.min.y, zoom); + if let Some(c) = boxes.get_mut(i) { + let to = moved_to(ui, picture_grab_id(ui, *block), area.min, (c.x, c.y), body.drag_started(), zoom, pull, snap); + *c = Container::new(to.0, to.1, c.width, c.blocks); + } + out.boxes = true; + } + } else if body.drag_started() && lift_picture.is_none() { + let lifted = printout.unwrap_or(*block..*block + 1); + // The whole of what is lifted, as drawn: every page and the card above them. + let drawn = hits.iter().filter(|(b, ..)| lifted.contains(b)).fold(*rect, |all, (_, r, _)| all.union(*r)); + lift_picture = Some(PictureLift { container: i, block: *block, blocks: lifted, rect: drawn }); } } } @@ -940,11 +955,11 @@ pub fn show( && let Some(whole) = boxes.get(lift.container).copied() { let range = container::blocks_of(boxes, lift.container); - if range.len() > 1 && range.contains(&lift.block) { + if range.len() > lift.blocks.len() && range.start <= lift.blocks.start && lift.blocks.end <= range.end { let next_top = ui .data(|d| d.get_temp::>(pageedit::after_picture_tops_id())) - .and_then(|tops| tops.iter().find(|(block, _)| *block == lift.block + 1).map(|(_, y)| *y)); - let (parts, picture_at) = lift_out(whole, range, lift.block, area.min, zoom, lift.rect, next_top); + .and_then(|tops| tops.iter().find(|(block, _)| *block == lift.blocks.end).map(|(_, y)| *y)); + let (parts, picture_at) = lift_out(whole, range, lift.blocks.clone(), area.min, zoom, lift.rect, next_top); boxes.splice(lift.container..=lift.container, parts); let picture_box = lift.container + picture_at; let pull = scroll_at_edges(ui, area.min.y, zoom); @@ -1872,16 +1887,56 @@ fn moved_to(ui: &Ui, id: egui::Id, page_min: egui::Pos2, at: (f32, f32), started } /// A picture that was taken hold of in a box shared with other blocks. -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Debug)] struct PictureLift { /// The box it is in. container: usize, - /// The picture's block. + /// The picture's block, the one the pointer took hold of. block: usize, - /// Where the picture is drawn, on screen. + /// The blocks that go with it: the picture alone, or the whole of a printout. + blocks: std::ops::Range, + /// Where they are drawn, on screen. rect: Rect, } +/// The file a printout page was made from, read from the page's picture: `assets/a.pdf` for +/// `assets/a.pdf.printout-003.png`. `None` for any other block. +fn printout_source(block: &Block) -> Option<&str> { + let Block::Image { src, .. } = block else { return None }; + let (source, page) = src.rsplit_once(".printout-")?; + let number = page.strip_suffix(".png")?; + (!number.is_empty() && number.bytes().all(|b| b.is_ascii_digit())).then_some(source) +} + +/// The blocks of the printout that `block` is a page of, within `range`: every page made from +/// the same file next to it, and the attachment card above the first page when it is the card +/// of that file. A printout is one thing on the page, as in OneNote: taking hold of any page +/// moves all of it. `None` when `block` is not a printout page. +fn printout_blocks(blocks: &[Block], range: &std::ops::Range, block: usize) -> Option> { + let source = printout_source(blocks.get(block)?)?; + let same = |i: usize| blocks.get(i).and_then(printout_source) == Some(source); + let mut start = block; + while start > range.start && same(start - 1) { + start -= 1; + } + let mut end = block + 1; + while end < range.end && same(end) { + end += 1; + } + // The card is the file's icon, `assets/a.pdf.svg`, or words linking to the file. + let card = |b: &Block| match b { + Block::Image { src, .. } => src.strip_suffix(".svg") == Some(source), + Block::Paragraph(spans) => { + spans.iter().any(|s| s.link.as_deref() == Some(source) || s.image.as_deref().and_then(|i| i.strip_suffix(".svg")) == Some(source)) + } + _ => false, + }; + if start > range.start && blocks.get(start - 1).is_some_and(card) { + start -= 1; + } + Some(start..end) +} + /// Where a picture being moved was taken hold of, kept for the length of the drag. Keyed by the /// picture's block, which does not change when its box is split off, where the box's own number /// does. @@ -1905,8 +1960,9 @@ fn picture_clicked(ui: &Ui, blocks: &[Block], page_dir: &std::path::Path, block: } } -/// The boxes that replace `whole`, which holds `range`, when the picture `block` in it is taken -/// out into a box of its own: the blocks before it, the picture, the blocks after it. A part +/// The boxes that replace `whole`, which holds `range`, when the picture in `lifted` (one block, +/// or a printout's card and pages) is taken out into a box of its own: the blocks before it, the +/// picture, the blocks after it. A part /// with nothing in it is left out. Also says which of the boxes is the picture's. /// /// Every part keeps the place it was drawn at, so the page looks the same afterwards. The @@ -1916,7 +1972,7 @@ fn picture_clicked(ui: &Ui, blocks: &[Block], page_dir: &std::path::Path, block: fn lift_out( whole: Container, range: std::ops::Range, - block: usize, + lifted: std::ops::Range, origin: Pos2, zoom: f32, picture: Rect, @@ -1926,14 +1982,19 @@ fn lift_out( const BETWEEN: f32 = 6.0; let zoom = zoom.max(0.01); let mut parts = Vec::with_capacity(3); - let before = block - range.start; + let before = lifted.start - range.start; if before > 0 { parts.push(Container::new(whole.x, whole.y, whole.width, before)); } let picture_at = parts.len(); // The box's inside starts a pad in from its left edge and a handle's height below its top. - parts.push(Container::new((picture.min.x - origin.x) / zoom - PAD, (picture.min.y - origin.y) / zoom - HANDLE, picture.width() / zoom, 1)); - let after = range.end - block - 1; + parts.push(Container::new( + (picture.min.x - origin.x) / zoom - PAD, + (picture.min.y - origin.y) / zoom - HANDLE, + picture.width() / zoom, + lifted.len(), + )); + let after = range.end - lifted.end; if after > 0 { let top = next_top.map_or(picture.max.y + BETWEEN * zoom, |y| y.max(picture.max.y)); parts.push(Container::new(whole.x, (top - origin.y) / zoom - HANDLE, whole.width, after)); @@ -1995,6 +2056,27 @@ fn start_a_box(blocks: &mut Vec, boxes: &mut Vec, caret: &mut #[cfg(test)] mod tests { + /// A printout's card and pages go together; a picture of another printout, or none, does not. + #[test] + fn a_printout_is_its_card_and_every_page() { + use notecraft_formats::markdown::Block; + let image = |src: &str| Block::Image { alt: String::new(), src: src.into(), width: None, height: None }; + let blocks = vec![ + Block::Paragraph(Vec::new()), + image("assets/a.pdf.svg"), + image("assets/a.pdf.printout-001.png"), + image("assets/a.pdf.printout-002.png"), + image("assets/b.pdf.printout-001.png"), + image("assets/photo.png"), + ]; + assert_eq!(super::printout_blocks(&blocks, &(0..6), 3), Some(1..4)); + assert_eq!(super::printout_blocks(&blocks, &(0..6), 2), Some(1..4)); + assert_eq!(super::printout_blocks(&blocks, &(2..6), 2), Some(2..4), "not past the box it is in"); + assert_eq!(super::printout_blocks(&blocks, &(0..6), 4), Some(4..5)); + assert_eq!(super::printout_blocks(&blocks, &(0..6), 5), None); + assert_eq!(super::printout_blocks(&blocks, &(0..6), 1), None, "the card alone is a picture"); + } + /// A box started on open paper begins under the pointer at every zoom. #[test] fn a_click_on_open_paper_starts_its_box_under_the_pointer_at_any_zoom() { @@ -2390,7 +2472,7 @@ more // The page's corner is at (100, 50); the box's inside starts at 100+40+6, 50+20+14. let origin = pos2(100.0, 50.0); let picture = Rect::from_min_size(pos2(146.0, 154.0), vec2(200.0, 120.0)); - let (parts, at) = lift_out(whole, 0..5, 2, origin, 1.0, picture, Some(290.0)); + let (parts, at) = lift_out(whole, 0..5, 2..3, origin, 1.0, picture, Some(290.0)); assert_eq!(parts.iter().map(|c| c.blocks).collect::>(), vec![2, 1, 2], "words, picture, words"); assert_eq!(at, 1); assert_eq!(parts[0], Container::new(40.0, 20.0, 400.0, 2), "the words before keep the box's place and width"); @@ -2402,9 +2484,9 @@ more fn lifting_the_first_or_last_picture_leaves_no_empty_box() { let whole = Container::new(0.0, 0.0, 300.0, 3); let picture = Rect::from_min_size(pos2(6.0, 14.0), vec2(150.0, 80.0)); - let (first, at) = lift_out(whole, 0..3, 0, pos2(0.0, 0.0), 1.0, picture, Some(100.0)); + let (first, at) = lift_out(whole, 0..3, 0..1, pos2(0.0, 0.0), 1.0, picture, Some(100.0)); assert_eq!((first.iter().map(|c| c.blocks).collect::>(), at), (vec![1, 2], 0)); - let (last, at) = lift_out(whole, 0..3, 2, pos2(0.0, 0.0), 1.0, picture, None); + let (last, at) = lift_out(whole, 0..3, 2..3, pos2(0.0, 0.0), 1.0, picture, None); assert_eq!((last.iter().map(|c| c.blocks).collect::>(), at), (vec![2, 1], 1)); } @@ -2413,12 +2495,12 @@ more let whole = Container::new(0.0, 0.0, 300.0, 4); // At 200 % the picture is drawn twice as far from the corner and twice as big. let picture = Rect::from_min_size(pos2(24.0, 68.0), vec2(300.0, 160.0)); - let (parts, _) = lift_out(whole, 0..4, 1, pos2(0.0, 0.0), 2.0, picture, Some(260.0)); + let (parts, _) = lift_out(whole, 0..4, 1..2, pos2(0.0, 0.0), 2.0, picture, Some(260.0)); assert_eq!((parts[1].x, parts[1].y, parts[1].width), (6.0, 20.0, 150.0), "page points, not screen points"); assert_eq!(parts[2].y, 116.0); assert_eq!(parts.iter().map(|c| c.blocks).sum::(), 4); // A box that is only a picture and a line, lifted below a range that starts later. - let (parts, _) = lift_out(Container::new(0.0, 0.0, 300.0, 2), 3..5, 4, pos2(0.0, 0.0), 1.0, picture, None); + let (parts, _) = lift_out(Container::new(0.0, 0.0, 300.0, 2), 3..5, 4..5, pos2(0.0, 0.0), 1.0, picture, None); assert_eq!(parts.iter().map(|c| c.blocks).collect::>(), vec![1, 1]); } } diff --git a/crates/ui-egui/src/history.rs b/crates/ui-egui/src/history.rs index 6c77376c..ee1a6a09 100644 --- a/crates/ui-egui/src/history.rs +++ b/crates/ui-egui/src/history.rs @@ -36,6 +36,10 @@ pub enum Kind { Edit, /// Letters going into this block. Typing(usize), + /// Boxes being moved by one press of the pointer, named by when it went down. Every frame of + /// a drag is one step, so Undo puts the boxes back where the drag found them, not where they + /// were a frame before. + Moving(u64), } /// The page as it stood at one moment. @@ -106,6 +110,7 @@ impl History { let last = &self.steps[self.at]; match (last.kind, step.kind) { (Kind::Typing(a), Kind::Typing(b)) => a == b && step.when.duration_since(last.when) < PAUSE, + (Kind::Moving(a), Kind::Moving(b)) => a == b, _ => false, } } @@ -272,4 +277,19 @@ mod tests { assert!(h.len() < 40, "the heavy steps were trimmed: {}", h.len()); assert!(h.can_undo(), "but there is still something to undo"); } + + /// A box dragged across the page records a step a frame: they are one step, so one Undo + /// puts it back where the drag began, and a long drag cannot push the older steps out. + #[test] + fn one_drag_is_one_step() { + let mut h = History::new(step("page", Kind::Edit)); + for frame in 0..(MAX_STEPS * 2) { + h.record(step(&format!("page moved {frame}"), Kind::Moving(7))); + } + assert_eq!(h.len(), 2, "the page as opened and the drag"); + assert_eq!(h.undo().map(|s| s.body.as_str()), Some("page")); + h.redo(); + h.record(step("moved again", Kind::Moving(8))); + assert_eq!(h.len(), 3, "the next drag is a step of its own"); + } } diff --git a/crates/ui-egui/tests/ui.rs b/crates/ui-egui/tests/ui.rs index e9112c72..e6afb67a 100644 --- a/crates/ui-egui/tests/ui.rs +++ b/crates/ui-egui/tests/ui.rs @@ -7980,6 +7980,76 @@ fn a_picture_in_a_shared_box_is_moved_by_dragging_it() { } } +/// A printout is one thing on the page, as in OneNote. Taking hold of any of its pages moves the +/// whole printout, its card and every page: in a box of its own the box moves, and in a box shared +/// with words the printout is lifted out together rather than page by page. One Ctrl+Z puts a +/// drag back however many frames it took. +#[test] +fn a_printout_moves_as_one_and_undo_puts_it_back() { + for shared in [false, true] { + let what = format!("shared={shared}"); + let t = TempDir::new("ui-printout-moves-whole"); + let mut s = session(&t); + let sample = create_sample(&mut s, Some(LOGO_PNG)).expect("sample"); + let path = sample.md_page.to_string_lossy().into_owned(); + s.attach_bytes(&path, "a.pdf", b"%PDF-1.4\n").expect("pdf"); + s.attach_bytes(&path, "a.pdf.svg", b"").expect("card"); + s.attach_bytes(&path, "a.pdf.printout-001.png", LOGO_PNG).expect("page 1"); + s.attach_bytes(&path, "a.pdf.printout-002.png", LOGO_PNG).expect("page 2"); + // Small pages, so both are in the window. + let printout = "[![a.pdf](assets/a.pdf.svg)](assets/a.pdf)\n\n\"Printout\n\n\"Printout\n"; + let body = if shared { format!("Before\n\n{printout}\nAfter\n") } else { printout.to_string() }; + let blocks = if shared { 5 } else { 3 }; + s.run("page.save", ¶ms(&[("path", &path), ("body", &body)])).expect("page"); + s.run("page.containers", ¶ms(&[("path", &path), ("boxes", &format!("0,0,500,{blocks}"))])).expect("one box"); + let mut app = NoteCraftApp::new(s); + app.open_path(&path); + let mut h = harness(app, LIGHT); + settle(&mut h); + let boxes = |h: &mut Harness<'_, NoteCraftApp>| { + let state = look_at(h); + state + .pointer("/page/containers") + .and_then(Value::as_array) + .map(|b| { + b.iter() + .map(|c| { + let f = |k: &str| c.get(k).and_then(Value::as_f64).unwrap_or(-1.0) as f32; + (f("x"), f("y"), c.get("blocks").and_then(Value::as_u64).unwrap_or(0)) + }) + .collect::>() + }) + .unwrap_or_default() + }; + let before = boxes(&mut h); + assert_eq!(before, vec![(0.0, 0.0, blocks as u64)], "one box to begin with in {what}"); + + // Take hold of the second page and drag it, over many frames. + let page_two = if shared { 3 } else { 2 }; + let start = rendered_picture_rect(&h, page_two).center(); + ask(&mut h, "ui.press", json!({"x": start.x, "y": start.y})).expect("press the page"); + for step in 1..=20 { + let f = step as f32; + ask(&mut h, "ui.move", json!({"x": start.x + 4.0 * f, "y": start.y + 3.0 * f})).expect("drag"); + } + ask(&mut h, "ui.release", json!({"x": start.x + 80.0, "y": start.y + 60.0})).expect("drop"); + settle(&mut h); + let after = boxes(&mut h); + if shared { + let counts: Vec = after.iter().map(|b| b.2).collect(); + assert_eq!(counts, vec![1, 3, 1], "the words, the whole printout, the words in {what}: {after:?}"); + } else { + assert_eq!(after.len(), 1, "still one box in {what}: {after:?}"); + assert!((after[0].0 - 80.0).abs() < 2.0 && (after[0].1 - 60.0).abs() < 2.0, "the box went with the pointer in {what}: {after:?}"); + } + + // One Ctrl+Z, and the page is as it was. + ask(&mut h, "ui.key", json!({"key": "Z", "ctrl": true})).expect("Ctrl+Z"); + settle(&mut h); + assert_eq!(boxes(&mut h), before, "one undo puts the whole drag back in {what}"); + } +} + // The native attachment-card double-click path is manually verified on Windows. The Linux // headless WGPU runner does not deliver the rendered attachment hit target consistently: the // same test stalls for the full suite timeout there while passing locally.