settings/display/
types.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use bitflags::bitflags;
use serde::{Deserialize, Serialize};

use crate::base::Merge;

#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DisplayInfo {
    /// The last brightness value that was manually set.
    pub manual_brightness_value: f32,
    pub auto_brightness_value: f32,
    pub auto_brightness: bool,
    pub screen_enabled: bool,
    pub low_light_mode: LowLightMode,
    pub theme: Option<Theme>,
}

impl DisplayInfo {
    pub(crate) const fn new(
        auto_brightness: bool,
        manual_brightness_value: f32,
        auto_brightness_value: f32,
        screen_enabled: bool,
        low_light_mode: LowLightMode,
        theme: Option<Theme>,
    ) -> DisplayInfo {
        DisplayInfo {
            manual_brightness_value,
            auto_brightness_value,
            auto_brightness,
            screen_enabled,
            low_light_mode,
            theme,
        }
    }

    pub(crate) fn is_finite(&self) -> bool {
        self.manual_brightness_value.is_finite()
    }
}

#[derive(Debug, Default, PartialEq, Copy, Clone)]
pub struct SetDisplayInfo {
    pub manual_brightness_value: Option<f32>,
    pub auto_brightness_value: Option<f32>,
    pub auto_brightness: Option<bool>,
    pub screen_enabled: Option<bool>,
    pub low_light_mode: Option<LowLightMode>,
    pub theme: Option<Theme>,
}

impl Merge<SetDisplayInfo> for DisplayInfo {
    fn merge(&self, other: SetDisplayInfo) -> Self {
        Self {
            manual_brightness_value: other
                .manual_brightness_value
                .unwrap_or(self.manual_brightness_value),
            auto_brightness_value: other
                .auto_brightness_value
                .unwrap_or(self.auto_brightness_value),
            auto_brightness: other.auto_brightness.unwrap_or(self.auto_brightness),
            screen_enabled: other.screen_enabled.unwrap_or(self.screen_enabled),
            low_light_mode: other.low_light_mode.unwrap_or(self.low_light_mode),
            theme: other.theme.or(self.theme),
        }
    }
}

#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize, Hash, Eq)]
pub enum LowLightMode {
    /// Device should not be in low-light mode.
    Disable,
    /// Device should not be in low-light mode and should transition
    /// out of it immediately.
    DisableImmediately,
    /// Device should be in low-light mode.
    Enable,
}

#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
pub enum ThemeType {
    Unknown,
    Default,
    Light,
    Dark,
}

bitflags! {
    #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct ThemeMode: u32 {
        /// Product can choose a theme based on ambient cues.
        const AUTO = 0b00000001;
    }
}

bitflags_serde_legacy::impl_traits!(ThemeMode);

#[derive(Debug, Clone, PartialEq, Copy, Serialize, Deserialize)]
pub struct Theme {
    pub theme_type: Option<ThemeType>,
    pub theme_mode: ThemeMode,
}

impl Theme {
    pub(super) fn new(theme_type: Option<ThemeType>, theme_mode: ThemeMode) -> Self {
        Self { theme_type, theme_mode }
    }
}

/// Builder for `Theme` that with a `build` method that returns
/// an `Option` that will be None if all the fields of the Theme would
/// otherwise be empty.
pub struct ThemeBuilder {
    theme_type: Option<ThemeType>,
    theme_mode: ThemeMode,
}

impl ThemeBuilder {
    pub(super) fn new() -> Self {
        Self { theme_type: None, theme_mode: ThemeMode::empty() }
    }

    pub(super) fn set_theme_type(&mut self, theme_type: Option<ThemeType>) -> &mut Self {
        self.theme_type = theme_type;
        self
    }

    pub(super) fn set_theme_mode(&mut self, theme_mode: ThemeMode) -> &mut Self {
        self.theme_mode = theme_mode;
        self
    }

    pub(super) fn build(&self) -> Option<Theme> {
        if self.theme_type.is_none() && self.theme_mode.is_empty() {
            None
        } else {
            Some(Theme { theme_type: self.theme_type, theme_mode: self.theme_mode })
        }
    }
}