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