From 14b118f9c4d6968e26f2d93b8d7404e7be7e9e26 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 16:27:53 -0800 Subject: [PATCH] Place browser drops in the tiled drawing under the pointer --- crates/ui-egui/src/canvas/panes.rs | 21 ++++++++++++ crates/ui-egui/src/place.rs | 35 +++++++++++--------- crates/ui-egui/src/tests/place.rs | 53 ++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 16 deletions(-) diff --git a/crates/ui-egui/src/canvas/panes.rs b/crates/ui-egui/src/canvas/panes.rs index 9cf1965cfc..9e25bcbf93 100644 --- a/crates/ui-egui/src/canvas/panes.rs +++ b/crates/ui-egui/src/canvas/panes.rs @@ -87,6 +87,8 @@ fn thumbnails(ctx: &egui::Context) -> Thumbnails { pub struct Panes { /// The panes the last frame drew, as (window, pane rect). pub(crate) last: Vec<(Win, egui::Rect)>, + /// The UI layer that drew the last tiled canvases. + last_layer: Option, /// The slots of `last`'s windows, in the same order: which windows they were, as document /// indices shift when a document closes. last_slots: Vec>, @@ -110,6 +112,7 @@ impl Panes { /// Forget every pane. The pictures go with the next frame that does not tile. fn clear(&mut self) { self.last.clear(); + self.last_layer = None; self.last_slots.clear(); self.fits.clear(); self.at = SplitAt::default(); @@ -322,6 +325,7 @@ pub(crate) fn tiled_area(app: &mut VectorcraftApp, ui: &mut Ui) -> bool { app.canvas.panes.fits.clear(); } app.canvas.panes.last.clear(); + app.canvas.panes.last_layer = None; app.canvas.panes.last_slots.clear(); return false; } @@ -361,8 +365,10 @@ pub(crate) fn tiled_area(app: &mut VectorcraftApp, ui: &mut Ui) -> bool { let mut lines = std::mem::take(&mut app.canvas.panes.at); let placed = lines.clone(); let mut cells: Vec<(Win, egui::Rect)> = vec![]; + let mut layer = None; let split = SplitViews::new("Documents", arranged, Bind::bound(&mut lines)).count(shown.len()).active(active_pane).show(ui, |ui, body| { cells = shown.iter().copied().zip(placed.placed(arranged, shown.len(), body.rect, body.env.direction().is_rtl())).collect(); + layer = Some(ui.layer_id()); let areas: Vec<(Win, egui::Rect)> = cells.iter().map(|(w, cell)| (*w, art_area(app, *w, *cell))).collect(); let unfitted = refit_resized(app, &areas); // The other panes first, so that what the canvas draws past its pane (the Contextual Task Bar) @@ -381,6 +387,7 @@ pub(crate) fn tiled_area(app: &mut VectorcraftApp, ui: &mut Ui) -> bool { let activated = split.output.activated; app.canvas.panes.at = lines; app.canvas.panes.last = cells; + app.canvas.panes.last_layer = layer; if let Some(&(w, _)) = activated.and_then(|i| app.canvas.panes.last.get(i)) { crate::doc_windows::show_window(app, w); } @@ -410,6 +417,20 @@ fn slot(app: &VectorcraftApp, (i, n): Win) -> Option { app.session.documents().get(i).map(|d| (d.uid, n)) } +/// The drawing and document point under a browser file drop in the last shown tiled panes. +pub(crate) fn file_drop_at(app: &VectorcraftApp, pos: egui::Pos2) -> Option<(u64, Point)> { + let panes = &app.canvas.panes; + let layer = panes.last_layer?; + if app.ui_ctx.as_ref().and_then(|ctx| ctx.layer_id_at(pos)).is_some_and(|front| front != layer) { + return None; + } + panes.last.iter().zip(&panes.last_slots).find_map(|(&(win, cell), &shown)| { + let (doc, _) = shown.filter(|&shown| Some(shown) == slot(app, win) && cell.contains(pos))?; + let view = crate::doc_windows::view(app, win)?; + view.rect.contains(pos).then(|| (doc, view.canvas(app.ui_scaling(), view.rect).to_doc(pos))) + }) +} + /// The area window `w`'s view shows its art in: what its body last showed, `cell` before the first. fn art_area(app: &VectorcraftApp, w: Win, cell: egui::Rect) -> egui::Rect { crate::doc_windows::view(app, w).map(|v| v.rect).filter(|r| r.is_positive()).unwrap_or(cell) diff --git a/crates/ui-egui/src/place.rs b/crates/ui-egui/src/place.rs index 1b2a678a4e..6458cc8d5d 100644 --- a/crates/ui-egui/src/place.rs +++ b/crates/ui-egui/src/place.rs @@ -68,8 +68,10 @@ pub enum DropTarget { /// Where a dropped file landed, as far as the platform says. #[derive(Clone, Copy, Debug, PartialEq)] pub enum DropSpot { - /// On the canvas, at this document point. + /// On the active canvas, at this document point. Canvas(Point), + /// On a tiled canvas, identified by its document uid and point. + TiledCanvas { doc: u64, at: Point }, /// Off the canvas: a panel, the tab bar, a dialog. Elsewhere, /// The platform gives no place (floating panel windows, desktop drags without a position). @@ -805,28 +807,29 @@ impl VectorcraftApp { /// plug-in reads opens wherever it is dropped, as File → Open opens it ([`io::opens_as`], /// [`fileio::opens_with_importer`]). pub fn drop_target(&self, name: &str, spot: DropSpot, shift: bool) -> DropTarget { - let (Some(st), Some(v)) = (self.session.active(), self.view()) else { return DropTarget::Open }; - let at = match spot { - _ if io::opens_as(name).is_some() || fileio::opens_with_importer(name) => return DropTarget::Open, - DropSpot::Elsewhere => return DropTarget::Open, - DropSpot::Canvas(at) => at, - DropSpot::Unknown if is_document(name) => return DropTarget::Open, - DropSpot::Unknown => v.center, + if io::opens_as(name).is_some() || fileio::opens_with_importer(name) { + return DropTarget::Open; + } + let (st, at) = match spot { + DropSpot::Canvas(at) => (self.view().and(self.session.active()), at), + DropSpot::TiledCanvas { doc, at } => (self.session.documents().iter().find(|st| st.uid == doc), at), + DropSpot::Unknown if !is_document(name) => { + let Some(view) = self.view() else { return DropTarget::Open }; + (self.session.active(), view.center) + } + _ => return DropTarget::Open, }; - DropTarget::Place(DropAt { doc: st.uid, reverts: st.reverts, at, embed: shift }) + st.map_or(DropTarget::Open, |st| DropTarget::Place(DropAt { doc: st.uid, reverts: st.reverts, at, embed: shift })) } /// Where a drop at `pos` on the window lands, for a host that follows its file drags itself (the - /// web's): on the active canvas as it last showed, else elsewhere; without a position, unknown. + /// web's): on the canvas shown under the pointer, else elsewhere; without a position, unknown. pub fn drop_spot(&self, pos: Option) -> DropSpot { let Some(pos) = pos else { return DropSpot::Unknown }; - match self.canvas_view() { - Some(view) if view.rect.contains(pos) => { - let at = view.to_doc(pos); - DropSpot::Canvas(Point::new(at.x, at.y)) - } - _ => DropSpot::Elsewhere, + if let Some(view) = self.canvas_view().filter(|view| view.rect.contains(pos)) { + return DropSpot::Canvas(view.to_doc(pos)); } + crate::canvas::panes::file_drop_at(self, pos).map_or(DropSpot::Elsewhere, |(doc, at)| DropSpot::TiledCanvas { doc, at }) } } diff --git a/crates/ui-egui/src/tests/place.rs b/crates/ui-egui/src/tests/place.rs index f23f63b763..6ae8dcd25c 100644 --- a/crates/ui-egui/src/tests/place.rs +++ b/crates/ui-egui/src/tests/place.rs @@ -462,6 +462,59 @@ fn images(app: &VectorcraftApp, i: usize) -> usize { n } +#[test] +fn a_browser_drop_uses_the_inactive_tiled_canvas_under_the_pointer() { + let mut app = app(); + let inbox = crate::place::PlaceInbox::default(); + app.services.place_inbox = Some(inbox.clone()); + let ctx = window(&app); + app.run("file.new", json!({"width": 600, "height": 500})).unwrap(); + app.run("window.arrange.tile", json!({})).unwrap(); + laid_out(&mut app, &ctx); + assert_eq!(app.session.active_index(), Some(1)); + + let (w, _) = app.canvas.panes.last.iter().copied().find(|(w, _)| w.0 == 0).unwrap(); + let view = crate::doc_windows::view(&app, w).unwrap(); + let pos = view.rect.center() + vec2(19.0, -13.0); + assert!(!app.canvas_view().unwrap().rect.contains(pos)); + let want = view.canvas(app.ui_scaling(), view.rect).to_doc(pos); + let name = "pane.png"; + let spot = app.drop_spot(Some(pos)); + let (doc, reverts) = (app.session.documents()[0].uid, app.session.documents()[0].reverts); + let drop = DropAt { doc, reverts, at: want, embed: true }; + assert_eq!(app.drop_target(name, spot, true), DropTarget::Place(drop)); + assert_eq!(app.drop_target("palette.gpl", spot, false), DropTarget::Open); + + inbox.lock().unwrap().push(crate::place::PlaceArrival { name: name.into(), bytes: png(20, 10, 72.0), drop }); + frame(&mut app, &ctx, vec![], &[], false); + assert_eq!((images(&app, 0), images(&app, 1)), (1, 0)); + let center = selected_image(&app).geometric_bounds().unwrap().center(); + assert!((center.x - want.x).abs() < 1e-6 && (center.y - want.y).abs() < 1e-6, "{center:?} vs {want:?}"); +} + +#[test] +fn a_browser_drop_cannot_reach_an_inactive_tile_through_a_floating_window() { + let mut app = app(); + let ctx = window(&app); + for _ in 0..2 { + app.run("file.new", json!({"width": 600, "height": 500})).unwrap(); + } + app.run("window.arrange.tile", json!({})).unwrap(); + app.run("window.arrange.floatInWindow", json!({"index": 2, "at": [45, 130]})).unwrap(); + app.run("document.activate", json!({"index": 1})).unwrap(); + laid_out(&mut app, &ctx); + assert_eq!(app.session.active_index(), Some(1)); + + let (tile, _) = app.canvas.panes.last.iter().copied().find(|(w, _)| w.0 == 0).unwrap(); + let tile = crate::doc_windows::view(&app, tile).unwrap().rect; + let floating = crate::doc_windows::view(&app, (2, app.ui.floating[0].view)).unwrap().rect; + let overlap = tile.intersect(floating); + assert!(overlap.is_positive(), "the floating window {floating:?} covers part of the tile {tile:?}"); + let pos = overlap.center(); + assert!(!app.canvas_view().unwrap().rect.contains(pos)); + assert_eq!(app.drop_target("covered.png", app.drop_spot(Some(pos)), false), DropTarget::Open); +} + /// The web reads dropped files asynchronously (#359): a file that arrives after another document /// became active still lands where it was dropped, and is dropped itself when that document closed. #[test]