Skip to content

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

Fill & Sign: Add text keeps what was typed on click-away, and its box grows (#918)

Shawn Skaggs40a4b411ce36
diff --git a/crates/ui-egui/src/fill_sign.rs b/crates/ui-egui/src/fill_sign.rs
index 27c7ee261..f7e45a8ef 100644
--- a/crates/ui-egui/src/fill_sign.rs
+++ b/crates/ui-egui/src/fill_sign.rs
@@ -406,8 +406,10 @@ pub(crate) fn page_input(
}
match tool {
FillTool::Text => {
- view.fill_text = Some(TypeBox { page, at: [at[0], at[1] + TEXT_SIZE * 0.6], text: String::new(), focus: true });
- None
+ // Clicking away from an open box commits it: the page sees this click before the box
+ // sees its lost focus, so replacing the box first would drop what was typed.
+ let open = view.fill_text.replace(TypeBox { page, at: [at[0], at[1] + TEXT_SIZE * 0.6], text: String::new(), focus: true });
+ open.filter(|tb| !tb.text.trim().is_empty()).map(|tb| FillAction::Edit(Box::new(typed(tb.page, tb.at, tb.text.trim(), author))))
}
FillTool::Date => Some(match date_text {
Ok(text) => FillAction::Edit(Box::new(typed(page, [at[0], at[1] + TEXT_SIZE * 0.6], text, author))),
@@ -455,7 +457,10 @@ pub(crate) fn type_box(ctx: &egui::Context, view: &mut DocView, info: &DocInfo,
let mut cancel = false;
egui::Area::new(egui::Id::new(("fill-text", view.id.0))).order(egui::Order::Foreground).fixed_pos(pos).show(ctx, |ui| {
let Some(t) = view.fill_text.as_mut() else { return };
- let width = ((t.text.len().max(8) as f32) * TEXT_SIZE as f32 * 0.6 * zoom).clamp(60.0, 600.0);
+ let width = ((t.text.chars().count().max(8) as f32) * TEXT_SIZE as f32 * 0.6 * zoom).clamp(60.0, 600.0);
+ // The area's max rect is last frame's size, which caps the edit's width; widen it so the
+ // box grows as text is typed.
+ ui.set_min_width(width);
let r = ui.add(
egui::TextEdit::singleline(&mut t.text)
.font(egui::FontId::proportional((TEXT_SIZE as f32 * zoom).max(8.0)))
diff --git a/crates/ui-egui/tests/fill_sign.rs b/crates/ui-egui/tests/fill_sign.rs
index 6f25b13c3..e45e83caf 100644
--- a/crates/ui-egui/tests/fill_sign.rs
+++ b/crates/ui-egui/tests/fill_sign.rs
@@ -124,6 +124,39 @@ fn text_marks_and_date() {
assert!(v.iter().any(|(t, _)| t == "Stamp"));
}
+/// The open type box, found as the focused text input.
+fn type_box_rect(h: &Harness<'static, PdfCraftApp>) -> egui::Rect {
+ h.query_all_by_role(egui::accesskit::Role::TextInput).find(|n| n.is_focused()).expect("an open type box").rect()
+}
+
+#[test]
+fn clicking_elsewhere_with_add_text_keeps_the_typed_text() {
+ let mut h = harness();
+ h.state_mut().execute("sign.fill.text");
+ click(&mut h, 50.0, 250.0);
+ h.event(egui::Event::Text("Ada Lovelace".into()));
+ h.run_steps(1);
+ // Still on Add text: the next click starts a new box, and must keep the first one.
+ click(&mut h, 50.0, 150.0);
+ h.run_steps(2);
+ let v = items(&h);
+ assert_eq!(v, vec![("FreeText".to_string(), Some("Ada Lovelace".to_string()))]);
+ assert!(h.state().views[0].fill_text.as_ref().is_some_and(|t| t.text.is_empty()), "a new, empty box is open");
+}
+
+#[test]
+fn the_type_box_grows_as_text_is_typed() {
+ let mut h = harness();
+ h.state_mut().execute("sign.fill.text");
+ click(&mut h, 50.0, 250.0);
+ h.run_steps(2);
+ let empty = type_box_rect(&h).width();
+ h.event(egui::Event::Text("Ada Lovelace, Countess of Lovelace".into()));
+ h.run_steps(3);
+ let full = type_box_rect(&h).width();
+ assert!(full > empty * 2.0, "the box widens with its text: {empty} -> {full}");
+}
+
#[test]
fn signing_draws_a_signature_once_and_places_it() {
let mut h = harness();