use std::time::Duration;
use agg_gui::{App, Size};
use agg_gui_wgpu::{Gpu, WgpuGfxCtx};
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct Frame {
pub width: u32,
pub height: u32,
pub device_scale: f64,
pub duration: Duration,
pub index: u64,
pub needs_layout: bool,
pub input_since_last_frame: bool,
}
impl Frame {
pub fn viewport(&self) -> Size {
Size::new(self.width as f64, self.height as f64)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct WindowGeometry {
pub width: u32,
pub height: u32,
pub maximized: bool,
pub fullscreen: bool,
pub scale_factor: f64,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ExitAction {
Exit,
Relaunch,
}
pub struct ShellControl<'a> {
pub(crate) policy: &'a mut crate::RedrawPolicy,
pub(crate) exit: &'a mut Option<ExitAction>,
pub(crate) painted: bool,
pub(crate) pointer_idle: bool,
}
impl ShellControl<'_> {
pub fn redraw_policy(&self) -> crate::RedrawPolicy {
*self.policy
}
pub fn set_redraw_policy(&mut self, policy: crate::RedrawPolicy) {
*self.policy = policy;
}
pub fn painted(&self) -> bool {
self.painted
}
pub fn pointer_idle(&self) -> bool {
self.pointer_idle
}
pub fn request_exit(&mut self) {
if self.exit.is_none() {
*self.exit = Some(ExitAction::Exit);
}
}
pub fn request_relaunch(&mut self) {
*self.exit = Some(ExitAction::Relaunch);
}
}
pub trait ShellHost {
fn on_frame(&mut self, _app: &mut App, _frame: &Frame) {}
fn paint(&mut self, app: &mut App, ctx: &mut WgpuGfxCtx, frame: &Frame) {
default_paint(app, ctx, frame);
}
fn after_paint(&mut self, _ctx: &mut WgpuGfxCtx, _frame: &Frame) {}
fn on_geometry_changed(&mut self, _geometry: WindowGeometry) {}
fn on_close_requested(&mut self, _app: &mut App) -> bool {
true
}
fn on_idle(&mut self, _app: &mut App, _control: &mut ShellControl<'_>) {}
fn on_gpu_rebuilt(&mut self, _app: &mut App, _gpu: &Gpu) {}
fn on_exit(&mut self, _app: &mut App) {}
}
pub struct NoHost;
impl ShellHost for NoHost {}
pub fn default_paint(app: &mut App, ctx: &mut WgpuGfxCtx, frame: &Frame) {
ctx.reset(frame.width as f32, frame.height as f32);
ctx.set_lcd_mode(agg_gui::font_settings::lcd_enabled());
if frame.needs_layout {
app.layout(frame.viewport());
}
app.paint(ctx);
}