From 904f131b0472230cd11aa787f412d5fb62c32fb9 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sat, 21 Nov 2026 21:53:54 +0700 Subject: [PATCH] ui-patterns: PresetDrop keeps its payload type erased so a browser borrowing its rows less long takes it --- .../src/patterns/presets/browser.rs | 42 +++++++++++++++---- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/crates/ui-patterns/src/patterns/presets/browser.rs b/crates/ui-patterns/src/patterns/presets/browser.rs index c2160b8f3..3f309ab61 100644 --- a/crates/ui-patterns/src/patterns/presets/browser.rs +++ b/crates/ui-patterns/src/patterns/presets/browser.rs @@ -39,6 +39,7 @@ use craft_behavior::{DisplayOrder, DropOperation, EditEvent, GesturePhase, IconI use craft_i18n::matcher::Matcher; use craft_theme::variant::Tone; use egui::{Id, Pos2, Rect, Ui}; +use std::any::Any; use std::cell::RefCell; use std::hash::{Hash, Hasher}; use std::rc::Rc; @@ -209,20 +210,43 @@ impl RowMap<'_, K> { } } -type DropMaker<'a, K> = Box>) -> DropKind<'a, BrowserKey> + 'a>; +/// The host's test of a payload, with the payload type erased so the drop's lifetime stays covariant: a browser that +/// borrows its rows for less than the host's closure lives takes the drop as it is. +type DropAccepts<'a, K> = Rc) -> bool + 'a>; /// A drop target on the presets: a host payload of type `P` dropped onto a preset or between presets. -pub struct PresetDrop<'a, K>(DropMaker<'a, K>); +pub struct PresetDrop<'a, K> { + place: DropPlace, + accepts: DropAccepts<'a, K>, + /// The drop kind of the payload type the drop was made for, over the rows of one browser pass. + build: for<'m> fn(DropPlace, DropAccepts<'m, K>, Rc>) -> DropKind<'m, BrowserKey>, +} impl<'a, K: Clone + Eq + 'a> PresetDrop<'a, K> { /// Takes payloads of type `P` at `place` where `accepts` agrees; the drop comes back as `PresetEvent::Dropped`. pub fn new(place: DropPlace, accepts: impl Fn(&P, &DropAt) -> bool + 'a) -> Self { - let accepts = Rc::new(accepts); - PresetDrop(Box::new(move |map| { - let accepts = Rc::clone(&accepts); - DropKind::new::

(place, move |payload, at| map.host(at).is_some_and(|at| accepts(payload, &at))) - })) + PresetDrop { + place, + accepts: Rc::new(move |payload, at| payload.downcast_ref::

().is_some_and(|payload| accepts(payload, at))), + build: drop_kind::, + } } + + /// The drop kind over the rows of `map`. + fn kind<'m>(&self, map: Rc>) -> DropKind<'m, BrowserKey> + where + 'a: 'm, + { + (self.build)(self.place, Rc::clone(&self.accepts), map) + } +} + +fn drop_kind<'m, P: Send + Sync + 'static, K: Clone + Eq>( + place: DropPlace, + accepts: DropAccepts<'m, K>, + map: Rc>, +) -> DropKind<'m, BrowserKey> { + DropKind::new::

(place, move |payload, at| map.host(at).is_some_and(|at| accepts(payload, &at))) } type BrowserToolsFn<'a> = Box; @@ -879,7 +903,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> PresetBrowser<'a, K> { grid = grid.drag_payload(app_drag::); } for drop in &drops { - grid = grid.takes_drop((drop.0)(Rc::clone(&map))); + grid = grid.takes_drop(drop.kind(Rc::clone(&map))); } if let Some(key) = &reveal { grid = grid.reveal(BrowserKey::Preset(key.clone())); @@ -937,7 +961,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> PresetBrowser<'a, K> { list = list.drag_payload(app_drag::); } for drop in &drops { - list = list.takes_drop((drop.0)(Rc::clone(&map))); + list = list.takes_drop(drop.kind(Rc::clone(&map))); } if let Some(key) = &reveal { list = list.reveal(BrowserKey::Preset(key.clone()));