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