aureole_core/
responsive.rs1use std::cell::{Cell, RefCell};
8use std::rc::{Rc, Weak};
9
10use reactive::{Memo, Scope, Signal, memo, signal};
11
12use crate::geometry::Rect;
13use crate::host::{WindowId, current_window_id};
14
15#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
17pub enum SizeClass {
18 Compact,
20 #[default]
22 Regular,
23 Wide,
25}
26
27impl SizeClass {
28 pub fn from_width(width: f32) -> Self {
31 let [regular, wide] = thresholds().get();
32 if width >= wide {
33 Self::Wide
34 } else if width >= regular {
35 Self::Regular
36 } else {
37 Self::Compact
38 }
39 }
40
41 pub fn is_compact(self) -> bool {
43 self == Self::Compact
44 }
45
46 pub fn is_wide(self) -> bool {
48 self == Self::Wide
49 }
50
51 pub fn pick<T>(self, compact: T, regular: T, wide: T) -> T {
53 match self {
54 Self::Compact => compact,
55 Self::Regular => regular,
56 Self::Wide => wide,
57 }
58 }
59}
60
61thread_local! {
62 static THRESHOLDS: Cell<Option<Signal<[f32; 2]>>> = const { Cell::new(None) };
64 static REGISTRY: RefCell<Vec<(Option<WindowId>, Weak<Inner>)>> = const { RefCell::new(Vec::new()) };
66}
67
68fn thresholds() -> Signal<[f32; 2]> {
69 THRESHOLDS.with(|cell| {
70 cell.get().unwrap_or_else(|| {
71 let s = reactive::detached(|| signal([640.0, 1024.0]));
72 cell.set(Some(s));
73 s
74 })
75 })
76}
77
78pub fn set_size_class_widths(regular: f32, wide: f32) {
81 thresholds().set_if_changed([regular, wide.max(regular)]);
82}
83
84pub fn window_size_class() -> SizeClass {
98 metrics().map_or_else(SizeClass::default, |m| m.class.get())
99}
100
101pub fn window_size() -> [f32; 2] {
103 metrics().map_or([0.0; 2], |m| m.size.get())
104}
105
106pub fn is_window_at_least(width: f32) -> bool {
114 let Some(m) = metrics() else { return false };
115 let bits = width.to_bits();
116 let existing = m.breakpoints.borrow().iter().find(|(w, _)| *w == bits).map(|(_, memo)| *memo);
117 let memo = existing.unwrap_or_else(|| {
118 let size = m.size;
119 let memo = m.scope.run(|| memo(move || size.get()[0] >= width));
121 m.breakpoints.borrow_mut().push((bits, memo));
122 memo
123 });
124 memo.get()
125}
126
127pub fn responsive<T: Clone + 'static>(compact: T, regular: T, wide: T) -> impl Fn() -> T + 'static {
130 move || window_size_class().pick(compact.clone(), regular.clone(), wide.clone())
131}
132
133#[derive(Clone, Copy, Debug, Default, PartialEq)]
135pub struct TitleBarInfo {
136 pub height: f32,
138 pub controls: Option<Rect>,
140 pub inset_left: f32,
142 pub inset_right: f32,
144}
145
146pub fn title_bar_info() -> TitleBarInfo {
160 metrics().map_or_else(TitleBarInfo::default, |m| m.title_bar.get())
161}
162
163pub(crate) struct Inner {
165 size: Signal<[f32; 2]>,
166 class: Memo<SizeClass>,
167 title_bar: Signal<TitleBarInfo>,
168 breakpoints: RefCell<Vec<(u32, Memo<bool>)>>,
169 scope: Scope,
170 alive: Cell<bool>,
172}
173
174#[derive(Clone)]
176pub(crate) struct WindowMetrics(Rc<Inner>);
177
178impl WindowMetrics {
179 pub fn attach(size: [f32; 2], window: Option<WindowId>) -> Self {
181 let scope = Scope::current().unwrap_or_default();
182 let size = signal(size);
183 let class = memo(move || SizeClass::from_width(size.get()[0]));
184 let title_bar = signal(TitleBarInfo::default());
185 let inner = Rc::new(Inner { size, class, title_bar, breakpoints: RefCell::new(Vec::new()), scope, alive: Cell::new(true) });
186 let metrics = Self(inner);
187 reactive::provide_context(metrics.clone());
188 let weak = Rc::downgrade(&metrics.0);
189 REGISTRY.with(|r| {
190 let mut r = r.borrow_mut();
191 r.retain(|(_, w)| w.upgrade().is_some_and(|i| i.alive.get()));
192 r.push((window, weak.clone()));
193 });
194 reactive::on_cleanup(move || {
195 if let Some(inner) = weak.upgrade() {
196 inner.alive.set(false);
197 }
198 });
199 metrics
200 }
201
202 pub fn set_size(&self, size: [f32; 2]) {
204 if self.0.alive.get() {
205 self.0.size.set_if_changed(size);
206 }
207 }
208
209 pub fn set_title_bar(&self, info: TitleBarInfo) {
211 if self.0.alive.get() {
212 self.0.title_bar.set_if_changed(info);
213 }
214 }
215}
216
217fn metrics() -> Option<Rc<Inner>> {
219 if let Some(m) = reactive::context::<WindowMetrics>() {
220 return m.0.alive.get().then_some(m.0);
221 }
222 let current = current_window_id();
223 REGISTRY.with(|r| {
224 let r = r.borrow();
225 let live = |(_, w): &(Option<WindowId>, Weak<Inner>)| w.upgrade().filter(|i| i.alive.get());
226 r.iter().rev().find(|(h, _)| current.is_some() && *h == current).and_then(live).or_else(|| r.iter().rev().find_map(live))
227 })
228}