test.rs

  1use super::CursorStyle;
  2use crate::{AnyAction, ClipboardItem};
  3use anyhow::Result;
  4use parking_lot::Mutex;
  5use pathfinder_geometry::vector::Vector2F;
  6use std::{
  7    any::Any,
  8    cell::RefCell,
  9    path::{Path, PathBuf},
 10    rc::Rc,
 11    sync::Arc,
 12};
 13use time::UtcOffset;
 14
 15pub struct Platform {
 16    dispatcher: Arc<dyn super::Dispatcher>,
 17    fonts: Arc<dyn super::FontSystem>,
 18    current_clipboard_item: Mutex<Option<ClipboardItem>>,
 19    cursor: Mutex<CursorStyle>,
 20}
 21
 22#[derive(Default)]
 23pub struct ForegroundPlatform {
 24    last_prompt_for_new_path_args: RefCell<Option<(PathBuf, Box<dyn FnOnce(Option<PathBuf>)>)>>,
 25}
 26
 27struct Dispatcher;
 28
 29pub struct Window {
 30    size: Vector2F,
 31    scale_factor: f32,
 32    current_scene: Option<crate::Scene>,
 33    event_handlers: Vec<Box<dyn FnMut(super::Event)>>,
 34    resize_handlers: Vec<Box<dyn FnMut()>>,
 35    close_handlers: Vec<Box<dyn FnOnce()>>,
 36    pub(crate) last_prompt: RefCell<Option<Box<dyn FnOnce(usize)>>>,
 37}
 38
 39impl ForegroundPlatform {
 40    pub(crate) fn simulate_new_path_selection(
 41        &self,
 42        result: impl FnOnce(PathBuf) -> Option<PathBuf>,
 43    ) {
 44        let (dir_path, callback) = self
 45            .last_prompt_for_new_path_args
 46            .take()
 47            .expect("prompt_for_new_path was not called");
 48        callback(result(dir_path));
 49    }
 50
 51    pub(crate) fn did_prompt_for_new_path(&self) -> bool {
 52        self.last_prompt_for_new_path_args.borrow().is_some()
 53    }
 54}
 55
 56impl super::ForegroundPlatform for ForegroundPlatform {
 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 on_menu_command(&self, _: Box<dyn FnMut(&dyn AnyAction)>) {}
 70
 71    fn set_menus(&self, _: Vec<crate::Menu>) {}
 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: &Path, f: Box<dyn FnOnce(Option<std::path::PathBuf>)>) {
 81        *self.last_prompt_for_new_path_args.borrow_mut() = Some((path.to_path_buf(), f));
 82    }
 83}
 84
 85impl Platform {
 86    fn new() -> Self {
 87        Self {
 88            dispatcher: Arc::new(Dispatcher),
 89            fonts: Arc::new(super::current::FontSystem::new()),
 90            current_clipboard_item: Default::default(),
 91            cursor: Mutex::new(CursorStyle::Arrow),
 92        }
 93    }
 94}
 95
 96impl super::Platform for Platform {
 97    fn dispatcher(&self) -> Arc<dyn super::Dispatcher> {
 98        self.dispatcher.clone()
 99    }
100
101    fn fonts(&self) -> std::sync::Arc<dyn super::FontSystem> {
102        self.fonts.clone()
103    }
104
105    fn activate(&self, _ignoring_other_apps: bool) {}
106
107    fn open_window(
108        &self,
109        _: usize,
110        options: super::WindowOptions,
111        _executor: Rc<super::executor::Foreground>,
112    ) -> Box<dyn super::Window> {
113        Box::new(Window::new(options.bounds.size()))
114    }
115
116    fn key_window_id(&self) -> Option<usize> {
117        None
118    }
119
120    fn quit(&self) {}
121
122    fn write_to_clipboard(&self, item: ClipboardItem) {
123        *self.current_clipboard_item.lock() = Some(item);
124    }
125
126    fn read_from_clipboard(&self) -> Option<ClipboardItem> {
127        self.current_clipboard_item.lock().clone()
128    }
129
130    fn open_url(&self, _: &str) {}
131
132    fn write_credentials(&self, _: &str, _: &str, _: &[u8]) -> Result<()> {
133        Ok(())
134    }
135
136    fn read_credentials(&self, _: &str) -> Result<Option<(String, Vec<u8>)>> {
137        Ok(None)
138    }
139
140    fn set_cursor_style(&self, style: CursorStyle) {
141        *self.cursor.lock() = style;
142    }
143
144    fn local_timezone(&self) -> UtcOffset {
145        UtcOffset::UTC
146    }
147}
148
149impl Window {
150    fn new(size: Vector2F) -> Self {
151        Self {
152            size,
153            event_handlers: Vec::new(),
154            resize_handlers: Vec::new(),
155            close_handlers: Vec::new(),
156            scale_factor: 1.0,
157            current_scene: None,
158            last_prompt: RefCell::new(None),
159        }
160    }
161}
162
163impl super::Dispatcher for Dispatcher {
164    fn is_main_thread(&self) -> bool {
165        true
166    }
167
168    fn run_on_main_thread(&self, task: async_task::Runnable) {
169        task.run();
170    }
171}
172
173impl super::WindowContext for Window {
174    fn size(&self) -> Vector2F {
175        self.size
176    }
177
178    fn scale_factor(&self) -> f32 {
179        self.scale_factor
180    }
181
182    fn titlebar_height(&self) -> f32 {
183        24.
184    }
185
186    fn present_scene(&mut self, scene: crate::Scene) {
187        self.current_scene = Some(scene);
188    }
189}
190
191impl super::Window for Window {
192    fn as_any_mut(&mut self) -> &mut dyn Any {
193        self
194    }
195
196    fn on_event(&mut self, callback: Box<dyn FnMut(crate::Event)>) {
197        self.event_handlers.push(callback);
198    }
199
200    fn on_resize(&mut self, callback: Box<dyn FnMut()>) {
201        self.resize_handlers.push(callback);
202    }
203
204    fn on_close(&mut self, callback: Box<dyn FnOnce()>) {
205        self.close_handlers.push(callback);
206    }
207
208    fn prompt(&self, _: crate::PromptLevel, _: &str, _: &[&str], f: Box<dyn FnOnce(usize)>) {
209        self.last_prompt.replace(Some(f));
210    }
211}
212
213pub fn platform() -> Platform {
214    Platform::new()
215}
216
217pub fn foreground_platform() -> ForegroundPlatform {
218    ForegroundPlatform::default()
219}