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(&self, _: super::PathPromptOptions) -> Option<Vec<std::path::PathBuf>> {
74 None
75 }
76
77 fn write_to_clipboard(&self, item: ClipboardItem) {
78 *self.current_clipboard_item.borrow_mut() = Some(item);
79 }
80
81 fn read_from_clipboard(&self) -> Option<ClipboardItem> {
82 self.current_clipboard_item.borrow().clone()
83 }
84}
85
86impl Window {
87 fn new(size: Vector2F) -> Self {
88 Self {
89 size,
90 event_handlers: Vec::new(),
91 resize_handlers: Vec::new(),
92 scale_factor: 1.0,
93 current_scene: None,
94 }
95 }
96}
97
98impl super::Dispatcher for Dispatcher {
99 fn is_main_thread(&self) -> bool {
100 true
101 }
102
103 fn run_on_main_thread(&self, task: async_task::Runnable) {
104 task.run();
105 }
106}
107
108impl super::WindowContext for Window {
109 fn size(&self) -> Vector2F {
110 self.size
111 }
112
113 fn scale_factor(&self) -> f32 {
114 self.scale_factor
115 }
116
117 fn present_scene(&mut self, scene: crate::Scene) {
118 self.current_scene = Some(scene);
119 }
120}
121
122impl super::Window for Window {
123 fn on_event(&mut self, callback: Box<dyn FnMut(crate::Event)>) {
124 self.event_handlers.push(callback);
125 }
126
127 fn on_resize(&mut self, callback: Box<dyn FnMut(&mut dyn super::WindowContext)>) {
128 self.resize_handlers.push(callback);
129 }
130}
131
132pub fn platform() -> impl super::Platform {
133 Platform::new()
134}