mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-15 23:32:37 +00:00
M12: in the browser, a portfolio's initial document also waits for file reads under way (drops, the file picker) (#1652)
This commit is contained in:
1 parent
a9c3d84273
commit
12d53d707e
4 files changed
+35
-2
No files matched your search
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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<std::sync::atomic::AtomicUsize>);
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
impl WebRead {
|
||||
/// Count a read starting in `reads`.
|
||||
pub(super) fn start(reads: &std::sync::Arc<std::sync::atomic::AtomicUsize>) -> 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);
|
||||
}
|
||||
}
|
||||
@@ -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<DocId>,
|
||||
/// 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<std::sync::atomic::AtomicUsize>,
|
||||
/// 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<Option<RightPanel>>,
|
||||
|
||||
Reference in new issue
Block a user