Skip to main content

freya_core/
platform.rs

1use std::rc::Rc;
2
3pub use mundy::{
4    AccentColor,
5    Srgba,
6};
7use torin::prelude::Size2D;
8
9use crate::{
10    accessibility::id::AccessibilityId,
11    prelude::{
12        State,
13        consume_root_context,
14    },
15    user_event::UserEvent,
16};
17
18#[derive(Clone, Copy, PartialEq, Eq, Default, Debug, Hash)]
19pub enum NavigationMode {
20    #[default]
21    NotKeyboard,
22    Keyboard,
23}
24
25#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
26pub enum PreferredTheme {
27    #[default]
28    Light,
29    Dark,
30}
31
32/// Access point to different Freya-managed states such as the focused node,
33/// root window size, navigation mode, and theme preference.
34///
35/// Retrieve it from any component with [`Platform::get`].
36#[derive(Clone)]
37pub struct Platform {
38    /// The [`AccessibilityId`] of the currently focused node.
39    pub focused_accessibility_id: State<AccessibilityId>,
40    /// The accessibility node data of the currently focused node.
41    pub focused_accessibility_node: State<accesskit::Node>,
42    /// The size of the root window.
43    pub root_size: State<Size2D>,
44    /// The current [`NavigationMode`].
45    pub navigation_mode: State<NavigationMode>,
46    /// The OS-level [`PreferredTheme`].
47    pub preferred_theme: State<PreferredTheme>,
48    /// Whether the app currently has the OS-level focus.
49    pub is_app_focused: State<bool>,
50    /// The OS-level [`AccentColor`].
51    pub accent_color: State<AccentColor>,
52    /// Sender used to dispatch [`UserEvent`]s to the active renderer.
53    pub sender: Rc<dyn Fn(UserEvent)>,
54}
55
56impl Platform {
57    /// Retrieve the [`Platform`] from the root context.
58    pub fn get() -> Self {
59        consume_root_context()
60    }
61
62    /// Dispatch a [`UserEvent`] to the active renderer.
63    pub fn send(&self, event: UserEvent) {
64        (self.sender)(event)
65    }
66}