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