PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
fix(ui): the Background preview follows the Replace checkbox
alehel120de553ab7d
diff --git a/crates/ui-egui/src/marks_ui.rs b/crates/ui-egui/src/marks_ui.rsindex b4c930e62..a075814aa 100644--- a/crates/ui-egui/src/marks_ui.rs+++ b/crates/ui-egui/src/marks_ui.rs@@ -138,6 +138,15 @@ impl BackdropCheck {}n}++ /// What the check found on `page` (0-based) of this version of `doc`: `None` until it has+ /// looked, `Some(None)` for a page with no backdrop of its own.+ pub fn found(&self, doc: &pdfcraft_engine::Document, page: usize) -> Option<Option<Backdrop>> {+ if self.key != Some((doc.id, doc.edit_generation())) {+ return None;+ }+ self.pages.get(page).copied().flatten()+ }}impl MarksDraft {@@ -279,6 +288,19 @@ fn behind(color: [f64; 3], opacity: f64) -> Color32 {rgb32(color.map(|c| 1.0 - a * (1.0 - c)))}+/// The preview's tint for a background colour on a page whose own backdrop is `found` (`None`+/// when it has none, or the check hasn't reached it): no colour where the page will still hide+/// the background after OK, because a picture covers it (a scan) or it keeps its white backdrop+/// with Replace unticked.+fn background_tint(d: &MarksDraft, found: Option<Backdrop>) -> Color32 {+ let hidden = match found {+ Some(Backdrop::Image) => true,+ Some(Backdrop::Fill) => !d.bg.replace_backdrop,+ None => false,+ };+ if hidden { Color32::WHITE } else { behind(d.bg.color, d.bg.opacity) }+}+/// The page (0-based) the preview shows: the current one when the range selects it, otherwise the/// first page the range selects (the current one again when it selects none).fn preview_page(range: &PageRange, current: usize, count: usize) -> usize {@@ -487,7 +509,8 @@ pub(crate) fn body(ui: &mut egui::Ui, app: &mut PdfCraftApp, t: &Tokens, kind: Mview.need_thumbnail(target, true);view.thumb_id(target)});- let tint = if kind == MarkKind::Background && !d.use_file { behind(d.bg.color, d.bg.opacity) } else { Color32::WHITE };+ let tint =+ if kind == MarkKind::Background && !d.use_file { background_tint(d, d.backdrops.found(doc, target).flatten()) } else { Color32::WHITE };let hf = d.hf.clone();let size_h = page.1.max(1.0);let (wm, fit) = (d.wm.clone(), d.fit);@@ -825,6 +848,19 @@ mod tests {assert_eq!(behind([0.2, 0.4, 0.6], f64::NAN), behind([0.2, 0.4, 0.6], 1.0), "opaque, as the engine has it");}+ #[test]+ fn the_preview_shows_a_background_only_where_ok_lets_it_show() {+ let mut d = MarksDraft::default();+ let colour = behind(d.bg.color, d.bg.opacity);+ assert_ne!(colour, Color32::WHITE);+ assert_eq!(background_tint(&d, None), colour, "no backdrop of its own");+ assert_eq!(background_tint(&d, Some(Backdrop::Fill)), colour, "its white backdrop is replaced");+ assert_eq!(background_tint(&d, Some(Backdrop::Image)), Color32::WHITE, "a picture covers it");+ d.bg.replace_backdrop = false;+ assert_eq!(background_tint(&d, Some(Backdrop::Fill)), Color32::WHITE, "its white backdrop stays and hides it");+ assert_eq!(background_tint(&d, None), colour, "unticked changes nothing without a backdrop");+ }+#[test]fn the_watermark_edit_carries_the_offset() {let mut d = MarksDraft::default();diff --git a/crates/ui-egui/tests/it/marks.rs b/crates/ui-egui/tests/it/marks.rsindex 2c8b39d55..f518dcd1c 100644--- a/crates/ui-egui/tests/it/marks.rs+++ b/crates/ui-egui/tests/it/marks.rs@@ -216,6 +216,38 @@ fn the_background_dialog_offers_to_replace_a_white_backdrop_and_warns_about_scanassert_eq!(backdrop(&h, 0), None, "Update keeps the backdrop replaced");}+/// The preview shows what OK will do to each page: the colour on bare paper and on a page whose+/// white backdrop is replaced, none on a scan, and none on that page with Replace unticked.+#[test]+fn the_background_preview_follows_the_replace_checkbox() {+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_cc| {+ let mut app = PdfCraftApp::new();+ app.set_option("language", "en").unwrap();+ app.open_bytes("doc.pdf", None, backdrop_fixture()).unwrap();+ app+ });+ h.run_steps(4);+ assert!(h.state_mut().execute("edit.background"));+ h.run_steps(3);+ let replace = "Page 1 paints its own white backdrop, which would hide the background. Replace it";+ h.get_by_label(replace);+ let [red, green, blue] = pdfcraft_engine::Background::default().color.map(|c| (c * 255.0).round() as u8);+ let colour = egui::Color32::from_rgb(red, green, blue);+ let tint = |h: &mut Harness<'static, PdfCraftApp>, page: usize| {+ h.state_mut().marks_draft.preview = Some(page);+ h.run_steps(2);+ wait_for_picture(h, page).2+ };+ assert_eq!(tint(&mut h, 0), colour, "page 1's backdrop is replaced, so the colour shows");+ assert_eq!(tint(&mut h, 1), egui::Color32::WHITE, "a picture covers page 2");+ assert_eq!(tint(&mut h, 2), colour, "page 3 is bare paper");+ h.get_by_label(replace).click();+ h.run_steps(2);+ assert!(!h.state().marks_draft.bg.replace_backdrop);+ assert_eq!(tint(&mut h, 0), egui::Color32::WHITE, "unticked, page 1 keeps its backdrop, which hides the colour");+ assert_eq!(tint(&mut h, 2), colour, "page 3 has no backdrop to keep");+}+/// Remove background says which pages keep their own white backdrop off: here one whose/// backdrop copy changed before the fill since the background replaced it.#[test]diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.tomlindex ae5e6a510..46e10e8b8 100644--- a/parity/acrobat-features.toml+++ b/parity/acrobat-features.toml@@ -2531,8 +2531,8 @@ milestone = "M7"status = "shipped"commands = ["edit.background"]tools = ["doc_background", "doc_backdrops"]-evidence = ["crates/ui-egui/tests/it/marks.rs::watermark_and_background_dialogs", "crates/engine/src/tests.rs::headers_footers_watermarks_and_backgrounds_show_update_and_remove", "crates/engine/src/tests.rs::backgrounds_and_watermarks_from_files", "crates/ui-egui/tests/it/marks.rs::backgrounds_and_watermarks_from_a_file", "crates/organize/src/tests.rs::a_page_becomes_a_form_xobject_upright", "crates/automation/tests/it/backdrop.rs::a_background_shows_on_a_page_that_paints_its_own_white_backdrop", "crates/ui-egui/tests/it/marks.rs::the_background_dialog_offers_to_replace_a_white_backdrop_and_warns_about_scans", "crates/edit/src/backdrop_tests.rs::content_that_is_not_a_page_backdrop_is_left_alone", "crates/ui-egui/tests/it/marks.rs::the_preview_shows_the_page_the_range_selects_under_the_marks"]-notes = "colour, or a picture from a file (an image or a page of a PDF) scaled relative to the page. A page that paints its own opaque white backdrop (browsers' Save as PDF, print drivers) used to hide the background entirely; the dialog now offers to replace that fill (on by default; Remove turns it back on, after edits and redactions too, unless the operators before it changed or it would no longer be painted first, and then says which pages keep it off). The replaced copy's tag holds no page content or reference, so text a redaction or Remove Hidden Information takes off such a page leaves the file. Our own choice: Acrobat's behaviour on such pages has not been observed. Still hidden: scanned pages (warned), backdrops drawn inside a form XObject, by a non-rectangular path, or in a named colour space. The dialog previews a colour as a tint of the page's own thumbnail (opaque pictures on the page are tinted there too, not in the result), a file as its frame and name"+evidence = ["crates/ui-egui/tests/it/marks.rs::watermark_and_background_dialogs", "crates/engine/src/tests.rs::headers_footers_watermarks_and_backgrounds_show_update_and_remove", "crates/engine/src/tests.rs::backgrounds_and_watermarks_from_files", "crates/ui-egui/tests/it/marks.rs::backgrounds_and_watermarks_from_a_file", "crates/organize/src/tests.rs::a_page_becomes_a_form_xobject_upright", "crates/automation/tests/it/backdrop.rs::a_background_shows_on_a_page_that_paints_its_own_white_backdrop", "crates/ui-egui/tests/it/marks.rs::the_background_dialog_offers_to_replace_a_white_backdrop_and_warns_about_scans", "crates/ui-egui/tests/it/marks.rs::the_background_preview_follows_the_replace_checkbox", "crates/edit/src/backdrop_tests.rs::content_that_is_not_a_page_backdrop_is_left_alone", "crates/ui-egui/tests/it/marks.rs::the_preview_shows_the_page_the_range_selects_under_the_marks"]+notes = "colour, or a picture from a file (an image or a page of a PDF) scaled relative to the page. A page that paints its own opaque white backdrop (browsers' Save as PDF, print drivers) used to hide the background entirely; the dialog now offers to replace that fill (on by default; Remove turns it back on, after edits and redactions too, unless the operators before it changed or it would no longer be painted first, and then says which pages keep it off). The replaced copy's tag holds no page content or reference, so text a redaction or Remove Hidden Information takes off such a page leaves the file. Our own choice: Acrobat's behaviour on such pages has not been observed. Still hidden: scanned pages (warned), backdrops drawn inside a form XObject, by a non-rectangular path, or in a named colour space. The dialog previews a colour as a tint of the page's own thumbnail (opaque pictures on the page are tinted there too, not in the result), a file as its frame and name; a page that will still hide the background (a scan, or a white backdrop with Replace unticked) previews without the colour"[[feature]]id = "edit.background-update-remove"