1use super::{CursorStyle, WindowBounds};
2use crate::{
3 geometry::vector::{vec2f, Vector2F},
4 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
77 fn set_menus(&self, _: Vec<crate::Menu>) {}
78
79 fn prompt_for_paths(
80 &self,
81 _: super::PathPromptOptions,
82 ) -> oneshot::Receiver<Option<Vec<PathBuf>>> {
83 let (_done_tx, done_rx) = oneshot::channel();
84 done_rx
85 }
86
87 fn prompt_for_new_path(&self, path: &Path) -> oneshot::Receiver<Option<PathBuf>> {
88 let (done_tx, done_rx) = oneshot::channel();
89 *self.last_prompt_for_new_path_args.borrow_mut() = Some((path.to_path_buf(), done_tx));
90 done_rx
91 }
92}
93
94impl Platform {
95 fn new() -> Self {
96 Self {
97 dispatcher: Arc::new(Dispatcher),
98 fonts: Arc::new(super::current::FontSystem::new()),
99 current_clipboard_item: Default::default(),
100 cursor: Mutex::new(CursorStyle::Arrow),
101 }
102 }
103}
104
105impl super::Platform for Platform {
106 fn dispatcher(&self) -> Arc<dyn super::Dispatcher> {
107 self.dispatcher.clone()
108 }
109
110 fn fonts(&self) -> std::sync::Arc<dyn super::FontSystem> {
111 self.fonts.clone()
112 }
113
114 fn activate(&self, _ignoring_other_apps: bool) {}
115
116 fn open_window(
117 &self,
118 _: usize,
119 options: super::WindowOptions,
120 _executor: Rc<super::executor::Foreground>,
121 ) -> Box<dyn super::Window> {
122 Box::new(Window::new(match options.bounds {
123 WindowBounds::Maximized => vec2f(1024., 768.),
124 WindowBounds::Fixed(rect) => rect.size(),
125 }))
126 }
127
128 fn key_window_id(&self) -> Option<usize> {
129 None
130 }
131
132 fn quit(&self) {}
133
134 fn write_to_clipboard(&self, item: ClipboardItem) {
135 *self.current_clipboard_item.lock() = Some(item);
136 }
137
138 fn read_from_clipboard(&self) -> Option<ClipboardItem> {
139 self.current_clipboard_item.lock().clone()
140 }
141
142 fn open_url(&self, _: &str) {}
143
144 fn write_credentials(&self, _: &str, _: &str, _: &[u8]) -> Result<()> {
145 Ok(())
146 }
147
148 fn read_credentials(&self, _: &str) -> Result<Option<(String, Vec<u8>)>> {
149 Ok(None)
150 }
151
152 fn delete_credentials(&self, _: &str) -> Result<()> {
153 Ok(())
154 }
155
156 fn set_cursor_style(&self, style: CursorStyle) {
157 *self.cursor.lock() = style;
158 }
159
160 fn local_timezone(&self) -> UtcOffset {
161 UtcOffset::UTC
162 }
163
164 fn path_for_auxiliary_executable(&self, _name: &str) -> Result<PathBuf> {
165 Err(anyhow!("app not running inside a bundle"))
166 }
167}
168
169impl Window {
170 fn new(size: Vector2F) -> Self {
171 Self {
172 size,
173 event_handlers: Vec::new(),
174 resize_handlers: Vec::new(),
175 close_handlers: Vec::new(),
176 scale_factor: 1.0,
177 current_scene: None,
178 last_prompt: Default::default(),
179 }
180 }
181}
182
183impl super::Dispatcher for Dispatcher {
184 fn is_main_thread(&self) -> bool {
185 true
186 }
187
188 fn run_on_main_thread(&self, task: async_task::Runnable) {
189 task.run();
190 }
191}
192
193impl super::WindowContext for Window {
194 fn size(&self) -> Vector2F {
195 self.size
196 }
197
198 fn scale_factor(&self) -> f32 {
199 self.scale_factor
200 }
201
202 fn titlebar_height(&self) -> f32 {
203 24.
204 }
205
206 fn present_scene(&mut self, scene: crate::Scene) {
207 self.current_scene = Some(scene);
208 }
209}
210
211impl super::Window for Window {
212 fn as_any_mut(&mut self) -> &mut dyn Any {
213 self
214 }
215
216 fn on_event(&mut self, callback: Box<dyn FnMut(crate::Event)>) {
217 self.event_handlers.push(callback);
218 }
219
220 fn on_resize(&mut self, callback: Box<dyn FnMut()>) {
221 self.resize_handlers.push(callback);
222 }
223
224 fn on_close(&mut self, callback: Box<dyn FnOnce()>) {
225 self.close_handlers.push(callback);
226 }
227
228 fn prompt(&self, _: crate::PromptLevel, _: &str, _: &[&str]) -> oneshot::Receiver<usize> {
229 let (done_tx, done_rx) = oneshot::channel();
230 self.last_prompt.replace(Some(done_tx));
231 done_rx
232 }
233
234 fn activate(&self) {}
235}
236
237pub fn platform() -> Platform {
238 Platform::new()
239}
240
241pub fn foreground_platform() -> ForegroundPlatform {
242 ForegroundPlatform::default()
243}