PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
Forms: round required-field border on radio buttons; Highlight fields is remembered (#372)
cyfung10314a5022225828
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 1480ca219..a54dff087 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -109,11 +109,14 @@ pub struct ViewDefaults {pub fit: Fit,/// The zoom with [`Fit::None`] (1.0 = 100%).pub zoom: f32,+ /// Highlight existing fields: a preference in Acrobat, so it carries over to the next+ /// document and the next session.+ pub highlight_fields: bool,}impl Default for ViewDefaults {fn default() -> Self {- Self { layout: PageLayout::Continuous, fit: Fit::Width, zoom: 1.0 }+ Self { layout: PageLayout::Continuous, fit: Fit::Width, zoom: 1.0, highlight_fields: false }}}@@ -340,7 +343,7 @@ impl DocView {rotation: 0,current: 0,organize: false,- highlight_fields: false,+ highlight_fields: defaults.highlight_fields,page_input: "1".into(),notice_dismissed: false,cover: false,@@ -1202,6 +1205,7 @@ pub fn document_area(app: &mut PdfCraftApp, index: usize, ui: &mut egui::Ui) {Some(Notice::Repairs) => app.dialog = Some(crate::Dialog::Properties(crate::PropsTab::Advanced)),Some(Notice::Security) => app.dialog = Some(crate::Dialog::Properties(crate::PropsTab::Security)),Some(Notice::Signatures) => app.right = Some(RightPanel::Signatures),+ Some(Notice::FieldHighlights(on)) => app.view_defaults.highlight_fields = on,None => {}}// No dialog, close prompt or palette over the page: only then does page input count.@@ -1739,17 +1743,25 @@ pub fn document_area(app: &mut PdfCraftApp, index: usize, ui: &mut egui::Ui) {}// Form-field highlight (Acrobat's "Highlight existing fields"); required fields get a- // red border.+ // red border. Radio buttons are round, and so is theirs (as in Acrobat).if view.highlight_fields {for f in form.iter() {let required = f.has(pdfcraft_engine::field_flags::REQUIRED);+ let round = f.kind == pdfcraft_engine::FormFieldKind::Radio;for w in f.widgets.iter().filter(|w| w.page == Some(i) && !w.hidden) {let r = w.rect;let sr = xf.user_rect(info, i, [r[0] as f32, r[1] as f32, r[2] as f32, r[3] as f32]);- painter.rect_filled(sr, CornerRadius::same(1), Color32::from_rgba_unmultiplied(0x6E, 0x8E, 0xF5, 48));+ let tint = Color32::from_rgba_unmultiplied(0x6E, 0x8E, 0xF5, 48);let (width, color) =if required { (2.0, Color32::from_rgb(0xE3, 0x22, 0x22)) } else { (1.0, Color32::from_rgb(0x6E, 0x8E, 0xF5)) };- painter.rect_stroke(sr, CornerRadius::same(1), Stroke::new(width, color), egui::StrokeKind::Inside);+ if round {+ let radius = sr.width().min(sr.height()) / 2.0;+ painter.circle_filled(sr.center(), radius, tint);+ painter.circle_stroke(sr.center(), radius - width / 2.0, Stroke::new(width, color));+ } else {+ painter.rect_filled(sr, CornerRadius::same(1), tint);+ painter.rect_stroke(sr, CornerRadius::same(1), Stroke::new(width, color), egui::StrokeKind::Inside);+ }}}}@@ -2275,6 +2287,8 @@ enum Notice {Security,Signatures,Repairs,+ /// Highlight fields was turned on or off.+ FieldHighlights(bool),}/// The notice bar above the pages: the signature status first (Acrobat's signature bar), then@@ -2310,6 +2324,7 @@ fn notices(}let mut open_security = false;let mut open_repairs = false;+ let mut toggled = None;let msg = if secured {Some(("lock", tl!("This document is secured. Some changes are restricted by its security settings.").to_string(), false))} else if let Some(x) = xfa {@@ -2362,6 +2377,7 @@ fn notices(let label = if view.highlight_fields { tl!("Hide field highlights") } else { tl!("Highlight fields") };if crate::widgets::pill_button(ui, label, view.highlight_fields).clicked() {view.highlight_fields = !view.highlight_fields;+ toggled = Some(Notice::FieldHighlights(view.highlight_fields));}}if secured && crate::widgets::pill_button(ui, tl!("Security settings"), false).clicked() {@@ -2376,7 +2392,7 @@ fn notices(if open_repairs {return Some(Notice::Repairs);}- open_security.then_some(Notice::Security)+ open_security.then_some(Notice::Security).or(toggled)}/// The floating quick-action bar at the left edge of the document area.diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex a80d6629b..1137e689c 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -1170,6 +1170,7 @@ impl PdfCraftApp {"default_mode": self.default_mode,"default_layout": self.view_defaults.layout.as_str(),"default_zoom": self.view_defaults.zoom_name(),+ "highlight_fields": self.view_defaults.highlight_fields,"language": self.language,"author": self.comment_prefs.author,// Drawn signatures keep their original form (older settings read the same).@@ -1207,6 +1208,9 @@ impl PdfCraftApp {if let Some(layout) = v["default_layout"].as_str().and_then(canvas::PageLayout::try_parse) {self.view_defaults.layout = layout;}+ if let Some(on) = v["highlight_fields"].as_bool() {+ self.view_defaults.highlight_fields = on;+ }if let Some(defaults) = v["default_zoom"].as_str().and_then(|zoom| self.view_defaults.with_zoom(zoom)) {self.view_defaults = defaults;}diff --git a/crates/ui-egui/tests/view.rs b/crates/ui-egui/tests/view.rsindex 52ff900ca..aedeff828 100644--- a/crates/ui-egui/tests/view.rs+++ b/crates/ui-egui/tests/view.rs@@ -513,6 +513,73 @@ fn highlight_fields_tints_the_field_area() {assert_ne!(before, after, "the field area is tinted when highlighting is on");}+/// A required radio group (`/Ff` 32768 radio + 2 required) with two 40 pt round buttons.+const RADIOS: &[u8] = b"%PDF-1.7+1 0 obj << /Type /Catalog /Pages 2 0 R /AcroForm << /Fields [4 0 R] >> >> endobj+2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 /MediaBox [0 0 300 400] >> endobj+3 0 obj << /Type /Page /Parent 2 0 R /Annots [5 0 R 6 0 R] >> endobj+4 0 obj << /FT /Btn /Ff 32770 /T (choice) /V /Off /Kids [5 0 R 6 0 R] >> endobj+5 0 obj << /Type /Annot /Subtype /Widget /Parent 4 0 R /Rect [50 300 90 340] /AS /Off /P 3 0 R >> endobj+6 0 obj << /Type /Annot /Subtype /Widget /Parent 4 0 R /Rect [150 300 190 340] /AS /Off /P 3 0 R >> endobj+trailer << /Root 1 0 R >>+%%EOF";++/// #260 (page 8): with fields highlighted, a required radio button gets a round red border, as+/// Acrobat draws it, not a square one around its corners.+#[test]+fn required_radio_buttons_get_a_round_red_border() {+ let _gpu = gpu();+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_cc| {+ let mut app = PdfCraftApp::new();+ app.open_bytes("radios.pdf", None, RADIOS.to_vec()).expect("opens");+ app.set_option("left", "closed").unwrap();+ app.set_option("panel", "none").unwrap();+ app.set_option("fields", "on").unwrap();+ app+ });+ for _ in 0..100 {+ h.run_steps(2);+ if !h.state().render_pending() {+ break;+ }+ std::thread::sleep(std::time::Duration::from_millis(5));+ }+ let r = rect(&h, 0).expect("page 1");+ let ppp = h.ctx.pixels_per_point();+ let img = h.render().expect("renders");+ // The first button spans x 50..90, y 300..340 on the 300×400 pt page (y up).+ let at = |x: f32, y: f32| {+ let p = egui::pos2(r.min.x + r.width() * (x / 300.0), r.min.y + r.height() * (1.0 - y / 400.0));+ *img.get_pixel((p.x * ppp) as u32, (p.y * ppp) as u32)+ };+ let red = |p: image::Rgba<u8>| p[0] > 180 && p[1] < 100 && p[2] < 100;+ // Fit width puts about 4 px in a point: 0.2 to 0.3 pt from an edge is inside a 2 px border.+ assert!(red(at(50.2, 320.0)), "the left of the circle is red: {:?}", at(50.2, 320.0));+ assert!(!red(at(50.3, 339.7)), "the widget's corner, outside the circle, is not: {:?}", at(50.3, 339.7));+}++/// Acrobat's Highlight existing fields is a preference, not a per-document choice: turned on+/// once, the next document (and the next session) opens with fields highlighted.+#[test]+fn field_highlighting_is_remembered() {+ use egui_kittest::kittest::Queryable;+ let mut h = form_harness();+ h.state_mut().set_option("panel", "none").unwrap();+ h.run_steps(4);+ assert!(!h.state().views[0].highlight_fields, "off by default");+ h.get_by_label("Highlight fields").click();+ h.run_steps(4);+ assert!(h.state().views[0].highlight_fields);+ h.state_mut().open_bytes("radios.pdf", None, RADIOS.to_vec()).expect("opens");+ h.run_steps(4);+ assert!(h.state().views[1].highlight_fields, "the next document opens highlighted");+ let saved = h.state().persist();+ let mut next = PdfCraftApp::new();+ next.restore(&saved);+ next.open_bytes("form.pdf", None, FORM.to_vec()).expect("opens");+ assert!(next.views[0].highlight_fields, "and so does the next session");+}+#[test]fn fonts_tab_lists_fonts_and_embedding() {use egui_kittest::kittest::Queryable;diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.tomlindex 5528a4ea3..1c9f04542 100644--- a/parity/acrobat-features.toml+++ b/parity/acrobat-features.toml@@ -4302,8 +4302,8 @@ milestone = "M3"status = "shipped"commands = []tools = []-evidence = ["crates/ui-egui/tests/view.rs::highlight_fields_tints_the_field_area"]-notes = ""+evidence = ["crates/ui-egui/tests/view.rs::highlight_fields_tints_the_field_area", "crates/ui-egui/tests/view.rs::required_radio_buttons_get_a_round_red_border", "crates/ui-egui/tests/view.rs::field_highlighting_is_remembered"]+notes = "remembered for the next document and session; required fields get a red border (round for radio buttons); the tint is drawn over the field, where Acrobat paints the highlight colour behind it"[[feature]]id = "form.field-appearance-display"