Skip to main content

gpui_base/motion/
reveal.rs

1use gpui::{
2    AnyElement, App, AvailableSpace, Bounds, ContentMask, Element, ElementId, GlobalElementId,
3    InspectorElementId, LayoutId, Pixels, Style, Window, px, relative, size,
4};
5
6/// A measured, clipped vertical reveal driven by normalized progress.
7pub struct MotionReveal {
8    id: ElementId,
9    progress: f32,
10    child: AnyElement,
11}
12
13#[derive(Clone, Copy, Default)]
14struct RevealState {
15    height: Option<Pixels>,
16}
17
18impl MotionReveal {
19    pub fn new(id: impl Into<ElementId>, progress: f32, child: AnyElement) -> Self {
20        Self {
21            id: id.into(),
22            progress: progress.clamp(0.0, 1.0),
23            child,
24        }
25    }
26}
27
28impl Element for MotionReveal {
29    type RequestLayoutState = ();
30    type PrepaintState = ();
31
32    fn id(&self) -> Option<ElementId> {
33        Some(self.id.clone())
34    }
35
36    fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
37        None
38    }
39
40    fn request_layout(
41        &mut self,
42        global_id: Option<&GlobalElementId>,
43        _: Option<&InspectorElementId>,
44        window: &mut Window,
45        cx: &mut App,
46    ) -> (LayoutId, Self::RequestLayoutState) {
47        let height = window.with_element_state(
48            global_id.expect("MotionReveal must have an id"),
49            |state: Option<RevealState>, _| {
50                let state = state.unwrap_or_default();
51                (state.height, state)
52            },
53        );
54        let mut style = Style::default();
55        style.size.width = relative(1.0).into();
56        match height {
57            None if self.progress > 0.0 => {}
58            None => style.size.height = px(0.0).into(),
59            Some(height) => style.size.height = (height * self.progress).into(),
60        }
61        (window.request_layout(style, None, cx), ())
62    }
63
64    fn prepaint(
65        &mut self,
66        global_id: Option<&GlobalElementId>,
67        _: Option<&InspectorElementId>,
68        bounds: Bounds<Pixels>,
69        _: &mut Self::RequestLayoutState,
70        window: &mut Window,
71        cx: &mut App,
72    ) -> Self::PrepaintState {
73        let measured = self.child.layout_as_root(
74            size(
75                AvailableSpace::Definite(bounds.size.width),
76                AvailableSpace::MinContent,
77            ),
78            window,
79            cx,
80        );
81        let changed = window.with_element_state(
82            global_id.expect("MotionReveal must have an id"),
83            |state: Option<RevealState>, _| {
84                let mut state = state.unwrap_or_default();
85                let changed = state.height != Some(measured.height);
86                state.height = Some(measured.height);
87                (changed, state)
88            },
89        );
90        if changed {
91            window.request_animation_frame();
92        }
93        window.with_content_mask(Some(ContentMask { bounds }), |window| {
94            self.child.prepaint_at(bounds.origin, window, cx);
95        });
96    }
97
98    fn paint(
99        &mut self,
100        _: Option<&GlobalElementId>,
101        _: Option<&InspectorElementId>,
102        bounds: Bounds<Pixels>,
103        _: &mut Self::RequestLayoutState,
104        _: &mut Self::PrepaintState,
105        window: &mut Window,
106        cx: &mut App,
107    ) {
108        window.with_content_mask(Some(ContentMask { bounds }), |window| {
109            self.child.paint(window, cx);
110        });
111    }
112}