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
 97pub fn platform() -> Platform {
 98    Platform::new()
 99}
100
101pub struct Platform {
102    dispatcher: Arc<dyn super::Dispatcher>,
103    fonts: Arc<dyn super::FontSystem>,
104    current_clipboard_item: Mutex<Option<ClipboardItem>>,
105    cursor: Mutex<CursorStyle>,
106}
107
108impl Platform {
109    fn new() -> Self {
110        Self {
111            dispatcher: Arc::new(Dispatcher),
112            fonts: Arc::new(super::current::FontSystem::new()),
113            current_clipboard_item: Default::default(),
114            cursor: Mutex::new(CursorStyle::Arrow),
115        }
116    }
117}
118
119impl super::Platform for Platform {
120    fn dispatcher(&self) -> Arc<dyn super::Dispatcher> {
121        self.dispatcher.clone()
122    }
123
124    fn fonts(&self) -> std::sync::Arc<dyn super::FontSystem> {
125        self.fonts.clone()
126    }
127
128    fn activate(&self, _ignoring_other_apps: bool) {}
129
130    fn hide(&self) {}
131
132    fn hide_other_apps(&self) {}
133
134    fn unhide_other_apps(&self) {}
135
136    fn quit(&self) {}
137
138    fn screen_by_id(&self, _id: uuid::Uuid) -> Option<Rc<dyn crate::Screen>> {
139        None
140    }
141
142    fn screens(&self) -> Vec<Rc<dyn crate::Screen>> {
143        Default::default()
144    }
145
146    fn open_window(
147        &self,
148        _: usize,
149        options: super::WindowOptions,
150        _executor: Rc<super::executor::Foreground>,
151    ) -> Box<dyn super::Window> {
152        Box::new(Window::new(match options.bounds {
153            WindowBounds::Maximized | WindowBounds::Fullscreen => vec2f(1024., 768.),
154            WindowBounds::Fixed(rect) => rect.size(),
155        }))
156    }
157
158    fn key_window_id(&self) -> Option<usize> {
159        None
160    }
161
162    fn add_status_item(&self) -> Box<dyn crate::Window> {
163        Box::new(Window::new(vec2f(24., 24.)))
164    }
165
166    fn write_to_clipboard(&self, item: ClipboardItem) {
167        *self.current_clipboard_item.lock() = Some(item);
168    }
169
170    fn read_from_clipboard(&self) -> Option<ClipboardItem> {
171        self.current_clipboard_item.lock().clone()
172    }
173
174    fn open_url(&self, _: &str) {}
175
176    fn write_credentials(&self, _: &str, _: &str, _: &[u8]) -> Result<()> {
177        Ok(())
178    }
179
180    fn read_credentials(&self, _: &str) -> Result<Option<(String, Vec<u8>)>> {
181        Ok(None)
182    }
183
184    fn delete_credentials(&self, _: &str) -> Result<()> {
185        Ok(())
186    }
187
188    fn set_cursor_style(&self, style: CursorStyle) {
189        *self.cursor.lock() = style;
190    }
191
192    fn should_auto_hide_scrollbars(&self) -> bool {
193        false
194    }
195
196    fn local_timezone(&self) -> UtcOffset {
197        UtcOffset::UTC
198    }
199
200    fn path_for_auxiliary_executable(&self, _name: &str) -> Result<PathBuf> {
201        Err(anyhow!("app not running inside a bundle"))
202    }
203
204    fn app_path(&self) -> Result<PathBuf> {
205        Err(anyhow!("app not running inside a bundle"))
206    }
207
208    fn app_version(&self) -> Result<AppVersion> {
209        Ok(AppVersion {
210            major: 1,
211            minor: 0,
212            patch: 0,
213        })
214    }
215
216    fn os_name(&self) -> &'static str {
217        "test"
218    }
219
220    fn os_version(&self) -> Result<AppVersion> {
221        Ok(AppVersion {
222            major: 1,
223            minor: 0,
224            patch: 0,
225        })
226    }
227}
228
229#[derive(Debug)]
230pub struct Screen;
231
232impl super::Screen for Screen {
233    fn as_any(&self) -> &dyn Any {
234        self
235    }
236
237    fn bounds(&self) -> RectF {
238        RectF::new(Vector2F::zero(), Vector2F::new(1920., 1080.))
239    }
240
241    fn display_uuid(&self) -> Option<uuid::Uuid> {
242        Some(uuid::Uuid::new_v4())
243    }
244}
245
246pub struct Window {
247    pub(crate) size: Vector2F,
248    scale_factor: f32,
249    current_scene: Option<crate::Scene>,
250    event_handlers: Vec<Box<dyn FnMut(super::Event) -> bool>>,
251    pub(crate) resize_handlers: Vec<Box<dyn FnMut()>>,
252    pub(crate) moved_handlers: Vec<Box<dyn FnMut()>>,
253    close_handlers: Vec<Box<dyn FnOnce()>>,
254    fullscreen_handlers: Vec<Box<dyn FnMut(bool)>>,
255    pub(crate) active_status_change_handlers: Vec<Box<dyn FnMut(bool)>>,
256    pub(crate) should_close_handler: Option<Box<dyn FnMut() -> bool>>,
257    pub(crate) title: Option<String>,
258    pub(crate) edited: bool,
259    pub(crate) pending_prompts: RefCell<VecDeque<oneshot::Sender<usize>>>,
260}
261
262impl Window {
263    fn new(size: Vector2F) -> Self {
264        Self {
265            size,
266            event_handlers: Default::default(),
267            resize_handlers: Default::default(),
268            moved_handlers: Default::default(),
269            close_handlers: Default::default(),
270            should_close_handler: Default::default(),
271            active_status_change_handlers: Default::default(),
272            fullscreen_handlers: Default::default(),
273            scale_factor: 1.0,
274            current_scene: None,
275            title: None,
276            edited: false,
277            pending_prompts: Default::default(),
278        }
279    }
280
281    pub fn title(&self) -> Option<String> {
282        self.title.clone()
283    }
284}
285
286impl super::Window for Window {
287    fn bounds(&self) -> WindowBounds {
288        WindowBounds::Fixed(RectF::new(Vector2F::zero(), self.size))
289    }
290
291    fn content_size(&self) -> Vector2F {
292        self.size
293    }
294
295    fn scale_factor(&self) -> f32 {
296        self.scale_factor
297    }
298
299    fn titlebar_height(&self) -> f32 {
300        24.
301    }
302
303    fn appearance(&self) -> crate::Appearance {
304        crate::Appearance::Light
305    }
306
307    fn screen(&self) -> Rc<dyn crate::Screen> {
308        Rc::new(Screen)
309    }
310
311    fn as_any_mut(&mut self) -> &mut dyn Any {
312        self
313    }
314
315    fn set_input_handler(&mut self, _: Box<dyn crate::InputHandler>) {}
316
317    fn prompt(&self, _: crate::PromptLevel, _: &str, _: &[&str]) -> oneshot::Receiver<usize> {
318        let (done_tx, done_rx) = oneshot::channel();
319        self.pending_prompts.borrow_mut().push_back(done_tx);
320        done_rx
321    }
322
323    fn activate(&self) {}
324
325    fn set_title(&mut self, title: &str) {
326        self.title = Some(title.to_string())
327    }
328
329    fn set_edited(&mut self, edited: bool) {
330        self.edited = edited;
331    }
332
333    fn show_character_palette(&self) {}
334
335    fn minimize(&self) {}
336
337    fn zoom(&self) {}
338
339    fn present_scene(&mut self, scene: crate::Scene) {
340        self.current_scene = Some(scene);
341    }
342
343    fn toggle_full_screen(&self) {}
344
345    fn on_event(&mut self, callback: Box<dyn FnMut(crate::Event) -> bool>) {
346        self.event_handlers.push(callback);
347    }
348
349    fn on_active_status_change(&mut self, callback: Box<dyn FnMut(bool)>) {
350        self.active_status_change_handlers.push(callback);
351    }
352
353    fn on_resize(&mut self, callback: Box<dyn FnMut()>) {
354        self.resize_handlers.push(callback);
355    }
356
357    fn on_fullscreen(&mut self, callback: Box<dyn FnMut(bool)>) {
358        self.fullscreen_handlers.push(callback)
359    }
360
361    fn on_moved(&mut self, callback: Box<dyn FnMut()>) {
362        self.moved_handlers.push(callback);
363    }
364
365    fn on_should_close(&mut self, callback: Box<dyn FnMut() -> bool>) {
366        self.should_close_handler = Some(callback);
367    }
368
369    fn on_close(&mut self, callback: Box<dyn FnOnce()>) {
370        self.close_handlers.push(callback);
371    }
372
373    fn on_appearance_changed(&mut self, _: Box<dyn FnMut()>) {}
374
375    fn is_topmost_for_position(&self, _position: Vector2F) -> bool {
376        true
377    }
378}