test.rs

  1use crate::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(crate) 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(crate) struct MainThreadPlatform {
 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 MainThreadPlatform {
 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::MainThreadPlatform for MainThreadPlatform {
 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(&str, Option<&dyn Any>)>) {}
 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
126impl Window {
127    fn new(size: Vector2F) -> Self {
128        Self {
129            size,
130            event_handlers: Vec::new(),
131            resize_handlers: Vec::new(),
132            close_handlers: Vec::new(),
133            scale_factor: 1.0,
134            current_scene: None,
135            last_prompt: RefCell::new(None),
136        }
137    }
138}
139
140impl super::Dispatcher for Dispatcher {
141    fn is_main_thread(&self) -> bool {
142        true
143    }
144
145    fn run_on_main_thread(&self, task: async_task::Runnable) {
146        task.run();
147    }
148}
149
150impl super::WindowContext for Window {
151    fn size(&self) -> Vector2F {
152        self.size
153    }
154
155    fn scale_factor(&self) -> f32 {
156        self.scale_factor
157    }
158
159    fn present_scene(&mut self, scene: crate::Scene) {
160        self.current_scene = Some(scene);
161    }
162}
163
164impl super::Window for Window {
165    fn as_any_mut(&mut self) -> &mut dyn Any {
166        self
167    }
168
169    fn on_event(&mut self, callback: Box<dyn FnMut(crate::Event)>) {
170        self.event_handlers.push(callback);
171    }
172
173    fn on_resize(&mut self, callback: Box<dyn FnMut(&mut dyn super::WindowContext)>) {
174        self.resize_handlers.push(callback);
175    }
176
177    fn on_close(&mut self, callback: Box<dyn FnOnce()>) {
178        self.close_handlers.push(callback);
179    }
180
181    fn prompt(&self, _: crate::PromptLevel, _: &str, _: &[&str], f: Box<dyn FnOnce(usize)>) {
182        self.last_prompt.replace(Some(f));
183    }
184}
185
186pub(crate) fn main_thread_platform() -> MainThreadPlatform {
187    MainThreadPlatform::default()
188}
189
190pub(crate) fn platform() -> Platform {
191    Platform::new()
192}