1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use std::{
borrow::Cow,
cell::RefCell,
rc::Rc,
};
use bytes::Bytes;
pub use mundy::{
AccentColor,
Srgba,
};
use rustc_hash::FxHashMap;
use torin::prelude::Size2D;
use crate::{
accessibility::id::AccessibilityId,
current_context::CurrentContext,
prelude::{
GlobalContexts,
State,
try_consume_root_context,
},
user_event::{
GlobalUserEvent,
UserEvent,
},
};
/// How the user is navigating the application.
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug, Hash)]
pub enum NavigationMode {
/// Navigation is driven by a pointing device or touch input.
#[default]
NotKeyboard,
/// Navigation is driven by keyboard input.
Keyboard,
}
/// The color theme preferred by the operating system.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum PreferredTheme {
/// The operating system prefers a light color theme.
#[default]
Light,
/// The operating system prefers a dark color theme.
Dark,
}
/// Operating system targeted by an application.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TargetPlatform {
/// Microsoft Windows.
Windows,
/// Apple macOS.
MacOs,
/// Linux.
Linux,
/// Google Android.
Android,
/// Apple iOS.
Ios,
/// An operating system Freya does not recognize.
Unknown,
}
impl TargetPlatform {
/// Returns the target platform from the current Freya context, or detects the compile target.
pub fn get() -> Self {
CurrentContext::try_with(|_| try_consume_root_context())
.flatten()
.unwrap_or_else(Self::detect)
}
/// Detects the operating system of the compile target.
pub fn detect() -> Self {
if cfg!(target_os = "windows") {
Self::Windows
} else if cfg!(target_os = "macos") {
Self::MacOs
} else if cfg!(target_os = "linux") {
Self::Linux
} else if cfg!(target_os = "android") {
Self::Android
} else if cfg!(target_os = "ios") {
Self::Ios
} else {
Self::Unknown
}
}
/// Returns whether this is a desktop operating system.
pub fn is_desktop(&self) -> bool {
matches!(self, Self::Windows | Self::MacOs | Self::Linux)
}
/// Returns whether this is a mobile operating system.
pub fn is_mobile(&self) -> bool {
matches!(self, Self::Android | Self::Ios)
}
}
/// Application-wide platform api.
#[derive(Clone)]
pub struct Platform {
sender: Rc<dyn Fn(GlobalUserEvent)>,
windows: Rc<RefCell<FxHashMap<u64, PlatformWindow>>>,
}
impl Platform {
pub fn new(sender: impl Fn(GlobalUserEvent) + 'static) -> Self {
Self {
sender: Rc::new(sender),
windows: Rc::default(),
}
}
#[track_caller]
pub fn get() -> Self {
GlobalContexts::get().get_context()
}
/// Returns the live window with this ID, panicking if it has closed or does not exist.
#[track_caller]
pub fn window(&self, window_id: impl Into<u64>) -> PlatformWindow {
let window_id = window_id.into();
self.try_window(window_id)
.unwrap_or_else(|| panic!("Window {window_id} does not exist or has closed."))
}
/// Returns the live window with this ID, or `None` if it has closed or does not exist.
pub fn try_window(&self, window_id: impl Into<u64>) -> Option<PlatformWindow> {
self.windows.borrow().get(&window_id.into()).cloned()
}
/// Returns the window from whose component, event handler, or scoped task this is called.
#[track_caller]
pub fn current_window(&self) -> PlatformWindow {
let window_id: CurrentWindowId = CurrentContext::try_with(|_| try_consume_root_context())
.flatten()
.expect(concat!(
"Platform::current_window() requires a Freya window context. ",
"In `spawn_global`, use `Platform::get().window(window_id)` or capture ",
"the current window before spawning."
));
self.window(window_id.0)
}
pub fn register_window(&self, window: PlatformWindow) {
self.windows.borrow_mut().insert(window.id, window);
}
pub fn unregister_window(&self, window_id: impl Into<u64>) {
let _removed_window = self.windows.borrow_mut().remove(&window_id.into());
}
pub fn send(&self, event: GlobalUserEvent) {
(self.sender)(event)
}
/// Open a URL with the system's default browser.
pub fn open_url(&self, url: impl Into<String>) {
self.send(GlobalUserEvent::OpenUrl(url.into()));
}
/// Request application exit.
pub fn exit(&self) {
self.send(GlobalUserEvent::Exit);
}
/// Load a font at runtime in every window.
///
/// # Example
///
/// ```rust,no_run
/// use freya_core::prelude::*;
///
/// fn load_rubik() {
/// let font = std::fs::read("./Rubik.ttf").expect("Failed to read the font file.");
/// Platform::get().load_font("Rubik", font);
/// }
/// ```
pub fn load_font(&self, font_name: impl Into<Cow<'static, str>>, font_data: impl Into<Bytes>) {
self.send(GlobalUserEvent::LoadFont {
font_name: font_name.into(),
font_data: font_data.into(),
});
}
}
/// The ID of the window whose component context is currently executing.
#[derive(Clone, Copy)]
pub struct CurrentWindowId(pub u64);
/// State and APIs for a given window.
///
/// # Example
///
/// ```rust,no_run
/// use freya_core::prelude::*;
///
/// fn app() -> impl IntoElement {
/// let window = Platform::get().current_window();
/// let root_size = *window.root_size.read();
/// let preferred_theme = *window.preferred_theme.read();
///
/// format!(
/// "Window: {:.0} × {:.0}, preferred theme: {preferred_theme:?}",
/// root_size.width, root_size.height,
/// )
/// }
/// ```
#[derive(Clone)]
pub struct PlatformWindow {
pub id: u64,
/// The [`AccessibilityId`] of the currently focused node in this window.
pub focused_accessibility_id: State<AccessibilityId>,
/// Accessibility data for this window's currently focused node.
pub focused_accessibility_node: State<accesskit::Node>,
/// This window's size in logical pixels.
pub root_size: State<Size2D>,
/// This window's effective rendering scale factor.
pub scale_factor: State<f64>,
/// The custom scale factor, changed through [`Self::set_custom_scale_factor`].
pub custom_scale_factor: State<f64>,
/// The current [`NavigationMode`] in this window.
pub navigation_mode: State<NavigationMode>,
/// The color theme preferred by the operating system for this window.
pub preferred_theme: State<PreferredTheme>,
/// Whether this window has operating-system focus.
pub is_app_focused: State<bool>,
/// The operating system's accent color, when available.
pub accent_color: State<AccentColor>,
/// Dispatches [`UserEvent`]s to this window's renderer.
pub sender: Rc<dyn Fn(UserEvent)>,
}
impl PlatformWindow {
pub fn send(&self, event: UserEvent) {
(self.sender)(event)
}
/// Request a render of this window.
pub fn request_redraw(&self) {
self.send(UserEvent::RequestRedraw);
}
/// Request a custom rendering scale factor for this window.
pub fn set_custom_scale_factor(&self, custom_scale_factor: f64) {
self.send(UserEvent::SetCustomScaleFactor(custom_scale_factor));
}
}