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