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