gpui_base/motion/
reveal.rs1use gpui::{
2 AnyElement, App, AvailableSpace, Bounds, ContentMask, Element, ElementId, GlobalElementId,
3 InspectorElementId, LayoutId, Pixels, Style, Window, px, relative, size,
4};
5
6pub 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
28#[cfg(not(feature = "gpui-fast"))]
29impl gpui::IntoElement for MotionReveal {
30 type Element = Self;
31
32 fn into_element(self) -> Self::Element {
33 self
34 }
35}
36
37impl Element for MotionReveal {
38 type RequestLayoutState = ();
39 type PrepaintState = ();
40
41 fn id(&self) -> Option<ElementId> {
42 Some(self.id.clone())
43 }
44
45 fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
46 None
47 }
48
49 fn request_layout(
50 &mut self,
51 global_id: Option<&GlobalElementId>,
52 _: Option<&InspectorElementId>,
53 window: &mut Window,
54 cx: &mut App,
55 ) -> (LayoutId, Self::RequestLayoutState) {
56 let height = window.with_element_state(
57 global_id.expect("MotionReveal must have an id"),
58 |state: Option<RevealState>, _| {
59 let state = state.unwrap_or_default();
60 (state.height, state)
61 },
62 );
63 let mut style = Style::default();
64 style.size.width = relative(1.0).into();
65 match height {
66 None if self.progress > 0.0 => {}
67 None => style.size.height = px(0.0).into(),
68 Some(height) => style.size.height = (height * self.progress).into(),
69 }
70 (window.request_layout(style, None, cx), ())
71 }
72
73 fn prepaint(
74 &mut self,
75 global_id: Option<&GlobalElementId>,
76 _: Option<&InspectorElementId>,
77 bounds: Bounds<Pixels>,
78 _: &mut Self::RequestLayoutState,
79 window: &mut Window,
80 cx: &mut App,
81 ) -> Self::PrepaintState {
82 let measured = self.child.layout_as_root(
83 size(
84 AvailableSpace::Definite(bounds.size.width),
85 AvailableSpace::MinContent,
86 ),
87 window,
88 cx,
89 );
90 let changed = window.with_element_state(
91 global_id.expect("MotionReveal must have an id"),
92 |state: Option<RevealState>, _| {
93 let mut state = state.unwrap_or_default();
94 let changed = state.height != Some(measured.height);
95 state.height = Some(measured.height);
96 (changed, state)
97 },
98 );
99 if changed {
100 window.request_animation_frame();
101 }
102 window.with_content_mask(Some(ContentMask { bounds }), |window| {
103 self.child.prepaint_at(bounds.origin, window, cx);
104 });
105 }
106
107 fn paint(
108 &mut self,
109 _: Option<&GlobalElementId>,
110 _: Option<&InspectorElementId>,
111 bounds: Bounds<Pixels>,
112 _: &mut Self::RequestLayoutState,
113 _: &mut Self::PrepaintState,
114 window: &mut Window,
115 cx: &mut App,
116 ) {
117 window.with_content_mask(Some(ContentMask { bounds }), |window| {
118 self.child.paint(window, cx);
119 });
120 }
121}