Skip to main content

sui_runtime/
output_range.rs

1//! How far each window's output reaches beyond sRGB and SDR white, for
2//! widgets whose styling depends on it (HDR theme modes). The platform sets
3//! it before rendering each frame.
4
5use std::{
6    collections::HashMap,
7    sync::{OnceLock, RwLock},
8};
9
10use sui_core::WindowId;
11use sui_reactive::Signal;
12
13/// How far a window's output reaches beyond sRGB and SDR white.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
15pub enum OutputColorRange {
16    /// sRGB colors up to SDR white.
17    Standard,
18    /// Colors beyond sRGB, up to SDR white.
19    WideGamut,
20    /// Light beyond SDR white, and colors beyond sRGB where the display has
21    /// them.
22    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
32/// Record what `window_id`'s output can show. Widgets observing
33/// [`window_output_color_range_signal`] are invalidated when it changes.
34pub fn set_window_output_color_range(window_id: WindowId, range: OutputColorRange) {
35    // Set outside the lock: observers run when the value changes.
36    window_output_color_range_signal(window_id).set(Some(range));
37}
38
39/// What `window_id`'s output can show, once the platform has said.
40pub 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
48/// What `window_id`'s output can show, as a signal to observe. It holds
49/// `None` until the platform has said, as it does without a platform.
50pub 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}