PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
M10: review of #1609: Edit-triggered recognition says nothing when it reads nothing
Philip8a6d7dede7ff
diff --git a/crates/ui-egui/src/ocr_ui.rs b/crates/ui-egui/src/ocr_ui.rsindex 0dcd5c9a6..34f023d6d 100644--- a/crates/ui-egui/src/ocr_ui.rs+++ b/crates/ui-egui/src/ocr_ui.rs@@ -65,6 +65,9 @@ pub struct BatchProgress {pub struct OcrRun {pub doc: DocId,pub progress: Arc<Mutex<OcrProgress>>,+ /// Started by Edit on a scanned page, not asked for: it shows no progress, and says nothing when+ /// it found no words (a page with text of its own is skipped, and Edit must not make noise).+ pub automatic: bool,}pub(crate) fn body(ui: &mut egui::Ui, app: &mut PdfCraftApp, t: &Tokens) -> (bool, bool) {@@ -165,7 +168,7 @@ impl PdfCraftApp {OcrPages::Range => (d.from.saturating_sub(1)..d.to).collect(),};let settings = d.settings();- self.start_ocr_pages(id, &pages, settings);+ self.start_ocr_pages(id, &pages, settings, false);}/// Edit on a scanned page: like Acrobat's "Converting scanned page to Editable Text and@@ -216,11 +219,11 @@ impl PdfCraftApp {let mut settings = self.ocr_draft.settings();settings.skip_text_pages = true;settings.native_text_only = true;- self.start_ocr_pages(id, &[page], settings);+ self.start_ocr_pages(id, &[page], settings, true);}/// Recognize `pages` (0-based; empty = all) of document `id` in the background.- fn start_ocr_pages(&mut self, id: DocId, pages: &[usize], settings: OcrSettings) {+ fn start_ocr_pages(&mut self, id: DocId, pages: &[usize], settings: OcrSettings, automatic: bool) {let Some(job) = self.session.ocr_job(id, pages, settings) else { return };let progress = Arc::new(Mutex::new(OcrProgress { total: job.pages.len(), ..Default::default() }));let p = progress.clone();@@ -245,7 +248,7 @@ impl PdfCraftApp {}#[cfg(target_arch = "wasm32")]work();- self.ocr_run = Some(OcrRun { doc: id, progress });+ self.ocr_run = Some(OcrRun { doc: id, progress, automatic });self.poll_ocr();}@@ -369,7 +372,7 @@ impl PdfCraftApp {}}let Some(run) = self.ocr_run.as_ref() else { return };- let (doc, progress) = (run.doc, run.progress.clone());+ let (doc, progress, automatic) = (run.doc, run.progress.clone(), run.automatic);let Ok(mut s) = progress.lock() else { return };let Some(result) = s.result.take() else {let msg = crate::i18n::fmt(@@ -377,7 +380,7 @@ impl PdfCraftApp {&[("d", &(s.done + 1).min(s.total.max(1)).to_string()), ("t", &s.total.max(1).to_string())],);drop(s);- if self.toast.as_ref().is_none_or(|t| t.0 != msg) {+ if !automatic && self.toast.as_ref().is_none_or(|t| t.0 != msg) {self.notify(msg);}if let Some(ctx) = &self.ctx {@@ -403,6 +406,10 @@ impl PdfCraftApp {{view.document_changed(&info);}+ // An automatic run that found nothing (a page that has text of its own) says nothing.+ if automatic && words == 0 {+ return;+ }let words_part = if words == 1 {crate::i18n::fmt(tl!("Recognized 1 word"), &[])} else {diff --git a/crates/ui-egui/tests/ocr.rs b/crates/ui-egui/tests/ocr.rsindex 531209634..86740c451 100644--- a/crates/ui-egui/tests/ocr.rs+++ b/crates/ui-egui/tests/ocr.rs@@ -273,6 +273,9 @@ fn editing_a_page_with_text_over_a_background_picture_changes_nothing() {assert!(h.state().ocr_run.is_none());assert_eq!(h.state().session.get(id).unwrap().can_undo(), None, "nothing was recognized or marked modified");assert!(!h.state().session.get(id).unwrap().dirty, "the file is not marked modified");+ // And Edit says nothing about recognizing: no progress, no "0 words ... already had text".+ let toast = h.state().toast.as_ref().map(|t| t.0.clone()).unwrap_or_default();+ assert!(!toast.contains("ecogniz") && !toast.contains("already had text"), "{toast:?}");}/// A page that is not a scan when Edit looks at it is looked at again after the document changes: