Skip to content

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

M3: scroll the view when drawing at an edge; take back a polygon's last point (#1549)

philip-tamae9fb426882d
diff --git a/ROADMAP.md b/ROADMAP.md
index 6c41bce09..9cfc7f3ea 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -156,6 +156,7 @@ Ranked; detail and the full milestone table in [docs/roadmap.md](docs/roadmap.md
| 8 | 1.0 polish: performance, right-to-left, missing languages, keyboard-only (M14) | 55–95 |
## Progress log
+- **2026-10-12 (M3, drawing markups):** While a comment is being drawn, the pointer in a band along any edge of the view (or just past it) scrolls the view up, down, left or right, so points beyond what is shown can be reached. A polygon, cloud or run of connected lines takes back its last point with Backspace, Delete or Undo (the first point's removal cancels it). Edge scrolling has unit tests of its maths only; it has not been exercised end to end in the app by an automated test.
- **2026-10-12 (gap 6, #1319, #1305, #1324):** Editing an XFA form keeps a radio item's stored text when it contains a space, so a later layout still selects it. Replace Pages on a split selection replaces those pages and leaves the gap; editing the dialog range still replaces the span. Marking a figure decorative rewrites only that figure's content stream, so a header, added text and unknown stream entries stay. Estimates unchanged.
- **2026-10-15 (M14, gap 24):** `misc.localization` and `misc.installers` move from planned to partial. Localization: 22 catalogs ship, but Chinese shows tofu in releases, Telugu has no release font and Arabic is not mirrored. Installers: macOS and Windows releases are signed, Linux packages are not. The Acrobat menu diff itself remains open. Estimates unchanged.
- **2026-10-13 (G14, Vietnamese localization):** Added Vietnamese (`vi`, Tiếng Việt) as an interface and date language with a 2,591-entry catalog (`plural_none`) covering 100% of the current localizable key union (plus `theme`/`Appearance`). Locale tags `vi` / `vi-VN` / `vi_VN.UTF-8` resolve; calendar months use numeric `Tháng 1`…`12` / `tháng 1`…`12`. Native-speaker community review recorded. The twelve-language localization mean moves from ≈59% to ≈68%; weighted readiness ≈49% → ≈50%. Status stays `partial`: engine/OS details, document contents, filenames, CLI/MCP and external help stay outside the catalog by design.
diff --git a/crates/ui-egui/src/autoscroll.rs b/crates/ui-egui/src/autoscroll.rs
index 7eea563ee..14751ffc5 100644
--- a/crates/ui-egui/src/autoscroll.rs
+++ b/crates/ui-egui/src/autoscroll.rs
@@ -188,6 +188,34 @@ impl AutoScroll {
}
}
+/// Width of the band along each edge of the viewport where a drawing in progress scrolls the view.
+const EDGE_BAND: f32 = 32.0;
+/// Scroll speed at the inner edge of the band, in points per second. It grows with the square of
+/// how far into the band (or past the edge) the pointer is, up to 1.5 bands deep.
+const DRAW_EDGE_SPEED: f32 = 700.0;
+
+/// How far to scroll the content this frame while a drawing is in progress and the pointer is in
+/// the band along an edge of `viewport` (or just past it): up, down, left and right. Positive
+/// moves the content down or right, like [`AutoScroll::update`]. The pointer must be within the
+/// viewport grown by one band, so a pointer over a neighbouring panel never scrolls the page.
+pub(crate) fn edge_scroll(pointer: Option<Pos2>, viewport: egui::Rect, dt: f32) -> Vec2 {
+ let Some(p) = pointer.filter(|p| p.is_finite()) else { return Vec2::ZERO };
+ if !dt.is_finite() || !viewport.is_positive() || !viewport.expand(EDGE_BAND).contains(p) {
+ return Vec2::ZERO;
+ }
+ let dt = dt.clamp(0.0, 0.05);
+ let axis = |p: f32, lo: f32, hi: f32| {
+ // A viewport narrower than two bands would scroll both ways at once: scroll neither.
+ if hi - lo < 2.0 * EDGE_BAND {
+ return 0.0;
+ }
+ let depth = |d: f32| (d / EDGE_BAND).clamp(0.0, 1.5);
+ let (before, after) = (depth(lo + EDGE_BAND - p), depth(p - (hi - EDGE_BAND)));
+ (before * before - after * after) * DRAW_EDGE_SPEED * dt
+ };
+ vec2(axis(p.x, viewport.left(), viewport.right()), axis(p.y, viewport.top(), viewport.bottom()))
+}
+
/// Within this distance of the viewport's top or bottom edge, a text-selection drag scrolls.
const EDGE: f32 = 30.0;
/// Points per second for each point the pointer is into the edge band or past the viewport.
@@ -366,3 +394,44 @@ mod tests {
}
}
}
+
+#[cfg(test)]
+mod edge_tests {
+ use super::*;
+
+ fn view() -> egui::Rect {
+ egui::Rect::from_min_max(egui::pos2(100.0, 50.0), egui::pos2(900.0, 650.0))
+ }
+
+ #[test]
+ fn scrolls_toward_each_of_the_four_edges() {
+ let dt = 1.0 / 60.0;
+ // Near the top or left edge the content moves down or right (positive), and the reverse
+ // near the bottom or right edge.
+ assert!(edge_scroll(Some(egui::pos2(500.0, 55.0)), view(), dt).y > 0.0);
+ assert!(edge_scroll(Some(egui::pos2(500.0, 645.0)), view(), dt).y < 0.0);
+ assert!(edge_scroll(Some(egui::pos2(105.0, 300.0)), view(), dt).x > 0.0);
+ assert!(edge_scroll(Some(egui::pos2(895.0, 300.0)), view(), dt).x < 0.0);
+ // Only the axis whose edge is near moves.
+ assert_eq!(edge_scroll(Some(egui::pos2(500.0, 55.0)), view(), dt).x, 0.0);
+ }
+
+ #[test]
+ fn the_middle_and_far_away_pointers_do_not_scroll() {
+ let dt = 1.0 / 60.0;
+ assert_eq!(edge_scroll(Some(egui::pos2(500.0, 300.0)), view(), dt), Vec2::ZERO);
+ assert_eq!(edge_scroll(Some(egui::pos2(500.0, 900.0)), view(), dt), Vec2::ZERO, "over another panel");
+ assert_eq!(edge_scroll(None, view(), dt), Vec2::ZERO);
+ assert_eq!(edge_scroll(Some(egui::pos2(f32::NAN, 55.0)), view(), dt), Vec2::ZERO);
+ assert_eq!(edge_scroll(Some(egui::pos2(500.0, 55.0)), view(), f32::INFINITY), Vec2::ZERO);
+ }
+
+ #[test]
+ fn it_speeds_up_the_closer_to_the_edge_and_past_it() {
+ let dt = 1.0 / 60.0;
+ let shallow = edge_scroll(Some(egui::pos2(500.0, 70.0)), view(), dt).y;
+ let deep = edge_scroll(Some(egui::pos2(500.0, 51.0)), view(), dt).y;
+ let past = edge_scroll(Some(egui::pos2(500.0, 40.0)), view(), dt).y;
+ assert!(0.0 < shallow && shallow < deep && deep < past, "{shallow} {deep} {past}");
+ }
+}
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 708d8c173..ba2d97040 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -2291,6 +2291,18 @@ pub fn document_area(app: &mut PdfCraftApp, index: usize, ui: &mut egui::Ui) {
Vec2::ZERO
};
+ // Drawing a comment: the pointer in a band along any edge (or just past it) scrolls the view
+ // that way, so a point beyond what is shown can be reached without letting go of the tool.
+ let edge_delta = if unobstructed && matches!(view.comments.gesture, Some(comments::Gesture::Draw { .. })) {
+ let (pointer, dt) = ui.input(|i| (if i.pointer.any_down() { i.pointer.latest_pos() } else { i.pointer.hover_pos() }, i.stable_dt));
+ crate::autoscroll::edge_scroll(pointer, avail, dt)
+ } else {
+ Vec2::ZERO
+ };
+ if edge_delta != Vec2::ZERO {
+ ui.ctx().request_repaint();
+ }
+
let content_w = view.content_width(info, avail.width());
let rects = view.layout(info, content_w);
// When not scrolling continuously, only one row is shown at a time: the current page, or the
@@ -2578,9 +2590,9 @@ pub fn document_area(app: &mut PdfCraftApp, index: usize, ui: &mut egui::Ui) {
ui.set_opacity(opacity);
}
// Middle-button auto-scroll, the keyboard, precise touchpad input (#759) and its glide
- // (#531); positive y moves the content down. All scroll without easing, unlike egui's
- // wheel default.
- let delta = auto_delta - vec2(0.0, key_scroll) + precise_scroll + glide;
+ // (#531), and a drawing's edge scroll; positive y moves the content down. All scroll
+ // without easing, unlike egui's wheel default.
+ let delta = auto_delta + edge_delta - vec2(0.0, key_scroll) + precise_scroll + glide;
if delta != Vec2::ZERO {
ui.scroll_with_delta_animation(delta, egui::style::ScrollAnimation::none());
}
diff --git a/crates/ui-egui/src/comments.rs b/crates/ui-egui/src/comments.rs
index 0789d16ae..37adb1ea0 100644
--- a/crates/ui-egui/src/comments.rs
+++ b/crates/ui-egui/src/comments.rs
@@ -493,6 +493,22 @@ pub struct CommentView {
pub tool_done: bool,
}
+impl CommentView {
+ /// Takes back the last point of a polygon, cloud or connected lines being drawn; the first
+ /// point's removal cancels the drawing. Returns whether there was such a drawing.
+ pub fn undo_point(&mut self) -> bool {
+ let Some(Gesture::Draw { tool, points, .. }) = self.gesture.as_mut() else { return false };
+ if !tool.clicks_points() {
+ return false;
+ }
+ points.pop();
+ if points.is_empty() {
+ self.gesture = None;
+ }
+ true
+ }
+}
+
/// Comments panel order (Acrobat: Sort by page, author, date, type, colour).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SortBy {
@@ -779,6 +795,17 @@ pub(crate) fn page_input(ui: &egui::Ui, resp: &egui::Response, cx: &PageCx<'_>,
ui.ctx().set_cursor_icon(egui::CursorIcon::Crosshair);
}
let finish = resp.double_clicked() || ui.input(|i| i.key_pressed(egui::Key::Enter));
+ // Backspace (or Delete) takes back the last point, e.g. after a wrong click; Undo does too.
+ // Only while a drawing is in progress: otherwise Delete belongs to the selected comment.
+ if matches!(cv.gesture, Some(Gesture::Draw { page, .. }) if page == cx.page)
+ && !ui.ctx().egui_wants_keyboard_input()
+ && ui.input_mut(|i| {
+ i.consume_key(egui::Modifiers::NONE, egui::Key::Backspace) || i.consume_key(egui::Modifiers::NONE, egui::Key::Delete)
+ })
+ {
+ cv.undo_point();
+ return true;
+ }
if resp.clicked()
&& over_page
&& let Some(p) = pointer
diff --git a/crates/ui-egui/src/editing.rs b/crates/ui-egui/src/editing.rs
index 608df92cb..be1347d68 100644
--- a/crates/ui-egui/src/editing.rs
+++ b/crates/ui-egui/src/editing.rs
@@ -172,6 +172,13 @@ impl PdfCraftApp {
/// Undo the last change: to the Combine files list while its tab shows, else the document.
pub fn undo(&mut self) {
+ // While a polygon, cloud or run of connected lines is being drawn, Undo takes back its
+ // last point instead of the last finished edit.
+ if let Some((i, _)) = self.active_ids()
+ && self.views[i].comments.undo_point()
+ {
+ return;
+ }
if self.combine_showing() {
self.combine_history_step(true);
} else {
diff --git a/crates/ui-egui/tests/comments.rs b/crates/ui-egui/tests/comments.rs
index 9926bc3f6..b0340d027 100644
--- a/crates/ui-egui/tests/comments.rs
+++ b/crates/ui-egui/tests/comments.rs
@@ -858,6 +858,46 @@ fn polygons_clouds_connected_lines_callouts_and_inserted_text() {
assert_eq!(h.state().quick_tool, QuickTool::Select);
}
+fn drawn_points(h: &Harness<'static, PdfCraftApp>) -> usize {
+ match &h.state().views[0].comments.gesture {
+ Some(pdfcraft_ui_egui::comments::Gesture::Draw { points, .. }) => points.len(),
+ _ => 0,
+ }
+}
+
+#[test]
+fn a_wrong_polygon_point_is_taken_back_with_backspace_or_undo() {
+ let mut h = harness(|app| app.set_option("quick", "polygon").unwrap());
+ for p in [(40.0, 40.0), (120.0, 40.0), (200.0, 150.0)] {
+ click_pt(&mut h, p);
+ }
+ assert_eq!(drawn_points(&h), 3);
+ // Backspace takes back the wrong click, then Undo takes back the one before it.
+ h.key_press(egui::Key::Backspace);
+ h.run_steps(2);
+ assert_eq!(drawn_points(&h), 2);
+ h.state_mut().undo();
+ h.run_steps(2);
+ assert_eq!(drawn_points(&h), 1);
+ // The right corner goes where the wrong one was, and the polygon finishes with all three.
+ click_pt(&mut h, (80.0, 110.0));
+ click_pt(&mut h, (120.0, 40.0));
+ assert_eq!(drawn_points(&h), 3);
+ h.key_press(egui::Key::Enter);
+ h.run_steps(3);
+ let c = comments(&h);
+ assert_eq!(c.len(), 1);
+ assert_eq!(c[0].subtype, "Polygon");
+ // Taking back the only point cancels the drawing without making a comment.
+ h.state_mut().set_option("quick", "polygon").unwrap();
+ click_pt(&mut h, (200.0, 200.0));
+ h.key_press(egui::Key::Backspace);
+ h.run_steps(2);
+ assert_eq!(drawn_points(&h), 0);
+ assert!(h.state().views[0].comments.gesture.is_none());
+ assert_eq!(comments(&h).len(), 1);
+}
+
#[test]
fn inserted_text_snaps_to_the_gap_between_characters_on_the_baseline() {
let mut h = harness(|app| app.set_option("quick", "caret").unwrap());