Files
vectorcraft/apps/vectorcraft-web/src/file_drops.rs
T

99 lines
3.5 KiB
Rust

//! 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);
}
}