gpui-base 0.7.3

Behavior, interaction, and infrastructure foundations for GPUI applications.
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
//! The appear motion shared by every [`Plot`](super::Plot): how far its data
//! marks have drawn in since the plot was first painted.
//!
//! This is behavior only. A plot decides what appearing looks like — a line
//! revealed from the left, bars growing from zero — and a styled layer
//! projects the timing through [`PlotMotion`](crate::PlotMotion).
//!
//! The appear lives in the plot's element state, so a plot that stops being
//! painted — a row a virtual list scrolled away — forgets it and draws in again
//! when it comes back. A [`PlotAppearScope`] around the list remembers which
//! plots have finished, for as long as the scope itself is painted.
use std::{cell::RefCell, collections::HashMap, panic::Location, rc::Rc};

use gpui::{
    AnyElement, App, Bounds, Element, ElementId, GlobalElementId, InspectorElementId, IntoElement,
    LayoutId, Pixels, Window,
};

use crate::{
    Theme,
    motion::{Easing, Presence},
};

/// How far a plot's data marks have appeared this frame, handed to
/// [`Plot::appear`](super::Plot::appear).
///
/// The appear starts on the first frame a plot's id is painted and runs over
/// the active [`PlotMotion`](crate::PlotMotion)'s appear. Base's default
/// duration is zero, and reduced motion skips it, so a plot is then complete
/// from its first frame.
#[derive(Clone)]
pub struct PlotAppear {
    /// Linear time through the appear, from `0` to `1`.
    time: f32,
    easing: Easing,
}

impl PlotAppear {
    /// A finished appear: every mark is complete.
    pub fn complete() -> Self {
        Self {
            time: 1.,
            easing: Easing::Linear,
        }
    }

    /// How far the whole plot has appeared, from `0` to `1`, eased.
    pub fn progress(&self) -> f32 {
        // Charts read this per mark on every frame, long after the appear is
        // done, so a finished appear skips sampling the curve.
        if self.time >= 1. {
            return 1.;
        }
        self.easing.sample(self.time)
    }

    /// Whether the appear is still running.
    pub fn is_appearing(&self) -> bool {
        self.time < 1.
    }

    /// How far mark `index` of `count` has appeared, from `0` to `1`, eased.
    ///
    /// The marks start one after another across the first `spread` of the
    /// appear (`0..1`) and each runs for the rest of it, so the last mark
    /// still finishes with the appear however many marks there are. A `spread`
    /// of `0` moves every mark together.
    pub fn staggered(&self, index: usize, count: usize, spread: f32) -> f32 {
        if count <= 1 || self.time >= 1. {
            return self.progress();
        }
        let spread = spread.clamp(0., 0.95);
        let start = spread * index.min(count - 1) as f32 / (count - 1) as f32;
        let time = ((self.time - start) / (1. - spread)).clamp(0., 1.);
        self.easing.sample(time)
    }
}

/// The element-state key of a plot's appear, within the plot's scope.
const APPEAR: &str = "__plot-appear";

/// The plots a [`PlotAppearScope`] has seen finish appearing, by global id,
/// with the generation that finished.
type Appeared = Rc<RefCell<HashMap<GlobalElementId, u64>>>;

thread_local! {
    /// The scopes being laid out or painted, innermost last. Only non-empty
    /// while a [`PlotAppearScope`] is drawing its child.
    static SCOPES: RefCell<Vec<Appeared>> = const { RefCell::new(Vec::new()) };
}

/// Remembers which plots inside it have finished appearing, so a plot that is
/// painted again after a gap — a row a virtual list scrolled out of view and
/// back — shows its data whole instead of drawing in again.
///
/// Wrap the list, or whatever region repaints its plots on and off, in one:
///
/// ```ignore
/// PlotAppearScope::new(("transcript", conversation_id), list(state, render_row).flex_1())
/// ```
///
/// A plot is remembered by its global element id and its
/// [`Plot::appear_generation`](super::Plot::appear_generation), once its appear
/// has finished; a new generation still replays it, and one taken away
/// mid-appear draws in again from the start. The memory is the scope's own
/// element state: it lasts while the scope is painted every frame and goes with
/// it, so a view that is closed, or a scope whose id changes — name it after the
/// content, such as a conversation — draws its plots in afresh. The innermost
/// scope wins. Without one, every plot draws in each time it is painted anew.
///
/// The scope takes no part in layout: it hands on its child's layout, so a
/// child that sizes itself, such as a `list`, keeps doing so.
pub struct PlotAppearScope {
    id: ElementId,
    child: Option<AnyElement>,
}

impl PlotAppearScope {
    /// Remember the appears of the plots in `child` under `id`, unique among
    /// its siblings.
    pub fn new(id: impl Into<ElementId>, child: impl IntoElement) -> Self {
        Self {
            id: id.into(),
            child: Some(child.into_any_element()),
        }
    }

    /// Run `f` with `appeared` as the innermost scope.
    fn within<R>(appeared: &Appeared, f: impl FnOnce() -> R) -> R {
        SCOPES.with_borrow_mut(|scopes| scopes.push(appeared.clone()));
        let result = f();
        SCOPES.with_borrow_mut(|scopes| scopes.pop());
        result
    }
}

impl IntoElement for PlotAppearScope {
    type Element = Self;

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

impl Element for PlotAppearScope {
    type RequestLayoutState = (Option<AnyElement>, Appeared);
    type PrepaintState = ();

    fn id(&self) -> Option<ElementId> {
        Some(self.id.clone())
    }

    fn source_location(&self) -> Option<&'static Location<'static>> {
        None
    }

    fn request_layout(
        &mut self,
        global_id: Option<&GlobalElementId>,
        _: Option<&InspectorElementId>,
        window: &mut Window,
        cx: &mut App,
    ) -> (LayoutId, Self::RequestLayoutState) {
        let appeared: Appeared = match global_id {
            Some(global_id) => window.with_element_state(global_id, |appeared, _| {
                let appeared: Appeared = appeared.unwrap_or_default();
                (appeared.clone(), appeared)
            }),
            None => Appeared::default(),
        };
        let mut child = self.child.take();
        let layout_id = Self::within(&appeared, || match child.as_mut() {
            Some(child) => child.request_layout(window, cx),
            None => window.request_layout(Default::default(), None, cx),
        });
        (layout_id, (child, appeared))
    }

    fn prepaint(
        &mut self,
        _: Option<&GlobalElementId>,
        _: Option<&InspectorElementId>,
        _: Bounds<Pixels>,
        (child, appeared): &mut Self::RequestLayoutState,
        window: &mut Window,
        cx: &mut App,
    ) -> Self::PrepaintState {
        // A virtual list lays its rows out here, so their plots appear here.
        if let Some(child) = child {
            Self::within(appeared, || {
                child.prepaint(window, cx);
            });
        }
    }

    fn paint(
        &mut self,
        _: Option<&GlobalElementId>,
        _: Option<&InspectorElementId>,
        _: Bounds<Pixels>,
        (child, appeared): &mut Self::RequestLayoutState,
        _: &mut Self::PrepaintState,
        window: &mut Window,
        cx: &mut App,
    ) {
        if let Some(child) = child {
            Self::within(appeared, || child.paint(window, cx));
        }
    }
}

/// The innermost scope, if any, cloned out so no borrow outlives the lookup.
fn current_scope() -> Option<Appeared> {
    SCOPES.with_borrow(|scopes| scopes.last().cloned())
}

/// Sample the appear of the plot painting under the window's current element
/// id, `global_id`. A new `generation` starts it over, and one the innermost
/// [`PlotAppearScope`] saw finish is complete at once. Called by
/// [`PlotElement`](super::PlotElement) within the plot's element scope on every
/// frame, so it borrows the theme rather than cloning it and builds its key
/// without allocating.
pub(super) fn track_appear(
    global_id: &GlobalElementId,
    generation: u64,
    window: &mut Window,
    cx: &mut App,
) -> PlotAppear {
    let scope = current_scope();
    if scope
        .as_ref()
        .is_some_and(|scope| scope.borrow().get(global_id) == Some(&generation))
    {
        return PlotAppear::complete();
    }
    let Some(policy) = cx
        .try_global::<Theme>()
        .map(|theme| theme.plot.motion().appear().clone())
    else {
        return PlotAppear::complete();
    };
    let easing = policy.curve().clone();
    // Presence keeps the linear time so the marks can each ease over their
    // own slice of it; see `PlotAppear::staggered`.
    let sample = Presence::new((ElementId::Integer(generation), APPEAR), true)
        .transition(policy.easing(Easing::Linear))
        .sample(window, cx);
    if sample.progress >= 1.
        && let Some(scope) = scope
    {
        scope.borrow_mut().insert(global_id.clone(), generation);
    }
    PlotAppear {
        time: sample.progress,
        easing,
    }
}

#[cfg(test)]
mod tests {
    use std::{cell::RefCell, rc::Rc, time::Duration};

    use gpui::{
        Bounds, Context, ElementId, IntoElement, ParentElement as _, Pixels, Render, Styled as _,
        TestAppContext, WindowHandle, px, size,
    };

    use super::*;
    use crate::{
        PlotMotion, PlotTheme,
        motion::Transition,
        plot::{Plot, PlotElement},
    };

    /// A plot that records the appear progress it is handed each frame.
    struct Recorder {
        samples: Rc<RefCell<Vec<f32>>>,
        generation: Option<u64>,
    }

    impl IntoElement for Recorder {
        type Element = PlotElement<Self>;

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

    impl Plot for Recorder {
        fn paint(&mut self, _: Bounds<Pixels>, _: &mut Window, _: &mut App) {}

        fn id(&self) -> Option<ElementId> {
            Some("recorder".into())
        }

        // Appear motion rides on the id alone, without the interactive layer.
        fn interactive(&self) -> bool {
            false
        }

        fn appear(&mut self, appear: PlotAppear, _: &mut Window, _: &mut App) {
            self.samples.borrow_mut().push(appear.progress());
        }

        fn appear_generation(&self) -> Option<u64> {
            self.generation
        }
    }

    struct RecorderView {
        samples: Rc<RefCell<Vec<f32>>>,
        generation: Option<u64>,
    }

    impl Render for RecorderView {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            Recorder {
                samples: self.samples.clone(),
                generation: self.generation,
            }
        }
    }

    /// A recorder that can be taken out of the tree and put back, inside a
    /// [`PlotAppearScope`] named `scope` or none.
    struct ScopedView {
        samples: Rc<RefCell<Vec<f32>>>,
        scope: Option<usize>,
        mounted: bool,
        generation: u64,
    }

    impl Render for ScopedView {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            let plot = self.mounted.then(|| Recorder {
                samples: self.samples.clone(),
                generation: Some(self.generation),
            });
            let body = gpui::div().size_full().children(plot);
            match self.scope {
                Some(scope) => PlotAppearScope::new(("scope", scope), body).into_any_element(),
                None => body.into_any_element(),
            }
        }
    }

    fn set_theme(cx: &mut TestAppContext) {
        cx.update(|cx| {
            cx.set_global(Theme::default());
            Theme::global_mut(cx).plot = PlotTheme::new().with_motion(
                PlotMotion::default()
                    .with_appear(Transition::new(Duration::from_millis(100)).ease(|t| t)),
            );
        });
    }

    fn open_scoped(
        cx: &mut TestAppContext,
        scope: Option<usize>,
    ) -> (WindowHandle<ScopedView>, Rc<RefCell<Vec<f32>>>) {
        set_theme(cx);
        let samples = Rc::new(RefCell::new(Vec::new()));
        let window = cx.open_window(size(px(100.), px(100.)), {
            let samples = samples.clone();
            move |_, _| ScopedView {
                samples,
                scope,
                mounted: true,
                generation: 0,
            }
        });
        cx.run_until_parked();
        (window, samples)
    }

    /// Change the view, then return the progress the plot was handed on the
    /// frame that follows, if it was painted.
    fn update_scoped(
        window: WindowHandle<ScopedView>,
        cx: &mut TestAppContext,
        f: impl FnOnce(&mut ScopedView),
    ) -> Option<f32> {
        window
            .update(cx, |view, _, cx| {
                f(view);
                view.samples.borrow_mut().clear();
                cx.notify();
            })
            .unwrap();
        cx.run_until_parked();
        window
            .update(cx, |view, _, _| view.samples.borrow().last().copied())
            .unwrap()
    }

    fn finish_appear(window: WindowHandle<ScopedView>, cx: &mut TestAppContext) {
        cx.executor().advance_clock(Duration::from_millis(100));
        window
            .update(cx, |_, window, cx| window.simulate_next_frame(cx))
            .unwrap();
        cx.run_until_parked();
    }

    fn open(
        cx: &mut TestAppContext,
        generation: Option<u64>,
    ) -> (WindowHandle<RecorderView>, Rc<RefCell<Vec<f32>>>) {
        set_theme(cx);
        let samples = Rc::new(RefCell::new(Vec::new()));
        let window = cx.open_window(size(px(100.), px(100.)), {
            let samples = samples.clone();
            move |_, _| RecorderView {
                samples,
                generation,
            }
        });
        cx.run_until_parked();
        (window, samples)
    }

    fn next_frame(window: WindowHandle<RecorderView>, cx: &mut TestAppContext) -> usize {
        let frames = window
            .update(cx, |_, window, cx| window.simulate_next_frame(cx))
            .unwrap();
        cx.run_until_parked();
        frames
    }

    #[gpui::test]
    fn test_plot_appears_once_over_the_theme_duration(cx: &mut TestAppContext) {
        let (window, samples) = open(cx, Some(0));
        assert_eq!(samples.borrow().last(), Some(&0.));

        cx.executor().advance_clock(Duration::from_millis(50));
        next_frame(window, cx);
        assert_eq!(samples.borrow().last(), Some(&0.5));

        cx.executor().advance_clock(Duration::from_millis(50));
        next_frame(window, cx);
        assert_eq!(samples.borrow().last(), Some(&1.));

        // Once whole, the plot stops asking for frames and stays whole.
        assert_eq!(next_frame(window, cx), 0);
        window.update(cx, |_, window, _| window.refresh()).unwrap();
        cx.run_until_parked();
        assert_eq!(samples.borrow().last(), Some(&1.));
    }

    #[gpui::test]
    fn test_reduced_motion_skips_the_appear(cx: &mut TestAppContext) {
        cx.update(|cx| cx.set_reduce_motion(true));
        let (window, samples) = open(cx, Some(0));
        assert_eq!(samples.borrow().first(), Some(&1.));
        assert_eq!(next_frame(window, cx), 0);
    }

    /// A plot that does not opt in is whole at once and asks for no frames,
    /// even with an appear duration in the theme.
    #[gpui::test]
    fn test_plot_without_a_generation_does_not_appear(cx: &mut TestAppContext) {
        let (window, samples) = open(cx, None);
        assert_eq!(samples.borrow().first(), Some(&1.));
        assert_eq!(next_frame(window, cx), 0);
    }

    /// A new generation starts the appear over.
    #[gpui::test]
    fn test_new_generation_replays_the_appear(cx: &mut TestAppContext) {
        let (window, samples) = open(cx, Some(0));
        cx.executor().advance_clock(Duration::from_millis(100));
        next_frame(window, cx);
        assert_eq!(samples.borrow().last(), Some(&1.));

        window
            .update(cx, |view, _, cx| {
                view.generation = Some(1);
                cx.notify();
            })
            .unwrap();
        cx.run_until_parked();
        assert_eq!(samples.borrow().last(), Some(&0.));
    }

    /// Inside a scope, a plot painted again after a gap is whole at once and
    /// asks for no frames.
    #[gpui::test]
    fn test_scope_keeps_a_finished_appear_across_a_remount(cx: &mut TestAppContext) {
        let (window, samples) = open_scoped(cx, Some(0));
        assert_eq!(samples.borrow().last(), Some(&0.));
        finish_appear(window, cx);
        assert_eq!(samples.borrow().last(), Some(&1.));

        assert_eq!(update_scoped(window, cx, |view| view.mounted = false), None);
        assert_eq!(
            update_scoped(window, cx, |view| view.mounted = true),
            Some(1.)
        );
        let frames = window
            .update(cx, |_, window, cx| window.simulate_next_frame(cx))
            .unwrap();
        assert_eq!(frames, 0);

        // A new generation still replays.
        assert_eq!(
            update_scoped(window, cx, |view| view.generation = 1),
            Some(0.)
        );
    }

    /// A plot taken away before its appear finished draws in again.
    #[gpui::test]
    fn test_scope_replays_an_unfinished_appear(cx: &mut TestAppContext) {
        let (window, _) = open_scoped(cx, Some(0));
        assert_eq!(update_scoped(window, cx, |view| view.mounted = false), None);
        assert_eq!(
            update_scoped(window, cx, |view| view.mounted = true),
            Some(0.)
        );
    }

    /// The memory goes with the scope: a scope under a new id, or one that
    /// stops being painted, draws its plots in afresh.
    #[gpui::test]
    fn test_scope_forgets_when_it_goes(cx: &mut TestAppContext) {
        let (window, _) = open_scoped(cx, Some(0));
        finish_appear(window, cx);
        assert_eq!(
            update_scoped(window, cx, |view| view.scope = Some(1)),
            Some(0.)
        );

        finish_appear(window, cx);
        assert_eq!(
            update_scoped(window, cx, |view| view.scope = None),
            Some(0.)
        );
        finish_appear(window, cx);
        assert_eq!(
            update_scoped(window, cx, |view| view.scope = Some(1)),
            Some(0.)
        );
    }

    /// Without a scope, a plot painted again after a gap draws in again.
    #[gpui::test]
    fn test_without_a_scope_a_remount_replays(cx: &mut TestAppContext) {
        let (window, _) = open_scoped(cx, None);
        finish_appear(window, cx);
        assert_eq!(update_scoped(window, cx, |view| view.mounted = false), None);
        assert_eq!(
            update_scoped(window, cx, |view| view.mounted = true),
            Some(0.)
        );
    }

    fn at(time: f32) -> PlotAppear {
        PlotAppear {
            time,
            easing: Easing::Linear,
        }
    }

    #[test]
    fn test_complete_appear() {
        let appear = PlotAppear::complete();
        assert!(!appear.is_appearing());
        assert_eq!(appear.progress(), 1.);
        assert_eq!(appear.staggered(3, 10, 0.5), 1.);
    }

    #[test]
    fn test_staggered_marks_share_the_appear() {
        // The first mark starts at once, the last once the spread has passed.
        assert_eq!(at(0.).staggered(0, 5, 0.5), 0.);
        assert_eq!(at(0.25).staggered(0, 5, 0.5), 0.5);
        assert_eq!(at(0.5).staggered(4, 5, 0.5), 0.);
        assert_eq!(at(0.75).staggered(4, 5, 0.5), 0.5);
        // Every mark finishes with the appear.
        for index in 0..5 {
            assert_eq!(at(1.).staggered(index, 5, 0.5), 1.);
        }
    }

    #[test]
    fn test_staggered_without_spread_moves_together() {
        assert_eq!(at(0.4).staggered(0, 3, 0.), 0.4);
        assert_eq!(at(0.4).staggered(2, 3, 0.), 0.4);
        // A lone mark ignores the spread.
        assert_eq!(at(0.4).staggered(0, 1, 0.5), 0.4);
    }
}