test.rs

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