lumen_core/
window_state.rs1use std::sync::{Mutex, OnceLock};
14
15#[derive(Debug, Clone)]
16struct WindowState {
17 title: String,
18 width: f32,
19 height: f32,
20 dpr: f32,
21}
22
23impl Default for WindowState {
24 fn default() -> Self {
25 Self {
26 title: String::new(),
27 width: 0.0,
28 height: 0.0,
29 dpr: 1.0,
30 }
31 }
32}
33
34fn cell() -> &'static Mutex<WindowState> {
35 static STATE: OnceLock<Mutex<WindowState>> = OnceLock::new();
36 STATE.get_or_init(|| Mutex::new(WindowState::default()))
37}
38
39pub fn title() -> String {
41 cell().lock().map(|s| s.title.clone()).unwrap_or_default()
42}
43
44pub fn set_title(title: &str) {
46 if let Ok(mut s) = cell().lock() {
47 s.title = title.to_string();
48 }
49}
50
51pub fn size() -> (f32, f32) {
53 cell()
54 .lock()
55 .map(|s| (s.width, s.height))
56 .unwrap_or((0.0, 0.0))
57}
58
59pub fn set_size(width: f32, height: f32) {
61 if let Ok(mut s) = cell().lock() {
62 s.width = width;
63 s.height = height;
64 }
65}
66
67pub fn dpr() -> f32 {
69 cell().lock().map(|s| s.dpr).unwrap_or(1.0)
70}
71
72pub fn set_dpr(dpr: f32) {
74 if let Ok(mut s) = cell().lock() {
75 s.dpr = dpr;
76 }
77}
78
79#[cfg(test)]
80mod tests {
81 use super::*;
82
83 #[test]
84 fn title_and_size_round_trip() {
85 set_title("Hello");
86 assert_eq!(title(), "Hello");
87 set_size(640.0, 480.0);
88 assert_eq!(size(), (640.0, 480.0));
89 set_dpr(2.0);
90 assert_eq!(dpr(), 2.0);
91 }
92}