Skip to main content

settings_model/
appearance.rs

1//! The `appearance` section's plain leaves and bounds, which the toolkit draws by and the
2//! settings store's typed keys write within. The schedule is [`crate::day_schedule`]'s.
3
4pub use crate::day_schedule::{MODE_KEY, SECTION};
5
6/// The field of the interface language, named as its language pack is (`de`; see
7/// `tilo_paths::language_of`). Unset follows the device's locale.
8pub const LANGUAGE: &str = "language";
9
10/// The interface language: the one the person chose ([`LANGUAGE`]), else the
11/// locale the device runs in (`LC_ALL`, `LC_MESSAGES`, `LANGUAGE`, `LANG`, read
12/// as POSIX).
13///
14/// Every surface resolves its language — and the language of the dates it
15/// formats — from this one answer, so a person who chose English on a German
16/// machine reads English throughout: the system locale is only the default,
17/// never a second source.
18#[must_use]
19pub fn preferred_language() -> Option<String> {
20    let chosen = crate::source::source().and_then(|source| {
21        crate::source::load_section_in(&*source, SECTION)
22            .get(LANGUAGE)?
23            .as_str()
24            .map(str::to_owned)
25    });
26    let system = tilo_process::environment::environment().locale.clone();
27    preferred_language_from(chosen.as_deref(), system.as_deref())
28}
29
30/// [`preferred_language`] over an explicit choice and system locale. A value
31/// that names no language (`C`, `POSIX`, malformed) falls back to the next
32/// source; `None` means neither named one.
33#[must_use]
34pub fn preferred_language_from(chosen: Option<&str>, system: Option<&str>) -> Option<String> {
35    [chosen, system].into_iter().flatten().find_map(|value| {
36        tilo_process::environment::parse_posix_locale(value).map(|locale| locale.to_string())
37    })
38}
39
40/// The section of this device's own appearance, beside the person's.
41pub const DEVICE_SECTION: &str = "appearance_device";
42
43/// The bounds of the person's text scale.
44pub const TEXT_SCALE_MIN: f32 = 0.5;
45pub const TEXT_SCALE_MAX: f32 = 4.0;
46
47/// The bounds of how much of a theme's translucency the person keeps.
48pub const TRANSLUCENCY_MIN: f32 = 0.0;
49pub const TRANSLUCENCY_MAX: f32 = 1.0;
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn the_chosen_language_wins_and_the_system_locale_is_only_the_default() {
57        assert_eq!(
58            preferred_language_from(Some("en-US"), Some("de_DE.UTF-8")).as_deref(),
59            Some("en-US")
60        );
61        assert_eq!(
62            preferred_language_from(Some("de_DE.UTF-8"), None).as_deref(),
63            Some("de-DE")
64        );
65        assert_eq!(
66            preferred_language_from(None, Some("de_DE.UTF-8")).as_deref(),
67            Some("de-DE"),
68            "with no choice the device's locale is the default"
69        );
70        assert_eq!(
71            preferred_language_from(Some("C"), Some("fr_FR.UTF-8")).as_deref(),
72            Some("fr-FR"),
73            "an explicit C names no language, so the default decides"
74        );
75        assert_eq!(preferred_language_from(Some("C"), Some("C.UTF-8")), None);
76        assert_eq!(preferred_language_from(Some("not a locale!"), None), None);
77        assert_eq!(preferred_language_from(None, None), None);
78    }
79}