Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Merge PR #3010: fix(ui): the mode tabs keep the hand over a page scrolled under them

aiongg5e9293739dc3
diff --cc crates/ui-egui/src/edit_text_ui.rs
index d272480d2,57cdb537c..ec53a2faf
--- a/crates/ui-egui/src/edit_text_ui.rs
+++ b/crates/ui-egui/src/edit_text_ui.rs
@@@ -1342,17 -1334,18 +1347,23 @@@ pub(crate) fn overlay(ctx: &egui::Conte
ed.max_width = ed.rect.width();
let scale = (ed.rect.width() / (ed.source_rect[2] - ed.source_rect[0]).abs().max(0.01)).max(0.01);
ed.size = (ed.look.size as f32 * scale).clamp(1.0, 300.0);
- let font = editor_font(&ed.look, ed.size);
+ // egui draws the caret at least one row of this font tall. Each row already has its source
+ // row's height, so the font stays below every row (half the region's smallest size): on a
+ // 12 pt row under a 24 pt title the caret is as tall as the row, not the title.
+ let smallest =
+ ed.source.as_ref().and_then(|(block, lines)| block.lines.iter().filter_map(|i| lines.get(*i)).map(|l| l.size as f32).reduce(f32::min));
+ let font = editor_font(&ed.look, smallest.map_or(ed.size, |s| (s * scale * 0.5).clamp(1.0, 300.0).min(ed.size)));
let text_color = color32(ed.look.color);
let mut done = None;
- egui::Area::new(egui::Id::new("edit-text-line")).order(egui::Order::Foreground).fixed_pos(Pos2::new(ed.rect.left(), ed.rect.top())).show(
- ctx,
- |ui| {
+ // Over the page, so only where the document shows: a paragraph scrolled under the mode bar or
+ // a side panel must leave them their pointer (#2755). It stays at its paragraph, cut off as
+ // the page is, rather than pushed back into the window.
+ egui::Area::new(egui::Id::new("edit-text-line"))
+ .order(egui::Order::Foreground)
+ .fixed_pos(Pos2::new(ed.rect.left(), ed.rect.top()))
+ .constrain_to(viewport)
+ .constrain(false)
+ .show(ctx, |ui| {
// The editor opens at the source size. Once text changes, show the real draft
// extent: single rows grow toward the page edge, paragraphs rewrap at their
// source width, and the visible frame grows/shrinks vertically with their rows.
diff --cc crates/ui-egui/tests/editing.rs
index 9f71772ad,8177842fd..d280ad089
--- a/crates/ui-egui/tests/editing.rs
+++ b/crates/ui-egui/tests/editing.rs
@@@ -4994,31 -4674,99 +4994,128 @@@ fn leaving_organize_goes_back_to_the_wo
assert_eq!(h.state().left, LeftPanel::AllTools);
}
+/// The Read mode command (not the mode bar) applies a paragraph still open in the editor, as
+/// putting the Edit text tool down does; left open, it would stay drawn in Read mode with no tool.
+#[test]
+fn read_mode_shortcut_applies_the_open_paragraph() {
+ use pdfcraft_ui_egui::{Mode, QuickTool};
+ let mut h = harness(1, |_| {});
+ assert!(h.state_mut().execute("edit.edit_text"));
+ h.run_steps(2);
+ // The line "Page 1" sits at (20, 150) on a 200 × 300 page, 24 pt.
+ let r = h.state().views[0].page_screen_rect(0).expect("on screen");
+ let at = egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height());
+ h.hover_at(at);
+ h.run_steps(1);
+ h.drag_at(at);
+ h.run_steps(1);
+ h.drop_at(at);
+ h.run_steps(3);
+ assert!(h.state().views[0].line_editor.is_some(), "the line opens for editing");
+ h.state_mut().views[0].line_editor.as_mut().unwrap().text = "Chapter One".into();
+ h.run_steps(1);
+ h.state_mut().run_command("view.read_mode");
+ h.run_steps(3);
+ let s = h.state();
+ assert_eq!(s.mode, Mode::Read);
+ assert_eq!(s.quick_tool, QuickTool::Select);
+ assert!(s.views[0].line_editor.is_none(), "no editor stays open in Read mode");
+ assert_eq!(s.session.get(s.views[0].id).unwrap().text_lines(0)[0].text, "Chapter One", "the edit is applied");
+}
++
+ /// Where a mode bar tab is, by its label.
+ fn mode_tab(h: &Harness<'static, PdfCraftApp>, label: &str) -> egui::Rect {
+ h.get_all_by_role_and_label(Role::Button, label).map(|n| n.rect()).find(|r| r.bottom() < 120.0).expect("a mode tab")
+ }
+
+ /// The cursor over each mode bar tab that page 1's text lies under, once that text is scrolled
+ /// up under the bar. With the All tools panel closed it lies under Read, Edit, Convert and E-Sign.
+ fn mode_tab_cursors(h: &mut Harness<'static, PdfCraftApp>, text: egui::Rect) -> [(&'static str, egui::CursorIcon); 4] {
+ h.state_mut().views[0].goto = Some((0, 0.5));
+ h.run_steps(4);
+ let offset = h.state().views[0].page_screen_rect(0).expect("on screen").top();
+ ["Read", "Edit", "Convert", "E-Sign"].map(|label| {
+ let at = mode_tab(h, label).center();
+ assert!(text.translate(egui::vec2(0.0, offset)).contains(at), "the text lies under {label}");
+ h.hover_at(at);
+ h.run_steps(2);
+ (label, h.output().platform_output.cursor_icon)
+ })
+ }
+
+ const HAND_ON_EVERY_TAB: [(&str, egui::CursorIcon); 4] = [
+ ("Read", egui::CursorIcon::PointingHand),
+ ("Edit", egui::CursorIcon::PointingHand),
+ ("Convert", egui::CursorIcon::PointingHand),
+ ("E-Sign", egui::CursorIcon::PointingHand),
+ ];
+
+ /// Page 1's text block on screen, relative to the page's top edge, in the window as it is now.
+ fn page_text(h: &Harness<'static, PdfCraftApp>) -> egui::Rect {
+ let id = h.state().views[0].id;
+ let block = h.state().session.get(id).unwrap().text_blocks(0)[0].clone();
+ let r = h.state().views[0].page_screen_rect(0).expect("on screen");
+ let k = r.width() / 200.0;
+ let at = |x: f64, y: f64| egui::pos2(r.left() + x as f32 * k, (300.0 - y as f32) * k);
+ egui::Rect::from_two_pos(at(block.rect[0], block.rect[1]), at(block.rect[2], block.rect[3]))
+ }
+
+ /// Text scrolled up under the mode bar leaves its tabs the hand (#2755). The Select tool's
+ /// text layer showed its I-beam there, over chrome that hides the page.
+ #[test]
+ fn mode_tabs_keep_the_hand_over_page_text_scrolled_under_them() {
+ let mut h = harness(2, |app| app.set_option("left", "closed").unwrap());
+ let text = page_text(&h);
+ // Where the page shows, its text takes the I-beam, once the page's text has arrived.
+ h.state_mut().views[0].goto = Some((0, 0.3));
+ h.run_steps(4);
+ let top = h.state().views[0].page_screen_rect(0).expect("on screen").top();
+ let mut cursor = egui::CursorIcon::Default;
+ for _ in 0..50 {
+ h.hover_at(text.center() + egui::vec2(0.0, top));
+ h.run_steps(2);
+ cursor = h.output().platform_output.cursor_icon;
+ if cursor == egui::CursorIcon::Text {
+ break;
+ }
+ }
+ assert_eq!(cursor, egui::CursorIcon::Text);
+ assert_eq!(mode_tab_cursors(&mut h, text), HAND_ON_EVERY_TAB);
+ }
+
+ /// Edit text, then the All tools panel closed, as reported (#2755): a paragraph scrolled up
+ /// under the mode bar gave its tabs the four-way arrow, or the I-beam once a paragraph was open.
+ /// The open paragraph's editor also covered the tabs and took their clicks.
+ #[test]
+ fn mode_tabs_keep_the_hand_over_a_paragraph_scrolled_under_them() {
+ let mut h = harness(2, |_| {});
+ assert!(h.state_mut().execute("edit.edit_text"));
+ h.state_mut().set_option("left", "closed").unwrap();
+ h.run_steps(4);
+ let text = page_text(&h);
+ assert_eq!(mode_tab_cursors(&mut h, text), HAND_ON_EVERY_TAB, "a closed paragraph under the tabs");
+ // Open it, where it shows.
+ h.state_mut().views[0].goto = Some((0, 0.3));
+ h.run_steps(4);
+ let top = h.state().views[0].page_screen_rect(0).expect("on screen").top();
+ click_at(&mut h, text.center() + egui::vec2(0.0, top));
+ assert!(h.state().views[0].line_editor.is_some());
+ assert_eq!(mode_tab_cursors(&mut h, text), HAND_ON_EVERY_TAB, "the open paragraph under the tabs");
+ // Nor does the editor's frame, with the paragraph's top edge just under a tab (the frame
+ // reaches 2 to 6 points outside it).
+ let convert = mode_tab(&h, "Convert").center();
+ h.state_mut().views[0].goto = Some((0, 0.0));
+ h.run_steps(4);
+ let page = h.state().views[0].page_screen_rect(0).expect("on screen");
+ h.state_mut().views[0].goto = Some((0, (page.top() - (convert.y + 4.0 - text.top())) / page.height()));
+ h.run_steps(4);
+ let page = h.state().views[0].page_screen_rect(0).expect("on screen");
+ assert!((page.top() + text.top() - convert.y - 4.0).abs() < 1.0, "Convert lies on the paragraph's frame");
+ h.hover_at(convert);
+ h.run_steps(2);
+ assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::PointingHand, "the open paragraph's frame under a tab");
+ h.state_mut().views[0].goto = Some((0, 0.5));
+ h.run_steps(4);
+ click_at(&mut h, convert);
+ assert_eq!(h.state().mode, pdfcraft_ui_egui::Mode::Convert, "the tab takes the click, not the paragraph's editor");
+ }