Skip to content

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

fix(ui): Add text opens a paragraph from just inside its edge again

alehel8a21ed1236fd
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 6bdf5660c..79c95d049 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -1276,10 +1276,8 @@ fn page_tool_input(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<
};
let on_objects = tool == QuickTool::EditText && can_modify && crate::object_ui::page_input(cx, view);
let over_source = tool == QuickTool::AddText
- && pointer.is_some_and(|p| {
- !crate::content_ui::hits_added(&xf, info, i, added, p)
- && lines.iter().any(|line| xf.user_rect(info, i, line.rect.map(|v| v as f32)).expand(2.0).contains(p))
- });
+ && pointer
+ .is_some_and(|p| !crate::content_ui::hits_added(&xf, info, i, added, p) && crate::edit_text_ui::over_paragraph(&xf, info, i, &lines, p));
let source_blocked = over_source && view.content.hold_blocked();
if over_source && resp.clicked() && !source_blocked {
if let Some(edit) = view.content.finish_draft(added) {
@@ -1433,16 +1431,14 @@ impl PageHits<'_> {
.filter(|e| !e.has_changes())
.and_then(|e| lines.get(e.block))
.map(|l| xf.user_rect(info, page, l.rect.map(|v| v as f32)).expand(2.0));
- // Add text acts on the page's paragraphs only over their text (it finishes the box
- // being typed there): not on pictures, nor beside a paragraph, where a click starts
- // a new box.
+ // Add text acts on the page's paragraphs only in their boxes, edges included (it
+ // finishes the box being typed there, `over_source`): not on pictures, nor beside a
+ // paragraph, where a click starts a new box.
let edit_text = tool == QuickTool::EditText;
- let body = |h: &crate::page_router::Hit| {
- matches!(h.owner, crate::page_router::Owner::Paragraph { part: crate::page_router::ParagraphPart::Body, .. })
- };
+ let on_paragraphs = edit_text || crate::edit_text_ui::over_paragraph(xf, info, page, lines, p);
(
edit_text.then(|| crate::edit_text_ui::picture_hit(xf, page, info, images, view, p)).flatten(),
- crate::edit_text_ui::hit(xf, info, page, lines, open.map(|e| e.block), p).filter(|h| edit_text || body(h)),
+ on_paragraphs.then(|| crate::edit_text_ui::hit(xf, info, page, lines, open.map(|e| e.block), p)).flatten(),
edit_text.then(|| crate::object_ui::hit(view, xf, info, page, frame, p)).flatten(),
edit_text.then(|| crate::edit_text_ui::picture_handle_hit(clip, xf, page, info, images, view, p)).flatten(),
)
diff --git a/crates/ui-egui/src/tools/edit_text_ui.rs b/crates/ui-egui/src/tools/edit_text_ui.rs
index 6ec31b001..f5f19582d 100644
--- a/crates/ui-egui/src/tools/edit_text_ui.rs
+++ b/crates/ui-egui/src/tools/edit_text_ui.rs
@@ -273,6 +273,12 @@ fn paragraph_box(xf: &PageXform, info: &DocInfo, page: usize, rect: [f64; 4]) ->
xf.user_rect(info, page, rect.map(|v| v as f32)).expand(2.0)
}
+/// Whether `p` lies in the box of one of the paragraphs `lines` on `page`, its edge grips there
+/// included, not beside it: where Add text acts on the page's own paragraphs.
+pub(crate) fn over_paragraph(xf: &PageXform, info: &DocInfo, page: usize, lines: &[pdfcraft_engine::TextBlock], p: Pos2) -> bool {
+ lines.iter().any(|l| paragraph_box(xf, info, page, l.rect).contains(p))
+}
+
/// The width handles need the page upright (or upside down): on a quarter-turned page the
/// screen's horizontal is the paragraph's vertical.
fn has_edges(info: &DocInfo, page: usize) -> bool {
diff --git a/crates/ui-egui/tests/it/editing.rs b/crates/ui-egui/tests/it/editing.rs
index 96020a67f..508e27bab 100644
--- a/crates/ui-egui/tests/it/editing.rs
+++ b/crates/ui-egui/tests/it/editing.rs
@@ -3971,6 +3971,29 @@ fn add_text_places_a_box_on_a_picture_and_beside_a_paragraph() {
assert!(h.state().views[0].content.draft.is_some(), "a click beside the paragraph starts a text box");
}
+/// Add text opens a paragraph with a click anywhere in its box, just inside its edge too, where the
+/// edge's grip shows (a drag there rewraps it, as with Edit text). Add text's hits left the grip
+/// out there, so the click neither opened the paragraph nor started a box.
+#[test]
+fn add_text_opens_a_paragraph_from_just_inside_its_edge() {
+ let mut h = harness(2, |_| {});
+ h.state_mut().quick_tool = pdfcraft_ui_egui::QuickTool::AddText;
+ h.run_steps(2);
+ 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;
+ // 2 px right of where the text starts: 4 px into its box, within its left edge's grip.
+ let inside = egui::pos2(r.left() + block.rect[0] as f32 * k, r.top() + (300.0 - ((block.rect[1] + block.rect[3]) / 2.0) as f32) * k)
+ + egui::vec2(2.0, 0.0);
+ h.hover_at(inside);
+ h.run_steps(2);
+ assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::ResizeHorizontal, "the edge's grip");
+ click_at(&mut h, inside);
+ assert!(h.state().views[0].line_editor.is_some(), "the click opens the paragraph for typing");
+ assert!(h.state().views[0].content.draft.is_none(), "and starts no text box");
+}
+
#[test]
fn dragging_the_rotate_knob_turns_a_page_image() {
let mut png = Vec::new();