bezel-ui 0.2.23

SwiftUI-flavored components for gpui — popovers, menus, buttons, toggles, and loaders
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
use std::{cell::Cell, rc::Rc, time::Duration};

use gpui::{App, Axis, Bounds, FocusHandle, Pixels, Point, Size, Window, point, px, size};
use motion::{Painter, TAB_SLIDE};
use web_time::Instant;

use super::{Drop, Feedback, Ghost, Outside};
use crate::scroll::Scroller;

/// Shared through the payload with every target of the drag.
#[derive(Default)]
pub(crate) struct Gesture {
    pub grab: Cell<Point<Pixels>>,
    pub size: Cell<Size<Pixels>>,
    pub claimed: Cell<bool>,
    pub hosted: Cell<bool>,
    /// The source domain draws its own ghost for the whole gesture, so no
    /// target draws one.
    pub ghosted: Cell<bool>,
    /// Where the ghost standing in for the item was last drawn.
    pub shown: Cell<Option<Point<Pixels>>>,
}

impl Gesture {
    /// Hides the in-place item while unclaimed, for a target drawing its own
    /// ghost at `origin` this frame.
    pub fn host(&self, origin: Point<Pixels>) {
        self.hosted.set(true);
        self.shown.set(Some(origin));
    }

    pub fn ghost_bounds(&self, pointer: Point<Pixels>) -> Bounds<Pixels> {
        let full = self.size.get();
        let grab = self.grab.get();
        let frame = size(full.width.min(px(180.)), full.height.min(px(32.)));
        let grab = point(grab.x.min(frame.width), grab.y.min(frame.height));
        Bounds::new(pointer - grab, frame)
    }
}

pub(super) type OnDrop<R, I> = dyn Fn(&Drop<R, I>, &mut Window, &mut App);
pub(super) type OnOutside<I> = dyn Fn(&Outside<I>, &mut Window, &mut App);
pub(super) type Accepts<I> = dyn Fn(&I) -> bool;
pub(super) type Lands<I> = dyn Fn(&I, Option<&I>, Option<&I>) -> bool;

pub(super) struct Config<R, I> {
    pub accepts: Option<Box<Accepts<I>>>,
    pub lands: Option<Box<Lands<I>>>,
    pub feedback: Feedback,
    pub axis_locked: bool,
    pub scroll: Option<Scroller>,
    pub dropped: Option<Rc<OnDrop<R, I>>>,
    pub outside: Option<Rc<OnOutside<I>>>,
    pub focus: FocusHandle,
}

impl<R, I> Config<R, I> {
    fn accepts(&self, item: &I) -> bool {
        self.accepts.as_ref().is_none_or(|accepts| accepts(item))
    }

    /// The allowed gap nearest `proposed` among `others`, the painted items
    /// the carried one is not.
    fn nearest(&self, item: &I, others: &[&Mark<I>], proposed: usize) -> Option<usize> {
        let Some(lands) = &self.lands else {
            return Some(proposed);
        };
        let allowed = |at: usize| {
            let after = at.checked_sub(1).map(|at| &others[at].item);
            let before = others.get(at).map(|mark| &mark.item);
            lands(item, after, before)
        };
        (0..=others.len()).find_map(|distance| {
            [proposed.checked_sub(distance), Some(proposed + distance)]
                .into_iter()
                .flatten()
                .filter(|at| *at <= others.len())
                .find(|at| allowed(*at))
        })
    }
}

pub(super) struct Mark<I> {
    pub item: I,
    pub bounds: Bounds<Pixels>,
}

/// A region as painted in the latest frame.
pub(super) struct Frame<R, I> {
    pub id: R,
    pub axis: Axis,
    pub origin: Point<Pixels>,
    /// The region's scroll offset as it entered the frame. Read once there:
    /// a list holds its state for the whole of its items' prepaint.
    pub scrolled: Point<Pixels>,
    pub visible: Bounds<Pixels>,
    pub handles: Vec<Mark<I>>,
    pub config: Rc<Config<R, I>>,
    /// The view that drew the region.
    pub painter: Painter,
}

impl<R, I: PartialEq> Frame<R, I> {
    /// The painted items that are not carried.
    fn others<'a>(&'a self, item: &'a I) -> impl Iterator<Item = &'a Mark<I>> + 'a {
        self.handles.iter().filter(move |mark| &mark.item != item)
    }

    fn position(&self, item: &I) -> Option<usize> {
        self.handles.iter().position(|mark| &mark.item == item)
    }

    fn reference(&self) -> Point<Pixels> {
        self.origin + self.scrolled
    }
}

#[derive(Clone, PartialEq)]
pub(super) struct Landing<R> {
    pub region: R,
    /// Insertion index among the region's painted items, the carried one
    /// left out.
    pub index: usize,
}

pub(super) struct Live<R, I> {
    pub item: I,
    pub from: R,
    pub gesture: Rc<Gesture>,
    pub origin: Point<Pixels>,
    pub extent: Pixels,
    pub landing: Option<Landing<R>>,
    pub detached: bool,
    pub pointer: Point<Pixels>,
    pub outside: Option<Rc<OnOutside<I>>>,
    pub previous_focus: Option<FocusHandle>,
}

struct Slide {
    from: Point<Pixels>,
    since: Instant,
    /// Drawn above its region, as the floating item or ghost it settles from.
    lifted: bool,
}

impl Slide {
    fn offset(&self, now: Instant) -> Option<Point<Pixels>> {
        let total = TAB_SLIDE.total().mul_f32(motion::speed_scale());
        let elapsed = now.saturating_duration_since(self.since);
        if elapsed >= total {
            return None;
        }
        Some(self.from * (1. - TAB_SLIDE.progress(elapsed.as_secs_f32() / total.as_secs_f32())))
    }
}

/// What represented an item on screen in the last frame it was placed.
enum Seen {
    /// Drawn in its slot, heading for `target`.
    Slot {
        target: Point<Pixels>,
        painted: Point<Pixels>,
    },
    /// Drawn in place, following the pointer.
    Floating(Point<Pixels>),
    /// Hidden while a ghost stood in for it, wherever the ghost reported.
    Ghost(Rc<Gesture>),
    /// Not drawn.
    Hidden,
}

struct Slot<I> {
    item: I,
    seen_at: Seen,
    /// The region's reference when `seen_at` was recorded.
    reference: Point<Pixels>,
    slide: Option<Slide>,
    seen: bool,
}

/// How a handle paints this frame.
pub(super) struct Placement {
    pub offset: Point<Pixels>,
    pub floating: bool,
    pub hidden: bool,
}

pub(super) struct State<R, I> {
    pub ghost: Option<Rc<Ghost<I>>>,
    pub live: Option<Live<R, I>>,
    pub press: Option<(I, Point<Pixels>)>,
    pub stale: bool,
    /// The last complete frame: what landings are computed against.
    frames: Vec<Frame<R, I>>,
    /// The frame being prepainted.
    building: Vec<Frame<R, I>>,
    current: Option<usize>,
    prepainted: bool,
    slots: Vec<Slot<I>>,
    drift_since: Option<Instant>,
    /// A gesture ended since the last frame began.
    ended: bool,
    /// This frame is the first laid out after a gesture ended: handles that
    /// moved slide into place. Outside a gesture and this frame they snap.
    settling: bool,
    /// Whether this frame has drifted yet: once, before the first region lays
    /// out, so every region in the frame reads the same offsets.
    drifted: bool,
}

pub(super) fn along(axis: Axis, point: Point<Pixels>) -> Pixels {
    match axis {
        Axis::Horizontal => point.x,
        Axis::Vertical => point.y,
    }
}

pub(super) fn across(axis: Axis, point: Point<Pixels>) -> Pixels {
    along(axis.invert(), point)
}

pub(super) fn length(axis: Axis, size: Size<Pixels>) -> Pixels {
    match axis {
        Axis::Horizontal => size.width,
        Axis::Vertical => size.height,
    }
}

fn on(axis: Axis, value: Pixels) -> Point<Pixels> {
    match axis {
        Axis::Horizontal => point(value, px(0.)),
        Axis::Vertical => point(px(0.), value),
    }
}

/// How far a tab may leave its strip across its axis and still be on it.
const DETACH: Pixels = px(12.);

impl<R: Clone + PartialEq, I: Clone + PartialEq> State<R, I> {
    pub fn new(ghost: Option<Rc<Ghost<I>>>) -> Self {
        Self {
            ghost,
            live: None,
            press: None,
            stale: false,
            frames: Vec::new(),
            building: Vec::new(),
            current: None,
            prepainted: false,
            slots: Vec::new(),
            drift_since: None,
            ended: false,
            settling: false,
            drifted: false,
        }
    }

    /// The first region built after a paint starts a new frame of geometry,
    /// and aims against the one just completed before anything renders.
    pub fn begin_frame(&mut self) {
        if !self.prepainted {
            return;
        }
        self.prepainted = false;
        self.drifted = false;
        self.settling = std::mem::take(&mut self.ended);
        self.frames = std::mem::take(&mut self.building);
        self.slots.retain(|slot| slot.seen);
        for slot in &mut self.slots {
            slot.seen = false;
        }
        if let Some(pointer) = self.live.as_ref().map(|live| live.pointer) {
            self.aim(pointer);
        }
    }

    pub fn enter(
        &mut self,
        id: R,
        axis: Axis,
        bounds: Bounds<Pixels>,
        visible: Bounds<Pixels>,
        config: Rc<Config<R, I>>,
        painter: Painter,
    ) -> Option<usize> {
        let frame = Frame {
            id,
            axis,
            origin: bounds.origin,
            scrolled: config
                .scroll
                .as_ref()
                .map(|scroll| scroll.offset())
                .unwrap_or_default(),
            visible,
            handles: Vec::new(),
            config,
            painter,
        };
        let at = match self.building.iter().position(|old| old.id == frame.id) {
            Some(at) => {
                self.building[at] = frame;
                at
            }
            None => {
                self.building.push(frame);
                self.building.len() - 1
            }
        };
        self.current.replace(at)
    }

    /// Redraw every view that drew one of the regions, in the frame being
    /// built or the last complete one.
    pub fn notify(&self, cx: &mut App) {
        let mut painters: Vec<Painter> = Vec::new();
        for frame in self.frames.iter().chain(&self.building) {
            if !painters.contains(&frame.painter) {
                painters.push(frame.painter);
                frame.painter.notify(cx);
            }
        }
    }

    pub fn leave(&mut self, previous: Option<usize>) {
        self.current = previous;
        self.prepainted = true;
    }

    pub fn place(
        &mut self,
        item: &I,
        bounds: Bounds<Pixels>,
        pointer: Point<Pixels>,
        cx: &mut App,
    ) -> Placement {
        let now = cx.background_executor().now();
        let Some(ix) = self.current else {
            return Placement {
                offset: Point::default(),
                floating: false,
                hidden: false,
            };
        };
        let frame = &mut self.building[ix];
        let (carried_before, m) = match &self.live {
            Some(live) => (
                frame.position(&live.item).is_some(),
                frame.others(&live.item).count(),
            ),
            None => (false, 0),
        };
        frame.handles.push(Mark {
            item: item.clone(),
            bounds,
        });
        let frame = &self.building[ix];
        let reference = frame.reference();
        let axis = frame.axis;
        let mut seen = None;
        let target = match &self.live {
            Some(live) if &live.item == item => {
                let gesture = &live.gesture;
                if self.ghost.is_some() || (gesture.hosted.get() && !gesture.claimed.get()) {
                    seen = Some(Seen::Ghost(gesture.clone()));
                    bounds.origin
                } else {
                    let mut at = pointer - gesture.grab.get();
                    if frame.config.axis_locked && !live.detached {
                        at += on(axis.invert(), across(axis, live.origin - at));
                    }
                    seen = Some(Seen::Floating(at));
                    at
                }
            }
            Some(live) if frame.config.feedback == Feedback::Displace => {
                let removed = frame.id == live.from && carried_before;
                let inserted = live
                    .landing
                    .as_ref()
                    .is_some_and(|landing| landing.region == frame.id && m >= landing.index);
                let shift = match (inserted, removed) {
                    (true, false) => live.extent,
                    (false, true) => -live.extent,
                    _ => px(0.),
                };
                bounds.origin + on(axis, shift)
            }
            _ => bounds.origin,
        };
        let painter = self.building[ix].painter;
        let moving = self.live.is_some() || self.settling;
        let slot = match self.slots.iter().position(|slot| &slot.item == item) {
            Some(at) => &mut self.slots[at],
            None => {
                self.slots.push(Slot {
                    item: item.clone(),
                    seen_at: Seen::Hidden,
                    reference,
                    slide: None,
                    seen: false,
                });
                self.slots.last_mut().unwrap()
            }
        };
        let hidden = matches!(seen, Some(Seen::Ghost(_)));
        if seen.is_some() {
            slot.slide = None;
        } else {
            let moved = reference - slot.reference;
            let from = match &slot.seen_at {
                Seen::Slot {
                    target: last,
                    painted,
                } if *last + moved != target => match moving {
                    true => Some((*painted + moved, false)),
                    false => {
                        slot.slide = None;
                        None
                    }
                },
                Seen::Slot { .. } => None,
                Seen::Floating(at) => Some((*at, true)),
                Seen::Ghost(gesture) => gesture.shown.get().map(|at| (at, true)),
                Seen::Hidden => None,
            };
            if let Some((from, lifted)) = from {
                let lifted = lifted || slot.slide.as_ref().is_some_and(|slide| slide.lifted);
                slot.slide = (from != target).then(|| Slide {
                    from: from - target,
                    since: now,
                    lifted,
                });
            }
        }
        if cx.reduce_motion() {
            slot.slide = None;
        }
        let sliding = match slot.slide.as_ref().and_then(|slide| slide.offset(now)) {
            Some(offset) => offset,
            None => {
                slot.slide = None;
                Point::default()
            }
        };
        let floating = matches!(seen, Some(Seen::Floating(_)))
            || slot.slide.as_ref().is_some_and(|slide| slide.lifted);
        slot.seen_at = seen.unwrap_or(Seen::Slot {
            target,
            painted: target + sliding,
        });
        slot.reference = reference;
        slot.seen = true;
        if slot.slide.is_some() {
            painter.lease(120., TAB_SLIDE.total(), cx);
        }
        Placement {
            offset: target + sliding - bounds.origin,
            floating,
            hidden,
        }
    }

    pub fn painted(&self, item: &I) -> Option<Point<Pixels>> {
        self.slots
            .iter()
            .find(|slot| &slot.item == item)
            .and_then(|slot| match slot.seen_at {
                Seen::Slot { painted, .. } => Some(painted),
                Seen::Floating(at) => Some(at),
                Seen::Ghost(_) | Seen::Hidden => None,
            })
    }

    pub fn start(
        &mut self,
        item: &I,
        gesture: Rc<Gesture>,
        cursor: Point<Pixels>,
        window: &mut Window,
        cx: &mut App,
    ) -> Point<Pixels> {
        let Some((frame, at)) = self
            .frames
            .iter()
            .find_map(|frame| frame.position(item).map(|at| (frame, at)))
        else {
            debug_assert!(false, "a drag handle is painted inside a region");
            return cursor;
        };
        let axis = frame.axis;
        let bounds = frame.handles[at].bounds;
        let grab = match &self.press {
            Some((pressed, grab)) if pressed == item => *grab,
            _ => cursor,
        };
        let start = along(axis, bounds.origin);
        let gap = match (frame.handles.get(at + 1), at.checked_sub(1)) {
            (Some(next), _) => along(axis, next.bounds.origin) - start - length(axis, bounds.size),
            (None, Some(previous)) => {
                let previous = frame.handles[previous].bounds;
                start - along(axis, previous.origin) - length(axis, previous.size)
            }
            (None, None) => px(0.),
        }
        .max(px(0.));
        let extent = length(axis, bounds.size) + gap;
        gesture.grab.set(grab);
        gesture.size.set(bounds.size);
        gesture.claimed.set(true);
        let origin = self.painted(item).unwrap_or(bounds.origin);
        let previous_focus = window.focused(cx);
        window.focus(&frame.config.focus, cx);
        self.live = Some(Live {
            item: item.clone(),
            from: frame.id.clone(),
            gesture,
            origin,
            extent,
            landing: Some(Landing {
                region: frame.id.clone(),
                index: at,
            }),
            detached: false,
            pointer: origin + grab,
            outside: frame.config.outside.clone(),
            previous_focus,
        });
        self.press = None;
        self.aim(window.mouse_position());
        self.notify(cx);
        grab
    }

    /// Recompute the landing for `pointer`. True when it changed.
    pub fn aim(&mut self, pointer: Point<Pixels>) -> bool {
        let Some(live) = self.live.as_mut() else {
            return false;
        };
        live.pointer = pointer;
        let item = &live.item;
        let grab = live.gesture.grab.get();
        let size = live.gesture.size.get();
        let mut claimed = false;
        let mut landing = None;
        for frame in self.frames.iter().rev() {
            if !frame.config.accepts(item) {
                continue;
            }
            let mut at = pointer;
            let visible = frame.visible;
            if visible.size.width <= px(0.) || visible.size.height <= px(0.) {
                continue;
            }
            if frame.config.axis_locked && frame.id == live.from {
                let axis = frame.axis;
                let across_at = across(axis, pointer);
                let (start, end) = (
                    across(axis, visible.origin),
                    across(axis, visible.origin) + length(axis.invert(), visible.size),
                );
                live.detached = (live.detached && !visible.contains(&pointer))
                    || across_at < start - DETACH
                    || across_at > end + DETACH;
                if live.detached {
                    continue;
                }
                claimed = true;
                at += on(axis.invert(), across_at.clamp(start, end) - across_at);
            }
            if !visible.contains(&at) {
                continue;
            }
            claimed = true;
            let axis = frame.axis;
            let centre = along(axis, pointer - grab) + length(axis, size) / 2.;
            let mut index = 0;
            for mark in frame.others(item) {
                let middle = along(axis, mark.bounds.origin) + length(axis, mark.bounds.size) / 2.;
                if centre <= middle {
                    break;
                }
                index += 1;
            }
            landing = frame
                .config
                .nearest(item, &frame.others(item).collect::<Vec<_>>(), index)
                .map(|index| Landing {
                    region: frame.id.clone(),
                    index,
                });
            break;
        }
        live.gesture.claimed.set(claimed);
        if landing == live.landing {
            return false;
        }
        live.landing = landing;
        true
    }

    /// The gap a region shows as an indicator: its position along the axis.
    pub fn indicator(&self, id: &R) -> Option<(Axis, Pixels, Bounds<Pixels>)> {
        let live = self.live.as_ref()?;
        let landing = live
            .landing
            .as_ref()
            .filter(|landing| &landing.region == id)?;
        let aimed = self.frames.iter().find(|frame| &frame.id == id)?;
        let others: Vec<_> = aimed.others(&live.item).collect();
        let before = others.get(landing.index).map(|mark| &mark.item);
        let after = landing
            .index
            .checked_sub(1)
            .and_then(|at| others.get(at))
            .map(|mark| &mark.item);
        // Drawn where the neighbours are painted in the frame being drawn.
        let frame = self
            .building
            .iter()
            .find(|frame| &frame.id == id)
            .unwrap_or(aimed);
        let axis = frame.axis;
        let bounds = |item: &I| {
            frame
                .handles
                .iter()
                .find(|mark| &mark.item == item)
                .map(|mark| mark.bounds)
        };
        let at = match (before.and_then(bounds), after.and_then(bounds)) {
            (Some(before), _) => along(axis, before.origin),
            (None, Some(after)) => along(axis, after.origin) + length(axis, after.size),
            (None, None) => along(axis, frame.origin) + px(1.),
        };
        Some((axis, at, frame.visible))
    }

    /// Ends the gesture. The caller commits whatever the landing names.
    pub fn end(&mut self, window: &mut Window, cx: &mut App) -> Option<Live<R, I>> {
        let live = self.live.take()?;
        self.ended = true;
        self.stale = false;
        self.drift_since = None;
        let focus = self
            .frames
            .iter()
            .find(|frame| frame.id == live.from)
            .map(|frame| frame.config.focus.clone());
        if focus.is_some_and(|focus| focus.is_focused(window)) {
            match &live.previous_focus {
                Some(previous) => window.focus(previous, cx),
                None => window.blur(cx),
            }
        }
        self.notify(cx);
        Some(live)
    }

    pub fn release(
        &mut self,
        pointer: Point<Pixels>,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<Commit<R, I>> {
        self.aim(pointer);
        let live = self.end(window, cx)?;
        let Some(landing) = live.landing.clone() else {
            if live.gesture.claimed.get() {
                return None;
            }
            return live.outside.map(|outside| {
                Commit::Outside(
                    outside,
                    Outside {
                        item: live.item,
                        position: pointer,
                    },
                )
            });
        };
        let frame = self
            .frames
            .iter()
            .find(|frame| frame.id == landing.region)?;
        if frame.id == live.from && frame.position(&live.item) == Some(landing.index) {
            return None;
        }
        let dropped = frame.config.dropped.clone()?;
        let others: Vec<_> = frame.others(&live.item).collect();
        let after = landing
            .index
            .checked_sub(1)
            .and_then(|at| others.get(at))
            .map(|mark| mark.item.clone());
        let before = others.get(landing.index).map(|mark| mark.item.clone());
        Some(Commit::Drop(
            dropped,
            Drop {
                item: live.item,
                from: live.from,
                region: landing.region,
                after,
                before,
            },
        ))
    }

    /// Scroll every region under the pointer while it rests near that
    /// region's visible edges, whether or not the region accepts the item.
    pub fn drift(&mut self, cx: &mut App) {
        if std::mem::replace(&mut self.drifted, true) {
            return;
        }
        let Some(pointer) = self.live.as_ref().map(|live| live.pointer) else {
            self.drift_since = None;
            return;
        };
        let now = cx.background_executor().now();
        let dt = self
            .drift_since
            .map(|since| now.saturating_duration_since(since).as_secs_f32().min(0.05))
            .unwrap_or(0.);
        let mut drifting = Vec::new();
        for frame in &self.frames {
            let Some(scroll) = frame.config.scroll.as_ref() else {
                continue;
            };
            if !frame.visible.contains(&pointer) {
                continue;
            }
            let axis = frame.axis;
            let start = along(axis, frame.visible.origin);
            let end = start + length(axis, frame.visible.size);
            let edge = px(24.).min((end - start) / 4.);
            let at = along(axis, pointer);
            let speed = if at < start + edge {
                ((start + edge - at) / edge) * 600.
            } else if at > end - edge {
                -((at - end + edge) / edge) * 600.
            } else {
                0.
            };
            let old = scroll.offset();
            let max = along(axis, scroll.max_offset());
            let can_move = (speed > 0. && along(axis, old) < px(0.))
                || (speed < 0. && along(axis, old) > -max);
            if !can_move {
                continue;
            }
            if !drifting.contains(&frame.painter) {
                drifting.push(frame.painter);
            }
            let value = (along(axis, old) + px(speed * dt)).clamp(-max, px(0.));
            scroll.set_offset(match axis {
                Axis::Horizontal => point(value, old.y),
                Axis::Vertical => point(old.x, value),
            });
        }
        if drifting.is_empty() {
            self.drift_since = None;
            return;
        }
        self.drift_since = Some(now);
        for painter in drifting {
            painter.lease(120., Duration::from_millis(100), cx);
        }
    }
}

pub(super) enum Commit<R, I> {
    Drop(Rc<OnDrop<R, I>>, Drop<R, I>),
    Outside(Rc<OnOutside<I>>, Outside<I>),
}