kael 0.1.2

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
use crate::{
    AnchoredFitMode, AnchoredPositionMode, AnyElement, AnyView, Context, Corner, DismissEvent,
    Edges, Entity, Hsla, InteractiveElement, IntoElement, ManagedView, ParentElement, Pixels,
    Point, Render, Styled, Subscription, WeakEntity, Window, anchored, deferred, div, hsla,
};

/// A stable identifier for a layer managed by [`LayerStack`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct LayerId(usize);

/// Controls how a layer is placed inside a [`LayerStack`].
#[derive(Clone, Copy, Default, PartialEq)]
pub enum LayerPlacement {
    /// Center the layer content within the window.
    #[default]
    Centered,
    /// Stretch the layer content to fill the window.
    Fullscreen,
    /// Position the layer using GPUI's anchored placement rules.
    Anchored(LayerAnchor),
}

/// Anchored placement configuration for a layer.
#[derive(Clone, Copy, PartialEq)]
pub struct LayerAnchor {
    anchor_corner: Corner,
    anchor_position: Option<Point<Pixels>>,
    position_mode: AnchoredPositionMode,
    fit_mode: AnchoredFitMode,
    offset: Point<Pixels>,
}

impl Default for LayerAnchor {
    fn default() -> Self {
        Self {
            anchor_corner: Corner::TopLeft,
            anchor_position: None,
            position_mode: AnchoredPositionMode::Window,
            fit_mode: AnchoredFitMode::SwitchAnchor,
            offset: Point::default(),
        }
    }
}

impl LayerAnchor {
    /// Creates an anchored placement using GPUI's default anchored behavior.
    pub fn new() -> Self {
        Self::default()
    }

    /// Anchors the layer to the given position.
    pub fn at(position: Point<Pixels>) -> Self {
        Self {
            anchor_position: Some(position),
            ..Self::default()
        }
    }

    /// Sets which corner of the anchored layer should be attached to the anchor position.
    pub fn anchor(mut self, anchor: Corner) -> Self {
        self.anchor_corner = anchor;
        self
    }

    /// Sets how the anchor position should be interpreted.
    pub fn position_mode(mut self, mode: AnchoredPositionMode) -> Self {
        self.position_mode = mode;
        self
    }

    /// Offsets the final anchored layer position.
    pub fn offset(mut self, offset: Point<Pixels>) -> Self {
        self.offset = offset;
        self
    }

    /// Snaps the anchored layer to the window edge when it would overflow.
    pub fn snap_to_window(mut self) -> Self {
        self.fit_mode = AnchoredFitMode::SnapToWindow;
        self
    }

    /// Snaps the anchored layer to the window edge while preserving the given margins.
    pub fn snap_to_window_with_margin(mut self, edges: impl Into<Edges<Pixels>>) -> Self {
        self.fit_mode = AnchoredFitMode::SnapToWindowWithMargin(edges.into());
        self
    }
}

/// Options that control how a layer behaves inside a [`LayerStack`].
#[derive(Clone, Copy, PartialEq)]
pub struct LayerOptions {
    placement: LayerPlacement,
    backdrop: Option<Hsla>,
    priority: usize,
    dismiss_on_click_outside: bool,
    dismiss_on_escape: bool,
}

impl Default for LayerOptions {
    fn default() -> Self {
        Self {
            placement: LayerPlacement::Centered,
            backdrop: None,
            priority: 0,
            dismiss_on_click_outside: false,
            dismiss_on_escape: false,
        }
    }
}

impl LayerOptions {
    /// Creates centered layer options appropriate for modal content.
    pub fn modal() -> Self {
        Self::default()
            .backdrop(hsla(0.0, 0.0, 0.0, 0.32))
            .dismiss_on_click_outside()
            .dismiss_on_escape()
            .priority(100)
    }

    /// Sets the placement used for the layer content.
    pub fn placement(mut self, placement: LayerPlacement) -> Self {
        self.placement = placement;
        self
    }

    /// Places the layer content using anchored positioning.
    pub fn anchored(mut self, anchor: LayerAnchor) -> Self {
        self.placement = LayerPlacement::Anchored(anchor);
        self
    }

    /// Places the layer content in the center of the window.
    pub fn centered(mut self) -> Self {
        self.placement = LayerPlacement::Centered;
        self
    }

    /// Places the layer content so it fills the window.
    pub fn fullscreen(mut self) -> Self {
        self.placement = LayerPlacement::Fullscreen;
        self
    }

    /// Paints a backdrop behind the layer content.
    pub fn backdrop(mut self, color: Hsla) -> Self {
        self.backdrop = Some(color);
        self
    }

    /// Sets the deferred draw priority for the layer.
    pub fn priority(mut self, priority: usize) -> Self {
        self.priority = priority;
        self
    }

    /// Dismisses the layer when the backdrop area is clicked.
    pub fn dismiss_on_click_outside(mut self) -> Self {
        self.dismiss_on_click_outside = true;
        self
    }

    /// Dismisses the layer when the escape key is pressed while it is focused.
    pub fn dismiss_on_escape(mut self) -> Self {
        self.dismiss_on_escape = true;
        self
    }

    fn needs_backdrop_capture(&self) -> bool {
        self.backdrop.is_some() || self.dismiss_on_click_outside
    }
}

struct LayerEntry {
    id: LayerId,
    view: AnyView,
    options: LayerOptions,
    _subscription: Subscription,
}

/// A stack of in-window layers such as modals and popovers.
pub struct LayerStack {
    next_layer_id: usize,
    layers: Vec<LayerEntry>,
}

impl LayerStack {
    /// Creates an empty layer stack.
    pub fn new() -> Self {
        Self {
            next_layer_id: 0,
            layers: Vec::new(),
        }
    }

    /// Returns the number of active layers.
    pub fn len(&self) -> usize {
        self.layers.len()
    }

    /// Returns whether the stack currently has no layers.
    pub fn is_empty(&self) -> bool {
        self.layers.is_empty()
    }

    /// Pushes a managed view onto the stack and returns its layer identifier.
    pub fn push<V: ManagedView>(
        &mut self,
        view: Entity<V>,
        options: LayerOptions,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> LayerId {
        let id = LayerId(self.next_layer_id);
        self.next_layer_id += 1;

        let dismiss_subscription = cx.subscribe(&view, move |stack, _, _: &DismissEvent, cx| {
            stack.dismiss(id, cx);
        });
        let release_subscription = cx.observe_release(&view, move |stack, _, cx| {
            stack.dismiss(id, cx);
        });

        cx.focus_view(&view, window);
        self.layers.push(LayerEntry {
            id,
            view: view.into(),
            options,
            _subscription: Subscription::join(dismiss_subscription, release_subscription),
        });
        cx.notify();
        id
    }

    /// Dismisses the layer with the given identifier if it is present.
    pub fn dismiss(&mut self, layer_id: LayerId, cx: &mut Context<Self>) -> bool {
        let original_len = self.layers.len();
        self.layers.retain(|entry| entry.id != layer_id);
        let dismissed = self.layers.len() != original_len;
        if dismissed {
            cx.notify();
        }
        dismissed
    }

    /// Removes all layers from the stack.
    pub fn clear(&mut self, cx: &mut Context<Self>) {
        if self.layers.is_empty() {
            return;
        }

        self.layers.clear();
        cx.notify();
    }
}

impl Render for LayerStack {
    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        let mut stack_root = div().absolute().top_0().left_0().w_full().h_full();
        let stack = cx.weak_entity();

        for layer in &self.layers {
            stack_root = stack_root.child(render_layer(layer, stack.clone()));
        }

        stack_root.into_any_element()
    }
}

fn render_layer(layer: &LayerEntry, stack: WeakEntity<LayerStack>) -> AnyElement {
    let mut overlay = div().absolute().top_0().left_0().w_full().h_full();

    if layer.options.needs_backdrop_capture() {
        let mut backdrop = div()
            .absolute()
            .top_0()
            .left_0()
            .w_full()
            .h_full()
            .occlude();

        if let Some(color) = layer.options.backdrop {
            backdrop = backdrop.bg(color);
        }

        overlay = overlay.child(backdrop);
    }

    overlay = overlay.child(render_layer_content(layer, stack));

    deferred(overlay)
        .priority(layer.options.priority)
        .into_any_element()
}

fn render_layer_content(layer: &LayerEntry, stack: WeakEntity<LayerStack>) -> AnyElement {
    let content = if layer.options.dismiss_on_click_outside || layer.options.dismiss_on_escape {
        let layer_id = layer.id;
        let mut content = div().child(layer.view.clone());

        if layer.options.dismiss_on_click_outside
            && !matches!(layer.options.placement, LayerPlacement::Fullscreen)
        {
            let dismiss_stack = stack.clone();
            content = content.on_mouse_down_out(move |_, _, cx| {
                let _ = dismiss_stack.update(cx, |stack, cx| {
                    stack.dismiss(layer_id, cx);
                });
            });
        }

        if layer.options.dismiss_on_escape {
            content = content.capture_key_down(move |event, _, cx| {
                if event.keystroke.key == "escape" {
                    let _ = stack.update(cx, |stack, cx| {
                        stack.dismiss(layer_id, cx);
                    });
                }
            });
        }

        content.into_any_element()
    } else {
        layer.view.clone().into_any_element()
    };

    match layer.options.placement {
        LayerPlacement::Centered => div()
            .absolute()
            .top_0()
            .left_0()
            .w_full()
            .h_full()
            .flex()
            .items_center()
            .justify_center()
            .child(content)
            .into_any_element(),
        LayerPlacement::Fullscreen => div()
            .absolute()
            .top_0()
            .left_0()
            .w_full()
            .h_full()
            .child(content)
            .into_any_element(),
        LayerPlacement::Anchored(anchor) => {
            let mut anchored_layer = anchored()
                .anchor(anchor.anchor_corner)
                .position_mode(anchor.position_mode)
                .offset(anchor.offset);

            if let Some(position) = anchor.anchor_position {
                anchored_layer = anchored_layer.position(position);
            }

            anchored_layer = match anchor.fit_mode {
                AnchoredFitMode::SwitchAnchor => anchored_layer,
                AnchoredFitMode::SnapToWindow => anchored_layer.snap_to_window(),
                AnchoredFitMode::SnapToWindowWithMargin(edges) => {
                    anchored_layer.snap_to_window_with_margin(edges)
                }
            };

            div()
                .absolute()
                .top_0()
                .left_0()
                .w_full()
                .h_full()
                .child(anchored_layer.child(content))
                .into_any_element()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        AppContext, Context, EventEmitter, FocusHandle, Focusable, InteractiveElement, IntoElement,
        Modifiers, ParentElement, StatefulInteractiveElement, TestAppContext, Window, div, point,
        px, rgb,
    };
    use std::{cell::Cell, rc::Rc};

    struct TestLayerView {
        focus: FocusHandle,
        clicks: Rc<Cell<usize>>,
    }

    impl EventEmitter<DismissEvent> for TestLayerView {}

    impl Focusable for TestLayerView {
        fn focus_handle(&self, _: &crate::App) -> FocusHandle {
            self.focus.clone()
        }
    }

    impl Render for TestLayerView {
        fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
            let clicks = self.clicks.clone();
            div()
                .track_focus(&self.focus)
                .id("panel")
                .debug_selector(|| "panel".to_string())
                .w(px(120.))
                .h(px(72.))
                .bg(rgb(0xffffff))
                .border_1()
                .border_color(rgb(0xd0d0d0))
                .on_click(move |_, _, _| {
                    clicks.set(clicks.get() + 1);
                })
        }
    }

    struct RootView {
        layer_stack: Entity<LayerStack>,
        background_clicks: Rc<Cell<usize>>,
    }

    impl Render for RootView {
        fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
            let background_clicks = self.background_clicks.clone();
            div()
                .relative()
                .size_full()
                .child(
                    div()
                        .id("background")
                        .debug_selector(|| "background".to_string())
                        .size_full()
                        .bg(rgb(0xf2f2f2))
                        .on_click(move |_, _, _| {
                            background_clicks.set(background_clicks.get() + 1);
                        }),
                )
                .child(self.layer_stack.clone())
        }
    }

    #[kael::test]
    fn layer_stack_dismisses_modal_on_backdrop_click(cx: &mut TestAppContext) {
        let background_clicks = Rc::new(Cell::new(0));
        let panel_clicks = Rc::new(Cell::new(0));

        let (root, cx) = cx.add_window_view({
            let background_clicks = background_clicks.clone();
            let panel_clicks = panel_clicks.clone();
            move |window, cx| {
                let layer_stack = cx.new(|_| LayerStack::new());
                let layer = cx.new(|cx| TestLayerView {
                    focus: cx.focus_handle(),
                    clicks: panel_clicks.clone(),
                });

                layer_stack.update(cx, |stack, cx| {
                    stack.push(layer, LayerOptions::modal(), window, cx);
                });

                RootView {
                    layer_stack,
                    background_clicks: background_clicks.clone(),
                }
            }
        });

        let panel_bounds = cx.debug_bounds("panel").unwrap();
        cx.simulate_click(panel_bounds.center(), Modifiers::default());

        let layer_stack = cx.read_entity(&root, |root, _| root.layer_stack.clone());
        assert_eq!(panel_clicks.get(), 1);
        assert_eq!(background_clicks.get(), 0);
        assert_eq!(cx.read_entity(&layer_stack, |stack, _| stack.len()), 1);

        cx.simulate_click(point(px(8.), px(8.)), Modifiers::default());

        assert_eq!(background_clicks.get(), 0);
        assert!(cx.read_entity(&layer_stack, |stack, _| stack.is_empty()));
    }

    #[kael::test]
    fn layer_stack_dismisses_when_view_emits_dismiss_event(cx: &mut TestAppContext) {
        let panel_clicks = Rc::new(Cell::new(0));

        let (root, cx) = cx.add_window_view({
            let panel_clicks = panel_clicks.clone();
            move |window, cx| {
                let layer_stack = cx.new(|_| LayerStack::new());
                let layer = cx.new(|cx| TestLayerView {
                    focus: cx.focus_handle(),
                    clicks: panel_clicks.clone(),
                });

                layer_stack.update(cx, |stack, cx| {
                    stack.push(layer.clone(), LayerOptions::modal(), window, cx);
                });

                RootView {
                    layer_stack,
                    background_clicks: Rc::new(Cell::new(0)),
                }
            }
        });

        let layer_stack = cx.read_entity(&root, |root, _| root.layer_stack.clone());
        let layer = cx.read_entity(&layer_stack, |stack, _| {
            stack.layers[0]
                .view
                .clone()
                .downcast::<TestLayerView>()
                .unwrap()
        });

        layer.update(cx, |_, cx: &mut Context<TestLayerView>| {
            cx.emit(DismissEvent);
        });

        assert!(cx.read_entity(&layer_stack, |stack, _| stack.is_empty()));
    }

    #[kael::test]
    fn layer_stack_dismisses_modal_on_escape(cx: &mut TestAppContext) {
        let panel_clicks = Rc::new(Cell::new(0));

        let (root, cx) = cx.add_window_view({
            let panel_clicks = panel_clicks.clone();
            move |window, cx| {
                let layer_stack = cx.new(|_| LayerStack::new());
                let layer = cx.new(|cx| TestLayerView {
                    focus: cx.focus_handle(),
                    clicks: panel_clicks.clone(),
                });

                layer_stack.update(cx, |stack, cx| {
                    stack.push(layer, LayerOptions::modal(), window, cx);
                });

                RootView {
                    layer_stack,
                    background_clicks: Rc::new(Cell::new(0)),
                }
            }
        });

        let layer_stack = cx.read_entity(&root, |root, _| root.layer_stack.clone());
        assert_eq!(cx.read_entity(&layer_stack, |stack, _| stack.len()), 1);

        cx.simulate_keystrokes("escape");

        assert!(cx.read_entity(&layer_stack, |stack, _| stack.is_empty()));
    }

    #[kael::test]
    fn layer_stack_positions_anchored_layers_in_window_space(cx: &mut TestAppContext) {
        let panel_clicks = Rc::new(Cell::new(0));
        let anchor_position = point(px(180.), px(96.));
        let offset = point(px(12.), px(8.));

        let (_root, cx) = cx.add_window_view({
            let panel_clicks = panel_clicks.clone();
            move |window, cx| {
                let layer_stack = cx.new(|_| LayerStack::new());
                let layer = cx.new(|cx| TestLayerView {
                    focus: cx.focus_handle(),
                    clicks: panel_clicks.clone(),
                });

                layer_stack.update(cx, |stack, cx| {
                    stack.push(
                        layer,
                        LayerOptions::default()
                            .anchored(LayerAnchor::at(anchor_position).offset(offset)),
                        window,
                        cx,
                    );
                });

                RootView {
                    layer_stack,
                    background_clicks: Rc::new(Cell::new(0)),
                }
            }
        });

        let panel_bounds = cx.debug_bounds("panel").unwrap();
        assert_eq!(panel_bounds.origin, anchor_position + offset);
    }
}