PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
Merge PR #4095: Comments: the composer hides while its comment is scrolled away (#4021)
aiongg1b404e6956a4
diff --cc crates/ui-egui/tests/it/comments.rsindex 36bd049dd,d97c09fbf..4477f502b--- a/crates/ui-egui/tests/it/comments.rs+++ b/crates/ui-egui/tests/it/comments.rs@@@ -675,38 -675,56 +675,88 @@@ fn sticky_note_composer_posts_and_returassert_eq!(comments(&h).len(), 1);}+/// #2583: a sticky note is saved NoRotate, so on a turned page its icon is drawn upright from+/// where it was placed (right and down), not where its `/Rect` lies as displayed. The Select tool+/// picks it where it is drawn.+#[test]+fn a_sticky_note_on_a_turned_page_is_picked_where_it_is_drawn() {+ let mut h = harness(|app| {+ app.apply_edit(pdfcraft_engine::Edit::RotatePages { pages: vec![0], degrees: 90 });+ app.set_option("quick", "note").unwrap();+ });+ settle(&mut h);+ let page = h.state().views[0].page_screen_rect(0).expect("page 1 on screen");+ // Screen pixels per point of the displayed page (300 × 200 pt turned to 200 × 300).+ let k = page.width() / 200.0;+ let placed = page.left_top() + egui::vec2(100.0 * k, 60.0 * k);+ click(&mut h, placed);+ field(&h, "Add a comment").type_text("Turned");+ h.run_steps(2);+ h.get_by_label("Post").click();+ h.run_steps(3);+ settle(&mut h);+ let c = comments(&h);+ assert_eq!(c.len(), 1);+ assert_eq!((c[0].subtype.as_str(), c[0].no_rotate), ("Text", true));+ assert_eq!(h.state().quick_tool, QuickTool::Select);+ // Where the note's /Rect lies as displayed, left of where it was placed: nothing.+ click(&mut h, placed + egui::vec2(-10.0 * k, 10.0 * k));+ assert_eq!(h.state().views[0].comments.selected, None);+ // On the icon, drawn right and down from where it was placed.+ click(&mut h, placed + egui::vec2(10.0 * k, 10.0 * k));+ assert_eq!(h.state().views[0].comments.selected, Some((0, 0)));+}++ /// The composer leaves with its comment when the page scrolls it out of the document, and comes+ /// back with the draft (#4021). It used to be pushed back into the window, over the mode bar and+ /// the toolbar, while its comment was out of sight.+ #[test]+ fn the_composer_hides_while_its_comment_is_scrolled_away() {+ let mut h = harness(|app| app.set_option("quick", "note").unwrap());+ click_pt(&mut h, (250.0, 180.0));+ field(&h, "Add a comment").type_text("Draft");+ h.run_steps(2);+ let editors = |h: &Harness<'static, PdfCraftApp>| -> Vec<egui::Rect> {+ h.query_all_by(|n| n.role() == egui::accesskit::Role::MultilineTextInput).map(|n| n.rect()).collect()+ };+ let viewport = h.state().views[0].viewport_rect();+ assert!(editors(&h).iter().any(|r| viewport.contains(r.center())), "the composer opens in the document");+ // The page scrolled up: it still shows, the comment near its top doesn't.+ h.state_mut().views[0].goto = Some((0, 0.4));+ h.run_steps(4);+ assert!(h.state().views[0].page_screen_rect(0).is_some_and(|r| r.bottom() > viewport.top()), "page 1 still shows");+ assert!(at(&h, 250.0, 180.0).y < viewport.top(), "the comment is scrolled away");+ let shown = editors(&h);+ assert!(shown.is_empty(), "the composer stays with its comment, not over the toolbar: {shown:?} (document {viewport:?})");+ // Back again, the draft is still there.+ h.state_mut().views[0].goto = Some((0, 0.0));+ h.run_steps(4);+ assert!(editors(&h).iter().any(|r| viewport.contains(r.center())), "the composer is back");+ assert_eq!(h.state().views[0].comments.composer.as_ref().map(|c| c.text.as_str()), Some("Draft"));+ }++ /// With its whole page scrolled off the screen the composer keeps its draft, and the editor takes+ /// the keyboard again when the page comes back (#4021).+ #[test]+ fn the_composer_takes_the_keyboard_back_after_its_page_was_off_screen() {+ let mut h = harness(|app| app.set_option("quick", "note").unwrap());+ click_pt(&mut h, (250.0, 180.0));+ field(&h, "Add a comment").type_text("Draft");+ h.run_steps(2);+ let editor_focused =+ |h: &Harness<'static, PdfCraftApp>| h.query_all_by(|n| n.role() == egui::accesskit::Role::MultilineTextInput).any(|n| n.is_focused());+ assert!(editor_focused(&h), "the new note's editor has the keyboard");+ let viewport = h.state().views[0].viewport_rect();+ h.state_mut().views[0].goto = Some((1, 0.6));+ h.run_steps(4);+ assert!(h.state().views[0].page_screen_rect(0).is_none_or(|r| r.bottom() < viewport.top()), "page 1 is off the screen");+ assert!(!editor_focused(&h), "nothing to type into while the page is away");+ h.state_mut().views[0].goto = Some((0, 0.0));+ h.run_steps(4);+ assert_eq!(h.state().views[0].comments.composer.as_ref().map(|c| c.text.as_str()), Some("Draft"));+ assert!(editor_focused(&h), "the editor has the keyboard again");+ }+/// The rectangle of the comment whose text starts with `text`.fn rect_of(h: &Harness<'static, PdfCraftApp>, text: &str) -> [f32; 4] {let c = comments(h);