gpui_material_foundation/
theme_scope.rs1use std::{cell::RefCell, ops::Deref, rc::Rc};
2
3use gpui::{
4 AnyElement, App, Bounds, Element, ElementId, GlobalElementId, InspectorElementId, IntoElement,
5 LayoutId, Pixels, Window, WindowId,
6};
7use gpui_base::Theme;
8use gpui_material_theme::{Density, Direction, MaterialTheme};
9
10use crate::root::{project_base_theme, window_override_handle, window_theme_handle};
11
12type ScopeKey = (*const App, WindowId);
13
14thread_local! {
15 static SCOPES: RefCell<Vec<(ScopeKey, Rc<MaterialTheme>)>> = const { RefCell::new(Vec::new()) };
16}
17
18fn scope_key(window: &Window, cx: &App) -> ScopeKey {
19 (cx, window.window_handle().window_id())
20}
21
22fn scoped_theme(window: &Window, cx: &App) -> Option<Rc<MaterialTheme>> {
23 let key = scope_key(window, cx);
24 SCOPES.with_borrow(|scopes| {
25 scopes
26 .iter()
27 .rev()
28 .find(|(id, _)| *id == key)
29 .map(|(_, theme)| theme.clone())
30 })
31}
32
33pub fn current_material_theme(
35 window: &Window,
36 cx: &App,
37) -> impl Deref<Target = MaterialTheme> + use<> {
38 scoped_theme(window, cx).unwrap_or_else(|| window_theme_handle(window, cx).clone())
39}
40
41pub fn current_base_theme(window: &Window, cx: &App) -> Theme {
44 project_base_theme(¤t_material_theme(window, cx), cx)
45}
46
47fn with_scope<R>(
48 theme: Option<&Rc<MaterialTheme>>,
49 window: &mut Window,
50 cx: &mut App,
51 draw: impl FnOnce(&mut Window, &mut App) -> R,
52) -> R {
53 let Some(theme) = theme else {
54 return draw(window, cx);
55 };
56 struct ScopeGuard;
57 impl Drop for ScopeGuard {
58 fn drop(&mut self) {
59 SCOPES.with_borrow_mut(|scopes| scopes.pop());
60 }
61 }
62 SCOPES.with_borrow_mut(|scopes| scopes.push((scope_key(window, cx), theme.clone())));
63 let _guard = ScopeGuard;
64 draw(window, cx)
65}
66
67pub struct ThemeScope {
70 child: AnyElement,
71 theme: Option<Rc<MaterialTheme>>,
72 direction: Option<Direction>,
73 density: Option<Density>,
74}
75
76impl ThemeScope {
77 pub fn new(child: impl IntoElement) -> Self {
79 Self {
80 child: child.into_any_element(),
81 theme: None,
82 direction: None,
83 density: None,
84 }
85 }
86
87 pub fn theme(mut self, theme: MaterialTheme) -> Self {
89 self.theme = Some(Rc::new(theme));
90 self
91 }
92
93 pub fn direction(mut self, direction: Direction) -> Self {
95 self.direction = Some(direction);
96 self
97 }
98
99 pub fn density(mut self, density: Density) -> Self {
101 self.density = Some(density);
102 self
103 }
104
105 pub fn inherit(child: impl IntoElement, window: &Window, cx: &App) -> AnyElement {
108 match scoped_theme(window, cx) {
109 Some(theme) => Self {
110 child: child.into_any_element(),
111 theme: Some(theme),
112 direction: None,
113 density: None,
114 }
115 .into_any_element(),
116 None => child.into_any_element(),
117 }
118 }
119
120 pub fn bind_action<F: Fn(&mut Window, &mut App) + 'static>(
124 action: F,
125 window: &Window,
126 cx: &App,
127 ) -> impl Fn(&mut Window, &mut App) + use<F> {
128 let theme =
129 scoped_theme(window, cx).or_else(|| window_override_handle(window, cx).cloned());
130 move |window, cx| with_scope(theme.as_ref(), window, cx, &action)
131 }
132}
133
134impl Element for ThemeScope {
135 type RequestLayoutState = ();
136 type PrepaintState = ();
137
138 fn id(&self) -> Option<ElementId> {
139 None
140 }
141
142 fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
143 None
144 }
145
146 fn request_layout(
147 &mut self,
148 _: Option<&GlobalElementId>,
149 _: Option<&InspectorElementId>,
150 window: &mut Window,
151 cx: &mut App,
152 ) -> (LayoutId, ()) {
153 if self.direction.is_some() || self.density.is_some() {
154 let mut theme = self.theme.take().unwrap_or_else(|| {
155 scoped_theme(window, cx).unwrap_or_else(|| window_theme_handle(window, cx).clone())
156 });
157 if let Some(direction) = self.direction
158 && theme.layout.direction != direction
159 {
160 Rc::make_mut(&mut theme).layout.direction = direction;
161 }
162 if let Some(density) = self.density
163 && theme.layout.density != density
164 {
165 Rc::make_mut(&mut theme).layout.density = density;
166 }
167 self.theme = Some(theme);
168 }
169 let layout = with_scope(self.theme.as_ref(), window, cx, |window, cx| {
170 self.child.request_layout(window, cx)
171 });
172 (layout, ())
173 }
174
175 fn prepaint(
176 &mut self,
177 _: Option<&GlobalElementId>,
178 _: Option<&InspectorElementId>,
179 _: Bounds<Pixels>,
180 _: &mut (),
181 window: &mut Window,
182 cx: &mut App,
183 ) {
184 with_scope(self.theme.as_ref(), window, cx, |window, cx| {
185 self.child.prepaint(window, cx);
186 });
187 }
188
189 fn paint(
190 &mut self,
191 _: Option<&GlobalElementId>,
192 _: Option<&InspectorElementId>,
193 _: Bounds<Pixels>,
194 _: &mut (),
195 _: &mut (),
196 window: &mut Window,
197 cx: &mut App,
198 ) {
199 with_scope(self.theme.as_ref(), window, cx, |window, cx| {
200 self.child.paint(window, cx);
201 });
202 }
203}