1use super::{AppStats, WindowInfo, WindowKey, WindowOptions, WindowStatus};
5use crate::{SharedResources, Style, Theme};
6use std::collections::HashMap;
7
8pub enum Command {
9 Open {
10 key: WindowKey,
11 options: Box<WindowOptions>,
12 declared: bool,
13 },
14 Close(WindowKey),
15 RequestClose(WindowKey),
16 Focus(WindowKey),
17 Title(WindowKey, String),
18 Size(WindowKey, f64, f64),
19 Visible(WindowKey, bool),
20 Repaint(Option<WindowKey>),
21 Exit,
22 Refused(WindowKey, String),
25}
26
27pub struct Control {
29 pub commands: Vec<Command>,
30 pub status: HashMap<WindowKey, WindowStatus>,
31 pub infos: HashMap<WindowKey, WindowInfo>,
32 pub main: WindowKey,
33 pub resources: SharedResources,
34 pub exit_after_present: bool,
36 pub stats: AppStats,
37 pub max_windows: Option<usize>,
40}
41
42impl Control {
43 pub fn new(main: WindowKey, resources: SharedResources) -> Self {
44 Self {
45 commands: Vec::new(),
46 status: HashMap::new(),
47 infos: HashMap::new(),
48 main,
49 resources,
50 exit_after_present: false,
51 stats: AppStats::default(),
52 max_windows: None,
53 }
54 }
55}
56
57#[derive(Clone, Copy, Debug, PartialEq, Eq)]
59pub enum OpenOutcome {
60 Requested,
62 AlreadyOpen,
64 Unsupported,
69}
70
71pub struct Windows<'a> {
77 pub(crate) control: &'a mut Control,
78}
79
80impl<'a> Windows<'a> {
81 #[doc(hidden)]
82 pub fn from_control(control: &'a mut Control) -> Self {
83 Self { control }
84 }
85}
86
87impl Windows<'_> {
88 pub fn open(&mut self, key: impl Into<WindowKey>, options: WindowOptions) -> OpenOutcome {
93 self.request(key.into(), options, false)
94 }
95
96 pub(crate) fn request(
97 &mut self,
98 key: WindowKey,
99 options: WindowOptions,
100 declared: bool,
101 ) -> OpenOutcome {
102 if matches!(
103 self.control.status.get(&key),
104 Some(WindowStatus::Pending | WindowStatus::Open)
105 ) {
106 return OpenOutcome::AlreadyOpen;
107 }
108 if let Some(limit) = self.control.max_windows {
109 let taken = self
110 .control
111 .status
112 .values()
113 .filter(|status| matches!(status, WindowStatus::Pending | WindowStatus::Open))
114 .count();
115 if taken >= limit {
116 let reason = format!("this platform shows at most {limit} window(s) at a time");
117 self.control
118 .status
119 .insert(key.clone(), WindowStatus::Failed(reason.clone()));
120 self.control.commands.push(Command::Refused(key, reason));
121 return OpenOutcome::Unsupported;
122 }
123 }
124 self.control
125 .status
126 .insert(key.clone(), WindowStatus::Pending);
127 self.control.commands.push(Command::Open {
128 key,
129 options: Box::new(options),
130 declared,
131 });
132 OpenOutcome::Requested
133 }
134
135 pub fn close(&mut self, key: impl Into<WindowKey>) {
139 self.control.commands.push(Command::Close(key.into()));
140 }
141
142 pub fn request_close(&mut self, key: impl Into<WindowKey>) {
145 self.control
146 .commands
147 .push(Command::RequestClose(key.into()));
148 }
149
150 pub fn focus(&mut self, key: impl Into<WindowKey>) {
153 self.control.commands.push(Command::Focus(key.into()));
154 }
155
156 pub fn set_title(&mut self, key: impl Into<WindowKey>, title: impl Into<String>) {
157 self.control
158 .commands
159 .push(Command::Title(key.into(), title.into()));
160 }
161
162 pub fn set_inner_size(&mut self, key: impl Into<WindowKey>, width: f64, height: f64) {
164 self.control
165 .commands
166 .push(Command::Size(key.into(), width, height));
167 }
168
169 pub fn set_visible(&mut self, key: impl Into<WindowKey>, visible: bool) {
171 self.control
172 .commands
173 .push(Command::Visible(key.into(), visible));
174 }
175
176 pub fn request_repaint(&mut self, key: impl Into<WindowKey>) {
180 self.control
181 .commands
182 .push(Command::Repaint(Some(key.into())));
183 }
184
185 pub fn request_repaint_all(&mut self) {
187 self.control.commands.push(Command::Repaint(None));
188 }
189
190 pub fn exit(&mut self) {
192 self.control.commands.push(Command::Exit);
193 }
194
195 pub fn is_open(&self, key: &WindowKey) -> bool {
197 matches!(
198 self.control.status.get(key),
199 Some(WindowStatus::Pending | WindowStatus::Open)
200 )
201 }
202
203 pub fn status(&self, key: &WindowKey) -> Option<&WindowStatus> {
204 self.control.status.get(key)
205 }
206
207 pub fn info(&self, key: &WindowKey) -> Option<&WindowInfo> {
209 self.control.infos.get(key)
210 }
211
212 pub fn keys(&self) -> impl Iterator<Item = &WindowKey> {
214 self.control
215 .status
216 .iter()
217 .filter(|(_, status)| !matches!(status, WindowStatus::Failed(_)))
218 .map(|(key, _)| key)
219 }
220
221 pub fn main_key(&self) -> &WindowKey {
222 &self.control.main
223 }
224
225 pub fn stats(&self) -> AppStats {
226 self.control.stats
227 }
228
229 pub fn resources(&self) -> &SharedResources {
231 &self.control.resources
232 }
233
234 pub fn set_theme(&mut self, theme: Theme) {
236 self.control.resources.set_theme(theme);
237 }
238
239 pub fn set_style(&mut self, style: Style) {
241 self.control.resources.set_style(style);
242 }
243}