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/windowing/system.rs
1//! Module containing the definition of the current windowing [`System`] the application is
2//! rendering to.
3 
4use std::fmt::{Display, Formatter};
5 
6use raw_window_handle::RawDisplayHandle;
7 
8/// The windowing system that is being used.
9#[derive(Copy, Clone, Debug)]
10pub enum System {
11 X11 { is_x_wayland: bool },
12 Wayland,
13 AppKit,
14 Windows,
15}
16 
17impl System {
18 /// Whether this window server protocol allows windows to programmatically "activate" or
19 /// show/focus themselves.
20 pub fn allows_programmatic_window_activation(&self) -> bool {
21 match self {
22 Self::AppKit | Self::X11 { .. } | Self::Windows => true,
23 Self::Wayland => false,
24 }
25 }
26}
27 
28impl Display for System {
29 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
30 match self {
31 System::X11 { is_x_wayland } => {
32 if *is_x_wayland {
33 write!(f, "Xwayland")
34 } else {
35 write!(f, "X11")
36 }
37 }
38 System::Wayland => write!(f, "Wayland"),
39 System::AppKit => write!(f, "AppKit"),
40 System::Windows => write!(f, "Windows"),
41 }
42 }
43}
44 
45impl TryFrom<RawDisplayHandle> for System {
46 type Error = CreateWindowingSystemError;
47 
48 fn try_from(raw_display_handle: RawDisplayHandle) -> Result<Self, Self::Error> {
49 let display = match raw_display_handle {
50 RawDisplayHandle::AppKit(_) => System::AppKit,
51 RawDisplayHandle::Wayland(_) => System::Wayland,
52 RawDisplayHandle::Xlib(_) | RawDisplayHandle::Xcb(_) => System::X11 {
53 is_x_wayland: std::env::var("WAYLAND_DISPLAY")
54 .ok()
55 .filter(|val| !val.is_empty())
56 .is_some(),
57 },
58 RawDisplayHandle::Windows(_) => System::Windows,
59 _ => {
60 return Err(Self::Error::UnrecognizedDisplayHandle);
61 }
62 };
63 
64 Ok(display)
65 }
66}
67 
68#[derive(thiserror::Error, Debug)]
69pub enum CreateWindowingSystemError {
70 #[error("Unrecognized DisplayHandle")]
71 UnrecognizedDisplayHandle,
72}
73