1use crate::ClipboardItem;
2use pathfinder_geometry::vector::Vector2F;
3use std::{
4 any::Any,
5 cell::RefCell,
6 path::{Path, PathBuf},
7 rc::Rc,
8 sync::Arc,
9};
10
11pub(crate) struct Platform {
12 dispatcher: Arc<dyn super::Dispatcher>,
13 fonts: Arc<dyn super::FontSystem>,
14 current_clipboard_item: RefCell<Option<ClipboardItem>>,
15 last_prompt_for_new_path_args: RefCell<Option<(PathBuf, Box<dyn FnOnce(Option<PathBuf>)>)>>,
16}
17
18struct Dispatcher;
19
20pub struct Window {
21 size: Vector2F,
22 scale_factor: f32,
23 current_scene: Option<crate::Scene>,
24 event_handlers: Vec<Box<dyn FnMut(super::Event)>>,
25 resize_handlers: Vec<Box<dyn FnMut(&mut dyn super::WindowContext)>>,
26}
27
28impl Platform {
29 fn new() -> Self {
30 Self {
31 dispatcher: Arc::new(Dispatcher),
32 fonts: Arc::new(super::current::FontSystem::new()),
33 current_clipboard_item: Default::default(),
34 last_prompt_for_new_path_args: Default::default(),
35 }
36 }
37
38 pub(crate) fn simulate_new_path_selection(
39 &self,
40 result: impl FnOnce(PathBuf) -> Option<PathBuf>,
41 ) {
42 let (dir_path, callback) = self
43 .last_prompt_for_new_path_args
44 .take()
45 .expect("prompt_for_new_path was not called");
46 callback(result(dir_path));
47 }
48}
49
50impl super::Platform for Platform {
51 fn on_menu_command(&self, _: Box<dyn FnMut(&str, Option<&dyn Any>)>) {}
52
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 dispatcher(&self) -> Arc<dyn super::Dispatcher> {
66 self.dispatcher.clone()
67 }
68
69 fn fonts(&self) -> std::sync::Arc<dyn super::FontSystem> {
70 self.fonts.clone()
71 }
72
73 fn activate(&self, _ignoring_other_apps: bool) {}
74
75 fn open_window(
76 &self,
77 _: usize,
78 options: super::WindowOptions,
79 _executor: Rc<super::executor::Foreground>,
80 ) -> Box<dyn super::Window> {
81 Box::new(Window::new(options.bounds.size()))
82 }
83
84 fn key_window_id(&self) -> Option<usize> {
85 None
86 }
87
88 fn set_menus(&self, _menus: Vec<crate::Menu>) {}
89
90 fn quit(&self) {}
91
92 fn prompt_for_paths(
93 &self,
94 _: super::PathPromptOptions,
95 _: Box<dyn FnOnce(Option<Vec<std::path::PathBuf>>)>,
96 ) {
97 }
98
99 fn prompt_for_new_path(&self, path: &Path, f: Box<dyn FnOnce(Option<std::path::PathBuf>)>) {
100 *self.last_prompt_for_new_path_args.borrow_mut() = Some((path.to_path_buf(), f));
101 }
102
103 fn write_to_clipboard(&self, item: ClipboardItem) {
104 *self.current_clipboard_item.borrow_mut() = Some(item);
105 }
106
107 fn read_from_clipboard(&self) -> Option<ClipboardItem> {
108 self.current_clipboard_item.borrow().clone()
109 }
110}
111
112impl Window {
113 fn new(size: Vector2F) -> Self {
114 Self {
115 size,
116 event_handlers: Vec::new(),
117 resize_handlers: Vec::new(),
118 scale_factor: 1.0,
119 current_scene: None,
120 }
121 }
122}
123
124impl super::Dispatcher for Dispatcher {
125 fn is_main_thread(&self) -> bool {
126 true
127 }
128
129 fn run_on_main_thread(&self, task: async_task::Runnable) {
130 task.run();
131 }
132}
133
134impl super::WindowContext for Window {
135 fn size(&self) -> Vector2F {
136 self.size
137 }
138
139 fn scale_factor(&self) -> f32 {
140 self.scale_factor
141 }
142
143 fn present_scene(&mut self, scene: crate::Scene) {
144 self.current_scene = Some(scene);
145 }
146}
147
148impl super::Window for Window {
149 fn on_event(&mut self, callback: Box<dyn FnMut(crate::Event)>) {
150 self.event_handlers.push(callback);
151 }
152
153 fn on_resize(&mut self, callback: Box<dyn FnMut(&mut dyn super::WindowContext)>) {
154 self.resize_handlers.push(callback);
155 }
156}
157
158pub(crate) fn platform() -> Platform {
159 Platform::new()
160}