use crate::platform::ControlFlow;
use crate::platform::event::Event;
use crate::platform::window::{Window, WindowAttributes};
pub mod headless;
#[cfg(target_os = "linux")]
pub mod wayland;
#[cfg(target_os = "linux")]
pub mod x11;
#[cfg(target_os = "windows")]
pub mod windows;
#[cfg(target_os = "macos")]
pub mod macos;
#[cfg(target_os = "ios")]
pub mod ios;
#[cfg(target_os = "android")]
pub mod android;
#[cfg(target_arch = "wasm32")]
#[allow(unsafe_code)]
pub mod web;
fn with_theme_changes<H>(mut handler: H) -> impl FnMut(Event, &dyn Window) -> ControlFlow
where
H: FnMut(Event, &dyn Window) -> ControlFlow,
{
move |event, window| {
if matches!(event, Event::Wake) {
if crate::platform::native::take_changed()
&& handler(Event::ThemeChanged, window) == ControlFlow::Exit
{
return ControlFlow::Exit;
}
for posted in crate::platform::shell::take_posted() {
if handler(posted, window) == ControlFlow::Exit {
return ControlFlow::Exit;
}
}
}
handler(event, window)
}
}
#[cfg_attr(
not(any(
target_os = "linux",
target_os = "windows",
target_os = "macos",
target_os = "android"
)),
allow(unused_mut)
)]
pub fn run<H>(attrs: WindowAttributes, handler: H)
where
H: FnMut(Event, &dyn Window) -> ControlFlow + 'static,
{
let mut handler = with_theme_changes(handler);
#[cfg(target_arch = "wasm32")]
{
web::run(attrs, handler);
}
#[cfg(target_os = "ios")]
{
let _ = ios::run(attrs, handler);
}
#[cfg(not(any(target_os = "ios", target_arch = "wasm32")))]
{
#[cfg(target_os = "linux")]
{
if wayland::available() {
match wayland::run(attrs.clone(), &mut handler) {
Ok(()) => return,
Err(err) => {
eprintln!(
"stipple: Wayland backend unavailable ({err}); falling back to X11/headless"
);
}
}
}
if x11::available() {
match x11::run(attrs.clone(), &mut handler) {
Ok(()) => return,
Err(err) => {
eprintln!(
"stipple: X11 backend unavailable ({err}); falling back to headless"
);
}
}
}
}
#[cfg(target_os = "windows")]
{
match windows::run(attrs.clone(), &mut handler) {
Ok(()) => return,
Err(err) => {
eprintln!(
"stipple: Windows backend unavailable ({err}); falling back to headless"
);
}
}
}
#[cfg(target_os = "macos")]
{
match macos::run(attrs.clone(), &mut handler) {
Ok(()) => return,
Err(err) => {
eprintln!(
"stipple: macOS backend unavailable ({err}); falling back to headless"
);
}
}
}
#[cfg(target_os = "android")]
{
match android::run(attrs.clone(), &mut handler) {
Ok(()) => return,
Err(err) => {
eprintln!(
"stipple: Android backend unavailable ({err}); falling back to headless"
);
}
}
}
let _ = headless::run(
attrs,
[Event::RedrawRequested, Event::CloseRequested],
handler,
);
}
}