use super::{commands::Control, WindowKey, WindowOptions, Windows};
use winit::keyboard::{KeyCode, ModifiersState};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CloseSource {
System,
Application,
}
pub struct CloseRequested<'a> {
pub(crate) key: &'a WindowKey,
pub(crate) source: CloseSource,
pub(crate) windows: Windows<'a>,
pub(crate) rejected: bool,
}
impl<'a> CloseRequested<'a> {
pub(crate) fn new(key: &'a WindowKey, source: CloseSource, control: &'a mut Control) -> Self {
Self {
key,
source,
windows: Windows { control },
rejected: false,
}
}
pub fn window(&self) -> &WindowKey {
self.key
}
pub fn source(&self) -> CloseSource {
self.source
}
pub fn is_main(&self) -> bool {
self.windows.main_key() == self.key
}
pub fn accept(&mut self) {
self.rejected = false;
}
pub fn reject(&mut self) {
self.rejected = true;
}
pub fn windows(&mut self) -> &mut Windows<'a> {
&mut self.windows
}
}
#[derive(Clone, Copy, Debug)]
pub struct GlobalShortcut<'a> {
pub window: &'a WindowKey,
pub key: KeyCode,
pub modifiers: ModifiersState,
pub repeat: bool,
}
#[derive(Default)]
pub struct WindowPlan {
pub(crate) declared: Vec<(WindowKey, WindowOptions)>,
}
impl WindowPlan {
pub fn window(&mut self, key: impl Into<WindowKey>, options: WindowOptions) -> &mut Self {
let key = key.into();
if !self.declared.iter().any(|(declared, _)| *declared == key) {
self.declared.push((key, options));
}
self
}
pub fn window_if(
&mut self,
condition: bool,
key: impl Into<WindowKey>,
options: impl FnOnce() -> WindowOptions,
) -> &mut Self {
if condition {
self.window(key, options());
}
self
}
}