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-platform/src/lib.rs
1//! Platform abstraction layer for StratoUI framework
2//!
3//! Provides cross-platform window management and event handling.
4 
5pub mod application;
6pub mod event_loop;
7pub mod window;
8 
9#[cfg(not(target_arch = "wasm32"))]
10pub mod desktop;
11 
12#[cfg(target_arch = "wasm32")]
13pub mod web;
14 
15pub use application::{Application, ApplicationBuilder};
16pub use event_loop::{EventLoop, EventLoopProxy};
17pub use window::{Window, WindowBuilder, WindowId};
18 
19use strato_core::event::Event;
20 
21/// Platform-specific error type
22#[derive(Debug, thiserror::Error)]
23pub enum PlatformError {
24 #[error("Window creation failed: {0}")]
25 WindowCreation(String),
26 
27 #[error("Event loop error: {0}")]
28 EventLoop(String),
29 
30 #[error("Platform not supported")]
31 Unsupported,
32 
33 #[error("WebAssembly error: {0}")]
34 #[cfg(target_arch = "wasm32")]
35 Wasm(String),
36}
37 
38/// Platform trait for OS-specific implementations
39pub trait Platform {
40 /// Initialize the platform
41 fn init() -> Result<Self, PlatformError>
42 where
43 Self: Sized;
44 
45 /// Create a window
46 fn create_window(&mut self, builder: WindowBuilder) -> Result<Window, PlatformError>;
47 
48 /// Run the event loop
49 fn run_event_loop(
50 &mut self,
51 callback: Box<dyn FnMut(Event) + 'static>,
52 ) -> Result<(), PlatformError>;
53 
54 /// Request a redraw
55 fn request_redraw(&self, window_id: WindowId);
56 
57 /// Exit the application
58 fn exit(&mut self);
59}
60 
61/// Get the current platform implementation
62pub fn current_platform() -> Box<dyn Platform> {
63 #[cfg(not(target_arch = "wasm32"))]
64 {
65 Box::new(desktop::DesktopPlatform::new())
66 }
67 
68 #[cfg(target_arch = "wasm32")]
69 {
70 Box::new(web::WebPlatform::new())
71 }
72}
73 
74pub mod init;
75 
76/// Initialize the platform layer
77pub fn init() -> Result<(), PlatformError> {
78 tracing::info!("StratoUI Platform initialized");
79 
80 #[cfg(target_arch = "wasm32")]
81 {
82 console_error_panic_hook::set_once();
83 }
84 
85 Ok(())
86}
87