Skip to main content

freya_core/
platform.rs

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