settings/accessibility/
types.rs

1// Copyright 2019 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 crate::base::Merge;
6use serde::{Deserialize, Serialize};
7
8#[derive(PartialEq, Default, Debug, Clone, Copy, Serialize, Deserialize)]
9pub struct AccessibilityInfo {
10    pub(crate) audio_description: Option<bool>,
11    pub(crate) screen_reader: Option<bool>,
12    pub(crate) color_inversion: Option<bool>,
13    pub(crate) enable_magnification: Option<bool>,
14    pub(crate) color_correction: Option<ColorBlindnessType>,
15    pub(crate) captions_settings: Option<CaptionsSettings>,
16}
17
18impl AccessibilityInfo {
19    pub(crate) fn is_finite(&self) -> bool {
20        self.captions_settings.map_or(true, |captions| captions.is_finite())
21    }
22}
23
24impl Merge for AccessibilityInfo {
25    fn merge(&self, other: Self) -> Self {
26        AccessibilityInfo {
27            audio_description: other.audio_description.or(self.audio_description),
28            screen_reader: other.screen_reader.or(self.screen_reader),
29            color_inversion: other.color_inversion.or(self.color_inversion),
30            enable_magnification: other.enable_magnification.or(self.enable_magnification),
31            color_correction: other
32                .color_correction
33                .map(ColorBlindnessType::into)
34                .or(self.color_correction),
35            captions_settings: match (self.captions_settings, other.captions_settings) {
36                (Some(caption_settings), Some(other_caption_settings)) => {
37                    Some(caption_settings.merge(other_caption_settings))
38                }
39                _ => other.captions_settings.or(self.captions_settings),
40            },
41        }
42    }
43}
44
45#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
46pub(crate) enum ColorBlindnessType {
47    /// No color blindness.
48    None,
49
50    /// Red-green color blindness due to reduced sensitivity to red light.
51    Protanomaly,
52
53    /// Red-green color blindness due to reduced sensitivity to green light.
54    Deuteranomaly,
55
56    /// Blue-yellow color blindness. It is due to reduced sensitivity to blue
57    /// light.
58    Tritanomaly,
59}
60
61impl From<fidl_fuchsia_settings::ColorBlindnessType> for ColorBlindnessType {
62    fn from(color_blindness_type: fidl_fuchsia_settings::ColorBlindnessType) -> Self {
63        match color_blindness_type {
64            fidl_fuchsia_settings::ColorBlindnessType::None => ColorBlindnessType::None,
65            fidl_fuchsia_settings::ColorBlindnessType::Protanomaly => {
66                ColorBlindnessType::Protanomaly
67            }
68            fidl_fuchsia_settings::ColorBlindnessType::Deuteranomaly => {
69                ColorBlindnessType::Deuteranomaly
70            }
71            fidl_fuchsia_settings::ColorBlindnessType::Tritanomaly => {
72                ColorBlindnessType::Tritanomaly
73            }
74        }
75    }
76}
77
78impl From<ColorBlindnessType> for fidl_fuchsia_settings::ColorBlindnessType {
79    fn from(color_blindness_type: ColorBlindnessType) -> Self {
80        match color_blindness_type {
81            ColorBlindnessType::None => fidl_fuchsia_settings::ColorBlindnessType::None,
82            ColorBlindnessType::Protanomaly => {
83                fidl_fuchsia_settings::ColorBlindnessType::Protanomaly
84            }
85            ColorBlindnessType::Deuteranomaly => {
86                fidl_fuchsia_settings::ColorBlindnessType::Deuteranomaly
87            }
88            ColorBlindnessType::Tritanomaly => {
89                fidl_fuchsia_settings::ColorBlindnessType::Tritanomaly
90            }
91        }
92    }
93}
94
95#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
96pub(crate) struct CaptionsSettings {
97    pub(crate) for_media: Option<bool>,
98    pub(crate) for_tts: Option<bool>,
99    pub(crate) font_style: Option<CaptionFontStyle>,
100    pub(crate) window_color: Option<ColorRgba>,
101    pub(crate) background_color: Option<ColorRgba>,
102}
103
104impl CaptionsSettings {
105    pub(crate) fn is_finite(&self) -> bool {
106        self.font_style.map_or(true, |font_style| font_style.is_finite())
107            && self.window_color.map_or(true, |window_color| window_color.is_finite())
108            && self.background_color.map_or(true, |bkg_color| bkg_color.is_finite())
109    }
110}
111
112impl Merge for CaptionsSettings {
113    fn merge(&self, other: Self) -> Self {
114        CaptionsSettings {
115            for_media: other.for_media.or(self.for_media),
116            for_tts: other.for_tts.or(self.for_tts),
117            window_color: other.window_color.or(self.window_color),
118            background_color: other.background_color.or(self.background_color),
119            font_style: match (self.font_style, other.font_style) {
120                (Some(style), Some(other_style)) => Some(style.merge(other_style)),
121                _ => other.font_style.or(self.font_style),
122            },
123        }
124    }
125}
126
127impl From<fidl_fuchsia_settings::CaptionsSettings> for CaptionsSettings {
128    fn from(src: fidl_fuchsia_settings::CaptionsSettings) -> Self {
129        CaptionsSettings {
130            for_media: src.for_media,
131            for_tts: src.for_tts,
132            font_style: src.font_style.map(fidl_fuchsia_settings::CaptionFontStyle::into),
133            window_color: src.window_color.map(fidl_fuchsia_ui_types::ColorRgba::into),
134            background_color: src.background_color.map(fidl_fuchsia_ui_types::ColorRgba::into),
135        }
136    }
137}
138
139impl From<CaptionsSettings> for fidl_fuchsia_settings::CaptionsSettings {
140    fn from(src: CaptionsSettings) -> Self {
141        fidl_fuchsia_settings::CaptionsSettings {
142            for_media: src.for_media,
143            for_tts: src.for_tts,
144            font_style: src.font_style.map(CaptionFontStyle::into),
145            window_color: src.window_color.map(ColorRgba::into),
146            background_color: src.background_color.map(ColorRgba::into),
147            ..Default::default()
148        }
149    }
150}
151
152#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
153pub(crate) struct CaptionFontStyle {
154    pub(crate) family: Option<CaptionFontFamily>,
155    pub(crate) color: Option<ColorRgba>,
156    pub(crate) relative_size: Option<f32>,
157    pub(crate) char_edge_style: Option<EdgeStyle>,
158}
159
160impl CaptionFontStyle {
161    pub(crate) fn is_finite(&self) -> bool {
162        self.color.map_or(true, |color| color.is_finite())
163            && self.relative_size.map_or(true, |size| size.is_finite())
164    }
165}
166
167impl Merge for CaptionFontStyle {
168    fn merge(&self, other: Self) -> Self {
169        CaptionFontStyle {
170            family: other.family.or(self.family),
171            color: other.color.or(self.color),
172            relative_size: other.relative_size.or(self.relative_size),
173            char_edge_style: other.char_edge_style.or(self.char_edge_style),
174        }
175    }
176}
177
178impl From<fidl_fuchsia_settings::CaptionFontStyle> for CaptionFontStyle {
179    fn from(src: fidl_fuchsia_settings::CaptionFontStyle) -> Self {
180        CaptionFontStyle {
181            family: src.family.map(fidl_fuchsia_settings::CaptionFontFamily::into),
182            color: src.color.map(fidl_fuchsia_ui_types::ColorRgba::into),
183            relative_size: src.relative_size,
184            char_edge_style: src.char_edge_style.map(fidl_fuchsia_settings::EdgeStyle::into),
185        }
186    }
187}
188
189impl From<CaptionFontStyle> for fidl_fuchsia_settings::CaptionFontStyle {
190    fn from(src: CaptionFontStyle) -> Self {
191        fidl_fuchsia_settings::CaptionFontStyle {
192            family: src.family.map(CaptionFontFamily::into),
193            color: src.color.map(ColorRgba::into),
194            relative_size: src.relative_size,
195            char_edge_style: src.char_edge_style.map(EdgeStyle::into),
196            ..Default::default()
197        }
198    }
199}
200
201/// Font family groups for closed captions, specified by 47 CFR §79.102(k).
202#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
203pub(crate) enum CaptionFontFamily {
204    Unknown,
205    MonospacedSerif,
206    ProportionalSerif,
207    MonospacedSansSerif,
208    ProportionalSansSerif,
209    Casual,
210    Cursive,
211    SmallCapitals,
212}
213
214impl From<fidl_fuchsia_settings::CaptionFontFamily> for CaptionFontFamily {
215    fn from(src: fidl_fuchsia_settings::CaptionFontFamily) -> Self {
216        match src {
217            fidl_fuchsia_settings::CaptionFontFamily::Unknown => CaptionFontFamily::Unknown,
218            fidl_fuchsia_settings::CaptionFontFamily::MonospacedSerif => {
219                CaptionFontFamily::MonospacedSerif
220            }
221            fidl_fuchsia_settings::CaptionFontFamily::ProportionalSerif => {
222                CaptionFontFamily::ProportionalSerif
223            }
224            fidl_fuchsia_settings::CaptionFontFamily::MonospacedSansSerif => {
225                CaptionFontFamily::MonospacedSansSerif
226            }
227            fidl_fuchsia_settings::CaptionFontFamily::ProportionalSansSerif => {
228                CaptionFontFamily::ProportionalSansSerif
229            }
230            fidl_fuchsia_settings::CaptionFontFamily::Casual => CaptionFontFamily::Casual,
231            fidl_fuchsia_settings::CaptionFontFamily::Cursive => CaptionFontFamily::Cursive,
232            fidl_fuchsia_settings::CaptionFontFamily::SmallCapitals => {
233                CaptionFontFamily::SmallCapitals
234            }
235        }
236    }
237}
238
239impl From<CaptionFontFamily> for fidl_fuchsia_settings::CaptionFontFamily {
240    fn from(src: CaptionFontFamily) -> Self {
241        match src {
242            CaptionFontFamily::Unknown => fidl_fuchsia_settings::CaptionFontFamily::Unknown,
243            CaptionFontFamily::MonospacedSerif => {
244                fidl_fuchsia_settings::CaptionFontFamily::MonospacedSerif
245            }
246            CaptionFontFamily::ProportionalSerif => {
247                fidl_fuchsia_settings::CaptionFontFamily::ProportionalSerif
248            }
249            CaptionFontFamily::MonospacedSansSerif => {
250                fidl_fuchsia_settings::CaptionFontFamily::MonospacedSansSerif
251            }
252            CaptionFontFamily::ProportionalSansSerif => {
253                fidl_fuchsia_settings::CaptionFontFamily::ProportionalSansSerif
254            }
255            CaptionFontFamily::Casual => fidl_fuchsia_settings::CaptionFontFamily::Casual,
256            CaptionFontFamily::Cursive => fidl_fuchsia_settings::CaptionFontFamily::Cursive,
257            CaptionFontFamily::SmallCapitals => {
258                fidl_fuchsia_settings::CaptionFontFamily::SmallCapitals
259            }
260        }
261    }
262}
263
264/// Edge style for fonts as specified in 47 CFR §79.103(c)(7)
265#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
266pub enum EdgeStyle {
267    /// No border around fonts.
268    None,
269
270    /// A shadow "behind" and slightly offset from each edge.
271    DropShadow,
272
273    /// A bevel that mimics a 3D raised effect.
274    Raised,
275
276    /// A bevel that mimics a 3D depressed effect.
277    Depressed,
278
279    /// A plain border around each shapes.
280    Outline,
281}
282
283impl From<fidl_fuchsia_settings::EdgeStyle> for EdgeStyle {
284    fn from(src: fidl_fuchsia_settings::EdgeStyle) -> Self {
285        match src {
286            fidl_fuchsia_settings::EdgeStyle::None => EdgeStyle::None,
287            fidl_fuchsia_settings::EdgeStyle::DropShadow => EdgeStyle::DropShadow,
288            fidl_fuchsia_settings::EdgeStyle::Raised => EdgeStyle::Raised,
289            fidl_fuchsia_settings::EdgeStyle::Depressed => EdgeStyle::Depressed,
290            fidl_fuchsia_settings::EdgeStyle::Outline => EdgeStyle::Outline,
291        }
292    }
293}
294
295impl From<EdgeStyle> for fidl_fuchsia_settings::EdgeStyle {
296    fn from(src: EdgeStyle) -> Self {
297        match src {
298            EdgeStyle::None => fidl_fuchsia_settings::EdgeStyle::None,
299            EdgeStyle::DropShadow => fidl_fuchsia_settings::EdgeStyle::DropShadow,
300            EdgeStyle::Raised => fidl_fuchsia_settings::EdgeStyle::Raised,
301            EdgeStyle::Depressed => fidl_fuchsia_settings::EdgeStyle::Depressed,
302            EdgeStyle::Outline => fidl_fuchsia_settings::EdgeStyle::Outline,
303        }
304    }
305}
306
307#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
308pub(crate) struct ColorRgba {
309    pub(crate) red: f32,
310    pub(crate) green: f32,
311    pub(crate) blue: f32,
312    pub(crate) alpha: f32,
313}
314
315impl ColorRgba {
316    pub(crate) fn is_finite(&self) -> bool {
317        self.red.is_finite()
318            && self.green.is_finite()
319            && self.blue.is_finite()
320            && self.alpha.is_finite()
321    }
322}
323
324impl From<fidl_fuchsia_ui_types::ColorRgba> for ColorRgba {
325    fn from(src: fidl_fuchsia_ui_types::ColorRgba) -> Self {
326        ColorRgba { red: src.red, green: src.green, blue: src.blue, alpha: src.alpha }
327    }
328}
329
330impl From<ColorRgba> for fidl_fuchsia_ui_types::ColorRgba {
331    fn from(src: ColorRgba) -> Self {
332        fidl_fuchsia_ui_types::ColorRgba {
333            red: src.red,
334            green: src.green,
335            blue: src.blue,
336            alpha: src.alpha,
337        }
338    }
339}