use std::cell::Cell;
use std::rc::Rc;
use std::time::Duration;
use lgwks_deps::gpui::{
App, AppContext as _, Application, Bounds, Pixels, SharedString, TitlebarOptions, WindowBounds,
WindowOptions, size,
};
use crate::runtime::{Percentiles, RuntimeError, RuntimeTotals};
mod lab;
use lab::InkLab;
const TILE_WIDTH: f64 = 248.0;
const TILE_HEIGHT: f64 = 168.0;
const MAX_FRAME_MS: f64 = 100.0;
const WAKE_FRAME_MS: f64 = 1000.0 / 60.0;
const MAX_SAMPLES: usize = 16_384;
const STALL_LIMIT: Duration = Duration::from_secs(10);
const STALL_CHECK: Duration = Duration::from_secs(1);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Launch {
pub seed: u64,
pub reduce_motion: bool,
pub dark: bool,
pub frame_limit: Option<u64>,
}
impl Launch {
#[must_use]
pub const fn new() -> Self {
Self {
seed: 1,
reduce_motion: false,
dark: false,
frame_limit: None,
}
}
}
impl Default for Launch {
fn default() -> Self {
Self::new()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum NativeError {
WindowOpen(String),
Runtime(RuntimeError),
NoFrames {
presented: u64,
},
}
impl std::fmt::Display for NativeError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
Self::WindowOpen(ref reason) => write!(formatter, "window refused: {reason}"),
Self::Runtime(error) => write!(formatter, "runtime refused a host: {error:?}"),
Self::NoFrames { presented } => write!(
formatter,
"the platform delivered no frame for {} s after {presented} presented frames; \
macOS sends none to a locked session or a sleeping display",
STALL_LIMIT.as_secs()
),
}
}
}
impl std::error::Error for NativeError {}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct SessionReport {
pub totals: RuntimeTotals,
pub frame_interval_ms: Percentiles,
pub refusals: u64,
pub paint_failures: u64,
pub wall_ms: f64,
}
type Finisher = Rc<Cell<Option<Box<dyn FnOnce(Result<SessionReport, NativeError>)>>>>;
fn finish(finisher: &Finisher, outcome: Result<SessionReport, NativeError>) {
if let Some(callback) = finisher.take() {
callback(outcome);
}
}
pub fn run(launch: Launch, on_finish: impl FnOnce(Result<SessionReport, NativeError>) + 'static) {
let finisher: Finisher = Rc::new(Cell::new(Some(Box::new(on_finish))));
Application::new().run(move |cx: &mut App| {
let lab = match InkLab::new(launch, Rc::clone(&finisher)) {
Ok(lab) => lab,
Err(error) => {
finish(&finisher, Err(error));
cx.quit();
return;
}
};
let bounds = Bounds::centered(None, size(pixels(1180.0), pixels(860.0)), cx);
let opened = cx.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
titlebar: Some(TitlebarOptions {
title: Some(SharedString::from("Drishti ยท ink lab")),
..TitlebarOptions::default()
}),
..WindowOptions::default()
},
move |window, cx| {
let view = cx.new(move |cx| {
let mut lab = lab;
lab.arm_watchdog(cx);
lab
});
let handle = view.downgrade();
window.on_window_should_close(cx, move |_, cx| {
if let Some(view) = handle.upgrade() {
view.read(cx).finish();
}
cx.quit();
true
});
view
},
);
match opened {
Ok(_) => cx.activate(true),
Err(error) => {
finish(
&finisher,
Err(NativeError::WindowOpen(format!("{error:#}"))),
);
cx.quit();
}
}
});
}
fn pixels(value: f64) -> Pixels {
Pixels::from(value)
}
fn narrow(value: f64) -> f32 {
f32::from(Pixels::from(value))
}