From 12d53d707e642b3f818010c5a16b1e888029bb06 Mon Sep 17 00:00:00 2001 From: Eric Kaden <608341+EricKaden@users.noreply.github.com> Date: Thu, 29 Oct 2026 04:49:37 -0400 Subject: [PATCH] M12: in the browser, a portfolio's initial document also waits for file reads under way (drops, the file picker) (#1652) --- crates/ui-egui/src/app.rs | 2 ++ crates/ui-egui/src/app/files.rs | 7 +++++-- crates/ui-egui/src/app/input.rs | 24 ++++++++++++++++++++++++ crates/ui-egui/src/lib.rs | 4 ++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 9403f33c71..070517fa2e 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -54,6 +54,8 @@ impl PdfCraftApp { right_panel_closed: false, portfolio_initial: None, portfolio_seen: None, + #[cfg(target_arch = "wasm32")] + web_reads: Default::default(), portfolio_panel: None, quick_tool: QuickTool::Select, space_hand: None, diff --git a/crates/ui-egui/src/app/files.rs b/crates/ui-egui/src/app/files.rs index 1da4a191d5..8450709a16 100644 --- a/crates/ui-egui/src/app/files.rs +++ b/crates/ui-egui/src/app/files.rs @@ -433,11 +433,12 @@ impl PdfCraftApp { // A prompt or picker over the window belongs to the document it was raised for (the // password of a file opened together with the portfolio, say): the initial document // waits for its answer, and opening it then would close the prompt. A file still - // opening in the background comes to the front when it arrives, so it is waited for too. + // opening in the background (in the open worker, or a browser read of a drop or a + // picked file) comes to the front when it arrives, so it is waited for too. #[cfg(not(target_arch = "wasm32"))] let busy = self.modal_open() || self.pickers.pending() || self.pending_opens.iter().any(|p| p.activate); #[cfg(target_arch = "wasm32")] - let busy = self.modal_open(); + let busy = self.modal_open() || self.web_reads.load(std::sync::atomic::Ordering::Relaxed) > 0; if !busy && let Some((doc, index)) = self.portfolio_initial.take() && self.active_ids().is_some_and(|(_, id)| id == doc) @@ -712,7 +713,9 @@ impl PdfCraftApp { { let inbox = self.inbox.clone(); let ctx = self.ctx.clone(); + let read = super::input::WebRead::start(&self.web_reads); wasm_bindgen_futures::spawn_local(async move { + let _read = read; if let Some(h) = rfd::AsyncFileDialog::new() .add_filter("PDF", &["pdf"]) .add_filter(tl!("Images and text"), &create_ui::CONVERTIBLE) diff --git a/crates/ui-egui/src/app/input.rs b/crates/ui-egui/src/app/input.rs index cda282e9ed..5a15c51695 100644 --- a/crates/ui-egui/src/app/input.rs +++ b/crates/ui-egui/src/app/input.rs @@ -129,7 +129,9 @@ impl PdfCraftApp { self.startup_superseded = true; let inbox = self.inbox.clone(); let ctx = ctx.clone(); + let read = WebRead::start(&self.web_reads); wasm_bindgen_futures::spawn_local(async move { + let _read = read; let name = f.path().file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_else(|| "dropped.pdf".into()); if let Ok(bytes) = f.bytes_async().await && let Ok(mut q) = inbox.lock() @@ -140,3 +142,25 @@ impl PdfCraftApp { }); } } + +/// One browser file read counted in `PdfCraftApp::web_reads` for as long as it lives, so the +/// count falls however the read ends. +#[cfg(target_arch = "wasm32")] +pub(super) struct WebRead(std::sync::Arc); + +#[cfg(target_arch = "wasm32")] +impl WebRead { + /// Count a read starting in `reads`. + pub(super) fn start(reads: &std::sync::Arc) -> Self { + reads.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + Self(reads.clone()) + } +} + +#[cfg(target_arch = "wasm32")] +impl Drop for WebRead { + /// The read is over, whether its file arrived or not. + fn drop(&mut self) { + self.0.fetch_sub(1, std::sync::atomic::Ordering::Relaxed); + } +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index f5a1ed5440..5cb48c67db 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -274,6 +274,10 @@ pub struct PdfCraftApp { portfolio_initial: Option<(DocId, usize)>, /// The document whose tab [`Self::present_portfolios`] last saw active. portfolio_seen: Option, + /// Browser file reads still under way (drops, the web file picker): each file opens in front + /// when it arrives. + #[cfg(target_arch = "wasm32")] + web_reads: std::sync::Arc, /// A portfolio's file list is showing in place of this panel (`None`: the panel was /// closed), given back when its tab is left. portfolio_panel: Option>,