Skip to content

refactor: move watermark and N-up off the UI thread#35

Merged
nelsonduarte merged 1 commit into
mainfrom
refactor/watermark-nup-background
May 6, 2026
Merged

refactor: move watermark and N-up off the UI thread#35
nelsonduarte merged 1 commit into
mainfrom
refactor/watermark-nup-background

Conversation

@nelsonduarte
Copy link
Copy Markdown
Owner

Summary

Migrate the watermark and N-up tools from synchronous UI-thread work to BasePage._run_background, following the same pattern compress / OCR / convert-images already use. Both tools loop over every page of the source PDF, so on large documents the UI freezes during the entire run.

Why now

  • The recent worker.py @slot dispatcher landed in PR #31 — cross-thread queued connections now route reliably to the main thread on Py3.14, so this migration is safe.
  • Pattern is well-established (compress / OCR / convert-images already use it). Mechanical application, low risk.
  • TODO.txt notes the remaining converters as tech debt; this is the most user-visible chunk.

Changes

  • app/tools/watermark.py: pre-flight on main thread (validate WM file, unlock encrypted source, parse page-target spec), then a tight do_work closure that loops over pages emitting per-page progress.
  • app/tools/nup.py: pre-flight resolves cell geometry and sheet orientation, then a do_work closure that builds output sheets calling show_pdf_page for each cell, emitting per-source-page progress.
  • app/translations.json: 4 new keys × 8 languages — progress.watermark.applying / .page and progress.nup.placing / .page.

Deferred (separate PRs)

  • page_numbers: prompts the user mid-flow about replacing existing page numbers — needs a clean main-thread split before / after the prompt.
  • import_pdf: 8 conversion paths (TXT / images / MD / DOCX / PPTX / XLSX / HTML / EPUB), each warrants individual review since some shell out to libreoffice-style libs and have their own progress shapes.

Test plan

  • Smoke test on Ubuntu 26.04 + Py3.14.4 (WSLg): runs each tool through _run_background, asserts _on_done fires on the main thread, asserts the output PDF is produced. Both pass.
  • Live GUI on Windows + Py3.14: large-document watermark and N-up no longer freeze the UI; cancel button works mid-run; progress dialog shows per-page label.
  • Spot-check encrypted-source path (password prompted in viewer, watermark / N-up applied in compact mode).
  • Spot-check pipeline mode (toast Save-as button after each).

🤖 Generated with Claude Code

Both tools loop over every page of the source PDF (watermark merges
the overlay onto each, N-up calls show_pdf_page per source page),
which freezes the UI on documents with more than a few dozen pages.

Migrate both to BasePage._run_background, following the same pattern
already used by compress / OCR / convert-images:
- All input validation (file exists, watermark file non-empty,
  password unlock, cell-geometry sanity) stays on the main thread so
  errors surface as proper QMessageBoxes before any work starts.
- The do_work closure is a tight loop with no Qt calls, only emits
  worker.progress.emit(pct, label) and checks worker.is_cancelled().
- on_done runs on the main thread (via the @slot dispatcher in
  worker.run_task) and shows the toast / pipeline_success.

Add 4 new i18n keys × 8 languages for the progress messages
(progress.watermark.applying / .page, progress.nup.placing / .page).

Verified end-to-end on Ubuntu 26.04 + Py3.14.4: a smoke test that
runs each tool through _run_background asserts on_done fires on the
main thread and the output PDF is produced (5,749 bytes for the
8-page watermark sample, 5,266 bytes for the 4-up sheet).

Page-numbers and import-pdf tools still run on the UI thread —
deferred because page-numbers prompts the user mid-flow about
existing numbers (needs main-thread split before/after the prompt)
and import-pdf has 8 conversion paths that warrant individual
review.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@nelsonduarte nelsonduarte merged commit a9e3e10 into main May 6, 2026
3 checks passed
@nelsonduarte nelsonduarte deleted the refactor/watermark-nup-background branch May 6, 2026 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant