Skip to content

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

fix(ui): Fill & Sign font size resizes the selected text (#1274)

mtlcl4100b77f4cb771b
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 0acab64a0..f39fbc24b 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -4582,12 +4582,29 @@ fn quick_bar(app: &mut PdfCraftApp, area: Rect, ui: &mut egui::Ui) {
});
if matches!(current_fill, Some(crate::fill_sign::FillTool::Text | crate::fill_sign::FillTool::Date)) {
ui.label(tl!("Font size:"));
- ui.add(
+ let size = ui.add(
egui::DragValue::new(&mut app.fill_text_size)
.range(crate::fill_sign::MIN_TEXT_SIZE..=crate::fill_sign::MAX_TEXT_SIZE)
.speed(0.25)
.suffix(" pt"),
);
+ // #1274: the size also applies to the Fill & Sign text that is selected,
+ // once per change (a typed value, or the end of a drag), so a drag adds
+ // one undo step and not one per frame.
+ if ((size.changed() && !size.dragged()) || size.drag_stopped())
+ && let Some((index, id)) = app.active_ids()
+ && let Some((page, annotation)) = app.views.get(index).and_then(|view| view.comments.selected)
+ && app.session.get(id).is_some_and(|doc| {
+ doc.allows_annotation()
+ && doc
+ .info
+ .annotations
+ .iter()
+ .any(|a| a.page == page && a.index == annotation && a.fill_sign && a.subtype == "FreeText" && !a.locked)
+ })
+ {
+ app.apply_edit(Edit::FontSizeAnnotation { page, index: annotation, size: app.fill_text_size });
+ }
}
if let QuickTool::Comment(tool) = app.quick_tool {
let (r, _) = ui.allocate_exact_size(vec2(32.0, 9.0), Sense::hover());
diff --git a/crates/ui-egui/tests/fill_sign.rs b/crates/ui-egui/tests/fill_sign.rs
index 27fd6dd8a..8f4019fe6 100644
--- a/crates/ui-egui/tests/fill_sign.rs
+++ b/crates/ui-egui/tests/fill_sign.rs
@@ -168,6 +168,60 @@ fn fill_text_size_controls_new_text_and_survives_settings_restore() {
assert_eq!(restored.fill_text_size, 24.0, "invalid settings do not change the size");
}
+/// #1274: with a placed Fill & Sign text selected, the Font size control resizes that text,
+/// not only text typed afterwards; with nothing selected it changes nothing on the page.
+#[test]
+fn fill_text_size_resizes_the_selected_text() {
+ let mut h = harness();
+ h.state_mut().execute("sign.fill.text");
+ click(&mut h, 50.0, 250.0);
+ h.event(egui::Event::Text("Resize me".into()));
+ h.run_steps(1);
+ h.key_press(egui::Key::Enter);
+ h.run_steps(3);
+ let height = |h: &Harness<'static, PdfCraftApp>| {
+ let s = h.state();
+ let a = s.session.get(s.views[0].id).unwrap().info.annotations.iter().find(|a| a.contents.as_deref() == Some("Resize me")).cloned().unwrap();
+ a.rect[3] - a.rect[1]
+ };
+ let before = height(&h);
+ let set_size = |h: &mut Harness<'static, PdfCraftApp>, value: &str| {
+ h.query_all_by_role(egui::accesskit::Role::SpinButton)
+ .find(|n| n.value().is_some_and(|v| v.ends_with(" pt")))
+ .expect("the Font size control")
+ .click();
+ h.run_steps(2);
+ h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
+ h.event(egui::Event::Text(value.into()));
+ h.run_steps(1);
+ h.key_press(egui::Key::Enter);
+ h.run_steps(4);
+ };
+ // Nothing selected: only the size for new text changes.
+ h.state_mut().views[0].comments.selected = None;
+ set_size(&mut h, "18");
+ assert_eq!(h.state().fill_text_size, 18.0);
+ assert_eq!(height(&h), before, "an unselected placed text keeps its size");
+ // The placed text picked up with the Add text tool, then resized.
+ let annotation = {
+ let s = h.state();
+ s.session
+ .get(s.views[0].id)
+ .unwrap()
+ .info
+ .annotations
+ .iter()
+ .find(|a| a.contents.as_deref() == Some("Resize me"))
+ .map(|a| (a.page, a.index))
+ .unwrap()
+ };
+ h.state_mut().views[0].comments.selected = Some(annotation);
+ set_size(&mut h, "36");
+ assert_eq!(h.state().fill_text_size, 36.0);
+ let after = height(&h);
+ assert!(after > before * 2.0, "36 pt text has a taller box than 10 pt text: {before} -> {after}");
+}
+
#[test]
fn the_type_box_grows_as_text_is_typed() {
let mut h = harness();