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