use super::{AppStats, WindowInfo, WindowKey, WindowOptions, WindowStatus};
use crate::{SharedResources, Style, Theme};
use std::collections::HashMap;
pub enum Command {
Open {
key: WindowKey,
options: Box<WindowOptions>,
declared: bool,
},
Close(WindowKey),
RequestClose(WindowKey),
Focus(WindowKey),
Title(WindowKey, String),
Size(WindowKey, f64, f64),
Visible(WindowKey, bool),
Repaint(Option<WindowKey>),
Exit,
Refused(WindowKey, String),
}
pub struct Control {
pub commands: Vec<Command>,
pub status: HashMap<WindowKey, WindowStatus>,
pub infos: HashMap<WindowKey, WindowInfo>,
pub main: WindowKey,
pub resources: SharedResources,
pub exit_after_present: bool,
pub stats: AppStats,
pub max_windows: Option<usize>,
}
impl Control {
pub fn new(main: WindowKey, resources: SharedResources) -> Self {
Self {
commands: Vec::new(),
status: HashMap::new(),
infos: HashMap::new(),
main,
resources,
exit_after_present: false,
stats: AppStats::default(),
max_windows: None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OpenOutcome {
Requested,
AlreadyOpen,
Unsupported,
}
pub struct Windows<'a> {
pub(crate) control: &'a mut Control,
}
impl<'a> Windows<'a> {
#[doc(hidden)]
pub fn from_control(control: &'a mut Control) -> Self {
Self { control }
}
}
impl Windows<'_> {
pub fn open(&mut self, key: impl Into<WindowKey>, options: WindowOptions) -> OpenOutcome {
self.request(key.into(), options, false)
}
pub(crate) fn request(
&mut self,
key: WindowKey,
options: WindowOptions,
declared: bool,
) -> OpenOutcome {
if matches!(
self.control.status.get(&key),
Some(WindowStatus::Pending | WindowStatus::Open)
) {
return OpenOutcome::AlreadyOpen;
}
if let Some(limit) = self.control.max_windows {
let taken = self
.control
.status
.values()
.filter(|status| matches!(status, WindowStatus::Pending | WindowStatus::Open))
.count();
if taken >= limit {
let reason = format!("this platform shows at most {limit} window(s) at a time");
self.control
.status
.insert(key.clone(), WindowStatus::Failed(reason.clone()));
self.control.commands.push(Command::Refused(key, reason));
return OpenOutcome::Unsupported;
}
}
self.control
.status
.insert(key.clone(), WindowStatus::Pending);
self.control.commands.push(Command::Open {
key,
options: Box::new(options),
declared,
});
OpenOutcome::Requested
}
pub fn close(&mut self, key: impl Into<WindowKey>) {
self.control.commands.push(Command::Close(key.into()));
}
pub fn request_close(&mut self, key: impl Into<WindowKey>) {
self.control
.commands
.push(Command::RequestClose(key.into()));
}
pub fn focus(&mut self, key: impl Into<WindowKey>) {
self.control.commands.push(Command::Focus(key.into()));
}
pub fn set_title(&mut self, key: impl Into<WindowKey>, title: impl Into<String>) {
self.control
.commands
.push(Command::Title(key.into(), title.into()));
}
pub fn set_inner_size(&mut self, key: impl Into<WindowKey>, width: f64, height: f64) {
self.control
.commands
.push(Command::Size(key.into(), width, height));
}
pub fn set_visible(&mut self, key: impl Into<WindowKey>, visible: bool) {
self.control
.commands
.push(Command::Visible(key.into(), visible));
}
pub fn request_repaint(&mut self, key: impl Into<WindowKey>) {
self.control
.commands
.push(Command::Repaint(Some(key.into())));
}
pub fn request_repaint_all(&mut self) {
self.control.commands.push(Command::Repaint(None));
}
pub fn exit(&mut self) {
self.control.commands.push(Command::Exit);
}
pub fn is_open(&self, key: &WindowKey) -> bool {
matches!(
self.control.status.get(key),
Some(WindowStatus::Pending | WindowStatus::Open)
)
}
pub fn status(&self, key: &WindowKey) -> Option<&WindowStatus> {
self.control.status.get(key)
}
pub fn info(&self, key: &WindowKey) -> Option<&WindowInfo> {
self.control.infos.get(key)
}
pub fn keys(&self) -> impl Iterator<Item = &WindowKey> {
self.control
.status
.iter()
.filter(|(_, status)| !matches!(status, WindowStatus::Failed(_)))
.map(|(key, _)| key)
}
pub fn main_key(&self) -> &WindowKey {
&self.control.main
}
pub fn stats(&self) -> AppStats {
self.control.stats
}
pub fn resources(&self) -> &SharedResources {
&self.control.resources
}
pub fn set_theme(&mut self, theme: Theme) {
self.control.resources.set_theme(theme);
}
pub fn set_style(&mut self, style: Style) {
self.control.resources.set_style(style);
}
}