1use super::{AppVersion, CursorStyle, WindowBounds};
2use crate::{
3 geometry::{
4 rect::RectF,
5 vector::{vec2f, Vector2F},
6 },
7 keymap_matcher::KeymapMatcher,
8 Action, 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
65 fn on_resign_active(&self, _: Box<dyn FnMut()>) {}
66
67 fn on_quit(&self, _: Box<dyn FnMut()>) {}
68
69 fn on_event(&self, _: Box<dyn FnMut(crate::Event) -> bool>) {}
70
71 fn on_open_urls(&self, _: Box<dyn FnMut(Vec<String>)>) {}
72
73 fn run(&self, _on_finish_launching: Box<dyn FnOnce()>) {
74 unimplemented!()
75 }
76
77 fn on_menu_command(&self, _: Box<dyn FnMut(&dyn Action)>) {}
78 fn on_validate_menu_command(&self, _: Box<dyn FnMut(&dyn Action) -> bool>) {}
79 fn on_will_open_menu(&self, _: Box<dyn FnMut()>) {}
80 fn set_menus(&self, _: Vec<Menu>, _: &KeymapMatcher) {}
81
82 fn prompt_for_paths(
83 &self,
84 _: super::PathPromptOptions,
85 ) -> oneshot::Receiver<Option<Vec<PathBuf>>> {
86 let (_done_tx, done_rx) = oneshot::channel();
87 done_rx
88 }
89
90 fn prompt_for_new_path(&self, path: &Path) -> oneshot::Receiver<Option<PathBuf>> {
91 let (done_tx, done_rx) = oneshot::channel();
92 *self.last_prompt_for_new_path_args.borrow_mut() = Some((path.to_path_buf(), done_tx));
93 done_rx
94 }
95}
96
97pub fn platform() -> Platform {
98 Platform::new()
99}
100
101pub struct Platform {
102 dispatcher: Arc<dyn super::Dispatcher>,
103 fonts: Arc<dyn super::FontSystem>,
104 current_clipboard_item: Mutex<Option<ClipboardItem>>,
105 cursor: Mutex<CursorStyle>,
106}
107
108impl Platform {
109 fn new() -> Self {
110 Self {
111 dispatcher: Arc::new(Dispatcher),
112 fonts: Arc::new(super::current::FontSystem::new()),
113 current_clipboard_item: Default::default(),
114 cursor: Mutex::new(CursorStyle::Arrow),
115 }
116 }
117}
118
119impl super::Platform for Platform {
120 fn dispatcher(&self) -> Arc<dyn super::Dispatcher> {
121 self.dispatcher.clone()
122 }
123
124 fn fonts(&self) -> std::sync::Arc<dyn super::FontSystem> {
125 self.fonts.clone()
126 }
127
128 fn activate(&self, _ignoring_other_apps: bool) {}
129
130 fn hide(&self) {}
131
132 fn hide_other_apps(&self) {}
133
134 fn unhide_other_apps(&self) {}
135
136 fn quit(&self) {}
137
138 fn screen_by_id(&self, _id: uuid::Uuid) -> Option<Rc<dyn crate::Screen>> {
139 None
140 }
141
142 fn screens(&self) -> Vec<Rc<dyn crate::Screen>> {
143 Default::default()
144 }
145
146 fn open_window(
147 &self,
148 _: usize,
149 options: super::WindowOptions,
150 _executor: Rc<super::executor::Foreground>,
151 ) -> Box<dyn super::Window> {
152 Box::new(Window::new(match options.bounds {
153 WindowBounds::Maximized | WindowBounds::Fullscreen => vec2f(1024., 768.),
154 WindowBounds::Fixed(rect) => rect.size(),
155 }))
156 }
157
158 fn key_window_id(&self) -> Option<usize> {
159 None
160 }
161
162 fn add_status_item(&self) -> Box<dyn crate::Window> {
163 Box::new(Window::new(vec2f(24., 24.)))
164 }
165
166 fn write_to_clipboard(&self, item: ClipboardItem) {
167 *self.current_clipboard_item.lock() = Some(item);
168 }
169
170 fn read_from_clipboard(&self) -> Option<ClipboardItem> {
171 self.current_clipboard_item.lock().clone()
172 }
173
174 fn open_url(&self, _: &str) {}
175
176 fn reveal_path(&self, _: &Path) {}
177
178 fn write_credentials(&self, _: &str, _: &str, _: &[u8]) -> Result<()> {
179 Ok(())
180 }
181
182 fn read_credentials(&self, _: &str) -> Result<Option<(String, Vec<u8>)>> {
183 Ok(None)
184 }
185
186 fn delete_credentials(&self, _: &str) -> Result<()> {
187 Ok(())
188 }
189
190 fn set_cursor_style(&self, style: CursorStyle) {
191 *self.cursor.lock() = style;
192 }
193
194 fn should_auto_hide_scrollbars(&self) -> bool {
195 false
196 }
197
198 fn local_timezone(&self) -> UtcOffset {
199 UtcOffset::UTC
200 }
201
202 fn path_for_auxiliary_executable(&self, _name: &str) -> Result<PathBuf> {
203 Err(anyhow!("app not running inside a bundle"))
204 }
205
206 fn app_path(&self) -> Result<PathBuf> {
207 Err(anyhow!("app not running inside a bundle"))
208 }
209
210 fn app_version(&self) -> Result<AppVersion> {
211 Ok(AppVersion {
212 major: 1,
213 minor: 0,
214 patch: 0,
215 })
216 }
217
218 fn os_name(&self) -> &'static str {
219 "test"
220 }
221
222 fn os_version(&self) -> Result<AppVersion> {
223 Ok(AppVersion {
224 major: 1,
225 minor: 0,
226 patch: 0,
227 })
228 }
229}
230
231#[derive(Debug)]
232pub struct Screen;
233
234impl super::Screen for Screen {
235 fn as_any(&self) -> &dyn Any {
236 self
237 }
238
239 fn bounds(&self) -> RectF {
240 RectF::new(Vector2F::zero(), Vector2F::new(1920., 1080.))
241 }
242
243 fn display_uuid(&self) -> Option<uuid::Uuid> {
244 Some(uuid::Uuid::new_v4())
245 }
246}
247
248pub struct Window {
249 pub(crate) size: Vector2F,
250 scale_factor: f32,
251 current_scene: Option<crate::Scene>,
252 event_handlers: Vec<Box<dyn FnMut(super::Event) -> bool>>,
253 pub(crate) resize_handlers: Vec<Box<dyn FnMut()>>,
254 pub(crate) moved_handlers: Vec<Box<dyn FnMut()>>,
255 close_handlers: Vec<Box<dyn FnOnce()>>,
256 fullscreen_handlers: Vec<Box<dyn FnMut(bool)>>,
257 pub(crate) active_status_change_handlers: Vec<Box<dyn FnMut(bool)>>,
258 pub(crate) should_close_handler: Option<Box<dyn FnMut() -> bool>>,
259 pub(crate) title: Option<String>,
260 pub(crate) edited: bool,
261 pub(crate) pending_prompts: RefCell<VecDeque<oneshot::Sender<usize>>>,
262}
263
264impl Window {
265 fn new(size: Vector2F) -> Self {
266 Self {
267 size,
268 event_handlers: Default::default(),
269 resize_handlers: Default::default(),
270 moved_handlers: Default::default(),
271 close_handlers: Default::default(),
272 should_close_handler: Default::default(),
273 active_status_change_handlers: Default::default(),
274 fullscreen_handlers: Default::default(),
275 scale_factor: 1.0,
276 current_scene: None,
277 title: None,
278 edited: false,
279 pending_prompts: Default::default(),
280 }
281 }
282
283 pub fn title(&self) -> Option<String> {
284 self.title.clone()
285 }
286}
287
288impl super::Window for Window {
289 fn bounds(&self) -> WindowBounds {
290 WindowBounds::Fixed(RectF::new(Vector2F::zero(), self.size))
291 }
292
293 fn content_size(&self) -> Vector2F {
294 self.size
295 }
296
297 fn scale_factor(&self) -> f32 {
298 self.scale_factor
299 }
300
301 fn titlebar_height(&self) -> f32 {
302 24.
303 }
304
305 fn appearance(&self) -> crate::Appearance {
306 crate::Appearance::Light
307 }
308
309 fn screen(&self) -> Rc<dyn crate::Screen> {
310 Rc::new(Screen)
311 }
312
313 fn as_any_mut(&mut self) -> &mut dyn Any {
314 self
315 }
316
317 fn set_input_handler(&mut self, _: Box<dyn crate::InputHandler>) {}
318
319 fn prompt(&self, _: crate::PromptLevel, _: &str, _: &[&str]) -> oneshot::Receiver<usize> {
320 let (done_tx, done_rx) = oneshot::channel();
321 self.pending_prompts.borrow_mut().push_back(done_tx);
322 done_rx
323 }
324
325 fn activate(&self) {}
326
327 fn set_title(&mut self, title: &str) {
328 self.title = Some(title.to_string())
329 }
330
331 fn set_edited(&mut self, edited: bool) {
332 self.edited = edited;
333 }
334
335 fn show_character_palette(&self) {}
336
337 fn minimize(&self) {}
338
339 fn zoom(&self) {}
340
341 fn present_scene(&mut self, scene: crate::Scene) {
342 self.current_scene = Some(scene);
343 }
344
345 fn toggle_full_screen(&self) {}
346
347 fn on_event(&mut self, callback: Box<dyn FnMut(crate::Event) -> bool>) {
348 self.event_handlers.push(callback);
349 }
350
351 fn on_active_status_change(&mut self, callback: Box<dyn FnMut(bool)>) {
352 self.active_status_change_handlers.push(callback);
353 }
354
355 fn on_resize(&mut self, callback: Box<dyn FnMut()>) {
356 self.resize_handlers.push(callback);
357 }
358
359 fn on_fullscreen(&mut self, callback: Box<dyn FnMut(bool)>) {
360 self.fullscreen_handlers.push(callback)
361 }
362
363 fn on_moved(&mut self, callback: Box<dyn FnMut()>) {
364 self.moved_handlers.push(callback);
365 }
366
367 fn on_should_close(&mut self, callback: Box<dyn FnMut() -> bool>) {
368 self.should_close_handler = Some(callback);
369 }
370
371 fn on_close(&mut self, callback: Box<dyn FnOnce()>) {
372 self.close_handlers.push(callback);
373 }
374
375 fn on_appearance_changed(&mut self, _: Box<dyn FnMut()>) {}
376
377 fn is_topmost_for_position(&self, _position: Vector2F) -> bool {
378 true
379 }
380}