Skip to main content

settings_model/
device_class.rs

1//! What kind of device a process's settings are read on, for the settings whose default differs by
2//! it: on by default where the device is the person's own computer, off where it is a page in a
3//! browser.
4//!
5//! The host says which: a native host leaves the default, and the web runtime installs
6//! [`DeviceClass::Web`] before it starts an app. A setting declares a default per class
7//! (`SettingsSchema::defaults_on`).
8
9use std::sync::atomic::{AtomicU8, Ordering};
10
11#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
12pub enum DeviceClass {
13    #[default]
14    Native,
15    Web,
16}
17
18static INSTALLED: AtomicU8 = AtomicU8::new(0);
19
20impl DeviceClass {
21    #[must_use]
22    pub fn current() -> Self {
23        match INSTALLED.load(Ordering::Acquire) {
24            1 => Self::Web,
25            _ => Self::Native,
26        }
27    }
28
29    pub fn install(self) {
30        let value = match self {
31            Self::Native => 0,
32            Self::Web => 1,
33        };
34        INSTALLED.store(value, Ordering::Release);
35    }
36}