gpui-rhai 0.1.3

A Rhai-authored declarative UI runtime for GPUI
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
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
use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet};
use std::rc::Rc;
use std::time::{Duration, Instant};

use thiserror::Error;

use crate::{ComponentInstancePath, NodeId, UiValue};

#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct GeometryBounds {
    pub x: f64,
    pub y: f64,
    pub width: f64,
    pub height: f64,
}

impl GeometryBounds {
    /// Construct validated logical geometry.
    ///
    /// # Errors
    ///
    /// Returns [`GeometryError::InvalidBounds`] for non-finite values or
    /// negative sizes.
    pub fn new(x: f64, y: f64, width: f64, height: f64) -> Result<Self, GeometryError> {
        if [x, y, width, height].into_iter().all(f64::is_finite) && width >= 0.0 && height >= 0.0 {
            Ok(Self {
                x,
                y,
                width,
                height,
            })
        } else {
            Err(GeometryError::InvalidBounds {
                x,
                y,
                width,
                height,
            })
        }
    }

    #[must_use]
    pub fn into_value(self) -> UiValue {
        UiValue::Map(BTreeMap::from([
            ("x".to_owned(), UiValue::Float(self.x)),
            ("y".to_owned(), UiValue::Float(self.y)),
            ("width".to_owned(), UiValue::Float(self.width)),
            ("height".to_owned(), UiValue::Float(self.height)),
        ]))
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ElementGeometry {
    pub layout: GeometryBounds,
    pub visual: GeometryBounds,
    pub clip: Option<GeometryBounds>,
}

#[derive(Clone, Debug, Default)]
struct GeometryState {
    committed: BTreeMap<NodeId, ElementGeometry>,
    presented: BTreeSet<NodeId>,
    readers: BTreeMap<NodeId, BTreeSet<ComponentInstancePath>>,
    dirty: BTreeSet<ComponentInstancePath>,
    layout_motion: BTreeMap<NodeId, LayoutMotionState>,
    shared_layout: BTreeMap<(String, String), (NodeId, GeometryBounds)>,
    motion_progress: BTreeMap<(NodeId, crate::MotionProperty), f64>,
    motion_trigger_targets: BTreeMap<(NodeId, crate::MotionProgressDriver), bool>,
    motion_triggers: BTreeMap<(NodeId, crate::MotionProgressDriver), TriggerMotionState>,
    canvas_transforms: BTreeMap<NodeId, CanvasMotionTransform>,
}

#[derive(Clone, Debug)]
struct LayoutMotionState {
    from: GeometryBounds,
    to: GeometryBounds,
    started: Instant,
    duration: Duration,
    easing: crate::MotionEasing,
}

#[derive(Clone, Debug)]
struct TriggerMotionState {
    from: f64,
    to: f64,
    started: Instant,
    duration: Duration,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct LayoutMotionSample {
    pub offset_x: f64,
    pub offset_y: f64,
    pub scale_x: f64,
    pub scale_y: f64,
    pub active: bool,
}

impl Default for LayoutMotionSample {
    fn default() -> Self {
        Self {
            offset_x: 0.0,
            offset_y: 0.0,
            scale_x: 1.0,
            scale_y: 1.0,
            active: false,
        }
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct TriggerMotionSample {
    pub value: f64,
    pub active: bool,
}

#[derive(Clone, Copy)]
pub(crate) struct LayoutMotionRequest<'a> {
    pub shared: Option<(&'a str, &'a str)>,
    pub duration: Duration,
    pub easing: crate::MotionEasing,
    pub preference: crate::MotionPreference,
    pub now: Instant,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct CanvasMotionTransform {
    pub rotate: f64,
    pub scale_x: f64,
    pub scale_y: f64,
    pub skew_x: f64,
    pub skew_y: f64,
    pub path_progress: Option<f64>,
}

impl Default for CanvasMotionTransform {
    fn default() -> Self {
        Self {
            rotate: 0.0,
            scale_x: 1.0,
            scale_y: 1.0,
            skew_x: 0.0,
            skew_y: 0.0,
            path_progress: None,
        }
    }
}

#[derive(Clone, Debug, Default)]
pub struct GeometryRegistry {
    inner: Rc<RefCell<Rc<GeometryState>>>,
}

impl GeometryRegistry {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.inner.borrow().committed.len()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.inner.borrow().committed.is_empty()
    }

    pub fn update(&self, node: NodeId, geometry: ElementGeometry) -> bool {
        let mut current = self.inner.borrow_mut();
        let state = Rc::make_mut(&mut current);
        state.presented.insert(node);
        if state.committed.get(&node) == Some(&geometry) {
            return false;
        }
        state.committed.insert(node, geometry);
        let readers = state.readers.get(&node).cloned().unwrap_or_default();
        state.dirty.extend(readers);
        true
    }

    pub(crate) fn sample_layout_motion(
        &self,
        node: NodeId,
        layout: GeometryBounds,
        request: LayoutMotionRequest<'_>,
    ) -> LayoutMotionSample {
        let mut current = self.inner.borrow_mut();
        let state = Rc::make_mut(&mut current);
        let shared_previous = request.shared.and_then(|(group, id)| {
            state
                .shared_layout
                .get(&(group.to_owned(), id.to_owned()))
                .copied()
                .filter(|(previous, _)| *previous != node)
                .map(|(_, bounds)| bounds)
        });
        let previous = state
            .committed
            .get(&node)
            .map(|geometry| geometry.layout)
            .or(shared_previous);
        let target_changed = state
            .layout_motion
            .get(&node)
            .is_none_or(|motion| motion.to != layout);
        if let Some(previous) = previous
            && previous != layout
            && target_changed
            && request.duration > Duration::ZERO
        {
            let from = state
                .layout_motion
                .get(&node)
                .map_or(previous, |motion| sample_layout_bounds(motion, request.now));
            state.layout_motion.insert(
                node,
                LayoutMotionState {
                    from,
                    to: layout,
                    started: request.now,
                    duration: request.duration,
                    easing: request.easing,
                },
            );
        }
        if let Some((group, id)) = request.shared {
            state
                .shared_layout
                .insert((group.to_owned(), id.to_owned()), (node, layout));
        }
        if request.preference != crate::MotionPreference::Normal {
            state.layout_motion.remove(&node);
            return LayoutMotionSample::default();
        }
        let Some(motion) = state.layout_motion.get(&node) else {
            return LayoutMotionSample::default();
        };
        let sampled = sample_layout_bounds(motion, request.now);
        let done = request.now.saturating_duration_since(motion.started) >= motion.duration;
        let result = LayoutMotionSample {
            offset_x: sampled.x - layout.x,
            offset_y: sampled.y - layout.y,
            // GPUI 0.2.2 exposes an element offset but no public arbitrary
            // subtree scale transform. Keep visual/hit geometry honest and
            // snap size while animating position.
            scale_x: 1.0,
            scale_y: 1.0,
            active: !done,
        };
        if done {
            state.layout_motion.remove(&node);
        }
        result
    }

    pub(crate) fn update_motion_progress(
        &self,
        node: NodeId,
        property: crate::MotionProperty,
        value: f64,
    ) -> bool {
        let mut current = self.inner.borrow_mut();
        let state = Rc::make_mut(&mut current);
        let key = (node, property);
        if state
            .motion_progress
            .get(&key)
            .is_some_and(|previous| (*previous - value).abs() <= f64::EPSILON)
        {
            false
        } else {
            state.motion_progress.insert(key, value);
            true
        }
    }

    pub(crate) fn motion_progress(
        &self,
        node: NodeId,
        property: crate::MotionProperty,
    ) -> Option<f64> {
        self.inner
            .borrow()
            .motion_progress
            .get(&(node, property))
            .copied()
    }

    pub(crate) fn delay_motion(&self, delay: Duration) {
        if delay.is_zero() {
            return;
        }
        let mut current = self.inner.borrow_mut();
        let state = Rc::make_mut(&mut current);
        for motion in state.layout_motion.values_mut() {
            motion.started = motion.started.checked_add(delay).unwrap_or(motion.started);
        }
        for motion in state.motion_triggers.values_mut() {
            motion.started = motion.started.checked_add(delay).unwrap_or(motion.started);
        }
    }

    pub(crate) fn set_motion_trigger(
        &self,
        node: NodeId,
        driver: crate::MotionProgressDriver,
        active: bool,
    ) {
        Rc::make_mut(&mut self.inner.borrow_mut())
            .motion_trigger_targets
            .insert((node, driver), active);
    }

    pub(crate) fn update_canvas_transform(&self, node: NodeId, transform: CanvasMotionTransform) {
        Rc::make_mut(&mut self.inner.borrow_mut())
            .canvas_transforms
            .insert(node, transform);
    }

    pub(crate) fn canvas_transform(&self, node: NodeId) -> CanvasMotionTransform {
        self.inner
            .borrow()
            .canvas_transforms
            .get(&node)
            .copied()
            .unwrap_or_default()
    }

    pub(crate) fn sample_motion_trigger(
        &self,
        node: NodeId,
        binding: &crate::MotionProgressBinding,
        preference: crate::MotionPreference,
        now: Instant,
    ) -> TriggerMotionSample {
        let mut current = self.inner.borrow_mut();
        let state = Rc::make_mut(&mut current);
        let key = (node, binding.driver);
        let target = if state
            .motion_trigger_targets
            .get(&key)
            .copied()
            .unwrap_or(false)
        {
            1.0
        } else {
            0.0
        };
        if preference != crate::MotionPreference::Normal {
            state.motion_triggers.remove(&key);
            return TriggerMotionSample {
                value: crate::motion::sample_progress_source(&binding.source, target),
                active: false,
            };
        }
        let duration = crate::motion::progress_source_duration(&binding.source);
        let trigger = state
            .motion_triggers
            .entry(key)
            .or_insert(TriggerMotionState {
                from: 0.0,
                to: 0.0,
                started: now,
                duration,
            });
        let sampled = sample_trigger_progress(trigger, now);
        if (trigger.to - target).abs() > f64::EPSILON {
            *trigger = TriggerMotionState {
                from: sampled,
                to: target,
                started: now,
                duration,
            };
        } else {
            trigger.duration = duration;
        }
        let progress = sample_trigger_progress(trigger, now);
        TriggerMotionSample {
            value: crate::motion::sample_progress_source(&binding.source, progress),
            active: (progress - trigger.to).abs() > 0.000_1,
        }
    }

    /// Read committed geometry and register one exact component dependency.
    ///
    /// # Errors
    ///
    /// Returns [`GeometryError::Unavailable`] before first prepaint or after
    /// unmount.
    pub fn read(
        &self,
        node: NodeId,
        reader: &ComponentInstancePath,
    ) -> Result<ElementGeometry, GeometryError> {
        let mut current = self.inner.borrow_mut();
        let state = Rc::make_mut(&mut current);
        let geometry = state
            .committed
            .get(&node)
            .copied()
            .ok_or(GeometryError::Unavailable(node))?;
        state
            .readers
            .entry(node)
            .or_default()
            .insert(reader.clone());
        Ok(geometry)
    }

    pub(crate) fn read_tracked(
        &self,
        node: NodeId,
        reader: &ComponentInstancePath,
    ) -> Option<ElementGeometry> {
        let mut current = self.inner.borrow_mut();
        let state = Rc::make_mut(&mut current);
        state
            .readers
            .entry(node)
            .or_default()
            .insert(reader.clone());
        state.committed.get(&node).copied()
    }

    pub(crate) fn register_readers(&self, node: NodeId, readers: BTreeSet<ComponentInstancePath>) {
        if readers.is_empty() {
            return;
        }
        let mut current = self.inner.borrow_mut();
        let state = Rc::make_mut(&mut current);
        let committed = state.committed.contains_key(&node);
        for reader in readers {
            if state
                .readers
                .entry(node)
                .or_default()
                .insert(reader.clone())
                && committed
            {
                state.dirty.insert(reader);
            }
        }
    }

    pub(crate) fn get(&self, node: NodeId) -> Option<ElementGeometry> {
        self.inner.borrow().committed.get(&node).copied()
    }

    pub(crate) fn begin_frame(&self) {
        let mut current = self.inner.borrow_mut();
        let state = Rc::make_mut(&mut current);
        state
            .shared_layout
            .retain(|_, (node, _)| state.presented.contains(node));
        state.presented.clear();
    }

    pub(crate) fn finish_frame(&self) {
        let mut current = self.inner.borrow_mut();
        let state = Rc::make_mut(&mut current);
        state
            .shared_layout
            .retain(|_, (node, _)| state.presented.contains(node));
    }

    pub(crate) fn is_presented(&self, node: NodeId) -> bool {
        self.inner.borrow().presented.contains(&node)
    }

    pub(crate) fn retain_nodes(&self, active: &BTreeSet<NodeId>) {
        let mut current = self.inner.borrow_mut();
        let state = Rc::make_mut(&mut current);
        state.committed.retain(|node, _| active.contains(node));
        state.presented.retain(|node| active.contains(node));
        state.readers.retain(|node, _| active.contains(node));
        state.layout_motion.retain(|node, _| active.contains(node));
        state
            .motion_progress
            .retain(|(node, _), _| active.contains(node));
        state
            .motion_trigger_targets
            .retain(|(node, _), _| active.contains(node));
        state
            .motion_triggers
            .retain(|(node, _), _| active.contains(node));
        state
            .canvas_transforms
            .retain(|node, _| active.contains(node));
    }

    pub(crate) fn retain_shared_layout_ids(&self, active: &BTreeSet<(String, String)>) {
        Rc::make_mut(&mut self.inner.borrow_mut())
            .shared_layout
            .retain(|identity, _| active.contains(identity));
    }

    pub(crate) fn take_dirty(&self) -> BTreeSet<ComponentInstancePath> {
        std::mem::take(&mut Rc::make_mut(&mut self.inner.borrow_mut()).dirty)
    }

    pub(crate) fn snapshot(&self) -> GeometrySnapshot {
        GeometrySnapshot(Rc::clone(&self.inner.borrow()))
    }

    pub(crate) fn restore(&self, snapshot: GeometrySnapshot) {
        *self.inner.borrow_mut() = snapshot.0;
    }
}

fn sample_layout_bounds(motion: &LayoutMotionState, now: Instant) -> GeometryBounds {
    let progress = if motion.duration.is_zero() {
        1.0
    } else {
        (now.saturating_duration_since(motion.started).as_secs_f64()
            / motion.duration.as_secs_f64())
        .clamp(0.0, 1.0)
    };
    let progress = match motion.easing {
        crate::MotionEasing::Linear => progress,
        crate::MotionEasing::EaseIn => progress * progress,
        crate::MotionEasing::EaseOut => 1.0 - (1.0 - progress).powi(2),
        crate::MotionEasing::EaseInOut if progress < 0.5 => 2.0 * progress * progress,
        crate::MotionEasing::EaseInOut => 1.0 - (-2.0 * progress + 2.0).powi(2) / 2.0,
    };
    let interpolate = |from: f64, to: f64| from + (to - from) * progress;
    GeometryBounds {
        x: interpolate(motion.from.x, motion.to.x),
        y: interpolate(motion.from.y, motion.to.y),
        width: interpolate(motion.from.width, motion.to.width),
        height: interpolate(motion.from.height, motion.to.height),
    }
}

fn sample_trigger_progress(motion: &TriggerMotionState, now: Instant) -> f64 {
    let progress = if motion.duration.is_zero() {
        1.0
    } else {
        (now.saturating_duration_since(motion.started).as_secs_f64()
            / motion.duration.as_secs_f64())
        .clamp(0.0, 1.0)
    };
    motion.from + (motion.to - motion.from) * progress
}

#[derive(Clone, Debug)]
pub(crate) struct GeometrySnapshot(Rc<GeometryState>);

#[derive(Clone, Debug, Error, PartialEq)]
pub enum GeometryError {
    #[error("invalid geometry x={x}, y={y}, width={width}, height={height}")]
    InvalidBounds {
        x: f64,
        y: f64,
        width: f64,
        height: f64,
    },
    #[error("geometry for retained node {0} is not committed")]
    Unavailable(NodeId),
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn changed_geometry_invalidates_exact_readers_and_snapshot_restores() {
        let mut tree = crate::RetainedUiTree::new();
        tree.reconcile(crate::UiNode::text("field")).unwrap();
        let node = tree.root_id().unwrap();
        let reader = ComponentInstancePath::root("Panel", "main");
        let registry = GeometryRegistry::new();
        let initial = ElementGeometry {
            layout: GeometryBounds::new(0.0, 0.0, 100.0, 20.0).unwrap(),
            visual: GeometryBounds::new(0.0, 0.0, 100.0, 20.0).unwrap(),
            clip: None,
        };
        registry.update(node, initial);
        assert!(registry.is_presented(node));
        registry.begin_frame();
        assert!(!registry.is_presented(node));
        assert!(!registry.update(node, initial));
        assert!(registry.is_presented(node));
        assert_eq!(registry.read(node, &reader).unwrap(), initial);
        let snapshot = registry.snapshot();
        registry.update(
            node,
            ElementGeometry {
                layout: GeometryBounds::new(0.0, 0.0, 120.0, 20.0).unwrap(),
                visual: GeometryBounds::new(0.0, 0.0, 120.0, 20.0).unwrap(),
                clip: None,
            },
        );
        assert!(registry.take_dirty().contains(&reader));
        registry.restore(snapshot);
        assert_eq!(registry.read(node, &reader).unwrap(), initial);
    }

    #[test]
    fn layout_and_trigger_motion_sample_from_committed_native_state() {
        let mut tree = crate::RetainedUiTree::new();
        tree.reconcile(crate::UiNode::text("target")).unwrap();
        let node = tree.root_id().unwrap();
        let registry = GeometryRegistry::new();
        let start = Instant::now();
        let old = GeometryBounds::new(0.0, 0.0, 100.0, 20.0).unwrap();
        registry.update(
            node,
            ElementGeometry {
                layout: old,
                visual: old,
                clip: None,
            },
        );
        let next = GeometryBounds::new(100.0, 40.0, 200.0, 40.0).unwrap();
        let initial = registry.sample_layout_motion(
            node,
            next,
            LayoutMotionRequest {
                shared: None,
                duration: Duration::from_millis(100),
                easing: crate::MotionEasing::Linear,
                preference: crate::MotionPreference::Normal,
                now: start,
            },
        );
        assert!((initial.offset_x + 100.0).abs() < 0.01);
        let middle = registry.sample_layout_motion(
            node,
            next,
            LayoutMotionRequest {
                shared: None,
                duration: Duration::from_millis(100),
                easing: crate::MotionEasing::Linear,
                preference: crate::MotionPreference::Normal,
                now: start + Duration::from_millis(50),
            },
        );
        assert!((middle.offset_x + 50.0).abs() < 0.01);

        let binding = crate::MotionProgressBinding::new(
            crate::MotionProgressDriver::Hover,
            crate::MotionSource::Transition(crate::MotionTransition::new(
                crate::MotionProperty::Opacity,
                0.5,
                1.0,
                100,
            )),
        )
        .unwrap();
        registry.set_motion_trigger(node, crate::MotionProgressDriver::Hover, true);
        let sample =
            registry.sample_motion_trigger(node, &binding, crate::MotionPreference::Normal, start);
        assert!(sample.active);
        let sample = registry.sample_motion_trigger(
            node,
            &binding,
            crate::MotionPreference::Normal,
            start + Duration::from_millis(100),
        );
        assert!((sample.value - 1.0).abs() < 0.01);
    }

    #[test]
    fn shared_layout_history_expires_after_its_last_presented_frame() {
        let mut tree = crate::RetainedUiTree::new();
        tree.reconcile(crate::UiNode::text("card")).unwrap();
        let node = tree.root_id().unwrap();
        let registry = GeometryRegistry::new();
        let bounds = GeometryBounds::new(0.0, 0.0, 100.0, 20.0).unwrap();
        registry.update(
            node,
            ElementGeometry {
                layout: bounds,
                visual: bounds,
                clip: None,
            },
        );
        registry.sample_layout_motion(
            node,
            bounds,
            LayoutMotionRequest {
                shared: Some(("cards", "alpha")),
                duration: Duration::from_millis(100),
                easing: crate::MotionEasing::Linear,
                preference: crate::MotionPreference::Normal,
                now: Instant::now(),
            },
        );
        assert_eq!(registry.inner.borrow().shared_layout.len(), 1);
        registry.begin_frame();
        assert_eq!(registry.inner.borrow().shared_layout.len(), 1);
        registry.begin_frame();
        assert!(registry.inner.borrow().shared_layout.is_empty());
    }
}