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