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