kael 0.2.0

GPU-accelerated native UI framework for Rust — build desktop apps with Metal, DirectX, and Vulkan rendering
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
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
use crate::{
    AnyElement, App, AppContext, Bounds, Component, Context, ElementId, InteractiveElement,
    IntoElement, List, ListAlignment, ListSizingBehavior, ListState, ParentElement, Pixels, Point,
    Render, RenderOnce, StatefulInteractiveElement, StyleRefinement, Styled, Window, div, px,
};
use std::rc::Rc;

const DEFAULT_OVERDRAW_PX: f32 = 200.0;
const DEFAULT_ESTIMATED_ITEM_HEIGHT_PX: f32 = 44.0;
const AUTO_SCROLL_THRESHOLD_PX: f32 = 36.0;
const AUTO_SCROLL_STEP_PX: f32 = 18.0;
const INSERTION_INDICATOR_HEIGHT_PX: f32 = 2.0;
const INSERTION_GAP_PX: f32 = 10.0;

#[derive(Clone, Debug, PartialEq, Eq)]
struct SortableDragPayload {
    list_id: String,
    index: usize,
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
struct DragSnapshot {
    source_index: Option<usize>,
    insertion_index: Option<usize>,
}

fn drag_reorder_target(drag: &DragSnapshot, item_count: usize) -> Option<(usize, usize)> {
    let source_index = drag.source_index?;
    let insertion_index = drag.insertion_index?;
    if source_index >= item_count || insertion_index > item_count {
        return None;
    }

    let target_index = if insertion_index > source_index {
        insertion_index.saturating_sub(1)
    } else {
        insertion_index
    };

    (target_index != source_index).then_some((source_index, target_index))
}

struct SortableListElementState {
    list_state: ListState,
    estimated_heights: Vec<Pixels>,
    alignment: ListAlignment,
    overdraw: Pixels,
    drag: DragSnapshot,
}

impl SortableListElementState {
    fn new(estimated_heights: &[Pixels], alignment: ListAlignment, overdraw: Pixels) -> Self {
        Self {
            list_state: ListState::new_estimated(
                estimated_heights.iter().copied(),
                alignment,
                overdraw,
            ),
            estimated_heights: estimated_heights.to_vec(),
            alignment,
            overdraw,
            drag: DragSnapshot::default(),
        }
    }

    fn sync(&mut self, estimated_heights: &[Pixels], alignment: ListAlignment, overdraw: Pixels) {
        if self.alignment != alignment || self.overdraw != overdraw {
            let scroll_top = self.list_state.logical_scroll_top();
            self.list_state =
                ListState::new_estimated(estimated_heights.iter().copied(), alignment, overdraw);
            self.list_state.scroll_to(scroll_top);
            self.alignment = alignment;
            self.overdraw = overdraw;
        } else if self.estimated_heights != estimated_heights {
            self.list_state
                .replace_estimated_heights(estimated_heights.iter().copied());
        }

        self.estimated_heights = estimated_heights.to_vec();
        let item_count = self.list_state.item_count();
        if self
            .drag
            .source_index
            .is_some_and(|source_index| source_index >= item_count)
        {
            self.clear_drag();
        }
    }

    fn begin_drag(&mut self, source_index: usize) {
        self.drag.source_index = Some(source_index);
        self.drag.insertion_index = Some(source_index);
    }

    fn clear_drag(&mut self) {
        self.drag = DragSnapshot::default();
    }

    fn reorder_target(&self) -> Option<(usize, usize)> {
        drag_reorder_target(&self.drag, self.list_state.item_count())
    }

    fn update_drag<D>(
        &mut self,
        payload: &SortableDragPayload,
        position: Point<Pixels>,
        bounds: Bounds<Pixels>,
        delegate: &D,
    ) where
        D: SortableDelegate,
    {
        let scroll_delta = auto_scroll_distance(position, bounds);
        if scroll_delta != Pixels::ZERO {
            self.list_state.scroll_by(scroll_delta);
        }

        let Some(insertion_index) = self.insertion_index_for_position(position, bounds) else {
            self.drag.insertion_index = None;
            return;
        };

        self.update_insertion_index(payload, insertion_index, delegate);
    }

    fn update_insertion_index<D>(
        &mut self,
        payload: &SortableDragPayload,
        insertion_index: usize,
        delegate: &D,
    ) where
        D: SortableDelegate,
    {
        let source_index = *self.drag.source_index.get_or_insert(payload.index);
        let candidate_target = if insertion_index > source_index {
            insertion_index.saturating_sub(1)
        } else {
            insertion_index
        };

        if candidate_target == source_index || delegate.can_move(source_index, candidate_target) {
            self.drag.insertion_index = Some(insertion_index);
        } else {
            self.drag.insertion_index = None;
        }
    }

    fn insertion_index_for_position(
        &self,
        position: Point<Pixels>,
        bounds: Bounds<Pixels>,
    ) -> Option<usize> {
        let item_count = self.list_state.item_count();
        if item_count == 0 {
            return Some(0);
        }

        let relative_y = position.y - bounds.top();
        let scroll_top = self.list_state.logical_scroll_top();
        let start_index = scroll_top.item_ix.min(item_count.saturating_sub(1));
        let mut insertion_index = item_count;
        let mut item_top = -scroll_top.offset_in_item;

        for ix in start_index..item_count {
            let item_height = self
                .list_state
                .bounds_for_item(ix)
                .map(|item_bounds| item_bounds.size.height)
                .unwrap_or_else(|| {
                    self.estimated_heights
                        .get(ix)
                        .copied()
                        .unwrap_or(px(DEFAULT_ESTIMATED_ITEM_HEIGHT_PX))
                });
            let item_center = item_top + item_height / 2.0;
            if relative_y < item_center {
                return Some(ix);
            }

            item_top += item_height;
            insertion_index = ix + 1;
            if item_top > bounds.size.height {
                break;
            }
        }

        Some(insertion_index)
    }
}

/// Creates a list with built-in internal drag-and-drop reordering.
#[track_caller]
pub fn sortable_list<D>(id: impl Into<ElementId>, delegate: D) -> SortableList<D>
where
    D: SortableDelegate,
{
    SortableList {
        element_id: id.into(),
        delegate: Rc::new(delegate),
        style: StyleRefinement::default(),
        sizing_behavior: ListSizingBehavior::default(),
        alignment: ListAlignment::Top,
        overdraw: px(DEFAULT_OVERDRAW_PX),
    }
}

/// Supplies item rendering and reorder behavior for a [`SortableList`].
pub trait SortableDelegate: 'static {
    /// Returns the current number of items in the list.
    fn item_count(&self) -> usize;

    /// Renders the item at `ix`.
    fn render_item(
        &self,
        ix: usize,
        is_dragging: bool,
        window: &mut Window,
        cx: &mut App,
    ) -> AnyElement;

    /// Returns the estimated height for an item that has not yet been measured.
    fn estimated_item_height(&self, _ix: usize) -> Pixels {
        px(DEFAULT_ESTIMATED_ITEM_HEIGHT_PX)
    }

    /// Returns whether an item can move from `from` to `to`.
    fn can_move(&self, _from: usize, _to: usize) -> bool {
        true
    }

    /// Applies a reorder after a successful drop.
    fn did_reorder(&self, from: usize, to: usize, window: &mut Window, cx: &mut App);
}

/// A list component that supports reordering its own items via drag and drop.
pub struct SortableList<D> {
    element_id: ElementId,
    delegate: Rc<D>,
    style: StyleRefinement,
    sizing_behavior: ListSizingBehavior,
    alignment: ListAlignment,
    overdraw: Pixels,
}

struct SortableDragPreview<D> {
    delegate: Rc<D>,
    index: usize,
    width: Option<Pixels>,
}

impl<D> SortableDragPreview<D> {
    fn new(delegate: Rc<D>, index: usize, width: Option<Pixels>) -> Self {
        Self {
            delegate,
            index,
            width,
        }
    }
}

impl<D> Render for SortableDragPreview<D>
where
    D: SortableDelegate,
{
    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        let mut preview = div()
            .rounded(px(10.0))
            .shadow_lg()
            .opacity(0.92)
            .child(self.delegate.render_item(self.index, true, window, cx));

        if let Some(width) = self.width {
            preview = preview.w(width);
        }

        preview
    }
}

impl<D> SortableList<D>
where
    D: SortableDelegate,
{
    /// Sets how the list chooses its rendered size.
    pub fn with_sizing_behavior(mut self, behavior: ListSizingBehavior) -> Self {
        self.sizing_behavior = behavior;
        self
    }

    /// Sets how items align within the available list space.
    pub fn with_alignment(mut self, alignment: ListAlignment) -> Self {
        self.alignment = alignment;
        self
    }

    /// Sets the extra measurement area above and below the viewport.
    pub fn with_overdraw(mut self, overdraw: Pixels) -> Self {
        self.overdraw = overdraw;
        self
    }

    fn estimated_heights(&self) -> Vec<Pixels> {
        (0..self.delegate.item_count())
            .map(|ix| self.delegate.estimated_item_height(ix))
            .collect()
    }

    fn build_list(
        &self,
        list_state: ListState,
        drag: DragSnapshot,
        selector_id: String,
        list_id: String,
        state: crate::Entity<SortableListElementState>,
    ) -> List {
        let item_count = self.delegate.item_count();
        let delegate = self.delegate.clone();
        crate::list(list_state, move |ix, window, cx| {
            let is_dragging = drag.source_index == Some(ix);
            let show_gap_before = drag.insertion_index == Some(ix)
                && drag_reorder_target(&drag, item_count).is_some();
            let show_gap_after = drag.insertion_index == Some(item_count)
                && ix + 1 == item_count
                && drag_reorder_target(&drag, item_count).is_some();

            let payload = SortableDragPayload {
                list_id: list_id.clone(),
                index: ix,
            };
            let delegate_for_drag = delegate.clone();
            let delegate_for_hover = delegate.clone();
            let delegate_for_drop = delegate.clone();
            let state_for_drag = state.clone();
            let state_for_hover = state.clone();
            let state_for_drop = state.clone();
            let list_id_for_hover = list_id.clone();
            let list_id_for_drop = list_id.clone();

            let mut item = div()
                .id(ElementId::named_usize(format!("{}-item", selector_id), ix))
                .flex()
                .flex_col()
                .cursor_move();

            if show_gap_before {
                item = item.child(
                    div().h(px(INSERTION_GAP_PX)).child(
                        div()
                            .h(px(INSERTION_INDICATOR_HEIGHT_PX))
                            .rounded(px(999.0))
                            .bg(crate::blue()),
                    ),
                );
            }

            item = item
                .on_drag(payload, move |payload, _, window, cx| {
                    state_for_drag.update(cx, |state, _| {
                        state.begin_drag(payload.index);
                    });
                    window.refresh();

                    let width = state_for_drag
                        .read(cx)
                        .list_state
                        .bounds_for_item(payload.index)
                        .map(|bounds| bounds.size.width);
                    let preview_delegate = delegate_for_drag.clone();
                    cx.new(move |_| {
                        SortableDragPreview::new(preview_delegate.clone(), payload.index, width)
                    })
                })
                .on_drag_move(
                    move |event: &crate::DragMoveEvent<SortableDragPayload>, window, cx| {
                        let payload = event.drag(cx).clone();
                        if payload.list_id != list_id_for_hover
                            || !event.bounds.contains(&event.event.position)
                        {
                            return;
                        }

                        let insertion_index = if event.event.position.y < event.bounds.center().y {
                            ix
                        } else {
                            ix + 1
                        };
                        state_for_hover.update(cx, |state, _| {
                            state.update_insertion_index(
                                &payload,
                                insertion_index,
                                delegate_for_hover.as_ref(),
                            );
                        });
                        window.refresh();
                    },
                )
                .on_drop(move |payload: &SortableDragPayload, window, cx| {
                    if payload.list_id != list_id_for_drop {
                        return;
                    }

                    let mut reorder = None;
                    state_for_drop.update(cx, |state, _| {
                        reorder = state.reorder_target();
                        state.clear_drag();
                    });

                    if let Some((from, to)) = reorder {
                        delegate_for_drop.did_reorder(from, to, window, cx);
                    }
                    window.refresh();
                })
                .child(
                    div()
                        .opacity(if is_dragging { 0.35 } else { 1.0 })
                        .child(delegate.render_item(ix, is_dragging, window, cx)),
                );

            if show_gap_after {
                item = item.child(
                    div().h(px(INSERTION_GAP_PX)).child(
                        div()
                            .h(px(INSERTION_INDICATOR_HEIGHT_PX))
                            .rounded(px(999.0))
                            .bg(crate::blue()),
                    ),
                );
            }

            item.into_any_element()
        })
        .with_sizing_behavior(self.sizing_behavior)
    }
}

impl<D> RenderOnce for SortableList<D>
where
    D: SortableDelegate,
{
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let estimated_heights = self.estimated_heights();
        let selector_id = self.element_id.to_string();
        let list_id = selector_id.clone();
        let state = window.use_keyed_state(self.element_id.clone(), cx, |_, _| {
            SortableListElementState::new(&estimated_heights, self.alignment, self.overdraw)
        });
        state.update(cx, |state, _| {
            state.sync(&estimated_heights, self.alignment, self.overdraw);
        });

        if !cx.has_active_drag() {
            state.update(cx, |state, _| {
                if state.drag.source_index.is_some() || state.drag.insertion_index.is_some() {
                    state.clear_drag();
                }
            });
        }

        let (list_state, drag) = {
            let snapshot = state.read(cx);
            (snapshot.list_state.clone(), snapshot.drag.clone())
        };

        let delegate_for_drag_move = self.delegate.clone();
        let delegate_for_drop = self.delegate.clone();
        let state_for_drag_move = state.clone();
        let state_for_drop = state;
        let list_id_for_can_drop = list_id.clone();
        let list_id_for_drag_move = list_id.clone();
        let list_id_for_drop = list_id.clone();
        let inner = self.build_list(
            list_state,
            drag,
            selector_id,
            list_id,
            state_for_drag_move.clone(),
        );

        let mut root = div();
        *root.style() = self.style.clone();
        root.id(self.element_id)
            .can_drop(move |value, _, _| {
                value
                    .downcast_ref::<SortableDragPayload>()
                    .is_some_and(|payload| payload.list_id == list_id_for_can_drop)
            })
            .on_drag_move(
                move |event: &crate::DragMoveEvent<SortableDragPayload>, window, cx| {
                    let payload = event.drag(cx).clone();
                    if payload.list_id != list_id_for_drag_move {
                        return;
                    }

                    state_for_drag_move.update(cx, |state, _| {
                        state.update_drag(
                            &payload,
                            event.event.position,
                            event.bounds,
                            delegate_for_drag_move.as_ref(),
                        );
                    });
                    window.refresh();
                },
            )
            .on_drop(move |payload: &SortableDragPayload, window, cx| {
                if payload.list_id != list_id_for_drop {
                    return;
                }

                let mut reorder = None;
                state_for_drop.update(cx, |state, _| {
                    reorder = state.reorder_target();
                    state.clear_drag();
                });

                if let Some((from, to)) = reorder {
                    delegate_for_drop.did_reorder(from, to, window, cx);
                }
                window.refresh();
            })
            .child(inner.w_full().h_full())
    }
}

impl<D> IntoElement for SortableList<D>
where
    D: SortableDelegate,
{
    type Element = Component<Self>;

    fn into_element(self) -> Self::Element {
        Component::new(self)
    }
}

impl<D> Styled for SortableList<D>
where
    D: SortableDelegate,
{
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

fn auto_scroll_distance(position: Point<Pixels>, bounds: Bounds<Pixels>) -> Pixels {
    let threshold = px(AUTO_SCROLL_THRESHOLD_PX);
    if position.y <= bounds.top() + threshold {
        -px(AUTO_SCROLL_STEP_PX)
    } else if position.y >= bounds.bottom() - threshold {
        px(AUTO_SCROLL_STEP_PX)
    } else {
        Pixels::ZERO
    }
}

#[cfg(test)]
mod tests {
    use super::{SortableDelegate, SortableDragPayload, SortableListElementState};
    use crate::{
        AnyElement, App, Bounds, IntoElement, ListAlignment, Pixels, Window, div, point, px,
    };

    struct AllowMoves;

    impl SortableDelegate for AllowMoves {
        fn item_count(&self) -> usize {
            4
        }

        fn render_item(
            &self,
            _ix: usize,
            _is_dragging: bool,
            _window: &mut Window,
            _cx: &mut App,
        ) -> AnyElement {
            div().into_any_element()
        }

        fn estimated_item_height(&self, _ix: usize) -> Pixels {
            px(36.0)
        }

        fn did_reorder(&self, _from: usize, _to: usize, _window: &mut Window, _cx: &mut App) {}
    }

    struct DenyMoves;

    impl SortableDelegate for DenyMoves {
        fn item_count(&self) -> usize {
            4
        }

        fn render_item(
            &self,
            _ix: usize,
            _is_dragging: bool,
            _window: &mut Window,
            _cx: &mut App,
        ) -> AnyElement {
            div().into_any_element()
        }

        fn estimated_item_height(&self, _ix: usize) -> Pixels {
            px(36.0)
        }

        fn can_move(&self, _from: usize, _to: usize) -> bool {
            false
        }

        fn did_reorder(&self, _from: usize, _to: usize, _window: &mut Window, _cx: &mut App) {}
    }

    #[test]
    fn reorder_target_translates_insertion_slot_to_final_index() {
        let estimated_heights = [px(36.0), px(36.0), px(36.0), px(36.0)];
        let mut state =
            SortableListElementState::new(&estimated_heights, ListAlignment::Top, px(200.0));

        state.begin_drag(0);
        state.drag.insertion_index = Some(3);
        assert_eq!(state.reorder_target(), Some((0, 2)));

        state.drag.insertion_index = Some(0);
        assert_eq!(state.reorder_target(), None);
    }

    #[test]
    fn update_insertion_index_respects_delegate_constraints() {
        let estimated_heights = [px(36.0), px(36.0), px(36.0), px(36.0)];
        let mut state =
            SortableListElementState::new(&estimated_heights, ListAlignment::Top, px(200.0));
        let payload = SortableDragPayload {
            list_id: "sortable".to_string(),
            index: 0,
        };

        state.begin_drag(0);
        state.update_insertion_index(&payload, 3, &AllowMoves);
        assert_eq!(state.drag.insertion_index, Some(3));

        state.update_insertion_index(&payload, 3, &DenyMoves);
        assert_eq!(state.drag.insertion_index, None);
    }

    #[test]
    fn insertion_index_for_position_uses_estimated_heights() {
        let estimated_heights = [px(36.0), px(36.0), px(36.0), px(36.0)];
        let state =
            SortableListElementState::new(&estimated_heights, ListAlignment::Top, px(200.0));
        let bounds = Bounds::from_corners(point(px(0.0), px(0.0)), point(px(220.0), px(220.0)));

        assert_eq!(
            state.insertion_index_for_position(point(px(10.0), px(10.0)), bounds),
            Some(0)
        );
        assert_eq!(
            state.insertion_index_for_position(point(px(10.0), px(80.0)), bounds),
            Some(2)
        );
        assert_eq!(
            state.insertion_index_for_position(point(px(10.0), px(150.0)), bounds),
            Some(4)
        );
    }
}