gpui_material_foundation/
overlay.rs1use std::rc::Rc;
2
3use gpui::{
4 Anchor, App, Div, ElementId, Hsla, InteractiveElement, IntoElement, RenderOnce, Role,
5 ScrollHandle, SharedString, Stateful, StatefulInteractiveElement, Styled, Window,
6 accesskit::Action, div,
7};
8use gpui_base::TestSupportExt as _;
9use gpui_material_theme::Direction;
10
11use crate::{current_material_strings, current_material_theme, scrim_color};
12
13type ScrimClick = Rc<dyn Fn(&mut Window, &mut App)>;
14
15#[derive(IntoElement)]
18pub struct Scrim {
19 id: ElementId,
20 color: Option<Hsla>,
21 opacity: f32,
22 accessibility_label: Option<SharedString>,
23 on_click: Option<ScrimClick>,
24}
25
26impl Scrim {
27 const DEFAULT_OPACITY: f32 = 0.32;
28
29 pub fn new(id: impl Into<ElementId>) -> Self {
31 Self {
32 id: id.into(),
33 color: None,
34 opacity: Self::DEFAULT_OPACITY,
35 accessibility_label: None,
36 on_click: None,
37 }
38 }
39
40 pub fn color(mut self, color: impl Into<Hsla>) -> Self {
42 self.color = Some(color.into());
43 self
44 }
45
46 pub fn opacity(mut self, opacity: f32) -> Self {
48 self.opacity = if opacity.is_nan() {
49 Self::DEFAULT_OPACITY
50 } else {
51 opacity.clamp(0., 1.)
52 };
53 self
54 }
55
56 pub fn accessibility_label(mut self, label: impl Into<SharedString>) -> Self {
59 self.accessibility_label = Some(label.into());
60 self
61 }
62
63 pub fn on_click(mut self, on_click: impl Fn(&mut Window, &mut App) + 'static) -> Self {
66 self.on_click = Some(Rc::new(on_click));
67 self
68 }
69}
70
71impl RenderOnce for Scrim {
72 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
73 let fill = self.color.map_or_else(
74 || scrim_color(¤t_material_theme(window, cx), self.opacity),
75 |color| color.opacity(self.opacity),
76 );
77 let backdrop = div()
78 .id(self.id)
79 .test_support()
80 .absolute()
81 .inset_0()
82 .bg(fill);
83 match self.on_click {
84 Some(on_click) => {
85 let on_accessibility_click = on_click.clone();
86 let label = self.accessibility_label.unwrap_or_else(|| {
87 SharedString::from(current_material_strings(window, cx).dismiss_overlay.clone())
88 });
89 backdrop
90 .occlude()
91 .role(Role::Button)
92 .aria_label(label)
93 .on_a11y_action(Action::Click, move |_, window, cx| {
96 on_accessibility_click(window, cx);
97 })
98 .on_click(move |_, window, cx| {
99 cx.stop_propagation();
100 on_click(window, cx);
101 })
102 }
103 None => backdrop,
104 }
105 }
106}
107
108pub fn logical_anchor(anchor: Anchor, direction: Direction) -> Anchor {
111 if direction == Direction::Ltr {
112 return anchor;
113 }
114 match anchor {
115 Anchor::TopLeft => Anchor::TopRight,
116 Anchor::TopRight => Anchor::TopLeft,
117 Anchor::BottomLeft => Anchor::BottomRight,
118 Anchor::BottomRight => Anchor::BottomLeft,
119 Anchor::LeftCenter => Anchor::RightCenter,
120 Anchor::RightCenter => Anchor::LeftCenter,
121 Anchor::TopCenter | Anchor::BottomCenter => anchor,
122 }
123}
124
125pub fn isolated_scroll(id: impl Into<ElementId>, handle: &ScrollHandle) -> Stateful<Div> {
128 div()
129 .id(id)
130 .min_h_0()
131 .overflow_y_scroll()
132 .occlude()
133 .track_scroll(handle)
134}
135
136#[cfg(test)]
137mod tests {
138 use super::*;
139
140 #[test]
141 fn logical_anchor_mirrors_horizontal_sides_in_rtl_only() {
142 for anchor in [
143 Anchor::TopLeft,
144 Anchor::TopCenter,
145 Anchor::TopRight,
146 Anchor::BottomLeft,
147 Anchor::BottomCenter,
148 Anchor::BottomRight,
149 Anchor::LeftCenter,
150 Anchor::RightCenter,
151 ] {
152 assert_eq!(logical_anchor(anchor, Direction::Ltr), anchor);
153 }
154 assert_eq!(
155 logical_anchor(Anchor::TopLeft, Direction::Rtl),
156 Anchor::TopRight
157 );
158 assert_eq!(
159 logical_anchor(Anchor::TopRight, Direction::Rtl),
160 Anchor::TopLeft
161 );
162 assert_eq!(
163 logical_anchor(Anchor::BottomLeft, Direction::Rtl),
164 Anchor::BottomRight
165 );
166 assert_eq!(
167 logical_anchor(Anchor::BottomRight, Direction::Rtl),
168 Anchor::BottomLeft
169 );
170 assert_eq!(
171 logical_anchor(Anchor::LeftCenter, Direction::Rtl),
172 Anchor::RightCenter
173 );
174 assert_eq!(
175 logical_anchor(Anchor::RightCenter, Direction::Rtl),
176 Anchor::LeftCenter
177 );
178 assert_eq!(
179 logical_anchor(Anchor::TopCenter, Direction::Rtl),
180 Anchor::TopCenter
181 );
182 assert_eq!(
183 logical_anchor(Anchor::BottomCenter, Direction::Rtl),
184 Anchor::BottomCenter
185 );
186 }
187}