platform.rs

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