test.rs

  1use super::{CursorStyle, WindowBounds};
  2use crate::{
  3    geometry::vector::{vec2f, Vector2F},
  4    Action, ClipboardItem,
  5};
  6use anyhow::{anyhow, Result};
  7use parking_lot::Mutex;
  8use postage::oneshot;
  9use std::{
 10    any::Any,
 11    cell::{Cell, RefCell},
 12    path::{Path, PathBuf},
 13    rc::Rc,
 14    sync::Arc,
 15};
 16use time::UtcOffset;
 17
 18pub struct Platform {
 19    dispatcher: Arc<dyn super::Dispatcher>,
 20    fonts: Arc<dyn super::FontSystem>,
 21    current_clipboard_item: Mutex<Option<ClipboardItem>>,
 22    cursor: Mutex<CursorStyle>,
 23}
 24
 25#[derive(Default)]
 26pub struct ForegroundPlatform {
 27    last_prompt_for_new_path_args: RefCell<Option<(PathBuf, oneshot::Sender<Option<PathBuf>>)>>,
 28}
 29
 30struct Dispatcher;
 31
 32pub struct Window {
 33    size: Vector2F,
 34    scale_factor: f32,
 35    current_scene: Option<crate::Scene>,
 36    event_handlers: Vec<Box<dyn FnMut(super::Event)>>,
 37    resize_handlers: Vec<Box<dyn FnMut()>>,
 38    close_handlers: Vec<Box<dyn FnOnce()>>,
 39    pub(crate) last_prompt: Cell<Option<oneshot::Sender<usize>>>,
 40}
 41
 42#[cfg(any(test, feature = "test-support"))]
 43impl ForegroundPlatform {
 44    pub(crate) fn simulate_new_path_selection(
 45        &self,
 46        result: impl FnOnce(PathBuf) -> Option<PathBuf>,
 47    ) {
 48        let (dir_path, mut done_tx) = self
 49            .last_prompt_for_new_path_args
 50            .take()
 51            .expect("prompt_for_new_path was not called");
 52        let _ = postage::sink::Sink::try_send(&mut done_tx, result(dir_path));
 53    }
 54
 55    pub(crate) fn did_prompt_for_new_path(&self) -> bool {
 56        self.last_prompt_for_new_path_args.borrow().is_some()
 57    }
 58}
 59
 60impl super::ForegroundPlatform for ForegroundPlatform {
 61    fn on_become_active(&self, _: Box<dyn FnMut()>) {}
 62
 63    fn on_resign_active(&self, _: Box<dyn FnMut()>) {}
 64
 65    fn on_quit(&self, _: Box<dyn FnMut()>) {}
 66
 67    fn on_event(&self, _: Box<dyn FnMut(crate::Event) -> bool>) {}
 68
 69    fn on_open_files(&self, _: Box<dyn FnMut(Vec<std::path::PathBuf>)>) {}
 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
 79    fn set_menus(&self, _: Vec<crate::Menu>) {}
 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
171impl Window {
172    fn new(size: Vector2F) -> Self {
173        Self {
174            size,
175            event_handlers: Vec::new(),
176            resize_handlers: Vec::new(),
177            close_handlers: Vec::new(),
178            scale_factor: 1.0,
179            current_scene: None,
180            last_prompt: Default::default(),
181        }
182    }
183}
184
185impl super::Dispatcher for Dispatcher {
186    fn is_main_thread(&self) -> bool {
187        true
188    }
189
190    fn run_on_main_thread(&self, task: async_task::Runnable) {
191        task.run();
192    }
193}
194
195impl super::WindowContext for Window {
196    fn size(&self) -> Vector2F {
197        self.size
198    }
199
200    fn scale_factor(&self) -> f32 {
201        self.scale_factor
202    }
203
204    fn titlebar_height(&self) -> f32 {
205        24.
206    }
207
208    fn present_scene(&mut self, scene: crate::Scene) {
209        self.current_scene = Some(scene);
210    }
211}
212
213impl super::Window for Window {
214    fn as_any_mut(&mut self) -> &mut dyn Any {
215        self
216    }
217
218    fn on_event(&mut self, callback: Box<dyn FnMut(crate::Event)>) {
219        self.event_handlers.push(callback);
220    }
221
222    fn on_resize(&mut self, callback: Box<dyn FnMut()>) {
223        self.resize_handlers.push(callback);
224    }
225
226    fn on_close(&mut self, callback: Box<dyn FnOnce()>) {
227        self.close_handlers.push(callback);
228    }
229
230    fn prompt(&self, _: crate::PromptLevel, _: &str, _: &[&str]) -> oneshot::Receiver<usize> {
231        let (done_tx, done_rx) = oneshot::channel();
232        self.last_prompt.replace(Some(done_tx));
233        done_rx
234    }
235
236    fn activate(&self) {}
237}
238
239pub fn platform() -> Platform {
240    Platform::new()
241}
242
243pub fn foreground_platform() -> ForegroundPlatform {
244    ForegroundPlatform::default()
245}