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)>>,
 38    resize_handlers: Vec<Box<dyn FnMut()>>,
 39    close_handlers: Vec<Box<dyn FnOnce()>>,
 40    pub(crate) pending_prompts: RefCell<VecDeque<oneshot::Sender<usize>>>,
 41}
 42
 43#[cfg(any(test, feature = "test-support"))]
 44impl ForegroundPlatform {
 45    pub(crate) fn simulate_new_path_selection(
 46        &self,
 47        result: impl FnOnce(PathBuf) -> Option<PathBuf>,
 48    ) {
 49        let (dir_path, mut done_tx) = self
 50            .last_prompt_for_new_path_args
 51            .take()
 52            .expect("prompt_for_new_path was not called");
 53        let _ = postage::sink::Sink::try_send(&mut done_tx, result(dir_path));
 54    }
 55
 56    pub(crate) fn did_prompt_for_new_path(&self) -> bool {
 57        self.last_prompt_for_new_path_args.borrow().is_some()
 58    }
 59}
 60
 61impl super::ForegroundPlatform for ForegroundPlatform {
 62    fn on_become_active(&self, _: Box<dyn FnMut()>) {}
 63
 64    fn on_resign_active(&self, _: Box<dyn FnMut()>) {}
 65
 66    fn on_quit(&self, _: Box<dyn FnMut()>) {}
 67
 68    fn on_event(&self, _: Box<dyn FnMut(crate::Event) -> bool>) {}
 69
 70    fn on_open_urls(&self, _: Box<dyn FnMut(Vec<String>)>) {}
 71
 72    fn run(&self, _on_finish_launching: Box<dyn FnOnce() -> ()>) {
 73        unimplemented!()
 74    }
 75
 76    fn on_menu_command(&self, _: Box<dyn FnMut(&dyn Action)>) {}
 77    fn on_validate_menu_command(&self, _: Box<dyn FnMut(&dyn Action) -> bool>) {}
 78    fn on_will_open_menu(&self, _: Box<dyn FnMut()>) {}
 79    fn set_menus(&self, _: Vec<crate::Menu>, _: &keymap::Matcher) {}
 80
 81    fn prompt_for_paths(
 82        &self,
 83        _: super::PathPromptOptions,
 84    ) -> oneshot::Receiver<Option<Vec<PathBuf>>> {
 85        let (_done_tx, done_rx) = oneshot::channel();
 86        done_rx
 87    }
 88
 89    fn prompt_for_new_path(&self, path: &Path) -> oneshot::Receiver<Option<PathBuf>> {
 90        let (done_tx, done_rx) = oneshot::channel();
 91        *self.last_prompt_for_new_path_args.borrow_mut() = Some((path.to_path_buf(), done_tx));
 92        done_rx
 93    }
 94}
 95
 96impl Platform {
 97    fn new() -> Self {
 98        Self {
 99            dispatcher: Arc::new(Dispatcher),
100            fonts: Arc::new(super::current::FontSystem::new()),
101            current_clipboard_item: Default::default(),
102            cursor: Mutex::new(CursorStyle::Arrow),
103        }
104    }
105}
106
107impl super::Platform for Platform {
108    fn dispatcher(&self) -> Arc<dyn super::Dispatcher> {
109        self.dispatcher.clone()
110    }
111
112    fn fonts(&self) -> std::sync::Arc<dyn super::FontSystem> {
113        self.fonts.clone()
114    }
115
116    fn activate(&self, _ignoring_other_apps: bool) {}
117
118    fn open_window(
119        &self,
120        _: usize,
121        options: super::WindowOptions,
122        _executor: Rc<super::executor::Foreground>,
123    ) -> Box<dyn super::Window> {
124        Box::new(Window::new(match options.bounds {
125            WindowBounds::Maximized => vec2f(1024., 768.),
126            WindowBounds::Fixed(rect) => rect.size(),
127        }))
128    }
129
130    fn key_window_id(&self) -> Option<usize> {
131        None
132    }
133
134    fn quit(&self) {}
135
136    fn write_to_clipboard(&self, item: ClipboardItem) {
137        *self.current_clipboard_item.lock() = Some(item);
138    }
139
140    fn read_from_clipboard(&self) -> Option<ClipboardItem> {
141        self.current_clipboard_item.lock().clone()
142    }
143
144    fn open_url(&self, _: &str) {}
145
146    fn write_credentials(&self, _: &str, _: &str, _: &[u8]) -> Result<()> {
147        Ok(())
148    }
149
150    fn read_credentials(&self, _: &str) -> Result<Option<(String, Vec<u8>)>> {
151        Ok(None)
152    }
153
154    fn delete_credentials(&self, _: &str) -> Result<()> {
155        Ok(())
156    }
157
158    fn set_cursor_style(&self, style: CursorStyle) {
159        *self.cursor.lock() = style;
160    }
161
162    fn local_timezone(&self) -> UtcOffset {
163        UtcOffset::UTC
164    }
165
166    fn path_for_auxiliary_executable(&self, _name: &str) -> Result<PathBuf> {
167        Err(anyhow!("app not running inside a bundle"))
168    }
169
170    fn app_path(&self) -> Result<PathBuf> {
171        Err(anyhow!("app not running inside a bundle"))
172    }
173
174    fn app_version(&self) -> Result<AppVersion> {
175        Ok(AppVersion {
176            major: 1,
177            minor: 0,
178            patch: 0,
179        })
180    }
181}
182
183impl Window {
184    fn new(size: Vector2F) -> Self {
185        Self {
186            size,
187            event_handlers: Vec::new(),
188            resize_handlers: Vec::new(),
189            close_handlers: Vec::new(),
190            scale_factor: 1.0,
191            current_scene: None,
192            pending_prompts: Default::default(),
193        }
194    }
195}
196
197impl super::Dispatcher for Dispatcher {
198    fn is_main_thread(&self) -> bool {
199        true
200    }
201
202    fn run_on_main_thread(&self, task: async_task::Runnable) {
203        task.run();
204    }
205}
206
207impl super::WindowContext for Window {
208    fn size(&self) -> Vector2F {
209        self.size
210    }
211
212    fn scale_factor(&self) -> f32 {
213        self.scale_factor
214    }
215
216    fn titlebar_height(&self) -> f32 {
217        24.
218    }
219
220    fn present_scene(&mut self, scene: crate::Scene) {
221        self.current_scene = Some(scene);
222    }
223}
224
225impl super::Window for Window {
226    fn as_any_mut(&mut self) -> &mut dyn Any {
227        self
228    }
229
230    fn on_event(&mut self, callback: Box<dyn FnMut(crate::Event)>) {
231        self.event_handlers.push(callback);
232    }
233
234    fn on_active_status_change(&mut self, _: Box<dyn FnMut(bool)>) {}
235
236    fn on_resize(&mut self, callback: Box<dyn FnMut()>) {
237        self.resize_handlers.push(callback);
238    }
239
240    fn on_close(&mut self, callback: Box<dyn FnOnce()>) {
241        self.close_handlers.push(callback);
242    }
243
244    fn prompt(&self, _: crate::PromptLevel, _: &str, _: &[&str]) -> oneshot::Receiver<usize> {
245        let (done_tx, done_rx) = oneshot::channel();
246        self.pending_prompts.borrow_mut().push_back(done_tx);
247        done_rx
248    }
249
250    fn activate(&self) {}
251}
252
253pub fn platform() -> Platform {
254    Platform::new()
255}
256
257pub fn foreground_platform() -> ForegroundPlatform {
258    ForegroundPlatform::default()
259}