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