platform.rs

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