platform.rs

  1use crate::{
  2    AnyWindowHandle, BackgroundExecutor, ClipboardItem, CursorStyle, DispatchTree, DisplayId,
  3    ForegroundExecutor, Platform, PlatformDisplay, PlatformTextSystem, TestDisplay, TestWindow,
  4    WindowOptions,
  5};
  6use anyhow::{anyhow, Result};
  7use collections::VecDeque;
  8use futures::channel::oneshot;
  9use parking_lot::Mutex;
 10use std::{
 11    cell::RefCell,
 12    path::PathBuf,
 13    rc::{Rc, Weak},
 14    sync::Arc,
 15};
 16
 17pub struct TestPlatform {
 18    background_executor: BackgroundExecutor,
 19    foreground_executor: ForegroundExecutor,
 20
 21    active_window: Arc<Mutex<Option<AnyWindowHandle>>>,
 22    active_display: Rc<dyn PlatformDisplay>,
 23    active_cursor: Mutex<CursorStyle>,
 24    current_clipboard_item: Mutex<Option<ClipboardItem>>,
 25    pub(crate) prompts: RefCell<TestPrompts>,
 26    weak: Weak<Self>,
 27}
 28
 29#[derive(Default)]
 30pub(crate) struct TestPrompts {
 31    multiple_choice: VecDeque<oneshot::Sender<usize>>,
 32    new_path: VecDeque<(PathBuf, oneshot::Sender<Option<PathBuf>>)>,
 33}
 34
 35impl TestPlatform {
 36    pub fn new(executor: BackgroundExecutor, foreground_executor: ForegroundExecutor) -> Rc<Self> {
 37        Rc::new_cyclic(|weak| TestPlatform {
 38            background_executor: executor,
 39            foreground_executor,
 40            prompts: Default::default(),
 41            active_cursor: Default::default(),
 42            active_display: Rc::new(TestDisplay::new()),
 43            active_window: Default::default(),
 44            current_clipboard_item: Mutex::new(None),
 45            weak: weak.clone(),
 46        })
 47    }
 48
 49    pub(crate) fn simulate_new_path_selection(
 50        &self,
 51        select_path: impl FnOnce(&std::path::Path) -> Option<std::path::PathBuf>,
 52    ) {
 53        let (path, tx) = self
 54            .prompts
 55            .borrow_mut()
 56            .new_path
 57            .pop_front()
 58            .expect("no pending new path prompt");
 59        tx.send(select_path(&path)).ok();
 60    }
 61
 62    pub(crate) fn simulate_prompt_answer(&self, response_ix: usize) {
 63        let tx = self
 64            .prompts
 65            .borrow_mut()
 66            .multiple_choice
 67            .pop_front()
 68            .expect("no pending multiple choice prompt");
 69        tx.send(response_ix).ok();
 70    }
 71
 72    pub(crate) fn has_pending_prompt(&self) -> bool {
 73        !self.prompts.borrow().multiple_choice.is_empty()
 74    }
 75
 76    pub(crate) fn prompt(&self) -> oneshot::Receiver<usize> {
 77        let (tx, rx) = oneshot::channel();
 78        self.prompts.borrow_mut().multiple_choice.push_back(tx);
 79        rx
 80    }
 81}
 82
 83// todo!("implement out what our tests needed in GPUI 1")
 84impl Platform for TestPlatform {
 85    fn background_executor(&self) -> BackgroundExecutor {
 86        self.background_executor.clone()
 87    }
 88
 89    fn foreground_executor(&self) -> ForegroundExecutor {
 90        self.foreground_executor.clone()
 91    }
 92
 93    fn text_system(&self) -> Arc<dyn PlatformTextSystem> {
 94        Arc::new(crate::platform::mac::MacTextSystem::new())
 95    }
 96
 97    fn run(&self, _on_finish_launching: Box<dyn FnOnce()>) {
 98        unimplemented!()
 99    }
100
101    fn quit(&self) {}
102
103    fn restart(&self) {
104        unimplemented!()
105    }
106
107    fn activate(&self, _ignoring_other_apps: bool) {
108        unimplemented!()
109    }
110
111    fn hide(&self) {
112        unimplemented!()
113    }
114
115    fn hide_other_apps(&self) {
116        unimplemented!()
117    }
118
119    fn unhide_other_apps(&self) {
120        unimplemented!()
121    }
122
123    fn displays(&self) -> Vec<std::rc::Rc<dyn crate::PlatformDisplay>> {
124        vec![self.active_display.clone()]
125    }
126
127    fn display(&self, id: DisplayId) -> Option<std::rc::Rc<dyn crate::PlatformDisplay>> {
128        self.displays().iter().find(|d| d.id() == id).cloned()
129    }
130
131    fn active_window(&self) -> Option<crate::AnyWindowHandle> {
132        unimplemented!()
133    }
134
135    fn open_window(
136        &self,
137        handle: AnyWindowHandle,
138        options: WindowOptions,
139    ) -> Box<dyn crate::PlatformWindow> {
140        *self.active_window.lock() = Some(handle);
141        Box::new(TestWindow::new(
142            options,
143            self.weak.clone(),
144            self.active_display.clone(),
145        ))
146    }
147
148    fn set_display_link_output_callback(
149        &self,
150        _display_id: DisplayId,
151        _callback: Box<dyn FnMut(&crate::VideoTimestamp, &crate::VideoTimestamp) + Send>,
152    ) {
153        unimplemented!()
154    }
155
156    fn start_display_link(&self, _display_id: DisplayId) {
157        unimplemented!()
158    }
159
160    fn stop_display_link(&self, _display_id: DisplayId) {
161        unimplemented!()
162    }
163
164    fn open_url(&self, _url: &str) {
165        unimplemented!()
166    }
167
168    fn on_open_urls(&self, _callback: Box<dyn FnMut(Vec<String>)>) {
169        unimplemented!()
170    }
171
172    fn prompt_for_paths(
173        &self,
174        _options: crate::PathPromptOptions,
175    ) -> oneshot::Receiver<Option<Vec<std::path::PathBuf>>> {
176        unimplemented!()
177    }
178
179    fn prompt_for_new_path(
180        &self,
181        directory: &std::path::Path,
182    ) -> oneshot::Receiver<Option<std::path::PathBuf>> {
183        let (tx, rx) = oneshot::channel();
184        self.prompts
185            .borrow_mut()
186            .new_path
187            .push_back((directory.to_path_buf(), tx));
188        rx
189    }
190
191    fn reveal_path(&self, _path: &std::path::Path) {
192        unimplemented!()
193    }
194
195    fn on_become_active(&self, _callback: Box<dyn FnMut()>) {}
196
197    fn on_resign_active(&self, _callback: Box<dyn FnMut()>) {}
198
199    fn on_quit(&self, _callback: Box<dyn FnMut()>) {}
200
201    fn on_reopen(&self, _callback: Box<dyn FnMut()>) {
202        unimplemented!()
203    }
204
205    fn on_event(&self, _callback: Box<dyn FnMut(crate::InputEvent) -> bool>) {
206        unimplemented!()
207    }
208
209    fn set_menus(&self, _menus: Vec<crate::Menu>, _dispatch_tree: Option<&DispatchTree>) {
210        unimplemented!()
211    }
212
213    fn on_app_menu_action(&self, _callback: Box<dyn FnMut(&dyn crate::Action)>) {
214        unimplemented!()
215    }
216
217    fn on_will_open_app_menu(&self, _callback: Box<dyn FnMut()>) {
218        unimplemented!()
219    }
220
221    fn on_validate_app_menu_command(&self, _callback: Box<dyn FnMut(&dyn crate::Action) -> bool>) {
222        unimplemented!()
223    }
224
225    fn os_name(&self) -> &'static str {
226        "test"
227    }
228
229    fn os_version(&self) -> Result<crate::SemanticVersion> {
230        Err(anyhow!("os_version called on TestPlatform"))
231    }
232
233    fn app_version(&self) -> Result<crate::SemanticVersion> {
234        Err(anyhow!("app_version called on TestPlatform"))
235    }
236
237    fn app_path(&self) -> Result<std::path::PathBuf> {
238        unimplemented!()
239    }
240
241    fn local_timezone(&self) -> time::UtcOffset {
242        unimplemented!()
243    }
244
245    fn path_for_auxiliary_executable(&self, _name: &str) -> Result<std::path::PathBuf> {
246        unimplemented!()
247    }
248
249    fn set_cursor_style(&self, style: crate::CursorStyle) {
250        *self.active_cursor.lock() = style;
251    }
252
253    fn should_auto_hide_scrollbars(&self) -> bool {
254        // todo()
255        true
256    }
257
258    fn write_to_clipboard(&self, item: ClipboardItem) {
259        *self.current_clipboard_item.lock() = Some(item);
260    }
261
262    fn read_from_clipboard(&self) -> Option<ClipboardItem> {
263        self.current_clipboard_item.lock().clone()
264    }
265
266    fn write_credentials(&self, _url: &str, _username: &str, _password: &[u8]) -> Result<()> {
267        Ok(())
268    }
269
270    fn read_credentials(&self, _url: &str) -> Result<Option<(String, Vec<u8>)>> {
271        Ok(None)
272    }
273
274    fn delete_credentials(&self, _url: &str) -> Result<()> {
275        Ok(())
276    }
277}