mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 21:49:17 +00:00
Merge pull request #9932 from makesomethingshit/fix/9814-web-file-drop-once
Web: route each dropped file once
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
//! 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<DroppedFileHandle>,
|
||||
pub(crate) pos: Option<Pos2>,
|
||||
pub(crate) shift: bool,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct FileDrops(Vec<DroppedFileHandle>);
|
||||
|
||||
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<DragPosition>>) -> Option<DropBatch> {
|
||||
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<Vec<u8>, String> {
|
||||
Err("test file has no contents".into())
|
||||
}
|
||||
}
|
||||
|
||||
fn check_handoff(at: Option<DragPosition>, expected_pos: Option<Pos2>, 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");
|
||||
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());
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
}
|
||||
@@ -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))]
|
||||
|
||||
@@ -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<Cell<Option<(f32, f32, bool)>>>;
|
||||
type DragPos = Rc<Cell<Option<DragPosition>>>;
|
||||
|
||||
/// 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<Movi
|
||||
}
|
||||
};
|
||||
app.adopt_context(ctx);
|
||||
Ok(Box::new(WebShell { app: Some(app), host, canvas: page, loss }))
|
||||
Ok(Box::new(WebShell { app: Some(app), host, canvas: page, loss, dropped: FileDrops::default() }))
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
@@ -376,6 +377,7 @@ struct WebShell {
|
||||
host: Host,
|
||||
canvas: web_sys::HtmlCanvasElement,
|
||||
loss: GraphicsLoss,
|
||||
dropped: FileDrops,
|
||||
}
|
||||
|
||||
impl WebShell {
|
||||
@@ -410,9 +412,9 @@ 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) {
|
||||
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);
|
||||
@@ -425,18 +427,13 @@ 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));
|
||||
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());
|
||||
|
||||
Reference in New Issue
Block a user