devela/sys/os/browser/web/event/key/
location.rs1use crate::{KeyMod, KeyMods};
11
12#[doc = crate::_tags!(interaction web)]
13#[doc = crate::_doc_meta!{location("sys/os/browser/web")}]
15#[repr(u8)]
18#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
19pub enum WebKeyLocation {
20 #[default]
22 Standard = 0,
23 Left = 1,
25 Right = 2,
27 NumPad = 3,
29}
30impl WebKeyLocation {
31 pub const fn from_repr(from: u8) -> Self {
33 use WebKeyLocation as L;
34 match from {
35 0 => L::Standard,
36 1 => L::Left,
37 2 => L::Right,
38 3 => L::NumPad,
39 _ => L::Standard,
40 }
41 }
42}
43
44#[rustfmt::skip]
45impl KeyMod {
46 pub const fn from_web_code(code: &str, location: WebKeyLocation) -> Option<Self> {
53 use {KeyMod as K, WebKeyLocation as L};
54 match (code.as_bytes(), location) {
55 (b"ShiftLeft", L::Left) => Some(K::LeftShift),
56 (b"ControlLeft", L::Left) => Some(K::LeftControl),
57 (b"AltLeft", L::Left) => Some(K::LeftAlt),
58 (b"MetaLeft", L::Left) => Some(K::LeftSuper),
59 (b"ShiftRight", L::Right) => Some(K::RightShift),
60 (b"ControlRight", L::Right) => Some(K::RightControl),
61 (b"AltRight", L::Right) => Some(K::RightAlt),
62 (b"MetaRight", L::Right) => Some(K::RightSuper),
63 (b"AltGraph", L::Standard) => Some(K::AltGr),
64 (b"Level5Shift", L::Standard) => Some(K::IsoLevel5Shift),
65 _ => None,
66 }
67 }
68 pub const fn to_web_code(self) -> (&'static str, WebKeyLocation) {
74 use {KeyMod as K, WebKeyLocation as L};
75 match self {
76 K::LeftShift => ("ShiftLeft", L::Left),
77 K::LeftControl => ("ControlLeft", L::Left),
78 K::LeftAlt => ("AltLeft", L::Left),
79 K::LeftSuper => ("MetaLeft", L::Left),
80 K::RightShift => ("ShiftRight", L::Right),
81 K::RightControl => ("ControlRight", L::Right),
82 K::RightAlt => ("AltRight", L::Right),
83 K::RightSuper => ("MetaRight", L::Right),
84 K::AltGr => ("AltGraph", L::Standard),
85 K::IsoLevel5Shift => ("Level5Shift", L::Standard),
86 }
87 }
88 pub const fn from_web_key(key: &str, location: WebKeyLocation) -> Option<Self> {
94 use {KeyMod as K, WebKeyLocation as L};
95 match (key.as_bytes(), location) {
96 (b"Shift", L::Left) => Some(K::LeftShift),
97 (b"Control", L::Left) => Some(K::LeftControl),
98 (b"Alt", L::Left) => Some(K::LeftAlt),
99 (b"Meta", L::Left) => Some(K::LeftSuper),
100 (b"Shift", L::Right) => Some(K::RightShift),
101 (b"Control", L::Right) => Some(K::RightControl),
102 (b"Alt", L::Right) => Some(K::RightAlt),
103 (b"Meta", L::Right) => Some(K::RightSuper),
104 (b"AltGraph", L::Standard) => Some(K::AltGr),
105 (b"Level5Shift", L::Standard) => Some(K::IsoLevel5Shift),
106 _ => None,
107 }
108 }
109 pub const fn to_web_key(self) -> (&'static str, WebKeyLocation) {
114 use {KeyMod as K, WebKeyLocation as L};
115 match self {
116 K::LeftShift => ("Shift", L::Left),
117 K::LeftControl => ("Control", L::Left),
118 K::LeftAlt => ("Alt", L::Left),
119 K::LeftSuper => ("Meta", L::Left),
120 K::RightShift => ("Shift", L::Right),
121 K::RightControl => ("Control", L::Right),
122 K::RightAlt => ("Alt", L::Right),
123 K::RightSuper => ("Meta", L::Right),
124 K::AltGr => ("AltGraph", L::Standard),
125 K::IsoLevel5Shift => ("Level5Shift", L::Standard),
126 }
127 }
128}
129
130impl KeyMods {
131 pub const fn from_web(bits: u8) -> Self {
143 Self::from_bits(bits as u16)
144 }
145
146 pub const fn to_web(self) -> u8 {
150 (self.bits() & 0x00FF) as u8
151 }
152}