sui_runtime/
output_range.rs1use std::{
6 collections::HashMap,
7 sync::{OnceLock, RwLock},
8};
9
10use sui_core::WindowId;
11use sui_reactive::Signal;
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
15pub enum OutputColorRange {
16 Standard,
18 WideGamut,
20 HighDynamicRange,
23}
24
25type RangeSignal = Signal<Option<OutputColorRange>>;
26
27fn store() -> &'static RwLock<HashMap<WindowId, RangeSignal>> {
28 static STORE: OnceLock<RwLock<HashMap<WindowId, RangeSignal>>> = OnceLock::new();
29 STORE.get_or_init(|| RwLock::new(HashMap::new()))
30}
31
32pub fn set_window_output_color_range(window_id: WindowId, range: OutputColorRange) {
35 window_output_color_range_signal(window_id).set(Some(range));
37}
38
39pub fn window_output_color_range(window_id: WindowId) -> Option<OutputColorRange> {
41 store()
42 .read()
43 .expect("output color range store lock should not be poisoned")
44 .get(&window_id)
45 .and_then(Signal::get)
46}
47
48pub fn window_output_color_range_signal(window_id: WindowId) -> RangeSignal {
51 if let Some(signal) = store()
52 .read()
53 .expect("output color range store lock should not be poisoned")
54 .get(&window_id)
55 {
56 return signal.clone();
57 }
58 store()
59 .write()
60 .expect("output color range store lock should not be poisoned")
61 .entry(window_id)
62 .or_insert_with(|| Signal::named("Window output color range", None))
63 .clone()
64}
65
66pub(crate) fn clear_window_output_color_range(window_id: WindowId) {
67 let removed = store()
68 .write()
69 .expect("output color range store lock should not be poisoned")
70 .remove(&window_id);
71 if let Some(signal) = removed {
72 signal.set(None);
73 }
74}
75
76pub(crate) fn clear_window_output_color_ranges() {
77 let removed = std::mem::take(
78 &mut *store()
79 .write()
80 .expect("output color range store lock should not be poisoned"),
81 );
82 for signal in removed.into_values() {
83 signal.set(None);
84 }
85}