test.rs

  1use super::{AppVersion, CursorStyle, WindowBounds};
  2use crate::{
  3    geometry::{
  4        rect::RectF,
  5        vector::{vec2f, Vector2F},
  6    },
  7    keymap_matcher::KeymapMatcher,
  8    Action, ClipboardItem, Menu,
  9};
 10use anyhow::{anyhow, Result};
 11use collections::VecDeque;
 12use parking_lot::Mutex;
 13use postage::oneshot;
 14use std::{
 15    any::Any,
 16    cell::RefCell,
 17    path::{Path, PathBuf},
 18    rc::Rc,
 19    sync::Arc,
 20};
 21use time::UtcOffset;
 22
 23struct Dispatcher;
 24
 25impl super::Dispatcher for Dispatcher {
 26    fn is_main_thread(&self) -> bool {
 27        true
 28    }
 29
 30    fn run_on_main_thread(&self, task: async_task::Runnable) {
 31        task.run();
 32    }
 33}
 34
 35pub fn foreground_platform() -> ForegroundPlatform {
 36    ForegroundPlatform::default()
 37}
 38
 39#[derive(Default)]
 40pub struct ForegroundPlatform {
 41    last_prompt_for_new_path_args: RefCell<Option<(PathBuf, oneshot::Sender<Option<PathBuf>>)>>,
 42}
 43
 44#[cfg(any(test, feature = "test-support"))]
 45impl ForegroundPlatform {
 46    pub(crate) fn simulate_new_path_selection(
 47        &self,
 48        result: impl FnOnce(PathBuf) -> Option<PathBuf>,
 49    ) {
 50        let (dir_path, mut done_tx) = self
 51            .last_prompt_for_new_path_args
 52            .take()
 53            .expect("prompt_for_new_path was not called");
 54        let _ = postage::sink::Sink::try_send(&mut done_tx, result(dir_path));
 55    }
 56
 57    pub(crate) fn did_prompt_for_new_path(&self) -> bool {
 58        self.last_prompt_for_new_path_args.borrow().is_some()
 59    }
 60}
 61
 62impl super::ForegroundPlatform for ForegroundPlatform {
 63    fn on_become_active(&self, _: Box<dyn FnMut()>) {}
 64
 65    fn on_resign_active(&self, _: Box<dyn FnMut()>) {}
 66
 67    fn on_quit(&self, _: Box<dyn FnMut()>) {}
 68
 69    fn on_event(&self, _: Box<dyn FnMut(crate::Event) -> bool>) {}
 70
 71    fn on_open_urls(&self, _: Box<dyn FnMut(Vec<String>)>) {}
 72
 73    fn run(&self, _on_finish_launching: Box<dyn FnOnce()>) {
 74        unimplemented!()
 75    }
 76
 77    fn on_menu_command(&self, _: Box<dyn FnMut(&dyn Action)>) {}
 78    fn on_validate_menu_command(&self, _: Box<dyn FnMut(&dyn Action) -> bool>) {}
 79    fn on_will_open_menu(&self, _: Box<dyn FnMut()>) {}
 80    fn set_menus(&self, _: Vec<Menu>, _: &KeymapMatcher) {}
 81
 82    fn prompt_for_paths(
 83        &self,
 84        _: super::PathPromptOptions,
 85    ) -> oneshot::Receiver<Option<Vec<PathBuf>>> {
 86        let (_done_tx, done_rx) = oneshot::channel();
 87        done_rx
 88    }
 89
 90    fn prompt_for_new_path(&self, path: &Path) -> oneshot::Receiver<Option<PathBuf>> {
 91        let (done_tx, done_rx) = oneshot::channel();
 92        *self.last_prompt_for_new_path_args.borrow_mut() = Some((path.to_path_buf(), done_tx));
 93        done_rx
 94    }
 95
 96    fn reveal_path(&self, _: &Path) {}
 97}
 98
 99pub fn platform() -> Platform {
100    Platform::new()
101}
102
103pub struct Platform {
104    dispatcher: Arc<dyn super::Dispatcher>,
105    fonts: Arc<dyn super::FontSystem>,
106    current_clipboard_item: Mutex<Option<ClipboardItem>>,
107    cursor: Mutex<CursorStyle>,
108}
109
110impl Platform {
111    fn new() -> Self {
112        Self {
113            dispatcher: Arc::new(Dispatcher),
114            fonts: Arc::new(super::current::FontSystem::new()),
115            current_clipboard_item: Default::default(),
116            cursor: Mutex::new(CursorStyle::Arrow),
117        }
118    }
119}
120
121impl super::Platform for Platform {
122    fn dispatcher(&self) -> Arc<dyn super::Dispatcher> {
123        self.dispatcher.clone()
124    }
125
126    fn fonts(&self) -> std::sync::Arc<dyn super::FontSystem> {
127        self.fonts.clone()
128    }
129
130    fn activate(&self, _ignoring_other_apps: bool) {}
131
132    fn hide(&self) {}
133
134    fn hide_other_apps(&self) {}
135
136    fn unhide_other_apps(&self) {}
137
138    fn quit(&self) {}
139
140    fn screen_by_id(&self, _id: uuid::Uuid) -> Option<Rc<dyn crate::Screen>> {
141        None
142    }
143
144    fn screens(&self) -> Vec<Rc<dyn crate::Screen>> {
145        Default::default()
146    }
147
148    fn open_window(
149        &self,
150        _: usize,
151        options: super::WindowOptions,
152        _executor: Rc<super::executor::Foreground>,
153    ) -> Box<dyn super::Window> {
154        Box::new(Window::new(match options.bounds {
155            WindowBounds::Maximized | WindowBounds::Fullscreen => vec2f(1024., 768.),
156            WindowBounds::Fixed(rect) => rect.size(),
157        }))
158    }
159
160    fn main_window_id(&self) -> Option<usize> {
161        None
162    }
163
164    fn add_status_item(&self) -> Box<dyn crate::Window> {
165        Box::new(Window::new(vec2f(24., 24.)))
166    }
167
168    fn write_to_clipboard(&self, item: ClipboardItem) {
169        *self.current_clipboard_item.lock() = Some(item);
170    }
171
172    fn read_from_clipboard(&self) -> Option<ClipboardItem> {
173        self.current_clipboard_item.lock().clone()
174    }
175
176    fn open_url(&self, _: &str) {}
177
178    fn write_credentials(&self, _: &str, _: &str, _: &[u8]) -> Result<()> {
179        Ok(())
180    }
181
182    fn read_credentials(&self, _: &str) -> Result<Option<(String, Vec<u8>)>> {
183        Ok(None)
184    }
185
186    fn delete_credentials(&self, _: &str) -> Result<()> {
187        Ok(())
188    }
189
190    fn set_cursor_style(&self, style: CursorStyle) {
191        *self.cursor.lock() = style;
192    }
193
194    fn should_auto_hide_scrollbars(&self) -> bool {
195        false
196    }
197
198    fn local_timezone(&self) -> UtcOffset {
199        UtcOffset::UTC
200    }
201
202    fn path_for_auxiliary_executable(&self, _name: &str) -> Result<PathBuf> {
203        Err(anyhow!("app not running inside a bundle"))
204    }
205
206    fn app_path(&self) -> Result<PathBuf> {
207        Err(anyhow!("app not running inside a bundle"))
208    }
209
210    fn app_version(&self) -> Result<AppVersion> {
211        Ok(AppVersion {
212            major: 1,
213            minor: 0,
214            patch: 0,
215        })
216    }
217
218    fn os_name(&self) -> &'static str {
219        "test"
220    }
221
222    fn os_version(&self) -> Result<AppVersion> {
223        Ok(AppVersion {
224            major: 1,
225            minor: 0,
226            patch: 0,
227        })
228    }
229
230    fn restart(&self) {}
231}
232
233#[derive(Debug)]
234pub struct Screen;
235
236impl super::Screen for Screen {
237    fn as_any(&self) -> &dyn Any {
238        self
239    }
240
241    fn bounds(&self) -> RectF {
242        RectF::new(Vector2F::zero(), Vector2F::new(1920., 1080.))
243    }
244
245    fn display_uuid(&self) -> Option<uuid::Uuid> {
246        Some(uuid::Uuid::new_v4())
247    }
248}
249
250pub struct Window {
251    pub(crate) size: Vector2F,
252    scale_factor: f32,
253    current_scene: Option<crate::Scene>,
254    event_handlers: Vec<Box<dyn FnMut(super::Event) -> bool>>,
255    pub(crate) resize_handlers: Vec<Box<dyn FnMut()>>,
256    pub(crate) moved_handlers: Vec<Box<dyn FnMut()>>,
257    close_handlers: Vec<Box<dyn FnOnce()>>,
258    fullscreen_handlers: Vec<Box<dyn FnMut(bool)>>,
259    pub(crate) active_status_change_handlers: Vec<Box<dyn FnMut(bool)>>,
260    pub(crate) should_close_handler: Option<Box<dyn FnMut() -> bool>>,
261    pub(crate) title: Option<String>,
262    pub(crate) edited: bool,
263    pub(crate) pending_prompts: RefCell<VecDeque<oneshot::Sender<usize>>>,
264}
265
266impl Window {
267    fn new(size: Vector2F) -> Self {
268        Self {
269            size,
270            event_handlers: Default::default(),
271            resize_handlers: Default::default(),
272            moved_handlers: Default::default(),
273            close_handlers: Default::default(),
274            should_close_handler: Default::default(),
275            active_status_change_handlers: Default::default(),
276            fullscreen_handlers: Default::default(),
277            scale_factor: 1.0,
278            current_scene: None,
279            title: None,
280            edited: false,
281            pending_prompts: Default::default(),
282        }
283    }
284
285    pub fn title(&self) -> Option<String> {
286        self.title.clone()
287    }
288}
289
290impl super::Window for Window {
291    fn bounds(&self) -> WindowBounds {
292        WindowBounds::Fixed(RectF::new(Vector2F::zero(), self.size))
293    }
294
295    fn content_size(&self) -> Vector2F {
296        self.size
297    }
298
299    fn scale_factor(&self) -> f32 {
300        self.scale_factor
301    }
302
303    fn titlebar_height(&self) -> f32 {
304        24.
305    }
306
307    fn appearance(&self) -> crate::Appearance {
308        crate::Appearance::Light
309    }
310
311    fn screen(&self) -> Rc<dyn crate::Screen> {
312        Rc::new(Screen)
313    }
314
315    fn as_any_mut(&mut self) -> &mut dyn Any {
316        self
317    }
318
319    fn set_input_handler(&mut self, _: Box<dyn crate::InputHandler>) {}
320
321    fn prompt(&self, _: crate::PromptLevel, _: &str, _: &[&str]) -> oneshot::Receiver<usize> {
322        let (done_tx, done_rx) = oneshot::channel();
323        self.pending_prompts.borrow_mut().push_back(done_tx);
324        done_rx
325    }
326
327    fn activate(&self) {}
328
329    fn set_title(&mut self, title: &str) {
330        self.title = Some(title.to_string())
331    }
332
333    fn set_edited(&mut self, edited: bool) {
334        self.edited = edited;
335    }
336
337    fn show_character_palette(&self) {}
338
339    fn minimize(&self) {}
340
341    fn zoom(&self) {}
342
343    fn present_scene(&mut self, scene: crate::Scene) {
344        self.current_scene = Some(scene);
345    }
346
347    fn toggle_full_screen(&self) {}
348
349    fn on_event(&mut self, callback: Box<dyn FnMut(crate::Event) -> bool>) {
350        self.event_handlers.push(callback);
351    }
352
353    fn on_active_status_change(&mut self, callback: Box<dyn FnMut(bool)>) {
354        self.active_status_change_handlers.push(callback);
355    }
356
357    fn on_resize(&mut self, callback: Box<dyn FnMut()>) {
358        self.resize_handlers.push(callback);
359    }
360
361    fn on_fullscreen(&mut self, callback: Box<dyn FnMut(bool)>) {
362        self.fullscreen_handlers.push(callback)
363    }
364
365    fn on_moved(&mut self, callback: Box<dyn FnMut()>) {
366        self.moved_handlers.push(callback);
367    }
368
369    fn on_should_close(&mut self, callback: Box<dyn FnMut() -> bool>) {
370        self.should_close_handler = Some(callback);
371    }
372
373    fn on_close(&mut self, callback: Box<dyn FnOnce()>) {
374        self.close_handlers.push(callback);
375    }
376
377    fn on_appearance_changed(&mut self, _: Box<dyn FnMut()>) {}
378
379    fn is_topmost_for_position(&self, _position: Vector2F) -> bool {
380        true
381    }
382}