#[doc(hidden)]
pub mod browser;
mod callbacks;
#[doc(hidden)]
pub mod commands;
#[cfg(feature = "file-dialogs")]
#[doc(hidden)]
pub mod dialogs;
mod frame;
#[doc(hidden)]
pub mod hub;
mod info;
mod key;
#[doc(hidden)]
pub mod options;
#[doc(hidden)]
pub mod registry;
mod runner;
#[doc(hidden)]
pub mod schedule;
pub use browser::{WebBackend, WebOptions};
pub use callbacks::{CloseRequested, CloseSource, GlobalShortcut, WindowPlan};
pub use commands::{OpenOutcome, Windows};
pub use frame::Frame;
pub use info::{AppStats, WindowError, WindowInfo, WindowStatus};
pub use key::WindowKey;
pub use options::{ExitPolicy, WindowOptions};
use crate::{Context, PresentationMode, RenderError};
use std::{error::Error, fmt};
use winit::{
dpi::LogicalSize,
window::{Window, WindowAttributes},
};
pub trait App {
fn update(&mut self, context: &mut Context, frame: &mut Frame<'_>);
fn windows(&mut self, _plan: &mut WindowPlan) {}
fn close_requested(&mut self, _request: &mut CloseRequested<'_>) {}
fn window_failed(&mut self, _key: &WindowKey, _error: &WindowError) {}
fn global_shortcut(
&mut self,
_shortcut: &GlobalShortcut<'_>,
_windows: &mut Windows<'_>,
) -> bool {
false
}
}
#[derive(Debug, Clone)]
pub struct RunOptions {
pub window_attributes: WindowAttributes,
pub presentation_mode: PresentationMode,
pub font_family: Option<crate::FontFamily>,
pub monospace_family: Option<crate::FontFamily>,
pub main_window: WindowKey,
pub exit_policy: ExitPolicy,
pub web: WebOptions,
pub accessibility: bool,
}
impl Default for RunOptions {
fn default() -> Self {
Self {
window_attributes: Window::default_attributes()
.with_title("zaxis")
.with_inner_size(LogicalSize::new(860.0, 560.0)),
presentation_mode: PresentationMode::default(),
font_family: None,
monospace_family: None,
main_window: WindowKey::main(),
exit_policy: ExitPolicy::default(),
web: WebOptions::default(),
accessibility: true,
}
}
}
impl RunOptions {
pub fn with_font_family(mut self, family: crate::FontFamily) -> Self {
self.font_family = Some(family);
self
}
pub fn with_monospace_family(mut self, family: crate::FontFamily) -> Self {
self.monospace_family = Some(family);
self
}
pub fn with_exit_policy(mut self, policy: ExitPolicy) -> Self {
self.exit_policy = policy;
self
}
pub fn with_accessibility(mut self, accessibility: bool) -> Self {
self.accessibility = accessibility;
self
}
pub fn with_main_window(mut self, key: impl Into<WindowKey>) -> Self {
self.main_window = key.into();
self
}
pub fn with_canvas_id(mut self, id: impl Into<String>) -> Self {
self.web.canvas_id = Some(id.into());
self
}
pub fn with_container_id(mut self, id: impl Into<String>) -> Self {
self.web.container_id = Some(id.into());
self
}
pub fn with_web_backend(mut self, backend: WebBackend) -> Self {
self.web.backend = backend;
self
}
pub fn with_rounded_corners(mut self, rounded: bool) -> Self {
self.window_attributes = round_corners(self.window_attributes, rounded);
self
}
}
#[cfg(target_os = "windows")]
fn round_corners(attributes: WindowAttributes, rounded: bool) -> WindowAttributes {
use winit::platform::windows::{CornerPreference, WindowAttributesExtWindows};
attributes
.with_undecorated_shadow(rounded)
.with_corner_preference(if rounded {
CornerPreference::Round
} else {
CornerPreference::DoNotRound
})
}
#[cfg(not(target_os = "windows"))]
fn round_corners(attributes: WindowAttributes, _rounded: bool) -> WindowAttributes {
attributes
}
#[cfg(not(target_arch = "wasm32"))]
pub fn run(app: impl App) -> Result<(), RunError> {
run_with_options(app, RunOptions::default())
}
#[cfg(target_arch = "wasm32")]
pub fn run(app: impl App + 'static) -> Result<(), RunError> {
run_with_options(app, RunOptions::default())
}
#[cfg(not(target_arch = "wasm32"))]
pub fn run_with_options(app: impl App, options: RunOptions) -> Result<(), RunError> {
runner::run(app, options)
}
#[cfg(target_arch = "wasm32")]
pub fn run_with_options(app: impl App + 'static, options: RunOptions) -> Result<(), RunError> {
runner::run(app, options)
}
#[derive(Debug)]
pub enum RunError {
EventLoop(winit::error::EventLoopError),
Window(winit::error::OsError),
Render(RenderError),
Web(String),
}
impl fmt::Display for RunError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EventLoop(error) => write!(f, "running the event loop: {error}"),
Self::Window(error) => write!(f, "creating the window: {error}"),
Self::Render(error) => error.fmt(f),
Self::Web(message) => write!(f, "preparing the page: {message}"),
}
}
}
impl Error for RunError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::EventLoop(error) => Some(error),
Self::Window(error) => Some(error),
Self::Render(error) => Some(error),
Self::Web(_) => None,
}
}
}