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