From 0f19b358b0b6336d9c60a2f19b08e37b554beea0 Mon Sep 17 00:00:00 2001 From: makesomethingshit Date: Sun, 29 Nov 2026 00:16:33 +0900 Subject: [PATCH 1/3] Web: route each dropped file once --- apps/vectorcraft-web/src/web.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/apps/vectorcraft-web/src/web.rs b/apps/vectorcraft-web/src/web.rs index 0272b70a3f..bd8bbd2ba6 100644 --- a/apps/vectorcraft-web/src/web.rs +++ b/apps/vectorcraft-web/src/web.rs @@ -176,7 +176,7 @@ async fn run(host: Host, canvas: web_sys::HtmlCanvasElement, moving: Option, } impl WebShell { @@ -410,9 +411,11 @@ impl eframe::App for WebShell { embed::logic(ctx, || app.close_decision()); } - /// Forward host-gathered pointer events into this frame's raw input, then let the app apply its - /// shared input hook before egui runs. + /// Gather browser drops and forward host pointer events before egui runs. fn raw_input_hook(&mut self, ctx: &egui::Context, raw: &mut egui::RawInput) { + // The shared file router reads drops at the start of a pass, before `ui` can take them. + // Keep them for the browser's drop position and async read path. + self.dropped.append(&mut raw.dropped_files); raw.events.extend(self.host.pointers.borrow_mut().take()); if let Some(app) = &mut self.app { app.raw_input(ctx, raw); @@ -425,13 +428,12 @@ impl eframe::App for WebShell { fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) { let ctx = ui.ctx().clone(); if let Some(app) = &mut self.app { - // DOM paste and drop are host input: read them in the UI phase, once per pass, not in - // `logic` where a hidden tab replays the last shown frame's input. + // Deliver host input in a visible UI pass. Hidden tabs only run `logic`. let pasted = self.host.pasted.borrow_mut().flavour.take(); if let Some((f, held)) = pasted { app.paste_from_host(&ctx, f, held); } - let dropped = ctx.input_mut(|i| std::mem::take(&mut i.raw.dropped_files)); + let dropped = std::mem::take(&mut self.dropped); if !dropped.is_empty() { let at = self.host.drag.take(); let z = ctx.zoom_factor(); From ed7a50825f73d6154a2176eb1fe317a385a59383 Mon Sep 17 00:00:00 2001 From: makesomethingshit Date: Sun, 29 Nov 2026 10:54:13 +0900 Subject: [PATCH 2/3] Web: test that the shell takes each browser drop once --- apps/vectorcraft-web/src/file_drops.rs | 97 ++++++++++++++++++++++++++ apps/vectorcraft-web/src/main.rs | 2 + apps/vectorcraft-web/src/web.rs | 19 ++--- 3 files changed, 106 insertions(+), 12 deletions(-) create mode 100644 apps/vectorcraft-web/src/file_drops.rs diff --git a/apps/vectorcraft-web/src/file_drops.rs b/apps/vectorcraft-web/src/file_drops.rs new file mode 100644 index 0000000000..8c3fe8e539 --- /dev/null +++ b/apps/vectorcraft-web/src/file_drops.rs @@ -0,0 +1,97 @@ +//! The hand-off from the browser's input hook to the visible UI pass. + +use std::cell::Cell; + +use egui::{DroppedFileHandle, Pos2, RawInput}; + +/// CSS pixels from the canvas origin, and whether Shift was held. +pub(crate) type DragPosition = (f32, f32, bool); + +pub(crate) struct DropBatch { + pub(crate) files: Vec, + pub(crate) pos: Option, + pub(crate) shift: bool, +} + +#[derive(Default)] +pub(crate) struct FileDrops(Vec); + +impl FileDrops { + pub(crate) fn capture(&mut self, raw: &mut RawInput) { + // The shared router reads dropped files at the start of the pass, before the web UI runs. + self.0.append(&mut raw.dropped_files); + } + + pub(crate) fn take(&mut self, ctx: &egui::Context, position: &Cell>) -> Option { + let files = std::mem::take(&mut self.0); + if files.is_empty() { + return None; + } + let at = position.take(); + let z = ctx.zoom_factor(); + let pos = at.map(|(x, y, _)| egui::pos2(x / z, y / z)); + let shift = at.is_some_and(|(_, _, shift)| shift); + Some(DropBatch { files, pos, shift }) + } +} + +#[cfg(all(test, not(target_arch = "wasm32")))] +mod tests { + use std::path::Path; + use std::sync::Arc; + use std::sync::atomic::{AtomicUsize, Ordering}; + + use super::*; + + #[derive(Debug)] + struct Svg; + + impl egui::DroppedFile for Svg { + fn path(&self) -> &Path { + Path::new("drawing.svg") + } + + fn bytes(&self) -> Result, String> { + Err("test file has no contents".into()) + } + } + + fn check_handoff(at: Option, expected_pos: Option, expected_shift: bool) { + let ctx = egui::Context::default(); + let shared_files = Arc::new(AtomicUsize::new(0)); + ctx.on_begin_pass("observe dropped files before the app UI", { + let shared_files = shared_files.clone(); + Arc::new(move |ui| { + shared_files.fetch_add(ui.ctx().input(|input| input.raw.dropped_files.len()), Ordering::Relaxed); + }) + }); + let mut drops = FileDrops::default(); + let position = Cell::new(at); + let svg = Arc::new(Svg) as DroppedFileHandle; + let mut raw = RawInput { dropped_files: vec![svg.clone()], ..Default::default() }; + + drops.capture(&mut raw); + let mut delivered = Vec::new(); + ctx.run_ui(raw, |ui| delivered.extend(drops.take(ui.ctx(), &position))).textures_delta.clear(); + let mut next = RawInput::default(); + drops.capture(&mut next); + ctx.run_ui(next, |ui| delivered.extend(drops.take(ui.ctx(), &position))).textures_delta.clear(); + + assert_eq!(shared_files.load(Ordering::Relaxed), 0, "the shared router must not see browser-owned drops"); + let [batch] = delivered.as_slice() else { panic!("the browser UI must receive exactly one batch") }; + let [file] = batch.files.as_slice() else { panic!("the browser UI must receive exactly one file") }; + assert!(Arc::ptr_eq(file, &svg)); + assert_eq!((batch.pos, batch.shift), (expected_pos, expected_shift)); + assert!(position.get().is_none()); + } + + #[test] + fn a_positioned_browser_drop_reaches_the_web_ui_once() { + check_handoff(Some((53.0, 27.0, true)), Some(egui::pos2(53.0, 27.0)), true); + } + + #[test] + fn a_browser_drop_without_a_position_reaches_the_web_ui_once() { + check_handoff(None, None, false); + } +} diff --git a/apps/vectorcraft-web/src/main.rs b/apps/vectorcraft-web/src/main.rs index 696c09aab1..f8a6015fa4 100644 --- a/apps/vectorcraft-web/src/main.rs +++ b/apps/vectorcraft-web/src/main.rs @@ -22,6 +22,8 @@ #[cfg(target_arch = "wasm32")] mod api; +#[cfg(any(target_arch = "wasm32", test))] +mod file_drops; #[cfg(target_arch = "wasm32")] mod fonts; #[cfg(any(target_arch = "wasm32", test))] diff --git a/apps/vectorcraft-web/src/web.rs b/apps/vectorcraft-web/src/web.rs index bd8bbd2ba6..64a596e5ba 100644 --- a/apps/vectorcraft-web/src/web.rs +++ b/apps/vectorcraft-web/src/web.rs @@ -22,6 +22,7 @@ use vectorcraft_ui_egui::print::{PrintJob, PrintService, Printer}; use vectorcraft_ui_egui::{Services, VectorcraftApp}; use wasm_bindgen::JsCast as _; +use crate::file_drops::{DragPosition, DropBatch, FileDrops}; use crate::locks::WebLocks; use crate::pencil::SharedPointers; @@ -39,7 +40,7 @@ struct OpenInboxes { /// Where the pointer was over the canvas during the last file drag (CSS px from its top-left) and /// whether Shift was held: where dropped files land. -type DragPos = Rc>>; +type DragPos = Rc>>; /// A picture or file pasted into the page, read and waiting for the next frame with the modifiers /// held at the paste, and the context to wake for it (the current runner's). @@ -176,7 +177,7 @@ async fn run(host: Host, canvas: web_sys::HtmlCanvasElement, moving: Option, + dropped: FileDrops, } impl WebShell { @@ -413,9 +414,7 @@ impl eframe::App for WebShell { /// Gather browser drops and forward host pointer events before egui runs. fn raw_input_hook(&mut self, ctx: &egui::Context, raw: &mut egui::RawInput) { - // The shared file router reads drops at the start of a pass, before `ui` can take them. - // Keep them for the browser's drop position and async read path. - self.dropped.append(&mut raw.dropped_files); + self.dropped.capture(raw); raw.events.extend(self.host.pointers.borrow_mut().take()); if let Some(app) = &mut self.app { app.raw_input(ctx, raw); @@ -433,12 +432,8 @@ impl eframe::App for WebShell { if let Some((f, held)) = pasted { app.paste_from_host(&ctx, f, held); } - let dropped = std::mem::take(&mut self.dropped); - if !dropped.is_empty() { - let at = self.host.drag.take(); - let z = ctx.zoom_factor(); - let (pos, shift) = (at.map(|(x, y, _)| egui::pos2(x / z, y / z)), at.is_some_and(|a| a.2)); - for f in dropped { + if let Some(DropBatch { files, pos, shift }) = self.dropped.take(&ctx, &self.host.drag) { + for f in files { let name = f.path().file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| "dropped".into()); let target = app.drop_target(&name, app.drop_spot(pos), shift); let (inbox, place_inbox, repaint) = (self.host.inbox.dropped.clone(), self.host.place_inbox.clone(), ctx.clone()); From 52553034ccea2e2705925a55b10da55ad90e86aa Mon Sep 17 00:00:00 2001 From: makesomethingshit Date: Sun, 29 Nov 2026 11:42:06 +0900 Subject: [PATCH 3/3] Web tests: assert the browser drop counts without explicit panics --- apps/vectorcraft-web/src/file_drops.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/apps/vectorcraft-web/src/file_drops.rs b/apps/vectorcraft-web/src/file_drops.rs index 8c3fe8e539..1b98d7b57b 100644 --- a/apps/vectorcraft-web/src/file_drops.rs +++ b/apps/vectorcraft-web/src/file_drops.rs @@ -78,9 +78,10 @@ mod tests { ctx.run_ui(next, |ui| delivered.extend(drops.take(ui.ctx(), &position))).textures_delta.clear(); assert_eq!(shared_files.load(Ordering::Relaxed), 0, "the shared router must not see browser-owned drops"); - let [batch] = delivered.as_slice() else { panic!("the browser UI must receive exactly one batch") }; - let [file] = batch.files.as_slice() else { panic!("the browser UI must receive exactly one file") }; - assert!(Arc::ptr_eq(file, &svg)); + assert_eq!(delivered.len(), 1, "the browser UI must receive exactly one batch"); + let batch = &delivered[0]; + assert_eq!(batch.files.len(), 1, "the browser UI must receive exactly one file"); + assert!(Arc::ptr_eq(&batch.files[0], &svg)); assert_eq!((batch.pos, batch.shift), (expected_pos, expected_shift)); assert!(position.get().is_none()); }