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