test.rs

  1use crate::ClipboardItem;
  2use pathfinder_geometry::vector::Vector2F;
  3use std::{any::Any, cell::RefCell, path::Path, 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 prompt_for_new_path(&self, _: &Path, _: Box<dyn FnOnce(Option<std::path::PathBuf>)>) {}
 81
 82    fn write_to_clipboard(&self, item: ClipboardItem) {
 83        *self.current_clipboard_item.borrow_mut() = Some(item);
 84    }
 85
 86    fn read_from_clipboard(&self) -> Option<ClipboardItem> {
 87        self.current_clipboard_item.borrow().clone()
 88    }
 89}
 90
 91impl Window {
 92    fn new(size: Vector2F) -> Self {
 93        Self {
 94            size,
 95            event_handlers: Vec::new(),
 96            resize_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
137pub fn platform() -> impl super::Platform {
138    Platform::new()
139}