1use std::path::Path;
11use std::sync::Once;
12
13use platform::system::{DialogKind, DialogOptions, FileFilter};
14use platform::{LineEdgeKeys, ModifierKey, PlatformBackend, RedoKeys};
15use scene::Color;
16
17use aureole_core::host::SystemBackend;
18use aureole_core::{Appearance, Conventions, Date, DisplayInfo, FileDialogDone, FileDialogKind, FileDialogRequest, Modifiers, Time};
19use winit::raw_window_handle::HasWindowHandle;
20
21pub(crate) fn backend() -> &'static dyn PlatformBackend {
23 platform::native()
24}
25
26struct PlatformSystem;
28
29impl SystemBackend for PlatformSystem {
30 fn appearance(&self) -> Appearance {
31 detect()
32 }
33
34 fn is_motion_reduced(&self) -> bool {
35 backend().appearance().is_motion_reduced()
36 }
37
38 fn accent_color(&self) -> Option<Color> {
39 detect_accent()
40 }
41
42 fn read_clipboard(&self) -> Option<String> {
43 backend().clipboard().read_text().unwrap_or_else(|error| {
44 tracing::debug!("读不了剪贴板:{error}");
45 None
46 })
47 }
48
49 fn write_clipboard(&self, text: String) -> bool {
50 backend().clipboard().write_text(&text).map_err(|error| tracing::warn!("写不进剪贴板:{error}")).is_ok()
51 }
52
53 fn open_url(&self, url: &str) -> bool {
54 backend().shell().open_url(url).map_err(|error| tracing::warn!(%url, "打不开网址:{error}")).is_ok()
55 }
56
57 fn conventions(&self) -> Conventions {
58 conventions(backend().conventions())
59 }
60
61 fn file_dialog(&self, request: FileDialogRequest, done: FileDialogDone) {
62 let parent = crate::window::current_window()
65 .or_else(|| if cfg!(target_os = "macos") { None } else { crate::window::front_window() })
66 .and_then(crate::window::native);
67 backend().dialog().file_dialog(dialog_options(request), parent.as_deref().map(|w| w as &dyn HasWindowHandle), done);
68 }
69
70 fn displays(&self) -> Vec<DisplayInfo> {
73 let Some(window) = crate::window::front_window().and_then(crate::window::native) else { return Vec::new() };
74 let primary = window.primary_monitor();
75 let mut displays: Vec<DisplayInfo> = window
76 .available_monitors()
77 .map(|monitor| {
78 let scale = monitor.scale_factor();
79 let position = monitor.position().to_logical::<f32>(scale);
80 let size = monitor.size().to_logical::<f32>(scale);
81 DisplayInfo {
82 name: monitor.name().unwrap_or_default(),
83 position: [position.x, position.y],
84 size: [size.width, size.height],
85 scale_factor: scale as f32,
86 primary: primary.as_ref() == Some(&monitor),
87 }
88 })
89 .collect();
90 displays.sort_by_key(|display| !display.primary);
91 displays
92 }
93
94 fn local_time(&self) -> Option<(Date, Time)> {
95 let [year, month, day, hour, minute, second] = platform::system::local_time()?;
96 let time = Time::checked(u8::try_from(hour).ok()?, u8::try_from(minute).ok()?, u8::try_from(second).ok()?.min(59))?;
98 Some((Date::checked(year, u8::try_from(month).ok()?, u8::try_from(day).ok()?)?, time))
99 }
100}
101
102pub(crate) fn dialog_options(request: FileDialogRequest) -> DialogOptions {
104 DialogOptions {
105 kind: match request.kind {
106 FileDialogKind::OpenFile => DialogKind::OpenFile,
107 FileDialogKind::OpenDirectory => DialogKind::OpenDirectory,
108 FileDialogKind::Save => DialogKind::Save,
109 },
110 multiple: request.multiple,
111 title: request.title,
112 directory: request.directory,
113 file_name: request.file_name,
114 filters: request.filters.into_iter().map(|f| FileFilter { name: f.name, extensions: f.extensions }).collect(),
115 }
116}
117
118pub fn capabilities() -> platform::Capabilities {
128 platform::with_backend(|backend| backend.capabilities())
129}
130
131fn conventions(platform: &platform::Conventions) -> Conventions {
133 let modifier = |key| match key {
134 ModifierKey::Shift => Modifiers::SHIFT,
135 ModifierKey::Control => Modifiers::CONTROL,
136 ModifierKey::Alt => Modifiers::ALT,
137 ModifierKey::Command => Modifiers::SUPER,
138 };
139 Conventions {
140 primary_modifier: modifier(platform.primary_modifier),
141 word_modifier: modifier(platform.word_modifier),
142 uses_arrows_for_line_edges: platform.line_edge == LineEdgeKeys::PrimaryArrows,
143 accepts_y_for_redo: platform.redo == RedoKeys::YOrShiftZ,
144 wheel_line_height: platform.wheel_line_height,
145 wheel_notch: platform.wheel_notch,
146 wheel_scrolls_pages: platform.wheel_scrolls_pages,
147 fractional_wheel_is_precise: platform.fractional_wheel_is_precise,
148 shift_wheel_scrolls_horizontally: platform.shift_wheel_scrolls_horizontally,
149 control_wheel_zooms: platform.control_wheel_zooms,
150 treats_control_click_as_secondary: platform.treats_control_click_as_secondary,
151 long_press_duration: platform.long_press_duration,
152 double_click_interval: platform.double_click_interval,
153 double_click_distance: platform.double_click_distance,
154 drag_threshold: platform.drag_threshold,
155 word_forward_skips_spaces: platform.word_forward_skips_spaces,
156 accepts_insert_for_clipboard: platform.accepts_insert_for_clipboard,
157 pads_ime_area: platform.pads_ime_area,
158 }
159}
160
161pub(crate) fn install() {
163 static INSTALLED: Once = Once::new();
164 INSTALLED.call_once(|| aureole_core::host::set_system_backend(PlatformSystem));
165}
166
167fn detect() -> Appearance {
168 if backend().appearance().is_dark_mode() == Some(true) { Appearance::Dark } else { Appearance::Light }
169}
170
171fn detect_accent() -> Option<Color> {
172 backend().appearance().accent_color().map(|[r, g, b]| Color::rgb(r, g, b))
173}
174
175pub(crate) fn reported_appearance(theme: winit::window::Theme) -> Appearance {
178 match backend().appearance().is_dark_mode() {
179 Some(true) => Appearance::Dark,
180 Some(false) => Appearance::Light,
181 None if theme == winit::window::Theme::Dark => Appearance::Dark,
182 None => Appearance::Light,
183 }
184}
185
186pub(crate) fn refresh_settings() {
188 if let Some(dark) = backend().appearance().is_dark_mode() {
190 aureole_core::host::set_system_appearance(if dark { Appearance::Dark } else { Appearance::Light });
191 }
192 aureole_core::host::set_motion_reduced(backend().appearance().is_motion_reduced());
193 aureole_core::host::set_system_accent_color(detect_accent());
194}
195
196pub(crate) fn settings_changed() {
198 aureole_core::host::set_system_appearance(detect());
199 refresh_settings();
200}
201
202pub fn system_appearance() -> Appearance {
208 install();
209 aureole_core::system_appearance()
210}
211
212pub fn appearance() -> Appearance {
215 install();
216 aureole_core::appearance()
217}
218
219pub fn is_motion_reduced() -> bool {
227 install();
228 aureole_core::is_motion_reduced()
229}
230
231pub fn is_low_power_mode() -> bool {
235 static FORCED: std::sync::OnceLock<Option<bool>> = std::sync::OnceLock::new();
236 let forced = FORCED.get_or_init(|| match std::env::var("AUREOLE_LOW_POWER").as_deref() {
237 Ok("1") => Some(true),
238 Ok("0") => Some(false),
239 _ => None,
240 });
241 forced.unwrap_or_else(|| backend().environment().is_low_power_mode())
242}
243
244pub fn system_accent_color() -> Option<Color> {
250 install();
251 aureole_core::system_accent_color()
252}
253
254pub fn displays() -> Vec<DisplayInfo> {
259 install();
260 aureole_core::displays()
261}
262
263pub fn open_url(url: &str) -> bool {
266 install();
267 aureole_core::open_url(url)
268}
269
270pub fn open_path(path: impl AsRef<Path>) -> bool {
273 let path = path.as_ref();
274 backend().shell().open_path(path).map_err(|error| tracing::warn!(path = %path.display(), "打不开:{error}")).is_ok()
275}
276
277pub fn reveal_path(path: impl AsRef<Path>) -> bool {
281 let path = path.as_ref();
282 backend().shell().reveal_path(path).map_err(|error| tracing::warn!(path = %path.display(), "没能在文件管理器里显示:{error}")).is_ok()
283}
284
285pub fn beep() -> bool {
289 platform::with_backend(|backend| backend.notification().beep()).map_err(|error| tracing::debug!("没有提示音:{error}")).is_ok()
290}
291
292pub fn notify(title: &str, body: &str) -> bool {
299 match platform::with_backend(|backend| backend.notification().notify(title, body)) {
300 Ok(()) => true,
301 Err(error) => {
302 tracing::info!(%title, %body, "系统通知不可用({}),通知的内容记在这里", error.reason());
304 false
305 }
306 }
307}