1use crate::schema::{WindowHeightClass, WindowSizeClass};
10use std::cell::{Cell, RefCell};
11use std::sync::Arc;
12
13#[derive(Clone, Copy, Debug, Default, PartialEq)]
21pub struct WindowSafeArea {
22 pub top: f32,
23 pub leading: f32,
24 pub trailing: f32,
25}
26
27#[derive(Clone, Copy, Debug, PartialEq)]
34pub struct WindowSize {
35 pub width_dp: f32,
36 pub height_dp: f32,
37 pub class: WindowSizeClass,
38 pub height_class: WindowHeightClass,
39 pub safe_area: WindowSafeArea,
40}
41
42impl WindowSize {
43 pub const fn new(
44 width_dp: f32,
45 height_dp: f32,
46 class: WindowSizeClass,
47 height_class: WindowHeightClass,
48 ) -> Self {
49 Self {
50 width_dp,
51 height_dp,
52 class,
53 height_class,
54 safe_area: WindowSafeArea {
55 top: 0.0,
56 leading: 0.0,
57 trailing: 0.0,
58 },
59 }
60 }
61
62 pub const fn with_safe_area(mut self, safe_area: WindowSafeArea) -> Self {
63 self.safe_area = safe_area;
64 self
65 }
66
67 pub fn is_compact(&self) -> bool {
69 matches!(self.class, WindowSizeClass::Compact)
70 }
71
72 pub fn is_medium(&self) -> bool {
74 matches!(self.class, WindowSizeClass::Medium)
75 }
76
77 pub fn is_expanded(&self) -> bool {
79 matches!(self.class, WindowSizeClass::Expanded)
80 }
81
82 pub fn is_short(&self) -> bool {
85 matches!(self.height_class, WindowHeightClass::Compact)
86 }
87
88 pub fn is_medium_height(&self) -> bool {
90 matches!(self.height_class, WindowHeightClass::Medium)
91 }
92
93 pub fn is_tall(&self) -> bool {
95 matches!(self.height_class, WindowHeightClass::Expanded)
96 }
97}
98
99impl Default for WindowSize {
100 fn default() -> Self {
106 Self::new(
107 0.0,
108 0.0,
109 WindowSizeClass::Compact,
110 WindowHeightClass::Compact,
111 )
112 }
113}
114
115struct Subscriber {
116 id: u64,
117 notify: Arc<dyn Fn() + Send + Sync>,
118}
119
120thread_local! {
121 static NODES: RefCell<Vec<(u32, WindowSize)>> = const { RefCell::new(Vec::new()) };
126 static NODE_SUBSCRIBERS: RefCell<Vec<(u32, Subscriber)>> = const { RefCell::new(Vec::new()) };
127 static CURRENT: Cell<WindowSize> = const { Cell::new(WindowSize {
128 width_dp: 0.0,
129 height_dp: 0.0,
130 class: WindowSizeClass::Compact,
131 height_class: WindowHeightClass::Compact,
132 safe_area: WindowSafeArea { top: 0.0, leading: 0.0, trailing: 0.0 },
133 }) };
134 static SUBSCRIBERS: RefCell<Vec<Subscriber>> = const { RefCell::new(Vec::new()) };
135 static WINDOWS: RefCell<Vec<(u32, WindowSize)>> = const { RefCell::new(Vec::new()) };
139 static WINDOW_SUBSCRIBERS: RefCell<Vec<(u32, Subscriber)>> = const { RefCell::new(Vec::new()) };
140 static NEXT_SUBSCRIBER_ID: Cell<u64> = const { Cell::new(1) };
141}
142
143pub fn window_size() -> WindowSize {
150 match crate::windows::current_window_id() {
151 0 => CURRENT.with(Cell::get),
152 window => window_size_of(window),
153 }
154}
155
156pub fn window_size_of(window: u32) -> WindowSize {
159 WINDOWS.with_borrow(|windows| {
160 windows
161 .iter()
162 .find(|(id, _)| *id == window)
163 .map(|(_, size)| *size)
164 .unwrap_or_default()
165 })
166}
167
168pub(crate) fn publish_window(window: u32, size: WindowSize) -> bool {
171 let changed =
172 WINDOWS.with_borrow_mut(
173 |windows| match windows.iter_mut().find(|(id, _)| *id == window) {
174 Some(entry) => {
175 if entry.1 == size {
176 return false;
177 }
178 entry.1 = size;
179 true
180 }
181 None => {
182 windows.push((window, size));
183 true
184 }
185 },
186 );
187 if !changed {
188 return false;
189 }
190 WINDOW_SUBSCRIBERS.with_borrow(|subscribers| {
191 let mut woke = false;
192 for (id, subscriber) in subscribers {
193 if *id == window {
194 (subscriber.notify)();
195 woke = true;
196 }
197 }
198 woke
199 })
200}
201
202pub(crate) fn window_state_len() -> usize {
204 WINDOWS.with_borrow(Vec::len) + WINDOW_SUBSCRIBERS.with_borrow(Vec::len)
205}
206
207pub(crate) fn forget_window(window: u32) {
209 WINDOWS.with_borrow_mut(|windows| windows.retain(|(id, _)| *id != window));
210}
211
212pub struct WindowSubscription {
214 id: u64,
215}
216
217pub fn subscribe_window(window: u32, notify: Arc<dyn Fn() + Send + Sync>) -> WindowSubscription {
220 let id = next_subscriber_id();
221 WINDOW_SUBSCRIBERS.with_borrow_mut(|subscribers| {
222 subscribers.push((window, Subscriber { id, notify }));
223 });
224 WindowSubscription { id }
225}
226
227impl Drop for WindowSubscription {
228 fn drop(&mut self) {
229 WINDOW_SUBSCRIBERS.with_borrow_mut(|subscribers| {
230 subscribers.retain(|(_, subscriber)| subscriber.id != self.id);
231 });
232 }
233}
234
235pub(crate) fn publish(size: WindowSize) -> bool {
240 if CURRENT.with(Cell::get) == size {
241 return false;
242 }
243 CURRENT.with(|current| current.set(size));
244 SUBSCRIBERS.with_borrow(|subscribers| {
245 for subscriber in subscribers {
246 (subscriber.notify)();
247 }
248 });
249 true
250}
251
252pub fn node_size(token: u32) -> WindowSize {
254 NODES.with_borrow(|nodes| {
255 nodes
256 .iter()
257 .find(|(name, _)| *name == token)
258 .map(|(_, size)| *size)
259 .unwrap_or_default()
260 })
261}
262
263pub(crate) fn publish_node(token: u32, size: WindowSize) -> bool {
268 let changed =
269 NODES.with_borrow_mut(
270 |nodes| match nodes.iter_mut().find(|(name, _)| *name == token) {
271 Some(entry) => {
272 if entry.1 == size {
273 return false;
274 }
275 entry.1 = size;
276 true
277 }
278 None => {
279 nodes.push((token, size));
280 true
281 }
282 },
283 );
284 if !changed {
285 return false;
286 }
287 NODE_SUBSCRIBERS.with_borrow(|subscribers| {
288 let mut woke = false;
289 for (name, subscriber) in subscribers {
290 if *name == token {
291 (subscriber.notify)();
292 woke = true;
293 }
294 }
295 woke
296 })
297}
298
299#[doc(hidden)]
301pub fn reset_window_size() {
302 CURRENT.with(|current| current.set(WindowSize::default()));
303 SUBSCRIBERS.with_borrow_mut(Vec::clear);
304 NODES.with_borrow_mut(Vec::clear);
305 NODE_SUBSCRIBERS.with_borrow_mut(Vec::clear);
306 WINDOWS.with_borrow_mut(Vec::clear);
307 WINDOW_SUBSCRIBERS.with_borrow_mut(Vec::clear);
308}
309
310pub struct WindowSizeSubscription {
313 id: u64,
314}
315
316pub fn subscribe(notify: Arc<dyn Fn() + Send + Sync>) -> WindowSizeSubscription {
319 let id = next_subscriber_id();
320 SUBSCRIBERS.with_borrow_mut(|subscribers| subscribers.push(Subscriber { id, notify }));
321 WindowSizeSubscription { id }
322}
323
324fn next_subscriber_id() -> u64 {
325 NEXT_SUBSCRIBER_ID.with(|next| {
326 let id = next.get();
327 next.set(id + 1);
328 id
329 })
330}
331
332impl Drop for WindowSizeSubscription {
333 fn drop(&mut self) {
334 SUBSCRIBERS.with_borrow_mut(|subscribers| {
335 subscribers.retain(|subscriber| subscriber.id != self.id);
336 });
337 }
338}
339
340#[derive(Clone, Copy, Debug, PartialEq)]
360pub struct NodeSize {
361 token: u32,
362 size: WindowSize,
363}
364
365impl NodeSize {
366 pub fn of(token: u32) -> Self {
368 Self {
369 token,
370 size: node_size(token),
371 }
372 }
373
374 pub const fn token(&self) -> i64 {
376 self.token as i64
377 }
378
379 pub fn is_compact(&self) -> bool {
381 self.size.is_compact()
382 }
383
384 pub fn is_medium(&self) -> bool {
386 self.size.is_medium()
387 }
388
389 pub fn is_expanded(&self) -> bool {
391 self.size.is_expanded()
392 }
393
394 pub const fn measured(&self) -> WindowSize {
396 self.size
397 }
398}
399
400thread_local! {
401 static NEXT_TOKEN: Cell<u32> = const { Cell::new(1) };
402}
403
404pub fn next_node_token() -> u32 {
406 NEXT_TOKEN.with(|next| {
407 let token = next.get();
408 next.set(token + 1);
409 token
410 })
411}
412
413pub struct NodeSizeSubscription {
418 token: u32,
419 id: u64,
420}
421
422impl NodeSizeSubscription {
423 pub fn token(&self) -> u32 {
425 self.token
426 }
427}
428
429pub fn subscribe_node(token: u32, notify: Arc<dyn Fn() + Send + Sync>) -> NodeSizeSubscription {
432 let id = next_subscriber_id();
433 NODE_SUBSCRIBERS.with_borrow_mut(|subscribers| {
434 subscribers.push((token, Subscriber { id, notify }));
435 });
436 NodeSizeSubscription { token, id }
437}
438
439impl Drop for NodeSizeSubscription {
440 fn drop(&mut self) {
441 NODE_SUBSCRIBERS.with_borrow_mut(|subscribers| {
444 subscribers.retain(|(_, subscriber)| subscriber.id != self.id);
445 });
446 NODES.with_borrow_mut(|nodes| nodes.retain(|(token, _)| *token != self.token));
447 }
448}
449
450#[cfg(test)]
451mod tests {
452 use super::*;
453
454 #[test]
455 fn fr19_9_safe_area_only_change_republishes() {
456 use std::sync::atomic::{AtomicUsize, Ordering};
457 reset_window_size();
458 let calls = Arc::new(AtomicUsize::new(0));
459 let counter = Arc::clone(&calls);
460 let _subscription = subscribe(Arc::new(move || {
461 counter.fetch_add(1, Ordering::SeqCst);
462 }));
463 let size = WindowSize::default().with_safe_area(WindowSafeArea {
464 top: 28.0,
465 leading: 70.0,
466 trailing: 0.0,
467 });
468 assert!(publish(size));
469 assert_eq!(window_size(), size);
470 assert_eq!(calls.load(Ordering::SeqCst), 1);
471 assert!(!publish(size));
472 assert_eq!(calls.load(Ordering::SeqCst), 1);
473 }
474
475 #[test]
476 fn fr20_window_size_starts_compact_before_the_renderer_measures_anything() {
477 reset_window_size();
478 assert_eq!(window_size(), WindowSize::default());
479 assert!(window_size().is_compact());
480 }
481
482 #[test]
483 fn fr20_publishing_the_same_size_twice_wakes_nobody() {
484 reset_window_size();
485 let size = WindowSize::new(
486 700.0,
487 800.0,
488 WindowSizeClass::Medium,
489 WindowHeightClass::Medium,
490 );
491 assert!(publish(size));
492 assert!(!publish(size));
493 reset_window_size();
494 }
495}