use std::path::PathBuf;
use agg_gui_wgpu::CopySrc;
use crate::bounds::WindowBoundsStore;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum WindowSize {
Logical(f64, f64),
Physical(u32, u32),
}
impl WindowSize {
pub(crate) fn to_winit(self) -> winit::dpi::Size {
match self {
Self::Logical(w, h) => winit::dpi::LogicalSize::new(w, h).into(),
Self::Physical(w, h) => winit::dpi::PhysicalSize::new(w, h).into(),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum RedrawPolicy {
#[default]
Reactive,
Continuous,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct WindowIcon {
pub rgba: Vec<u8>,
pub width: u32,
pub height: u32,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct ScreenshotConfig {
pub path: PathBuf,
pub settle_frames: u32,
}
#[non_exhaustive]
pub struct ShellConfig {
pub title: String,
pub size: WindowSize,
pub min_size: Option<WindowSize>,
pub icon: Option<WindowIcon>,
pub maximized: bool,
pub fullscreen: bool,
pub redraw_policy: RedrawPolicy,
pub copy_src: CopySrc,
pub present_mode: wgpu::PresentMode,
pub device_label: &'static str,
pub optional_features: wgpu::Features,
pub screenshot: Option<ScreenshotConfig>,
pub os_tooltip_timings: bool,
pub bounds_store: Option<Box<dyn WindowBoundsStore>>,
}
impl Default for ShellConfig {
fn default() -> Self {
Self {
title: String::new(),
size: WindowSize::Logical(1280.0, 720.0),
min_size: None,
icon: None,
maximized: false,
fullscreen: false,
redraw_policy: RedrawPolicy::Reactive,
copy_src: CopySrc::Never,
present_mode: wgpu::PresentMode::AutoVsync,
device_label: "agg-gui-shell",
optional_features: wgpu::Features::empty(),
screenshot: None,
os_tooltip_timings: true,
bounds_store: None,
}
}
}
impl ShellConfig {
pub fn new(title: impl Into<String>) -> Self {
Self {
title: title.into(),
..Self::default()
}
}
pub fn with_logical_size(mut self, w: f64, h: f64) -> Self {
self.size = WindowSize::Logical(w, h);
self
}
pub fn with_physical_size(mut self, w: u32, h: u32) -> Self {
self.size = WindowSize::Physical(w, h);
self
}
pub fn with_min_logical_size(mut self, w: f64, h: f64) -> Self {
self.min_size = Some(WindowSize::Logical(w, h));
self
}
pub fn with_min_physical_size(mut self, w: u32, h: u32) -> Self {
self.min_size = Some(WindowSize::Physical(w, h));
self
}
pub fn with_icon_rgba(mut self, rgba: impl Into<Vec<u8>>, width: u32, height: u32) -> Self {
self.icon = Some(WindowIcon {
rgba: rgba.into(),
width,
height,
});
self
}
pub fn with_maximized(mut self, maximized: bool) -> Self {
self.maximized = maximized;
self
}
pub fn with_fullscreen(mut self, fullscreen: bool) -> Self {
self.fullscreen = fullscreen;
self
}
pub fn with_redraw_policy(mut self, policy: RedrawPolicy) -> Self {
self.redraw_policy = policy;
self
}
pub fn with_copy_src(mut self, copy_src: CopySrc) -> Self {
self.copy_src = copy_src;
self
}
pub fn with_present_mode(mut self, present_mode: wgpu::PresentMode) -> Self {
self.present_mode = present_mode;
self
}
pub fn with_device_label(mut self, label: &'static str) -> Self {
self.device_label = label;
self
}
pub fn with_optional_features(mut self, features: wgpu::Features) -> Self {
self.optional_features = features;
self
}
pub fn with_screenshot(mut self, path: impl Into<PathBuf>, settle_frames: u32) -> Self {
self.screenshot = Some(ScreenshotConfig {
path: path.into(),
settle_frames,
});
self
}
pub fn with_os_tooltip_timings(mut self, enabled: bool) -> Self {
self.os_tooltip_timings = enabled;
self
}
pub fn with_bounds_store(mut self, store: impl WindowBoundsStore + 'static) -> Self {
self.bounds_store = Some(Box::new(store));
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn builder_defaults_and_overrides() {
let cfg = ShellConfig::new("Title");
assert_eq!(cfg.title, "Title");
assert_eq!(cfg.size, WindowSize::Logical(1280.0, 720.0));
assert_eq!(cfg.redraw_policy, RedrawPolicy::Reactive);
assert!(cfg.os_tooltip_timings);
let cfg = cfg
.with_physical_size(1600, 900)
.with_min_logical_size(640.0, 480.0)
.with_redraw_policy(RedrawPolicy::Continuous)
.with_screenshot("out.png", 6);
assert_eq!(cfg.size, WindowSize::Physical(1600, 900));
assert_eq!(cfg.min_size, Some(WindowSize::Logical(640.0, 480.0)));
assert_eq!(cfg.redraw_policy, RedrawPolicy::Continuous);
assert_eq!(cfg.screenshot.map(|s| s.settle_frames), Some(6));
}
#[test]
fn min_size_can_be_given_in_either_unit() {
let cfg = ShellConfig::new("Title").with_min_physical_size(320, 240);
assert_eq!(cfg.min_size, Some(WindowSize::Physical(320, 240)));
}
}