StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.
| 1 | use core::fmt; |
| 2 | use std::{ |
| 3 | collections::HashMap, |
| 4 | sync::atomic::{AtomicUsize, Ordering}, |
| 5 | }; |
| 6 | |
| 7 | use serde::{Deserialize, Serialize}; |
| 8 | |
| 9 | use crate::{core::view::AnyViewHandle, AnyView, EntityId}; |
| 10 | |
| 11 | /// A unique identifier for a window. |
| 12 | /// |
| 13 | /// These are globally unique and not reused across the lifetime of the |
| 14 | /// application. |
| 15 | #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] |
| 16 | pub struct WindowId(usize); |
| 17 | |
| 18 | impl WindowId { |
| 19 | /// Constructs a new globally-unique window ID. |
| 20 | #[allow(clippy::new_without_default)] |
| 21 | pub fn new() -> WindowId { |
| 22 | static NEXT_ID: AtomicUsize = AtomicUsize::new(0); |
| 23 | let raw = NEXT_ID.fetch_add(1, Ordering::Relaxed); |
| 24 | WindowId(raw) |
| 25 | } |
| 26 | |
| 27 | pub fn from_usize(value: usize) -> WindowId { |
| 28 | WindowId(value) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | impl fmt::Display for WindowId { |
| 33 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 34 | std::fmt::Display::fmt(&self.0, f) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /// A structure holding all application state that is linked to a particular |
| 39 | /// window. |
| 40 | #[derive(Default)] |
| 41 | pub(super) struct Window { |
| 42 | /// The set of views owned by this window, keyed by view ID. |
| 43 | pub views: HashMap<EntityId, Box<dyn AnyView>>, |
| 44 | |
| 45 | /// A handle to the window's root view (top of the view hierarchy), if any. |
| 46 | pub root_view: Option<AnyViewHandle>, |
| 47 | |
| 48 | /// The ID of the currently focused view, if any. |
| 49 | pub focused_view: Option<EntityId>, |
| 50 | } |
| 51 |