GPU-accelerated UI toolkit (Vulkan)
git clone https://git.lucas.co/cce-ui.git
src/scale.rs (2.9K)
1 use std::sync::{OnceLock, RwLock};
2
3 static SCALE_FACTOR: RwLock<f32> = RwLock::new(1.0);
4 static FORCED_SCALE: OnceLock<Option<f32>> = OnceLock::new();
5
6 /// `CCE_FORCE_SCALE`: HiDPI override for foreign compositors that report a
7 /// scale-1 output (the display-manager greeter under cage). In forced mode the
8 /// compositor's logical coordinate space is treated as physical: configure
9 /// sizes and pointer positions are divided by this factor, layout/rendering
10 /// scale up by it, and the surface keeps `buffer_scale` 1 so the buffer still
11 /// matches the size the compositor configured. Unset (the normal case, under
12 /// cce-fx) this is `None` and nothing changes.
13 pub fn forced_scale() -> Option<f32> {
14 *FORCED_SCALE.get_or_init(|| {
15 std::env::var("CCE_FORCE_SCALE")
16 .ok()
17 .and_then(|v| v.parse::<f32>().ok())
18 .filter(|s| *s > 0.0 && (*s - 1.0).abs() > 0.001)
19 })
20 }
21 static APP_ID: RwLock<String> = RwLock::new(String::new());
22 static FULLSCREEN: RwLock<bool> = RwLock::new(false);
23 static MAXIMIZED: RwLock<bool> = RwLock::new(false);
24
25 // A window's properties are its own (`crate::window_state::Props`): read from the window
26 // whose code is running, else from these process-wide values, which every setter also
27 // writes — the last any window set, what a worker thread reads (`docs/rfc-global-state.md`,
28 // phase 4).
29
30 /// The HiDPI scale of the window being drawn (off a window's thread, the last one set).
31 pub fn scale_factor() -> f32 {
32 crate::window_state::entered(|w| w.props.borrow().scale).unwrap_or_else(process_scale_factor)
33 }
34
35 pub(crate) fn process_scale_factor() -> f32 {
36 *SCALE_FACTOR.read().unwrap()
37 }
38
39 pub fn set_scale_factor(scale: f32) {
40 crate::window_state::entered(|w| w.props.borrow_mut().scale = scale);
41 if let Ok(mut lock) = SCALE_FACTOR.write() {
42 *lock = scale;
43 }
44 }
45
46 pub fn app_id() -> String {
47 crate::window_state::entered(|w| w.props.borrow().app_id.clone()).unwrap_or_else(process_app_id)
48 }
49
50 pub(crate) fn process_app_id() -> String {
51 APP_ID.read().unwrap().clone()
52 }
53
54 pub fn set_app_id(id: String) {
55 crate::window_state::entered(|w| w.props.borrow_mut().app_id = id.clone());
56 if let Ok(mut lock) = APP_ID.write() {
57 *lock = id;
58 }
59 }
60
61 pub fn is_fullscreen() -> bool {
62 crate::window_state::entered(|w| w.props.borrow().fullscreen).unwrap_or_else(|| *FULLSCREEN.read().unwrap())
63 }
64
65 pub fn set_fullscreen(fs: bool) {
66 crate::window_state::entered(|w| w.props.borrow_mut().fullscreen = fs);
67 if let Ok(mut lock) = FULLSCREEN.write() {
68 *lock = fs;
69 }
70 }
71
72 pub fn is_maximized() -> bool {
73 crate::window_state::entered(|w| w.props.borrow().maximized).unwrap_or_else(|| *MAXIMIZED.read().unwrap())
74 }
75
76 pub fn set_maximized(m: bool) {
77 crate::window_state::entered(|w| w.props.borrow_mut().maximized = m);
78 if let Ok(mut lock) = MAXIMIZED.write() {
79 *lock = m;
80 }
81 }