mod callbacks;
mod commands;
mod frame;
mod hub;
#[cfg(test)]
mod hub_tests;
mod info;
mod key;
mod options;
mod registry;
#[cfg(test)]
mod registry_tests;
mod runner;
mod schedule;
#[cfg(test)]
mod shared_tests;
#[cfg(test)]
mod tests;
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, SharedResources};
use runner::Runner;
#[cfg(test)]
use schedule::repaint_schedule;
#[cfg(test)]
use std::time::{Duration, Instant};
use std::{error::Error, fmt};
use winit::{
dpi::LogicalSize,
event_loop::{ControlFlow, EventLoop},
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,
}
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(),
}
}
}
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_main_window(mut self, key: impl Into<WindowKey>) -> Self {
self.main_window = key.into();
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
}
pub fn run(app: impl App) -> Result<(), RunError> {
run_with_options(app, RunOptions::default())
}
pub fn run_with_options(app: impl App, options: RunOptions) -> Result<(), RunError> {
let event_loop = EventLoop::<()>::with_user_event()
.build()
.map_err(RunError::EventLoop)?;
event_loop.set_control_flow(ControlFlow::Wait);
let proxy = event_loop.create_proxy();
let resources = match (&options.font_family, &options.monospace_family) {
(None, None) => SharedResources::new(),
(family, monospace) => SharedResources::with_font_families(
family.clone().unwrap_or_default(),
monospace
.clone()
.or_else(crate::FontFamily::default_monospace),
),
};
resources.set_image_waker(move || {
let _ = proxy.send_event(());
});
let mut runner = Runner::new(app, options, resources);
let result = event_loop.run_app(&mut runner).map_err(RunError::EventLoop);
runner.error.take().map_or(result, Err)
}
#[derive(Debug)]
pub enum RunError {
EventLoop(winit::error::EventLoopError),
Window(winit::error::OsError),
Render(RenderError),
}
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),
}
}
}
impl Error for RunError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(match self {
Self::EventLoop(error) => error,
Self::Window(error) => error,
Self::Render(error) => error,
})
}
}