1use crate::Result;
2#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
3use crate::SystemIntegrationError;
4
5#[cfg(target_os = "linux")]
6#[path = "preferences/linux.rs"]
7mod platform;
8#[cfg(target_os = "macos")]
9#[path = "preferences/macos.rs"]
10mod platform;
11#[cfg(target_os = "windows")]
12#[path = "preferences/windows.rs"]
13mod platform;
14#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
15mod platform {
16 use super::*;
17
18 pub(super) fn system_preferences() -> Result<SystemPreferences> {
19 Ok(SystemPreferences::default())
20 }
21
22 pub(super) fn permission_status(_kind: PermissionKind) -> Result<PermissionStatus> {
23 Ok(PermissionStatus::Unknown)
24 }
25
26 pub(super) fn request_permission(
27 _kind: PermissionKind,
28 _completion: impl FnOnce(Result<PermissionStatus>) + Send + 'static,
29 ) -> Result<()> {
30 Err(SystemIntegrationError::Unsupported)
31 }
32}
33
34#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
36pub enum ColorScheme {
37 Light,
38 Dark,
39 #[default]
40 Unknown,
41}
42
43#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
45pub struct SystemColor {
46 pub red: u8,
47 pub green: u8,
48 pub blue: u8,
49 pub alpha: u8,
50}
51
52impl SystemColor {
53 pub const fn rgba(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
54 Self {
55 red,
56 green,
57 blue,
58 alpha,
59 }
60 }
61
62 pub const fn rgb(red: u8, green: u8, blue: u8) -> Self {
63 Self::rgba(red, green, blue, 255)
64 }
65}
66
67#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
69#[repr(u8)]
70pub enum SystemColorRole {
71 Accent,
72 Highlight,
73 HighlightText,
74 WindowBackground,
75 WindowText,
76 ControlBackground,
77 ControlText,
78 Link,
79}
80
81impl SystemColorRole {
82 const COUNT: usize = 8;
83
84 const fn index(self) -> usize {
85 self as usize
86 }
87}
88
89#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
93pub struct SystemPreferences {
94 color_scheme: ColorScheme,
95 reduce_motion: Option<bool>,
96 reduce_transparency: Option<bool>,
97 increase_contrast: Option<bool>,
98 differentiate_without_color: Option<bool>,
99 invert_colors: Option<bool>,
100 forced_colors: Option<bool>,
101 screen_reader: Option<bool>,
102 switch_control: Option<bool>,
103 system_colors: [Option<SystemColor>; SystemColorRole::COUNT],
104}
105
106impl SystemPreferences {
107 pub fn snapshot() -> Result<Self> {
108 platform::system_preferences()
109 }
110
111 #[allow(clippy::too_many_arguments)]
112 pub(crate) const fn new(
113 color_scheme: ColorScheme,
114 reduce_motion: Option<bool>,
115 reduce_transparency: Option<bool>,
116 increase_contrast: Option<bool>,
117 differentiate_without_color: Option<bool>,
118 invert_colors: Option<bool>,
119 screen_reader: Option<bool>,
120 switch_control: Option<bool>,
121 ) -> Self {
122 Self {
123 color_scheme,
124 reduce_motion,
125 reduce_transparency,
126 increase_contrast,
127 differentiate_without_color,
128 invert_colors,
129 forced_colors: None,
130 screen_reader,
131 switch_control,
132 system_colors: [None; SystemColorRole::COUNT],
133 }
134 }
135
136 pub const fn color_scheme(self) -> ColorScheme {
137 self.color_scheme
138 }
139
140 pub const fn with_color_scheme(mut self, color_scheme: ColorScheme) -> Self {
141 self.color_scheme = color_scheme;
142 self
143 }
144
145 pub const fn with_reduce_motion(mut self, reduce_motion: Option<bool>) -> Self {
146 self.reduce_motion = reduce_motion;
147 self
148 }
149
150 pub const fn with_reduce_transparency(mut self, reduce_transparency: Option<bool>) -> Self {
151 self.reduce_transparency = reduce_transparency;
152 self
153 }
154
155 pub const fn with_increase_contrast(mut self, increase_contrast: Option<bool>) -> Self {
156 self.increase_contrast = increase_contrast;
157 self
158 }
159
160 pub const fn with_differentiate_without_color(
161 mut self,
162 differentiate_without_color: Option<bool>,
163 ) -> Self {
164 self.differentiate_without_color = differentiate_without_color;
165 self
166 }
167
168 pub const fn with_invert_colors(mut self, invert_colors: Option<bool>) -> Self {
169 self.invert_colors = invert_colors;
170 self
171 }
172
173 pub const fn with_forced_colors(mut self, forced_colors: Option<bool>) -> Self {
174 self.forced_colors = forced_colors;
175 self
176 }
177
178 pub const fn with_system_color(
179 mut self,
180 role: SystemColorRole,
181 color: Option<SystemColor>,
182 ) -> Self {
183 self.system_colors[role.index()] = color;
184 self
185 }
186
187 pub const fn with_screen_reader(mut self, screen_reader: Option<bool>) -> Self {
188 self.screen_reader = screen_reader;
189 self
190 }
191
192 pub const fn with_switch_control(mut self, switch_control: Option<bool>) -> Self {
193 self.switch_control = switch_control;
194 self
195 }
196
197 pub const fn reduce_motion(self) -> Option<bool> {
198 self.reduce_motion
199 }
200
201 pub const fn reduce_transparency(self) -> Option<bool> {
202 self.reduce_transparency
203 }
204
205 pub const fn increase_contrast(self) -> Option<bool> {
206 self.increase_contrast
207 }
208
209 pub const fn differentiate_without_color(self) -> Option<bool> {
210 self.differentiate_without_color
211 }
212
213 pub const fn invert_colors(self) -> Option<bool> {
214 self.invert_colors
215 }
216
217 pub const fn forced_colors(self) -> Option<bool> {
218 self.forced_colors
219 }
220
221 pub const fn system_color(self, role: SystemColorRole) -> Option<SystemColor> {
222 self.system_colors[role.index()]
223 }
224
225 pub const fn accent_color(self) -> Option<SystemColor> {
226 self.system_color(SystemColorRole::Accent)
227 }
228
229 pub const fn screen_reader(self) -> Option<bool> {
230 self.screen_reader
231 }
232
233 pub const fn switch_control(self) -> Option<bool> {
234 self.switch_control
235 }
236}
237
238#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
240pub enum PermissionKind {
241 Camera,
242 Microphone,
243 ScreenRecording,
244 Accessibility,
245}
246
247#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
249pub enum PermissionStatus {
250 NotDetermined,
251 Granted,
252 Denied,
253 Restricted,
254 Unknown,
255}
256
257pub struct PermissionManager;
259
260impl PermissionManager {
261 pub fn status(kind: PermissionKind) -> Result<PermissionStatus> {
262 platform::permission_status(kind)
263 }
264
265 pub fn request(
271 kind: PermissionKind,
272 completion: impl FnOnce(Result<PermissionStatus>) + Send + 'static,
273 ) -> Result<()> {
274 platform::request_permission(kind, completion)
275 }
276}
277
278#[cfg(test)]
279mod tests {
280 use super::*;
281
282 #[test]
283 fn unknown_preferences_preserve_unsupported_fields() {
284 let preferences = SystemPreferences::default();
285 assert_eq!(preferences.color_scheme(), ColorScheme::Unknown);
286 assert_eq!(preferences.reduce_motion(), None);
287 assert_eq!(preferences.reduce_transparency(), None);
288 assert_eq!(preferences.increase_contrast(), None);
289 assert_eq!(preferences.differentiate_without_color(), None);
290 assert_eq!(preferences.invert_colors(), None);
291 assert_eq!(preferences.forced_colors(), None);
292 assert_eq!(preferences.accent_color(), None);
293 assert_eq!(preferences.screen_reader(), None);
294 assert_eq!(preferences.switch_control(), None);
295 }
296}