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