stipple 0.0.3

Stipple: a cross-platform, self-drawn UI toolkit in Rust.
Documentation
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
//! Drag and drop in the [`App`]: drags from other applications
//! ([`Event::DragEnter`](crate::platform::Event::DragEnter) and friends) and
//! drags started inside the app on an
//! [`Element::drag_source`](crate::core::Element::drag_source), both routed to
//! the [`Element::on_drop`](crate::core::Element::on_drop) target under the
//! pointer. See `docs/drag-and-drop.md`.

use crate::core::{DragSourceId, DropId, Font, LayoutNode, drag_source_at, drop_at, find_drop};
use crate::geometry::{Point, Rect};
use crate::platform::{DragOffer, DropData};
use crate::render::{Color, Scene};
use crate::style::Theme;
use crate::{App, Pane};

/// How far (logical pixels) a press on a drag source moves before it becomes
/// an in-app drag.
const DRAG_THRESHOLD: f64 = 4.0;

/// The drag-and-drop state of one window.
#[derive(Debug, Default)]
pub(crate) struct DndState {
    /// What the drag over the window carries, while one is.
    offer: Option<DragOffer>,
    /// The drop target under the drag, whether it accepts it, and the
    /// pointer within it.
    pub(crate) over: Option<(DropId, bool, Point)>,
    /// The drop target under the drag, if it accepts it.
    pub(crate) target: Option<DropId>,
    /// The target the app highlighted in the frame just built, and in the one
    /// on screen (for damage).
    pub(crate) highlight: Option<DropId>,
    pub(crate) painted: Option<DropId>,
    /// The drop targets' easing drag-over levels.
    pub(crate) fades: crate::core::DropFades,
    /// The drop targets whose view read their state in the last build.
    pub(crate) queried: Vec<DropId>,
    /// A press on a drag source, not yet moved far enough to drag.
    press: Option<(DragSourceId, Point)>,
    /// The payload of the in-app drag in progress.
    internal: Option<DropData>,
}

impl DndState {
    /// Whether an in-app drag is in progress (it owns pointer moves and the
    /// release).
    pub(crate) fn dragging_internally(&self) -> bool {
        self.internal.is_some()
    }
}

impl<S> Pane<S> {
    /// Whether a gesture latched on a press (a text selection, a drag or an
    /// in-app drag) owns the pointer's moves.
    pub(crate) fn pointer_latched(&self) -> bool {
        self.text_selecting.is_some() || self.dragging.is_some() || self.dnd.dragging_internally()
    }
}

/// Paint the hover highlight of drop target `id`: the theme's hover overlay
/// with a focus-colored border, so the target reads as "drop here".
pub(crate) fn paint_drop_target(tree: &LayoutNode, id: DropId, scene: &mut Scene, theme: &Theme) {
    let Some(node) = find_drop(tree, id) else {
        return;
    };
    let fill = theme.palette.hover_overlay;
    let ring = theme.palette.focus_ring;
    let b = node.bounds;
    let r = node.decoration.radius;
    if r > 0.0 {
        scene.fill_round_rect(b, r, fill);
    } else {
        scene.fill_rect(b, fill);
    }
    // A 2 px border inside the bounds, as four bars.
    let w = 2.0f64.min(b.width() / 2.0).min(b.height() / 2.0);
    let bars = [
        Rect::from_xywh(b.min_x(), b.min_y(), b.width(), w),
        Rect::from_xywh(b.min_x(), b.max_y() - w, b.width(), w),
        Rect::from_xywh(b.min_x(), b.min_y() + w, w, b.height() - 2.0 * w),
        Rect::from_xywh(b.max_x() - w, b.min_y() + w, w, b.height() - 2.0 * w),
    ];
    for bar in bars {
        scene.fill_rect(bar, Color { a: 0xff, ..ring });
    }
}

impl<S: 'static> Pane<S> {
    /// Move the drag (carrying `self.dnd.offer`) to `pos`: find the drop
    /// target under it and whether it accepts the drag, telling the old and
    /// new accepting targets' hover handlers when that changes, and asking
    /// for a rebuild when a view reading the state has something new to show.
    /// Returns whether a target accepts it.
    fn dnd_hover(&mut self, state: &mut S, theme: &Theme, font: Option<&Font>, pos: Point) -> bool {
        let Some(offer) = self.dnd.offer else {
            return false;
        };
        self.last_pointer = pos;
        self.ensure_tree(state, theme, font);
        let over = self
            .tree
            .as_ref()
            .and_then(|t| drop_at(t, pos))
            .map(|(id, b)| {
                let local = Point::new(pos.x - b.min_x(), pos.y - b.min_y());
                (id, self.handlers.drop_accepts(id, offer), local)
            });
        let target = over.filter(|o| o.1).map(|o| o.0);
        if target != self.dnd.target {
            if let Some(old) = self.dnd.target {
                self.handlers.dispatch_drop_hover(old, false, state);
            }
            if let Some(new) = target {
                self.handlers.dispatch_drop_hover(new, true, state);
            }
            self.dnd.target = target;
            self.dirty = true;
        }
        let key = |o: Option<(DropId, bool, Point)>| o.map(|(id, ok, _)| (id, ok));
        // Entering, leaving or a new verdict rebuilds; so does a move over a
        // target whose view follows the pointer.
        let moved = over != self.dnd.over && over.is_some_and(|o| self.dnd.queried.contains(&o.0));
        if key(over) != key(self.dnd.over) || moved {
            self.dirty = true;
        }
        self.dnd.over = over;
        target.is_some()
    }

    /// A drag carrying `offer` entered the window at `pos`. Returns whether a
    /// drop target there accepts it.
    pub(crate) fn drag_enter(
        &mut self,
        state: &mut S,
        theme: &Theme,
        font: Option<&Font>,
        pos: Point,
        offer: DragOffer,
    ) -> bool {
        self.dnd.offer = Some(offer);
        self.dnd_hover(state, theme, font, pos)
    }

    /// The drag moved to `pos`. Returns whether a drop target there accepts it.
    pub(crate) fn drag_over(
        &mut self,
        state: &mut S,
        theme: &Theme,
        font: Option<&Font>,
        pos: Point,
    ) -> bool {
        self.dnd_hover(state, theme, font, pos)
    }

    /// The drag left the window (or was cancelled).
    pub(crate) fn drag_leave(&mut self, state: &mut S) {
        if let Some(old) = self.dnd.target.take() {
            self.handlers.dispatch_drop_hover(old, false, state);
        }
        if self.dnd.over.take().is_some() {
            self.dirty = true;
        }
        self.dnd.offer = None;
    }

    /// `data` was dropped at `pos`: hand it to the drop target there that
    /// accepts it. Returns whether one did.
    pub(crate) fn drop_data(
        &mut self,
        state: &mut S,
        theme: &Theme,
        font: Option<&Font>,
        pos: Point,
        data: &DropData,
    ) -> bool {
        // The drop's own kind decides (a drag offering files and text drops
        // one of them), so look again with it.
        self.dnd.offer = Some(data.kind());
        self.dnd_hover(state, theme, font, pos);
        let target = self.dnd.target;
        self.drag_leave(state);
        let Some(id) = target else {
            return false;
        };
        self.handlers.dispatch_drop(id, data, pos, state);
        self.dirty = true;
        true
    }

    /// A left press at `pos`: latch the drag source under it, if any, so
    /// moving far enough starts an in-app drag.
    pub(crate) fn source_press(&mut self, pos: Point) {
        self.dnd.press = self
            .tree
            .as_ref()
            .and_then(|t| drag_source_at(t, pos))
            .map(|id| (id, pos));
    }

    /// The pointer moved to `pos` with the button down. Starts an in-app drag
    /// once a latched press has moved far enough, and moves one in progress.
    /// Returns whether an in-app drag owns the move.
    pub(crate) fn source_move(
        &mut self,
        state: &mut S,
        theme: &Theme,
        font: Option<&Font>,
        pos: Point,
    ) -> bool {
        if self.dnd.internal.is_some() {
            self.dnd_hover(state, theme, font, pos);
            return true;
        }
        let Some((id, origin)) = self.dnd.press else {
            return false;
        };
        if (pos.x - origin.x).hypot(pos.y - origin.y) < DRAG_THRESHOLD {
            return false;
        }
        self.dnd.press = None;
        let Some(data) = self.handlers.dispatch_drag_source(id, state) else {
            return false;
        };
        // The press became a drag: it no longer taps or drags its element.
        self.pressed = None;
        self.dragging = None;
        self.dirty = true;
        self.dnd.offer = Some(data.kind());
        self.dnd.internal = Some(data);
        self.dnd_hover(state, theme, font, pos);
        true
    }

    /// The button was released at `pos`. Drops an in-app drag in progress on
    /// the target there. Returns whether an in-app drag owned the release.
    pub(crate) fn source_release(
        &mut self,
        state: &mut S,
        theme: &Theme,
        font: Option<&Font>,
        pos: Point,
    ) -> bool {
        self.dnd.press = None;
        match self.dnd.internal.take() {
            Some(data) => {
                self.drop_data(state, theme, font, pos, &data);
                true
            }
            None => false,
        }
    }

    /// Cancel an in-app drag in progress (Escape). Returns whether there was
    /// one.
    pub(crate) fn source_cancel(&mut self, state: &mut S) -> bool {
        self.dnd.press = None;
        if self.dnd.internal.take().is_some() {
            self.drag_leave(state);
            true
        } else {
            false
        }
    }
}

impl<S: 'static> App<S> {
    /// A drag from another application, carrying `offer`, entered the primary
    /// window at `pos`. Returns whether a drop target there accepts it. (The
    /// event loop calls this for [`Event::DragEnter`](crate::platform::Event::DragEnter);
    /// public for tests.)
    pub fn drag_enter(&mut self, pos: Point, offer: DragOffer) -> bool {
        self.pane_drag_enter(0, pos, offer)
    }

    /// The drag over the primary window moved to `pos`. Returns whether a drop
    /// target there accepts it.
    pub fn drag_over(&mut self, pos: Point) -> bool {
        self.pane_drag_over(0, pos)
    }

    /// The drag left the primary window without dropping.
    pub fn drag_leave(&mut self) {
        let App { state, panes, .. } = self;
        panes[0].drag_leave(state);
    }

    /// Drop `data` at `pos` in the primary window. Returns whether a drop
    /// target took it.
    pub fn drop_data(&mut self, pos: Point, data: &DropData) -> bool {
        self.pane_drop(0, pos, data)
    }

    /// The drop target an acceptable drag is hovering in the primary window.
    pub fn drop_target(&self) -> Option<DropId> {
        self.panes[0].dnd.target
    }

    /// Whether the primary window's next frame needs building (state, hover,
    /// focus or a drag changed, or something animates).
    pub fn needs_frame(&self) -> bool {
        self.panes[0].dirty || self.panes[0].animating
    }

    pub(crate) fn pane_drag_enter(&mut self, idx: usize, pos: Point, offer: DragOffer) -> bool {
        let App {
            state,
            theme,
            font,
            panes,
            ..
        } = self;
        panes[idx].drag_enter(state, theme, font.as_ref(), pos, offer)
    }

    pub(crate) fn pane_drag_over(&mut self, idx: usize, pos: Point) -> bool {
        let App {
            state,
            theme,
            font,
            panes,
            ..
        } = self;
        panes[idx].drag_over(state, theme, font.as_ref(), pos)
    }

    pub(crate) fn pane_drag_leave(&mut self, idx: usize) {
        let App { state, panes, .. } = self;
        panes[idx].drag_leave(state);
    }

    pub(crate) fn pane_drop(&mut self, idx: usize, pos: Point, data: &DropData) -> bool {
        let App {
            state,
            theme,
            font,
            panes,
            ..
        } = self;
        panes[idx].drop_data(state, theme, font.as_ref(), pos, data)
    }

    pub(crate) fn pane_source_move(&mut self, idx: usize, pos: Point) -> bool {
        let App {
            state,
            theme,
            font,
            panes,
            ..
        } = self;
        panes[idx].source_move(state, theme, font.as_ref(), pos)
    }

    pub(crate) fn pane_source_release(&mut self, idx: usize, pos: Point) -> bool {
        let App {
            state,
            theme,
            font,
            panes,
            ..
        } = self;
        panes[idx].source_release(state, theme, font.as_ref(), pos)
    }

    pub(crate) fn pane_source_cancel(&mut self, idx: usize) -> bool {
        let App { state, panes, .. } = self;
        panes[idx].source_cancel(state)
    }

    /// Press the left button at `pos` in the primary window as the event loop
    /// does (taps, drags, text selection and drag sources latch), for tests
    /// of in-app drags.
    pub fn pointer_press(&mut self, pos: Point) {
        self.pane_pointer_down(0, pos);
    }

    /// Move the pointer to `pos` in the primary window with the button down,
    /// as the event loop does. Returns whether anything changed.
    pub fn pointer_drag_to(&mut self, pos: Point) -> bool {
        self.pane_pointer_move(0, pos)
    }

    /// Release the left button at `pos` in the primary window, as the event
    /// loop does.
    pub fn pointer_release(&mut self, pos: Point) {
        self.pane_pointer_up(0, pos);
    }
}

#[cfg(test)]
mod tests {
    use crate::App;
    use crate::core::{BoxStyle, Cx, DropState, Element};
    use crate::geometry::{Point, Size};
    use crate::platform::{DragOffer, DropData};
    use crate::widgets::column;
    use std::cell::Cell;
    use std::path::PathBuf;

    #[derive(Default)]
    struct St {
        files: Vec<PathBuf>,
        moved: Vec<String>,
        hovering: bool,
        taps: u32,
    }

    thread_local! {
        static SEEN: Cell<DropState> = Cell::new(DropState::default());
    }

    fn block() -> Element {
        Element::boxed(BoxStyle::default())
            .width(200.0)
            .height(40.0)
    }

    /// A draggable item on top (y 0..40) and a files-and-internal drop zone
    /// below it (y 40..80) whose view reads its drop state.
    fn app() -> App<St> {
        App::new(St::default(), |_: &St, cx: &mut Cx<St>| {
            let item = block()
                .on_tap(cx, |s: &mut St| s.taps += 1)
                .drag_source(cx, |_| Some(DropData::Internal("item-1".into())));
            let accepts = DragOffer {
                files: true,
                text: false,
                internal: true,
            };
            let zone = cx.register_drop(accepts, |s: &mut St, data, _| match data {
                DropData::Files(f) => s.files.extend(f.iter().cloned()),
                DropData::Internal(id) => s.moved.push(id.clone()),
                DropData::Text(_) => unreachable!("refused"),
            });
            cx.set_drop_hover(zone, |s: &mut St, on| s.hovering = on);
            SEEN.with(|c| c.set(cx.drop_state(zone)));
            column(vec![item, block().drop_target(zone)])
        })
        .logical_size(Size::new(200.0, 80.0))
    }

    #[test]
    fn os_drag_hovers_accepts_and_drops_files() {
        let mut app = app();
        let _ = app.render_frame();
        // Over the item: no drop target there.
        assert!(!app.drag_enter(Point::new(10.0, 10.0), DragOffer::FILES));
        assert_eq!(app.drop_target(), None);
        // Onto the zone: accepted, hovered, and the view sees it.
        assert!(app.drag_over(Point::new(30.0, 50.0)));
        assert!(app.state().hovering);
        assert!(app.needs_frame(), "entering rebuilds");
        let _ = app.render_frame();
        let seen = SEEN.with(Cell::get);
        assert!(seen.over && seen.accepted);
        assert_eq!(seen.position, Some(Point::new(30.0, 10.0)));
        // A move over a target whose view reads its state rebuilds too.
        assert!(app.drag_over(Point::new(35.0, 55.0)));
        assert!(app.needs_frame());
        let _ = app.render_frame();
        assert_eq!(SEEN.with(Cell::get).position, Some(Point::new(35.0, 15.0)));
        // The drop lands, ends the hover and rebuilds.
        let dropped = DropData::Files(vec![PathBuf::from("/tmp/a.txt")]);
        assert!(app.drop_data(Point::new(35.0, 55.0), &dropped));
        assert_eq!(app.state().files, vec![PathBuf::from("/tmp/a.txt")]);
        assert!(!app.state().hovering);
        assert!(app.needs_frame());
        let _ = app.render_frame();
        assert!(!SEEN.with(Cell::get).over);
    }

    #[test]
    fn refused_kind_is_over_but_not_accepted() {
        let mut app = app();
        let _ = app.render_frame();
        assert!(!app.drag_enter(Point::new(30.0, 50.0), DragOffer::TEXT));
        assert!(!app.state().hovering);
        let _ = app.render_frame();
        let seen = SEEN.with(Cell::get);
        assert!(seen.over && !seen.accepted, "{seen:?}");
        assert!(!app.drop_data(Point::new(30.0, 50.0), &DropData::Text("x".into())));
        app.drag_leave();
        let _ = app.render_frame();
        assert!(!SEEN.with(Cell::get).over);
    }

    #[test]
    fn in_app_drag_moves_an_item_without_tapping() {
        let mut app = app();
        let _ = app.render_frame();
        app.pointer_press(Point::new(20.0, 20.0));
        // Under the threshold: still a press.
        app.pointer_drag_to(Point::new(22.0, 21.0));
        assert_eq!(app.drop_target(), None);
        app.pointer_drag_to(Point::new(20.0, 30.0));
        app.pointer_drag_to(Point::new(20.0, 60.0));
        assert!(app.drop_target().is_some());
        assert!(app.state().hovering);
        app.pointer_release(Point::new(20.0, 60.0));
        assert_eq!(app.state().moved, vec!["item-1".to_string()]);
        assert_eq!(app.state().taps, 0, "a press that drags doesn't tap");
        assert!(!app.state().hovering);
        // A plain click still taps.
        app.pointer_press(Point::new(20.0, 20.0));
        app.pointer_release(Point::new(20.0, 20.0));
        assert_eq!(app.state().taps, 1);
    }

    #[test]
    fn an_unread_target_gets_the_app_highlight() {
        let mut app = App::new((), |_: &(), cx: &mut Cx<()>| {
            block().height(80.0).on_drop(cx, |_, _, _| {})
        })
        .logical_size(Size::new(200.0, 80.0));
        let (before, _) = app.render_frame();
        assert!(app.drag_enter(Point::new(10.0, 10.0), DragOffer::TEXT));
        let (during, damage) = app.render_frame();
        assert_eq!(damage, crate::core::Damage::Full);
        assert_ne!(before.as_bytes(), during.as_bytes(), "highlighted");
        app.drag_leave();
        let (after, _) = app.render_frame();
        assert_eq!(before.as_bytes(), after.as_bytes(), "highlight gone");
    }
}