Seregon/StratoSDK

StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.

Rust/27.3 KB/No license
crates/strato-ui-core/src/core/window.rs
StratoSDK / crates / strato-ui-core / src / core / window.rs
1use core::fmt;
2use std::{
3 collections::HashMap,
4 sync::atomic::{AtomicUsize, Ordering},
5};
6 
7use serde::{Deserialize, Serialize};
8 
9use 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)]
16pub struct WindowId(usize);
17 
18impl 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 
32impl 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)]
41pub(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