diff --git a/apps/deckcraft-web/src/web.rs b/apps/deckcraft-web/src/web.rs index f7d891e0a..3b4430b5a 100644 --- a/apps/deckcraft-web/src/web.rs +++ b/apps/deckcraft-web/src/web.rs @@ -4,8 +4,9 @@ use std::sync::Arc; use craft::automation::control::ControlChannel; use craft::host::target::ShellInfo; -use craft::host::{AppInfo, Host}; +use craft::host::{AppInfo, Host, Wake}; use craft::platform::browser_files::BrowserFiles; +use craft::platform::open::{DroppedFile, FileIntake, IntakeSender}; use craft::settings::Location; use craft::ui::runtime::InstallSpec; use craft::ui::runtime::env::SystemReader; @@ -72,11 +73,13 @@ pub fn start() { let host = Host::builder(AppInfo { id: "deckcraft", name: "DeckCraft", version: env!("CARGO_PKG_VERSION") }) .files(Arc::new(files)) .build(); + let wake = cc.egui_ctx.clone(); + let (intake, drops) = FileIntake::new(Wake::new(move || wake.request_repaint())); let platform = Platform { install, host, settings: Location::Memory, - intake: None, + intake: Some(intake), audio_out: None, control: Some(ControlChannel::new(crate::api::install(cc.egui_ctx.clone()))), frame: Frame::Native, @@ -107,7 +110,7 @@ pub fn start() { { crate::host::Bridge::set_author(&mut app, &author); } - Ok(Box::new(WebShell { app, bridge: app_bridge })) + Ok(Box::new(WebShell { app, bridge: app_bridge, drops })) }), ) .await; @@ -159,6 +162,8 @@ struct WebShell { app: SlideApp, /// The parent page that keeps the files (`?host=parent`). bridge: Option, + /// Feeds the files dropped on the canvas into the app's intake once the browser has read them. + drops: IntakeSender, } impl eframe::App for WebShell { @@ -179,8 +184,29 @@ impl eframe::App for WebShell { } } - /// Delegate only this pass's input to the app's framework hooks. + /// Delegate only this pass's input to the app's framework hooks. Browsers read dropped files + /// only asynchronously: this pass's drops are taken here and reach the app's intake, in drop + /// order, once read. A file that cannot be read arrives without bytes, which the app reports. fn raw_input_hook(&mut self, ctx: &egui::Context, raw: &mut egui::RawInput) { + let dropped = std::mem::take(&mut raw.dropped_files); + if !dropped.is_empty() { + let drops = self.drops.clone(); + wasm_bindgen_futures::spawn_local(async move { + let mut files = Vec::with_capacity(dropped.len()); + for file in dropped { + let name = file.path().file_name().map(|name| name.to_string_lossy().into_owned()).unwrap_or_default(); + let bytes = match file.bytes_async().await { + Ok(bytes) => Some(bytes.into()), + Err(error) => { + log::error!("couldn't read dropped file {name}: {error}"); + None + } + }; + files.push(DroppedFile { path: None, name, bytes }); + } + drops.dropped(files); + }); + } self.app.raw_input_hook(ctx, raw); } diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index e673a5eae..b385c4b8f 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -97,10 +97,10 @@ impl SlideApp { embed::install(ctx, install, &fonts)?; host.clone().install(ctx); - use craft::settings::Settings as _; let mut options = craft::settings::OpenOptions::::new(location); #[cfg(not(target_arch = "wasm32"))] { + use craft::settings::Settings as _; let portable = craft::settings::Portable { markers: crate::settings::DeckSettings::PORTABLE_MARKERS, dir: crate::settings::DeckSettings::PORTABLE_DIR, @@ -194,6 +194,7 @@ impl SlideApp { integrated_titlebar: !matches!(frame, craft::window::policy::Frame::Native), backstage: None, pending_urls: Vec::new(), + #[cfg(not(target_arch = "wasm32"))] print_previews: Default::default(), #[cfg(not(target_arch = "wasm32"))] updates_rt: Default::default(), @@ -217,6 +218,7 @@ impl SlideApp { } /// Removes temporary PDFs created for printing. + #[cfg(not(target_arch = "wasm32"))] pub fn cleanup_print_previews(&mut self) { self.print_previews.cleanup(); } @@ -248,8 +250,10 @@ impl SlideApp { log::error!("Could not save settings: {error}"); } #[cfg(not(target_arch = "wasm32"))] - self.start_pending_update(); - self.cleanup_print_previews(); + { + self.start_pending_update(); + self.cleanup_print_previews(); + } } /// Displays a transient message in the status bar. @@ -429,10 +433,10 @@ impl SlideApp { } else { backstage::page(app, ui, cx); } - app.shell_body_end(ui, cx); + app.shell_body_end(ui); }) .page_kind(page_kind) - .body(|ui, cx, app| { + .body(|ui, _, app| { if app.session.active().is_some() { match app.ui.view { ViewMode::Sorter => sorter::show(app, ui), @@ -440,14 +444,14 @@ impl SlideApp { _ => crate::panels::show_editor(app, ui), } } - app.shell_body_end(ui, cx); + app.shell_body_end(ui); }) .show(ui, self); self.perf.frame_ms = craft::platform::clock::monotonic_ms() - started; self.issue_captures(ui); } - fn shell_body_end(&mut self, ui: &mut egui::Ui, _cx: &mut craft::ui::contract::BodyCx<'_>) { + fn shell_body_end(&mut self, ui: &mut egui::Ui) { let ctx = ui.ctx().clone(); self.opening_ui(ui); // PLAN: assumed doc_window::show(&mut SlideApp, &mut Ui) (shell-w3) @@ -476,8 +480,6 @@ impl SlideApp { { self.icon_sheet = None; } - #[cfg(target_arch = "wasm32")] - self.web_window_drop(_cx); self.drain_requests(); dialogs::pump(self, &ctx); self.sync_settings(); @@ -594,35 +596,3 @@ impl SlideApp { self.pending_urls.push(url.to_owned()); } } - -#[cfg(target_arch = "wasm32")] -struct WebWindowFiles(egui::Rect); - -#[cfg(target_arch = "wasm32")] -impl craft::ui::contract::WindowDropTarget for WebWindowFiles { - type Drop = craft::ui::contract::OsFiles; - - fn accept(&self, payload: &Self::Drop, _: craft::ui::contract::DropOperation) -> Option { - (!payload.files.is_empty()).then(|| payload.clone()) - } - - fn mark(&self, _: &Self::Drop) -> craft::ui::contract::DropMark { - craft::ui::contract::DropMark::Into(self.0) - } -} - -#[cfg(target_arch = "wasm32")] -impl SlideApp { - fn web_window_drop(&mut self, cx: &mut craft::ui::contract::BodyCx<'_>) { - use craft::ui::contract::Pointer as _; - if let Some(files) = cx.window_drop_target::(&WebWindowFiles(cx.rect)) { - for file in files.files { - if let Some(bytes) = file.bytes { - self.open_or_insert_bytes(&file.name, bytes.to_vec()); - } else { - self.set_status("The dropped file has no readable content"); - } - } - } - } -} diff --git a/crates/ui-egui/src/app/files.rs b/crates/ui-egui/src/app/files.rs index 9d1f2791a..d2d36af83 100644 --- a/crates/ui-egui/src/app/files.rs +++ b/crates/ui-egui/src/app/files.rs @@ -261,13 +261,23 @@ impl SlideApp { /// layout, slides and options) is written to the temporary folder and opened in the system's /// PDF viewer, which has the printers; on the web it downloads. pub fn print_pdf(&mut self, params: Value) -> bool { + if self.session.active().is_none() { + return false; + } + let params = if params.is_object() { params } else { json!({}) }; + #[cfg(not(target_arch = "wasm32"))] + if !self.host.files().browser_saves() { + return self.print_preview(params); + } + self.export_as("pdf", params) + } + + /// Writes the print PDF to a private temporary file and opens it in the system's PDF viewer. + #[cfg(not(target_arch = "wasm32"))] + fn print_preview(&mut self, mut params: Value) -> bool { let Some(title) = self.session.active().map(|d| d.title()) else { return false; }; - let mut params = if params.is_object() { params } else { json!({}) }; - if self.host.files().browser_saves() { - return self.export_as("pdf", params); - } let title: String = title.chars().map(|c| if c.is_alphanumeric() || matches!(c, ' ' | '-') { c } else { '_' }).collect(); let preview_path = match self.print_previews.create("deckcraft-print", &format!("{title} (print).pdf")) { Ok(path) => path, diff --git a/crates/ui-egui/src/inbox_tests.rs b/crates/ui-egui/src/inbox_tests.rs index 0deac7597..1d8b7c764 100644 --- a/crates/ui-egui/src/inbox_tests.rs +++ b/crates/ui-egui/src/inbox_tests.rs @@ -4,6 +4,7 @@ use std::sync::Arc; use craft::host::{PickId, Picked, ScriptedFiles}; +use craft::platform::open::DroppedFile; use deckcraft_model::ShapeKind; use serde_json::json; @@ -124,6 +125,33 @@ fn late_picker_files_keep_their_document_slide_and_operation() { assert_eq!(app.session.active().unwrap().selection.slide, 0); } +/// The web shell hands files dropped on the canvas to the intake as name and bytes, with no +/// path, once the browser has read them: such a picture lands on the current slide, and a file +/// the browser could not read is reported instead of vanishing. +#[test] +fn a_pathless_drop_with_bytes_inserts_and_an_unread_one_is_reported() { + let (intake, drops) = craft::platform::open::FileIntake::new(craft::host::Wake::new(|| {})); + let mut h = crate::test_harness::harness_with(Session::with_new(), Services::default(), egui_kittest::Harness::builder(), move |platform| { + platform.intake = Some(intake) + }); + h.run_steps(2); + let (uid, slide) = { + let doc = h.state().session.active().unwrap(); + (doc.uid, doc.current_slide().unwrap().id) + }; + let dropped = |name: &str, bytes: Option>| DroppedFile { path: None, name: name.into(), bytes: bytes.map(Into::into) }; + + drops.dropped(vec![dropped("shot.png", Some(png()))]); + h.run_steps(2); + assert_eq!(pictures(h.state(), uid, slide), 1, "the dropped picture is inserted on the slide in view"); + + h.state_mut().status = None; + drops.dropped(vec![dropped("unread.png", None)]); + h.run_steps(2); + assert_eq!(pictures(h.state(), uid, slide), 1, "nothing is inserted for an unread file"); + assert!(h.state().status.is_some(), "the unread file is reported"); +} + /// Closing the presentation, or dismissing the picker, drops the file instead of inserting /// it into whatever is open now. #[test] diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 2bf436c64..8084f1d3f 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -220,6 +220,7 @@ pub struct SlideApp { /// File view, or the start page when no presentation is open. pub backstage: Option, pending_urls: Vec, + #[cfg(not(target_arch = "wasm32"))] print_previews: craft::platform::temp::TempFiles, #[cfg(not(target_arch = "wasm32"))] updates_rt: updates_ui::Runtime,