1use crate::ClipboardItem;
2use pathfinder_geometry::vector::Vector2F;
3use std::{any::Any, cell::RefCell, rc::Rc, sync::Arc};
4
5struct Platform {
6 dispatcher: Arc<dyn super::Dispatcher>,
7 fonts: Arc<dyn super::FontSystem>,
8 current_clipboard_item: RefCell<Option<ClipboardItem>>,
9}
10
11struct Dispatcher;
12
13pub struct Window {
14 size: Vector2F,
15 scale_factor: f32,
16 current_scene: Option<crate::Scene>,
17 event_handlers: Vec<Box<dyn FnMut(super::Event)>>,
18 resize_handlers: Vec<Box<dyn FnMut(&mut dyn super::WindowContext)>>,
19}
20
21impl Platform {
22 fn new() -> Self {
23 Self {
24 dispatcher: Arc::new(Dispatcher),
25 fonts: Arc::new(super::current::FontSystem::new()),
26 current_clipboard_item: RefCell::new(None),
27 }
28 }
29}
30
31impl super::Platform for Platform {
32 fn on_menu_command(&self, _: Box<dyn FnMut(&str, Option<&dyn Any>)>) {}
33
34 fn on_become_active(&self, _: Box<dyn FnMut()>) {}
35
36 fn on_resign_active(&self, _: Box<dyn FnMut()>) {}
37
38 fn on_event(&self, _: Box<dyn FnMut(crate::Event) -> bool>) {}
39
40 fn on_open_files(&self, _: Box<dyn FnMut(Vec<std::path::PathBuf>)>) {}
41
42 fn run(&self, _on_finish_launching: Box<dyn FnOnce() -> ()>) {
43 unimplemented!()
44 }
45
46 fn dispatcher(&self) -> Arc<dyn super::Dispatcher> {
47 self.dispatcher.clone()
48 }
49
50 fn fonts(&self) -> std::sync::Arc<dyn super::FontSystem> {
51 self.fonts.clone()
52 }
53
54 fn activate(&self, _ignoring_other_apps: bool) {}
55
56 fn open_window(
57 &self,
58 _: usize,
59 options: super::WindowOptions,
60 _executor: Rc<super::executor::Foreground>,
61 ) -> Box<dyn super::Window> {
62 Box::new(Window::new(options.bounds.size()))
63 }
64
65 fn key_window_id(&self) -> Option<usize> {
66 None
67 }
68
69 fn set_menus(&self, _menus: Vec<crate::Menu>) {}
70
71 fn quit(&self) {}
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 write_to_clipboard(&self, item: ClipboardItem) {
81 *self.current_clipboard_item.borrow_mut() = Some(item);
82 }
83
84 fn read_from_clipboard(&self) -> Option<ClipboardItem> {
85 self.current_clipboard_item.borrow().clone()
86 }
87}
88
89impl Window {
90 fn new(size: Vector2F) -> Self {
91 Self {
92 size,
93 event_handlers: Vec::new(),
94 resize_handlers: Vec::new(),
95 scale_factor: 1.0,
96 current_scene: None,
97 }
98 }
99}
100
101impl super::Dispatcher for Dispatcher {
102 fn is_main_thread(&self) -> bool {
103 true
104 }
105
106 fn run_on_main_thread(&self, task: async_task::Runnable) {
107 task.run();
108 }
109}
110
111impl super::WindowContext for Window {
112 fn size(&self) -> Vector2F {
113 self.size
114 }
115
116 fn scale_factor(&self) -> f32 {
117 self.scale_factor
118 }
119
120 fn present_scene(&mut self, scene: crate::Scene) {
121 self.current_scene = Some(scene);
122 }
123}
124
125impl super::Window for Window {
126 fn on_event(&mut self, callback: Box<dyn FnMut(crate::Event)>) {
127 self.event_handlers.push(callback);
128 }
129
130 fn on_resize(&mut self, callback: Box<dyn FnMut(&mut dyn super::WindowContext)>) {
131 self.resize_handlers.push(callback);
132 }
133}
134
135pub fn platform() -> impl super::Platform {
136 Platform::new()
137}