use std::fmt;
use std::path::PathBuf;
use std::rc::Rc;
use std::time::Duration;
pub use raw_window_handle::RawWindowHandle;
use crate::{IconImage, Rect, ScreenId, Size, ViewRef};
#[derive(Clone, Debug, PartialEq)]
pub struct WindowConfig {
pub title: String,
pub icon: Option<IconImage>,
pub size: [f64; 2],
pub position: Option<[f64; 2]>,
pub decorations: bool,
pub resizable: bool,
pub level: WindowLevel,
pub all_desktops: bool,
pub background: WindowBackground,
pub shadow: bool,
pub visible: bool,
pub macos: MacosWindowConfig,
pub windows: WindowsWindowConfig,
pub linux: LinuxWindowConfig,
}
impl Default for WindowConfig {
fn default() -> WindowConfig {
WindowConfig {
title: String::new(),
icon: None,
size: [900.0, 600.0],
position: None,
decorations: true,
resizable: true,
level: WindowLevel::Normal,
all_desktops: false,
background: WindowBackground::Solid,
shadow: true,
visible: true,
macos: MacosWindowConfig::default(),
windows: WindowsWindowConfig::default(),
linux: LinuxWindowConfig::default(),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct MacosWindowConfig {
pub title_bar: MacosTitleBar,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum MacosTitleBar {
#[default]
Standard,
Hidden { height: f64 },
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct WindowsWindowConfig {
pub app_user_model_id: Option<String>,
pub relaunch: Option<WindowsRelaunch>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct WindowsRelaunch {
pub command: String,
pub display_name: String,
pub icon: Option<WindowsIconResource>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WindowsIconResource {
pub path: PathBuf,
pub index: i32,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct LinuxWindowConfig {
pub app_id: Option<String>,
pub activation_token: Option<String>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum WindowLevel {
#[default]
Normal,
AlwaysOnTop,
AlwaysOnBottom,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum WindowBackground {
#[default]
Solid,
Transparent,
Blurred,
Glass,
}
impl WindowBackground {
pub fn sees_through(self) -> bool {
self != WindowBackground::Solid
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TitleBarAction {
Maximize,
Minimize,
Nothing,
#[default]
Unknown,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct RequestId(pub u64);
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ViewError {
Unsupported,
Failed(String),
}
impl fmt::Display for ViewError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ViewError::Unsupported => f.write_str("the platform makes no views on request"),
ViewError::Failed(reason) => {
write!(f, "the platform could not make the view: {reason}")
}
}
}
}
impl std::error::Error for ViewError {}
pub type WindowRef = Rc<dyn HostWindow>;
pub trait HostWindow: 'static {
fn view(&self) -> ViewRef;
fn frame(&self) -> Rect;
fn set_frame(&self, frame: Rect, animate: Option<Duration>);
fn set_title(&self, title: &str);
fn set_icon(&self, icon: Option<&IconImage>);
fn show(&self);
fn hide(&self);
fn activate(&self);
fn is_minimized(&self) -> bool;
fn is_occluded(&self) -> bool;
fn close(&self);
fn size_limits(&self) -> Option<&dyn WindowSizeLimits>;
fn drag(&self);
fn is_maximized(&self) -> bool;
fn set_maximized(&self, maximized: bool);
fn set_minimized(&self, minimized: bool);
fn is_fullscreen(&self) -> bool;
fn set_fullscreen(&self, fullscreen: bool, screen: Option<ScreenId>);
fn title_bar_double_click_action(&self) -> TitleBarAction {
TitleBarAction::Unknown
}
fn native_handle(&self) -> Option<RawWindowHandle>;
}
pub trait WindowSizeLimits {
fn set_min_size(&self, size: Option<Size>);
fn set_max_size(&self, size: Option<Size>);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_default_window_is_an_ordinary_visible_one() {
let config = WindowConfig::default();
assert!(config.decorations && config.visible);
assert_eq!(config.level, WindowLevel::Normal);
assert_eq!(config.background, WindowBackground::Solid);
assert_eq!(config.macos.title_bar, MacosTitleBar::Standard);
assert_eq!(config.windows, WindowsWindowConfig::default());
assert_eq!(config.linux, LinuxWindowConfig::default());
}
#[test]
fn every_background_but_solid_sees_through() {
assert!(!WindowBackground::Solid.sees_through());
assert!(WindowBackground::Transparent.sees_through());
assert!(WindowBackground::Blurred.sees_through());
assert!(WindowBackground::Glass.sees_through());
}
}