1use super::{AppVersion, CursorStyle, WindowBounds};
2use crate::{
3 geometry::{
4 rect::RectF,
5 vector::{vec2f, Vector2F},
6 },
7 keymap, Action, ClipboardItem,
8};
9use anyhow::{anyhow, Result};
10use collections::VecDeque;
11use parking_lot::Mutex;
12use postage::oneshot;
13use std::{
14 any::Any,
15 cell::RefCell,
16 path::{Path, PathBuf},
17 rc::Rc,
18 sync::Arc,
19};
20use time::UtcOffset;
21
22pub struct Platform {
23 dispatcher: Arc<dyn super::Dispatcher>,
24 fonts: Arc<dyn super::FontSystem>,
25 current_clipboard_item: Mutex<Option<ClipboardItem>>,
26 cursor: Mutex<CursorStyle>,
27}
28
29#[derive(Default)]
30pub struct ForegroundPlatform {
31 last_prompt_for_new_path_args: RefCell<Option<(PathBuf, oneshot::Sender<Option<PathBuf>>)>>,
32}
33
34struct Dispatcher;
35
36pub struct Window {
37 size: Vector2F,
38 scale_factor: f32,
39 current_scene: Option<crate::Scene>,
40 event_handlers: Vec<Box<dyn FnMut(super::Event) -> bool>>,
41 resize_handlers: Vec<Box<dyn FnMut()>>,
42 close_handlers: Vec<Box<dyn FnOnce()>>,
43 fullscreen_handlers: Vec<Box<dyn FnMut(bool)>>,
44 pub(crate) active_status_change_handlers: Vec<Box<dyn FnMut(bool)>>,
45 pub(crate) should_close_handler: Option<Box<dyn FnMut() -> bool>>,
46 pub(crate) title: Option<String>,
47 pub(crate) edited: bool,
48 pub(crate) pending_prompts: RefCell<VecDeque<oneshot::Sender<usize>>>,
49}
50
51#[cfg(any(test, feature = "test-support"))]
52impl ForegroundPlatform {
53 pub(crate) fn simulate_new_path_selection(
54 &self,
55 result: impl FnOnce(PathBuf) -> Option<PathBuf>,
56 ) {
57 let (dir_path, mut done_tx) = self
58 .last_prompt_for_new_path_args
59 .take()
60 .expect("prompt_for_new_path was not called");
61 let _ = postage::sink::Sink::try_send(&mut done_tx, result(dir_path));
62 }
63
64 pub(crate) fn did_prompt_for_new_path(&self) -> bool {
65 self.last_prompt_for_new_path_args.borrow().is_some()
66 }
67}
68
69impl super::ForegroundPlatform for ForegroundPlatform {
70 fn on_become_active(&self, _: Box<dyn FnMut()>) {}
71
72 fn on_resign_active(&self, _: Box<dyn FnMut()>) {}
73
74 fn on_quit(&self, _: Box<dyn FnMut()>) {}
75
76 fn on_event(&self, _: Box<dyn FnMut(crate::Event) -> bool>) {}
77
78 fn on_open_urls(&self, _: Box<dyn FnMut(Vec<String>)>) {}
79
80 fn run(&self, _on_finish_launching: Box<dyn FnOnce()>) {
81 unimplemented!()
82 }
83
84 fn on_menu_command(&self, _: Box<dyn FnMut(&dyn Action)>) {}
85 fn on_validate_menu_command(&self, _: Box<dyn FnMut(&dyn Action) -> bool>) {}
86 fn on_will_open_menu(&self, _: Box<dyn FnMut()>) {}
87 fn set_menus(&self, _: Vec<crate::Menu>, _: &keymap::Matcher) {}
88
89 fn prompt_for_paths(
90 &self,
91 _: super::PathPromptOptions,
92 ) -> oneshot::Receiver<Option<Vec<PathBuf>>> {
93 let (_done_tx, done_rx) = oneshot::channel();
94 done_rx
95 }
96
97 fn prompt_for_new_path(&self, path: &Path) -> oneshot::Receiver<Option<PathBuf>> {
98 let (done_tx, done_rx) = oneshot::channel();
99 *self.last_prompt_for_new_path_args.borrow_mut() = Some((path.to_path_buf(), done_tx));
100 done_rx
101 }
102}
103
104impl Platform {
105 fn new() -> Self {
106 Self {
107 dispatcher: Arc::new(Dispatcher),
108 fonts: Arc::new(super::current::FontSystem::new()),
109 current_clipboard_item: Default::default(),
110 cursor: Mutex::new(CursorStyle::Arrow),
111 }
112 }
113}
114
115impl super::Platform for Platform {
116 fn dispatcher(&self) -> Arc<dyn super::Dispatcher> {
117 self.dispatcher.clone()
118 }
119
120 fn fonts(&self) -> std::sync::Arc<dyn super::FontSystem> {
121 self.fonts.clone()
122 }
123
124 fn activate(&self, _ignoring_other_apps: bool) {}
125
126 fn hide(&self) {}
127
128 fn hide_other_apps(&self) {}
129
130 fn unhide_other_apps(&self) {}
131
132 fn quit(&self) {}
133
134 fn open_window(
135 &self,
136 _: usize,
137 options: super::WindowOptions,
138 _executor: Rc<super::executor::Foreground>,
139 ) -> Box<dyn super::Window> {
140 Box::new(Window::new(match options.bounds {
141 WindowBounds::Maximized => vec2f(1024., 768.),
142 WindowBounds::Fixed(rect) => rect.size(),
143 }))
144 }
145
146 fn key_window_id(&self) -> Option<usize> {
147 None
148 }
149
150 fn add_status_item(&self) -> Box<dyn crate::Window> {
151 Box::new(Window::new(vec2f(24., 24.)))
152 }
153
154 fn write_to_clipboard(&self, item: ClipboardItem) {
155 *self.current_clipboard_item.lock() = Some(item);
156 }
157
158 fn read_from_clipboard(&self) -> Option<ClipboardItem> {
159 self.current_clipboard_item.lock().clone()
160 }
161
162 fn open_url(&self, _: &str) {}
163
164 fn write_credentials(&self, _: &str, _: &str, _: &[u8]) -> Result<()> {
165 Ok(())
166 }
167
168 fn read_credentials(&self, _: &str) -> Result<Option<(String, Vec<u8>)>> {
169 Ok(None)
170 }
171
172 fn delete_credentials(&self, _: &str) -> Result<()> {
173 Ok(())
174 }
175
176 fn set_cursor_style(&self, style: CursorStyle) {
177 *self.cursor.lock() = style;
178 }
179
180 fn local_timezone(&self) -> UtcOffset {
181 UtcOffset::UTC
182 }
183
184 fn path_for_auxiliary_executable(&self, _name: &str) -> Result<PathBuf> {
185 Err(anyhow!("app not running inside a bundle"))
186 }
187
188 fn app_path(&self) -> Result<PathBuf> {
189 Err(anyhow!("app not running inside a bundle"))
190 }
191
192 fn app_version(&self) -> Result<AppVersion> {
193 Ok(AppVersion {
194 major: 1,
195 minor: 0,
196 patch: 0,
197 })
198 }
199
200 fn os_name(&self) -> &'static str {
201 "test"
202 }
203
204 fn os_version(&self) -> Result<AppVersion> {
205 Ok(AppVersion {
206 major: 1,
207 minor: 0,
208 patch: 0,
209 })
210 }
211}
212
213impl Window {
214 fn new(size: Vector2F) -> Self {
215 Self {
216 size,
217 event_handlers: Default::default(),
218 resize_handlers: Default::default(),
219 close_handlers: Default::default(),
220 should_close_handler: Default::default(),
221 active_status_change_handlers: Default::default(),
222 fullscreen_handlers: Default::default(),
223 scale_factor: 1.0,
224 current_scene: None,
225 title: None,
226 edited: false,
227 pending_prompts: Default::default(),
228 }
229 }
230
231 pub fn title(&self) -> Option<String> {
232 self.title.clone()
233 }
234}
235
236impl super::Dispatcher for Dispatcher {
237 fn is_main_thread(&self) -> bool {
238 true
239 }
240
241 fn run_on_main_thread(&self, task: async_task::Runnable) {
242 task.run();
243 }
244}
245
246impl super::Window for Window {
247 fn as_any_mut(&mut self) -> &mut dyn Any {
248 self
249 }
250
251 fn on_event(&mut self, callback: Box<dyn FnMut(crate::Event) -> bool>) {
252 self.event_handlers.push(callback);
253 }
254
255 fn on_active_status_change(&mut self, callback: Box<dyn FnMut(bool)>) {
256 self.active_status_change_handlers.push(callback);
257 }
258
259 fn on_fullscreen(&mut self, callback: Box<dyn FnMut(bool)>) {
260 self.fullscreen_handlers.push(callback)
261 }
262
263 fn on_resize(&mut self, callback: Box<dyn FnMut()>) {
264 self.resize_handlers.push(callback);
265 }
266
267 fn on_close(&mut self, callback: Box<dyn FnOnce()>) {
268 self.close_handlers.push(callback);
269 }
270
271 fn set_input_handler(&mut self, _: Box<dyn crate::InputHandler>) {}
272
273 fn prompt(&self, _: crate::PromptLevel, _: &str, _: &[&str]) -> oneshot::Receiver<usize> {
274 let (done_tx, done_rx) = oneshot::channel();
275 self.pending_prompts.borrow_mut().push_back(done_tx);
276 done_rx
277 }
278
279 fn activate(&self) {}
280
281 fn set_title(&mut self, title: &str) {
282 self.title = Some(title.to_string())
283 }
284
285 fn set_edited(&mut self, edited: bool) {
286 self.edited = edited;
287 }
288
289 fn on_should_close(&mut self, callback: Box<dyn FnMut() -> bool>) {
290 self.should_close_handler = Some(callback);
291 }
292
293 fn show_character_palette(&self) {}
294
295 fn minimize(&self) {}
296
297 fn zoom(&self) {}
298
299 fn toggle_full_screen(&self) {}
300
301 fn bounds(&self) -> RectF {
302 RectF::new(Default::default(), self.size)
303 }
304
305 fn content_size(&self) -> Vector2F {
306 self.size
307 }
308
309 fn scale_factor(&self) -> f32 {
310 self.scale_factor
311 }
312
313 fn titlebar_height(&self) -> f32 {
314 24.
315 }
316
317 fn present_scene(&mut self, scene: crate::Scene) {
318 self.current_scene = Some(scene);
319 }
320
321 fn appearance(&self) -> crate::Appearance {
322 crate::Appearance::Light
323 }
324
325 fn on_appearance_changed(&mut self, _: Box<dyn FnMut()>) {}
326}
327
328pub fn platform() -> Platform {
329 Platform::new()
330}
331
332pub fn foreground_platform() -> ForegroundPlatform {
333 ForegroundPlatform::default()
334}