test.rs

  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    pub(crate) fn did_prompt_for_new_path(&self) -> bool {
 50        self.last_prompt_for_new_path_args.borrow().is_some()
 51    }
 52}
 53
 54impl super::Platform for Platform {
 55    fn on_menu_command(&self, _: Box<dyn FnMut(&str, Option<&dyn Any>)>) {}
 56
 57    fn on_become_active(&self, _: Box<dyn FnMut()>) {}
 58
 59    fn on_resign_active(&self, _: Box<dyn FnMut()>) {}
 60
 61    fn on_event(&self, _: Box<dyn FnMut(crate::Event) -> bool>) {}
 62
 63    fn on_open_files(&self, _: Box<dyn FnMut(Vec<std::path::PathBuf>)>) {}
 64
 65    fn run(&self, _on_finish_launching: Box<dyn FnOnce() -> ()>) {
 66        unimplemented!()
 67    }
 68
 69    fn dispatcher(&self) -> Arc<dyn super::Dispatcher> {
 70        self.dispatcher.clone()
 71    }
 72
 73    fn fonts(&self) -> std::sync::Arc<dyn super::FontSystem> {
 74        self.fonts.clone()
 75    }
 76
 77    fn activate(&self, _ignoring_other_apps: bool) {}
 78
 79    fn open_window(
 80        &self,
 81        _: usize,
 82        options: super::WindowOptions,
 83        _executor: Rc<super::executor::Foreground>,
 84    ) -> Box<dyn super::Window> {
 85        Box::new(Window::new(options.bounds.size()))
 86    }
 87
 88    fn key_window_id(&self) -> Option<usize> {
 89        None
 90    }
 91
 92    fn set_menus(&self, _menus: Vec<crate::Menu>) {}
 93
 94    fn quit(&self) {}
 95
 96    fn prompt_for_paths(
 97        &self,
 98        _: super::PathPromptOptions,
 99        _: Box<dyn FnOnce(Option<Vec<std::path::PathBuf>>)>,
100    ) {
101    }
102
103    fn prompt_for_new_path(&self, path: &Path, f: Box<dyn FnOnce(Option<std::path::PathBuf>)>) {
104        *self.last_prompt_for_new_path_args.borrow_mut() = Some((path.to_path_buf(), f));
105    }
106
107    fn write_to_clipboard(&self, item: ClipboardItem) {
108        *self.current_clipboard_item.borrow_mut() = Some(item);
109    }
110
111    fn read_from_clipboard(&self) -> Option<ClipboardItem> {
112        self.current_clipboard_item.borrow().clone()
113    }
114}
115
116impl Window {
117    fn new(size: Vector2F) -> Self {
118        Self {
119            size,
120            event_handlers: Vec::new(),
121            resize_handlers: Vec::new(),
122            scale_factor: 1.0,
123            current_scene: None,
124        }
125    }
126}
127
128impl super::Dispatcher for Dispatcher {
129    fn is_main_thread(&self) -> bool {
130        true
131    }
132
133    fn run_on_main_thread(&self, task: async_task::Runnable) {
134        task.run();
135    }
136}
137
138impl super::WindowContext for Window {
139    fn size(&self) -> Vector2F {
140        self.size
141    }
142
143    fn scale_factor(&self) -> f32 {
144        self.scale_factor
145    }
146
147    fn present_scene(&mut self, scene: crate::Scene) {
148        self.current_scene = Some(scene);
149    }
150}
151
152impl super::Window for Window {
153    fn on_event(&mut self, callback: Box<dyn FnMut(crate::Event)>) {
154        self.event_handlers.push(callback);
155    }
156
157    fn on_resize(&mut self, callback: Box<dyn FnMut(&mut dyn super::WindowContext)>) {
158        self.resize_handlers.push(callback);
159    }
160}
161
162pub(crate) fn platform() -> Platform {
163    Platform::new()
164}