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 size: Vector2F,
38 scale_factor: f32,
39 current_scene: Option<crate::Scene>,
40 event_handlers: Vec<Box<dyn FnMut(super::Event) -> bool>>,
41 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 open_window(
135 &self,
136 _: usize,
137 options: super::WindowOptions,
138 _executor: Rc<super::executor::Foreground>,
139 ) -> Box<dyn super::Window> {
140 Box::new(Window::new(match options.bounds {
141 WindowBounds::Maximized => vec2f(1024., 768.),
142 WindowBounds::Fixed(rect) => rect.size(),
143 }))
144 }
145
146 fn key_window_id(&self) -> Option<usize> {
147 None
148 }
149
150 fn add_status_item(&self) -> Box<dyn crate::Window> {
151 Box::new(Window::new(vec2f(24., 24.)))
152 }
153
154 fn write_to_clipboard(&self, item: ClipboardItem) {
155 *self.current_clipboard_item.lock() = Some(item);
156 }
157
158 fn read_from_clipboard(&self) -> Option<ClipboardItem> {
159 self.current_clipboard_item.lock().clone()
160 }
161
162 fn open_url(&self, _: &str) {}
163
164 fn write_credentials(&self, _: &str, _: &str, _: &[u8]) -> Result<()> {
165 Ok(())
166 }
167
168 fn read_credentials(&self, _: &str) -> Result<Option<(String, Vec<u8>)>> {
169 Ok(None)
170 }
171
172 fn delete_credentials(&self, _: &str) -> Result<()> {
173 Ok(())
174 }
175
176 fn set_cursor_style(&self, style: CursorStyle) {
177 *self.cursor.lock() = style;
178 }
179
180 fn local_timezone(&self) -> UtcOffset {
181 UtcOffset::UTC
182 }
183
184 fn path_for_auxiliary_executable(&self, _name: &str) -> Result<PathBuf> {
185 Err(anyhow!("app not running inside a bundle"))
186 }
187
188 fn app_path(&self) -> Result<PathBuf> {
189 Err(anyhow!("app not running inside a bundle"))
190 }
191
192 fn app_version(&self) -> Result<AppVersion> {
193 Ok(AppVersion {
194 major: 1,
195 minor: 0,
196 patch: 0,
197 })
198 }
199}
200
201impl Window {
202 fn new(size: Vector2F) -> Self {
203 Self {
204 size,
205 event_handlers: Default::default(),
206 resize_handlers: Default::default(),
207 close_handlers: Default::default(),
208 should_close_handler: Default::default(),
209 active_status_change_handlers: Default::default(),
210 fullscreen_handlers: Default::default(),
211 scale_factor: 1.0,
212 current_scene: None,
213 title: None,
214 edited: false,
215 pending_prompts: Default::default(),
216 }
217 }
218
219 pub fn title(&self) -> Option<String> {
220 self.title.clone()
221 }
222}
223
224impl super::Dispatcher for Dispatcher {
225 fn is_main_thread(&self) -> bool {
226 true
227 }
228
229 fn run_on_main_thread(&self, task: async_task::Runnable) {
230 task.run();
231 }
232}
233
234impl super::Window for Window {
235 fn as_any_mut(&mut self) -> &mut dyn Any {
236 self
237 }
238
239 fn on_event(&mut self, callback: Box<dyn FnMut(crate::Event) -> bool>) {
240 self.event_handlers.push(callback);
241 }
242
243 fn on_active_status_change(&mut self, callback: Box<dyn FnMut(bool)>) {
244 self.active_status_change_handlers.push(callback);
245 }
246
247 fn on_fullscreen(&mut self, callback: Box<dyn FnMut(bool)>) {
248 self.fullscreen_handlers.push(callback)
249 }
250
251 fn on_resize(&mut self, callback: Box<dyn FnMut()>) {
252 self.resize_handlers.push(callback);
253 }
254
255 fn on_close(&mut self, callback: Box<dyn FnOnce()>) {
256 self.close_handlers.push(callback);
257 }
258
259 fn set_input_handler(&mut self, _: Box<dyn crate::InputHandler>) {}
260
261 fn prompt(&self, _: crate::PromptLevel, _: &str, _: &[&str]) -> oneshot::Receiver<usize> {
262 let (done_tx, done_rx) = oneshot::channel();
263 self.pending_prompts.borrow_mut().push_back(done_tx);
264 done_rx
265 }
266
267 fn activate(&self) {}
268
269 fn set_title(&mut self, title: &str) {
270 self.title = Some(title.to_string())
271 }
272
273 fn set_edited(&mut self, edited: bool) {
274 self.edited = edited;
275 }
276
277 fn on_should_close(&mut self, callback: Box<dyn FnMut() -> bool>) {
278 self.should_close_handler = Some(callback);
279 }
280
281 fn show_character_palette(&self) {}
282
283 fn minimize(&self) {}
284
285 fn zoom(&self) {}
286
287 fn toggle_full_screen(&self) {}
288
289 fn bounds(&self) -> RectF {
290 RectF::new(Default::default(), self.size)
291 }
292
293 fn content_size(&self) -> Vector2F {
294 self.size
295 }
296
297 fn scale_factor(&self) -> f32 {
298 self.scale_factor
299 }
300
301 fn titlebar_height(&self) -> f32 {
302 24.
303 }
304
305 fn present_scene(&mut self, scene: crate::Scene) {
306 self.current_scene = Some(scene);
307 }
308
309 fn appearance(&self) -> crate::Appearance {
310 crate::Appearance::Light
311 }
312
313 fn on_appearance_changed(&mut self, _: Box<dyn FnMut()>) {}
314}
315
316pub fn platform() -> Platform {
317 Platform::new()
318}
319
320pub fn foreground_platform() -> ForegroundPlatform {
321 ForegroundPlatform::default()
322}