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