Skip to main content

settings_keyboard/
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 anyhow::{Error, anyhow};
6use serde::{Deserialize, Serialize};
7use settings_common::inspect::event::Nameable;
8
9#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
10pub struct KeyboardInfo {
11    pub(crate) keymap: Option<KeymapId>,
12    pub(crate) autorepeat: Option<Autorepeat>,
13}
14
15impl KeyboardInfo {
16    pub(crate) fn is_valid(&self) -> bool {
17        self.autorepeat.is_none_or(|x| x.is_valid())
18    }
19}
20
21impl Nameable for KeyboardInfo {
22    const NAME: &str = "Keyboard";
23}
24
25#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
26pub(crate) enum KeymapId {
27    /// The US_QWERTY keymap. This is also the default if no settings are
28    /// ever applied.
29    UsQwerty,
30
31    /// The FR AZERTY keymap.
32    FrAzerty,
33
34    /// The US Dvorak keymap.
35    UsDvorak,
36
37    /// The US Colemak keymap.
38    UsColemak,
39}
40
41impl TryFrom<fidl_fuchsia_input::KeymapId> for KeymapId {
42    type Error = Error;
43
44    fn try_from(src: fidl_fuchsia_input::KeymapId) -> Result<Self, Self::Error> {
45        match src {
46            fidl_fuchsia_input::KeymapId::UsQwerty => Ok(KeymapId::UsQwerty),
47            fidl_fuchsia_input::KeymapId::FrAzerty => Ok(KeymapId::FrAzerty),
48            fidl_fuchsia_input::KeymapId::UsDvorak => Ok(KeymapId::UsDvorak),
49            fidl_fuchsia_input::KeymapId::UsColemak => Ok(KeymapId::UsColemak),
50            _ => Err(anyhow!("Received an invalid keymap id: {src:?}.")),
51        }
52    }
53}
54
55impl From<KeymapId> for fidl_fuchsia_input::KeymapId {
56    fn from(src: KeymapId) -> Self {
57        match src {
58            KeymapId::UsQwerty => fidl_fuchsia_input::KeymapId::UsQwerty,
59            KeymapId::FrAzerty => fidl_fuchsia_input::KeymapId::FrAzerty,
60            KeymapId::UsDvorak => fidl_fuchsia_input::KeymapId::UsDvorak,
61            KeymapId::UsColemak => fidl_fuchsia_input::KeymapId::UsColemak,
62        }
63    }
64}
65
66#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
67pub(crate) struct Autorepeat {
68    /// The delay between key actuation and autorepeat actuation. Meaningful values are positive
69    /// integers. Zero means the field has been cleared.
70    pub delay: i64,
71    /// The period between two successive autorepeat actuations (1/rate). Meaningful values are
72    /// positive integers. Zero means the field has been cleared.
73    pub period: i64,
74}
75
76impl Autorepeat {
77    pub(crate) fn is_valid(&self) -> bool {
78        self.delay >= 0 && self.period >= 0
79    }
80}
81
82impl From<fidl_fuchsia_settings::Autorepeat> for Autorepeat {
83    fn from(src: fidl_fuchsia_settings::Autorepeat) -> Self {
84        Autorepeat { delay: src.delay, period: src.period }
85    }
86}
87
88impl From<Autorepeat> for fidl_fuchsia_settings::Autorepeat {
89    fn from(src: Autorepeat) -> Self {
90        fidl_fuchsia_settings::Autorepeat { delay: src.delay, period: src.period }
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97
98    #[fuchsia::test]
99    fn test_try_from_keymapid() {
100        assert!(KeymapId::try_from(fidl_fuchsia_input::KeymapId::UsQwerty).is_ok());
101
102        assert!(KeymapId::try_from(fidl_fuchsia_input::KeymapId::unknown()).is_err());
103
104        assert_eq!(
105            KeymapId::try_from(fidl_fuchsia_input::KeymapId::FrAzerty).unwrap(),
106            KeymapId::FrAzerty
107        );
108    }
109}