use crate::window;
use crate::{Pt, Spot};
#[cfg(target_os = "android")]
use android_activity::AndroidApp;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct WindowConfig {
pub title: String,
pub width: Pt,
pub height: Pt,
pub resizable: bool,
pub fullscreen: bool,
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
pub canvas_id: Option<String>,
pub transparent: bool,
pub update_hz: u32,
}
impl Default for WindowConfig {
fn default() -> Self {
Self {
title: "spot".to_string(),
width: Pt(800.0),
height: Pt(600.0),
resizable: true,
fullscreen: false,
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
canvas_id: None,
transparent: false,
update_hz: 60,
}
}
}
impl WindowConfig {
pub(crate) fn fixed_update_step(&self) -> Duration {
assert!(
self.update_hz > 0,
"WindowConfig::update_hz must be greater than zero"
);
let step = Duration::from_secs_f64(1.0 / f64::from(self.update_hz));
assert!(
!step.is_zero(),
"WindowConfig::update_hz is too high to represent as a Duration"
);
step
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_update_rate_is_sixty_hz() {
let config = WindowConfig::default();
assert_eq!(config.update_hz, 60);
assert!((config.fixed_update_step().as_secs_f64() - 1.0 / 60.0).abs() < 1e-9);
}
#[test]
fn update_rate_controls_fixed_step() {
let config = WindowConfig {
update_hz: 120,
..Default::default()
};
assert!((config.fixed_update_step().as_secs_f64() - 1.0 / 120.0).abs() < 1e-9);
}
#[test]
#[should_panic(expected = "update_hz must be greater than zero")]
fn zero_update_rate_is_rejected() {
WindowConfig {
update_hz: 0,
..Default::default()
}
.fixed_update_step();
}
}
#[cfg(not(target_os = "android"))]
pub fn run<T: Spot + 'static>(window: WindowConfig) {
<window::WinitWgpuBackend as window::WindowBackend>::run::<T>(window);
}
#[cfg(target_os = "android")]
pub fn run<T: Spot + 'static>(window: WindowConfig, app: AndroidApp) {
<window::WinitWgpuBackend as window::WindowBackend>::run::<T>(window, app);
}