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