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