settings/display/
types.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use bitflags::bitflags;
6use serde::{Deserialize, Serialize};
7use settings_common::inspect::event::Nameable;
8use settings_common::utils::Merge;
9
10#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
11#[serde(deny_unknown_fields)]
12pub struct DisplayInfo {
13    /// The last brightness value that was manually set.
14    pub manual_brightness_value: f32,
15    pub auto_brightness_value: f32,
16    pub auto_brightness: bool,
17    pub screen_enabled: bool,
18    pub low_light_mode: LowLightMode,
19    pub theme: Option<Theme>,
20}
21
22impl DisplayInfo {
23    pub(crate) const fn new(
24        auto_brightness: bool,
25        manual_brightness_value: f32,
26        auto_brightness_value: f32,
27        screen_enabled: bool,
28        low_light_mode: LowLightMode,
29        theme: Option<Theme>,
30    ) -> DisplayInfo {
31        DisplayInfo {
32            manual_brightness_value,
33            auto_brightness_value,
34            auto_brightness,
35            screen_enabled,
36            low_light_mode,
37            theme,
38        }
39    }
40
41    pub(crate) fn is_finite(&self) -> bool {
42        self.manual_brightness_value.is_finite()
43    }
44}
45
46impl Nameable for DisplayInfo {
47    const NAME: &str = "Display";
48}
49
50#[derive(Debug, Default, PartialEq, Copy, Clone)]
51pub struct SetDisplayInfo {
52    pub manual_brightness_value: Option<f32>,
53    pub auto_brightness_value: Option<f32>,
54    pub auto_brightness: Option<bool>,
55    pub screen_enabled: Option<bool>,
56    pub low_light_mode: Option<LowLightMode>,
57    pub theme: Option<Theme>,
58}
59
60impl Merge<SetDisplayInfo> for DisplayInfo {
61    fn merge(&self, other: SetDisplayInfo) -> Self {
62        Self {
63            manual_brightness_value: other
64                .manual_brightness_value
65                .unwrap_or(self.manual_brightness_value),
66            auto_brightness_value: other
67                .auto_brightness_value
68                .unwrap_or(self.auto_brightness_value),
69            auto_brightness: other.auto_brightness.unwrap_or(self.auto_brightness),
70            screen_enabled: other.screen_enabled.unwrap_or(self.screen_enabled),
71            low_light_mode: other.low_light_mode.unwrap_or(self.low_light_mode),
72            theme: other.theme.or(self.theme),
73        }
74    }
75}
76
77#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize, Hash, Eq)]
78pub enum LowLightMode {
79    /// Device should not be in low-light mode.
80    Disable,
81    /// Device should not be in low-light mode and should transition
82    /// out of it immediately.
83    DisableImmediately,
84    /// Device should be in low-light mode.
85    Enable,
86}
87
88#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
89pub enum ThemeType {
90    Unknown,
91    Default,
92    Light,
93    Dark,
94}
95
96bitflags! {
97    #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
98    pub struct ThemeMode: u32 {
99        /// Product can choose a theme based on ambient cues.
100        const AUTO = 0b00000001;
101    }
102}
103
104bitflags_serde_legacy::impl_traits!(ThemeMode);
105
106#[derive(Debug, Clone, PartialEq, Copy, Serialize, Deserialize)]
107pub struct Theme {
108    pub theme_type: Option<ThemeType>,
109    pub theme_mode: ThemeMode,
110}
111
112impl Theme {
113    pub(super) fn new(theme_type: Option<ThemeType>, theme_mode: ThemeMode) -> Self {
114        Self { theme_type, theme_mode }
115    }
116}
117
118/// Builder for `Theme` that with a `build` method that returns
119/// an `Option` that will be None if all the fields of the Theme would
120/// otherwise be empty.
121pub struct ThemeBuilder {
122    theme_type: Option<ThemeType>,
123    theme_mode: ThemeMode,
124}
125
126impl ThemeBuilder {
127    pub(super) fn new() -> Self {
128        Self { theme_type: None, theme_mode: ThemeMode::empty() }
129    }
130
131    pub(super) fn set_theme_type(&mut self, theme_type: Option<ThemeType>) -> &mut Self {
132        self.theme_type = theme_type;
133        self
134    }
135
136    pub(super) fn set_theme_mode(&mut self, theme_mode: ThemeMode) -> &mut Self {
137        self.theme_mode = theme_mode;
138        self
139    }
140
141    pub(super) fn build(&self) -> Option<Theme> {
142        if self.theme_type.is_none() && self.theme_mode.is_empty() {
143            None
144        } else {
145            Some(Theme { theme_type: self.theme_type, theme_mode: self.theme_mode })
146        }
147    }
148}