mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-02 00:36:46 +00:00
M10: review of #1609: Edit-triggered recognition says nothing when it reads nothing
CodeRabbit: the recognition Edit starts used the same notifications as a run the user asked for, so turning Edit on for a page with text of its own (skipped by the run) showed "Recognizing text..." and then "Recognized 0 words on 0 pages; 1 page already had text". The run Edit starts is now marked automatic: it shows no progress toast, and no completion toast when it added no words. A run the user asks for, and an automatic run that did read words, notify as before. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
33fe9f440b
commit
8a6d7dede7
2 files changed
+16
-6
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in new issue
Block a user