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