use crate::schema::{WindowHeightClass, WindowSizeClass};
use std::cell::{Cell, RefCell};
use std::sync::Arc;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct WindowSafeArea {
pub top: f32,
pub leading: f32,
pub trailing: f32,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct WindowSize {
pub width_dp: f32,
pub height_dp: f32,
pub class: WindowSizeClass,
pub height_class: WindowHeightClass,
pub safe_area: WindowSafeArea,
}
impl WindowSize {
pub const fn new(
width_dp: f32,
height_dp: f32,
class: WindowSizeClass,
height_class: WindowHeightClass,
) -> Self {
Self {
width_dp,
height_dp,
class,
height_class,
safe_area: WindowSafeArea {
top: 0.0,
leading: 0.0,
trailing: 0.0,
},
}
}
pub const fn with_safe_area(mut self, safe_area: WindowSafeArea) -> Self {
self.safe_area = safe_area;
self
}
pub fn is_compact(&self) -> bool {
matches!(self.class, WindowSizeClass::Compact)
}
pub fn is_medium(&self) -> bool {
matches!(self.class, WindowSizeClass::Medium)
}
pub fn is_expanded(&self) -> bool {
matches!(self.class, WindowSizeClass::Expanded)
}
pub fn is_short(&self) -> bool {
matches!(self.height_class, WindowHeightClass::Compact)
}
pub fn is_medium_height(&self) -> bool {
matches!(self.height_class, WindowHeightClass::Medium)
}
pub fn is_tall(&self) -> bool {
matches!(self.height_class, WindowHeightClass::Expanded)
}
}
impl Default for WindowSize {
fn default() -> Self {
Self::new(
0.0,
0.0,
WindowSizeClass::Compact,
WindowHeightClass::Compact,
)
}
}
struct Subscriber {
id: u64,
notify: Arc<dyn Fn() + Send + Sync>,
}
thread_local! {
static NODES: RefCell<Vec<(u32, WindowSize)>> = const { RefCell::new(Vec::new()) };
static NODE_SUBSCRIBERS: RefCell<Vec<(u32, Subscriber)>> = const { RefCell::new(Vec::new()) };
static CURRENT: Cell<WindowSize> = const { Cell::new(WindowSize {
width_dp: 0.0,
height_dp: 0.0,
class: WindowSizeClass::Compact,
height_class: WindowHeightClass::Compact,
safe_area: WindowSafeArea { top: 0.0, leading: 0.0, trailing: 0.0 },
}) };
static SUBSCRIBERS: RefCell<Vec<Subscriber>> = const { RefCell::new(Vec::new()) };
static WINDOWS: RefCell<Vec<(u32, WindowSize)>> = const { RefCell::new(Vec::new()) };
static WINDOW_SUBSCRIBERS: RefCell<Vec<(u32, Subscriber)>> = const { RefCell::new(Vec::new()) };
static NEXT_SUBSCRIBER_ID: Cell<u64> = const { Cell::new(1) };
}
pub fn window_size() -> WindowSize {
match crate::windows::current_window_id() {
0 => CURRENT.with(Cell::get),
window => window_size_of(window),
}
}
pub fn window_size_of(window: u32) -> WindowSize {
WINDOWS.with_borrow(|windows| {
windows
.iter()
.find(|(id, _)| *id == window)
.map(|(_, size)| *size)
.unwrap_or_default()
})
}
pub(crate) fn publish_window(window: u32, size: WindowSize) -> bool {
let changed =
WINDOWS.with_borrow_mut(
|windows| match windows.iter_mut().find(|(id, _)| *id == window) {
Some(entry) => {
if entry.1 == size {
return false;
}
entry.1 = size;
true
}
None => {
windows.push((window, size));
true
}
},
);
if !changed {
return false;
}
WINDOW_SUBSCRIBERS.with_borrow(|subscribers| {
let mut woke = false;
for (id, subscriber) in subscribers {
if *id == window {
(subscriber.notify)();
woke = true;
}
}
woke
})
}
pub(crate) fn window_state_len() -> usize {
WINDOWS.with_borrow(Vec::len) + WINDOW_SUBSCRIBERS.with_borrow(Vec::len)
}
pub(crate) fn forget_window(window: u32) {
WINDOWS.with_borrow_mut(|windows| windows.retain(|(id, _)| *id != window));
}
pub struct WindowSubscription {
id: u64,
}
pub fn subscribe_window(window: u32, notify: Arc<dyn Fn() + Send + Sync>) -> WindowSubscription {
let id = next_subscriber_id();
WINDOW_SUBSCRIBERS.with_borrow_mut(|subscribers| {
subscribers.push((window, Subscriber { id, notify }));
});
WindowSubscription { id }
}
impl Drop for WindowSubscription {
fn drop(&mut self) {
WINDOW_SUBSCRIBERS.with_borrow_mut(|subscribers| {
subscribers.retain(|(_, subscriber)| subscriber.id != self.id);
});
}
}
pub(crate) fn publish(size: WindowSize) -> bool {
if CURRENT.with(Cell::get) == size {
return false;
}
CURRENT.with(|current| current.set(size));
SUBSCRIBERS.with_borrow(|subscribers| {
for subscriber in subscribers {
(subscriber.notify)();
}
});
true
}
pub fn node_size(token: u32) -> WindowSize {
NODES.with_borrow(|nodes| {
nodes
.iter()
.find(|(name, _)| *name == token)
.map(|(_, size)| *size)
.unwrap_or_default()
})
}
pub(crate) fn publish_node(token: u32, size: WindowSize) -> bool {
let changed =
NODES.with_borrow_mut(
|nodes| match nodes.iter_mut().find(|(name, _)| *name == token) {
Some(entry) => {
if entry.1 == size {
return false;
}
entry.1 = size;
true
}
None => {
nodes.push((token, size));
true
}
},
);
if !changed {
return false;
}
NODE_SUBSCRIBERS.with_borrow(|subscribers| {
let mut woke = false;
for (name, subscriber) in subscribers {
if *name == token {
(subscriber.notify)();
woke = true;
}
}
woke
})
}
#[doc(hidden)]
pub fn reset_window_size() {
CURRENT.with(|current| current.set(WindowSize::default()));
SUBSCRIBERS.with_borrow_mut(Vec::clear);
NODES.with_borrow_mut(Vec::clear);
NODE_SUBSCRIBERS.with_borrow_mut(Vec::clear);
WINDOWS.with_borrow_mut(Vec::clear);
WINDOW_SUBSCRIBERS.with_borrow_mut(Vec::clear);
}
pub struct WindowSizeSubscription {
id: u64,
}
pub fn subscribe(notify: Arc<dyn Fn() + Send + Sync>) -> WindowSizeSubscription {
let id = next_subscriber_id();
SUBSCRIBERS.with_borrow_mut(|subscribers| subscribers.push(Subscriber { id, notify }));
WindowSizeSubscription { id }
}
fn next_subscriber_id() -> u64 {
NEXT_SUBSCRIBER_ID.with(|next| {
let id = next.get();
next.set(id + 1);
id
})
}
impl Drop for WindowSizeSubscription {
fn drop(&mut self) {
SUBSCRIBERS.with_borrow_mut(|subscribers| {
subscribers.retain(|subscriber| subscriber.id != self.id);
});
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct NodeSize {
token: u32,
size: WindowSize,
}
impl NodeSize {
pub fn of(token: u32) -> Self {
Self {
token,
size: node_size(token),
}
}
pub const fn token(&self) -> i64 {
self.token as i64
}
pub fn is_compact(&self) -> bool {
self.size.is_compact()
}
pub fn is_medium(&self) -> bool {
self.size.is_medium()
}
pub fn is_expanded(&self) -> bool {
self.size.is_expanded()
}
pub const fn measured(&self) -> WindowSize {
self.size
}
}
thread_local! {
static NEXT_TOKEN: Cell<u32> = const { Cell::new(1) };
}
pub fn next_node_token() -> u32 {
NEXT_TOKEN.with(|next| {
let token = next.get();
next.set(token + 1);
token
})
}
pub struct NodeSizeSubscription {
token: u32,
id: u64,
}
impl NodeSizeSubscription {
pub fn token(&self) -> u32 {
self.token
}
}
pub fn subscribe_node(token: u32, notify: Arc<dyn Fn() + Send + Sync>) -> NodeSizeSubscription {
let id = next_subscriber_id();
NODE_SUBSCRIBERS.with_borrow_mut(|subscribers| {
subscribers.push((token, Subscriber { id, notify }));
});
NodeSizeSubscription { token, id }
}
impl Drop for NodeSizeSubscription {
fn drop(&mut self) {
NODE_SUBSCRIBERS.with_borrow_mut(|subscribers| {
subscribers.retain(|(_, subscriber)| subscriber.id != self.id);
});
NODES.with_borrow_mut(|nodes| nodes.retain(|(token, _)| *token != self.token));
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fr19_9_safe_area_only_change_republishes() {
use std::sync::atomic::{AtomicUsize, Ordering};
reset_window_size();
let calls = Arc::new(AtomicUsize::new(0));
let counter = Arc::clone(&calls);
let _subscription = subscribe(Arc::new(move || {
counter.fetch_add(1, Ordering::SeqCst);
}));
let size = WindowSize::default().with_safe_area(WindowSafeArea {
top: 28.0,
leading: 70.0,
trailing: 0.0,
});
assert!(publish(size));
assert_eq!(window_size(), size);
assert_eq!(calls.load(Ordering::SeqCst), 1);
assert!(!publish(size));
assert_eq!(calls.load(Ordering::SeqCst), 1);
}
#[test]
fn fr20_window_size_starts_compact_before_the_renderer_measures_anything() {
reset_window_size();
assert_eq!(window_size(), WindowSize::default());
assert!(window_size().is_compact());
}
#[test]
fn fr20_publishing_the_same_size_twice_wakes_nobody() {
reset_window_size();
let size = WindowSize::new(
700.0,
800.0,
WindowSizeClass::Medium,
WindowHeightClass::Medium,
);
assert!(publish(size));
assert!(!publish(size));
reset_window_size();
}
}