use std::{
collections::HashMap,
sync::{OnceLock, RwLock},
};
use sui_core::WindowId;
use sui_reactive::Signal;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum OutputColorRange {
Standard,
WideGamut,
HighDynamicRange,
}
type RangeSignal = Signal<Option<OutputColorRange>>;
fn store() -> &'static RwLock<HashMap<WindowId, RangeSignal>> {
static STORE: OnceLock<RwLock<HashMap<WindowId, RangeSignal>>> = OnceLock::new();
STORE.get_or_init(|| RwLock::new(HashMap::new()))
}
pub fn set_window_output_color_range(window_id: WindowId, range: OutputColorRange) {
window_output_color_range_signal(window_id).set(Some(range));
}
pub fn window_output_color_range(window_id: WindowId) -> Option<OutputColorRange> {
store()
.read()
.expect("output color range store lock should not be poisoned")
.get(&window_id)
.and_then(Signal::get)
}
pub fn window_output_color_range_signal(window_id: WindowId) -> RangeSignal {
if let Some(signal) = store()
.read()
.expect("output color range store lock should not be poisoned")
.get(&window_id)
{
return signal.clone();
}
store()
.write()
.expect("output color range store lock should not be poisoned")
.entry(window_id)
.or_insert_with(|| Signal::named("Window output color range", None))
.clone()
}
pub(crate) fn clear_window_output_color_range(window_id: WindowId) {
let removed = store()
.write()
.expect("output color range store lock should not be poisoned")
.remove(&window_id);
if let Some(signal) = removed {
signal.set(None);
}
}
pub(crate) fn clear_window_output_color_ranges() {
let removed = std::mem::take(
&mut *store()
.write()
.expect("output color range store lock should not be poisoned"),
);
for signal in removed.into_values() {
signal.set(None);
}
}