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