edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching·0 forks·Rust·18.3 MiB·ff99ad6·Tahinli·6 days ago·1055 commits
| 1 | use crate::{ |
| 2 | AnyWindowHandle, BackgroundExecutor, ClipboardItem, CursorStyle, DevicePixels, |
| 3 | DummyKeyboardMapper, ForegroundExecutor, Keymap, NoopTextSystem, Platform, PlatformDisplay, |
| 4 | PlatformKeyboardLayout, PlatformKeyboardMapper, PlatformTextSystem, PromptButton, |
| 5 | ScreenCaptureFrame, ScreenCaptureSource, ScreenCaptureStream, SourceMetadata, Task, |
| 6 | TestDisplay, TestWindow, WindowAppearance, WindowParams, size, |
| 7 | }; |
| 8 | use anyhow::Result; |
| 9 | use collections::VecDeque; |
| 10 | use futures::channel::oneshot; |
| 11 | use parking_lot::Mutex; |
| 12 | use std::{ |
| 13 | cell::RefCell, |
| 14 | path::{Path, PathBuf}, |
| 15 | rc::{Rc, Weak}, |
| 16 | sync::Arc, |
| 17 | }; |
| 18 | #[cfg(target_os = "windows")] |
| 19 | use windows::Win32::{ |
| 20 | Graphics::Imaging::{CLSID_WICImagingFactory, IWICImagingFactory}, |
| 21 | System::Com::{CLSCTX_INPROC_SERVER, CoCreateInstance}, |
| 22 | }; |
| 23 | |
| 24 | /// TestPlatform implements the Platform trait for use in tests. |
| 25 | pub(crate) struct TestPlatform { |
| 26 | background_executor: BackgroundExecutor, |
| 27 | foreground_executor: ForegroundExecutor, |
| 28 | |
| 29 | pub(crate) active_window: RefCell<Option<TestWindow>>, |
| 30 | active_display: Rc<dyn PlatformDisplay>, |
| 31 | active_cursor: Mutex<CursorStyle>, |
| 32 | current_clipboard_item: Mutex<Option<ClipboardItem>>, |
| 33 | #[cfg(any(target_os = "linux", target_os = "freebsd"))] |
| 34 | current_primary_item: Mutex<Option<ClipboardItem>>, |
| 35 | pub(crate) prompts: RefCell<TestPrompts>, |
| 36 | screen_capture_sources: RefCell<Vec<TestScreenCaptureSource>>, |
| 37 | pub opened_url: RefCell<Option<String>>, |
| 38 | pub text_system: Arc<dyn PlatformTextSystem>, |
| 39 | #[cfg(target_os = "windows")] |
| 40 | bitmap_factory: std::mem::ManuallyDrop<IWICImagingFactory>, |
| 41 | weak: Weak<Self>, |
| 42 | } |
| 43 | |
| 44 | #[derive(Clone)] |
| 45 | /// A fake screen capture source, used for testing. |
| 46 | pub struct TestScreenCaptureSource {} |
| 47 | |
| 48 | /// A fake screen capture stream, used for testing. |
| 49 | pub struct TestScreenCaptureStream {} |
| 50 | |
| 51 | impl ScreenCaptureSource for TestScreenCaptureSource { |
| 52 | fn metadata(&self) -> Result<SourceMetadata> { |
| 53 | Ok(SourceMetadata { |
| 54 | id: 0, |
| 55 | is_main: None, |
| 56 | label: None, |
| 57 | resolution: size(DevicePixels(1), DevicePixels(1)), |
| 58 | }) |
| 59 | } |
| 60 | |
| 61 | fn stream( |
| 62 | &self, |
| 63 | _foreground_executor: &ForegroundExecutor, |
| 64 | _frame_callback: Box<dyn Fn(ScreenCaptureFrame) + Send>, |
| 65 | ) -> oneshot::Receiver<Result<Box<dyn ScreenCaptureStream>>> { |
| 66 | let (mut tx, rx) = oneshot::channel(); |
| 67 | let stream = TestScreenCaptureStream {}; |
| 68 | tx.send(Ok(Box::new(stream) as Box<dyn ScreenCaptureStream>)) |
| 69 | .ok(); |
| 70 | rx |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | impl ScreenCaptureStream for TestScreenCaptureStream { |
| 75 | fn metadata(&self) -> Result<SourceMetadata> { |
| 76 | TestScreenCaptureSource {}.metadata() |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | struct TestPrompt { |
| 81 | msg: String, |
| 82 | detail: Option<String>, |
| 83 | answers: Vec<String>, |
| 84 | tx: oneshot::Sender<usize>, |
| 85 | } |
| 86 | |
| 87 | #[derive(Default)] |
| 88 | pub(crate) struct TestPrompts { |
| 89 | multiple_choice: VecDeque<TestPrompt>, |
| 90 | new_path: VecDeque<(PathBuf, oneshot::Sender<Result<Option<PathBuf>>>)>, |
| 91 | } |
| 92 | |
| 93 | impl TestPlatform { |
| 94 | pub fn new(executor: BackgroundExecutor, foreground_executor: ForegroundExecutor) -> Rc<Self> { |
| 95 | #[cfg(target_os = "windows")] |
| 96 | let bitmap_factory = unsafe { |
| 97 | windows::Win32::System::Ole::OleInitialize(None) |
| 98 | .expect("unable to initialize Windows OLE"); |
| 99 | std::mem::ManuallyDrop::new( |
| 100 | CoCreateInstance(&CLSID_WICImagingFactory, None, CLSCTX_INPROC_SERVER) |
| 101 | .expect("Error creating bitmap factory."), |
| 102 | ) |
| 103 | }; |
| 104 | |
| 105 | let text_system = Arc::new(NoopTextSystem); |
| 106 | |
| 107 | Rc::new_cyclic(|weak| TestPlatform { |
| 108 | background_executor: executor, |
| 109 | foreground_executor, |
| 110 | prompts: Default::default(), |
| 111 | screen_capture_sources: Default::default(), |
| 112 | active_cursor: Default::default(), |
| 113 | active_display: Rc::new(TestDisplay::new()), |
| 114 | active_window: Default::default(), |
| 115 | current_clipboard_item: Mutex::new(None), |
| 116 | #[cfg(any(target_os = "linux", target_os = "freebsd"))] |
| 117 | current_primary_item: Mutex::new(None), |
| 118 | weak: weak.clone(), |
| 119 | opened_url: Default::default(), |
| 120 | #[cfg(target_os = "windows")] |
| 121 | bitmap_factory, |
| 122 | text_system, |
| 123 | }) |
| 124 | } |
| 125 | |
| 126 | pub(crate) fn simulate_new_path_selection( |
| 127 | &self, |
| 128 | select_path: impl FnOnce(&std::path::Path) -> Option<std::path::PathBuf>, |
| 129 | ) { |
| 130 | let (path, tx) = self |
| 131 | .prompts |
| 132 | .borrow_mut() |
| 133 | .new_path |
| 134 | .pop_front() |
| 135 | .expect("no pending new path prompt"); |
| 136 | self.background_executor().set_waiting_hint(None); |
| 137 | tx.send(Ok(select_path(&path))).ok(); |
| 138 | } |
| 139 | |
| 140 | #[track_caller] |
| 141 | pub(crate) fn simulate_prompt_answer(&self, response: &str) { |
| 142 | let prompt = self |
| 143 | .prompts |
| 144 | .borrow_mut() |
| 145 | .multiple_choice |
| 146 | .pop_front() |
| 147 | .expect("no pending multiple choice prompt"); |
| 148 | self.background_executor().set_waiting_hint(None); |
| 149 | let Some(ix) = prompt.answers.iter().position(|a| a == response) else { |
| 150 | panic!( |
| 151 | "PROMPT: {}\n{:?}\n{:?}\nCannot respond with {}", |
| 152 | prompt.msg, prompt.detail, prompt.answers, response |
| 153 | ) |
| 154 | }; |
| 155 | prompt.tx.send(ix).ok(); |
| 156 | } |
| 157 | |
| 158 | pub(crate) fn has_pending_prompt(&self) -> bool { |
| 159 | !self.prompts.borrow().multiple_choice.is_empty() |
| 160 | } |
| 161 | |
| 162 | pub(crate) fn pending_prompt(&self) -> Option<(String, String)> { |
| 163 | let prompts = self.prompts.borrow(); |
| 164 | let prompt = prompts.multiple_choice.front()?; |
| 165 | Some(( |
| 166 | prompt.msg.clone(), |
| 167 | prompt.detail.clone().unwrap_or_default(), |
| 168 | )) |
| 169 | } |
| 170 | |
| 171 | pub(crate) fn set_screen_capture_sources(&self, sources: Vec<TestScreenCaptureSource>) { |
| 172 | *self.screen_capture_sources.borrow_mut() = sources; |
| 173 | } |
| 174 | |
| 175 | pub(crate) fn prompt( |
| 176 | &self, |
| 177 | msg: &str, |
| 178 | detail: Option<&str>, |
| 179 | answers: &[PromptButton], |
| 180 | ) -> oneshot::Receiver<usize> { |
| 181 | let (tx, rx) = oneshot::channel(); |
| 182 | let answers: Vec<String> = answers.iter().map(|s| s.label().to_string()).collect(); |
| 183 | self.background_executor() |
| 184 | .set_waiting_hint(Some(format!("PROMPT: {:?} {:?}", msg, detail))); |
| 185 | self.prompts |
| 186 | .borrow_mut() |
| 187 | .multiple_choice |
| 188 | .push_back(TestPrompt { |
| 189 | msg: msg.to_string(), |
| 190 | detail: detail.map(|s| s.to_string()), |
| 191 | answers, |
| 192 | tx, |
| 193 | }); |
| 194 | rx |
| 195 | } |
| 196 | |
| 197 | pub(crate) fn set_active_window(&self, window: Option<TestWindow>) { |
| 198 | let executor = self.foreground_executor(); |
| 199 | let previous_window = self.active_window.borrow_mut().take(); |
| 200 | self.active_window.borrow_mut().clone_from(&window); |
| 201 | |
| 202 | executor |
| 203 | .spawn(async move { |
| 204 | if let Some(previous_window) = previous_window { |
| 205 | if let Some(window) = window.as_ref() |
| 206 | && Rc::ptr_eq(&previous_window.0, &window.0) |
| 207 | { |
| 208 | return; |
| 209 | } |
| 210 | previous_window.simulate_active_status_change(false); |
| 211 | } |
| 212 | if let Some(window) = window { |
| 213 | window.simulate_active_status_change(true); |
| 214 | } |
| 215 | }) |
| 216 | .detach(); |
| 217 | } |
| 218 | |
| 219 | pub(crate) fn did_prompt_for_new_path(&self) -> bool { |
| 220 | !self.prompts.borrow().new_path.is_empty() |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | impl Platform for TestPlatform { |
| 225 | fn background_executor(&self) -> BackgroundExecutor { |
| 226 | self.background_executor.clone() |
| 227 | } |
| 228 | |
| 229 | fn foreground_executor(&self) -> ForegroundExecutor { |
| 230 | self.foreground_executor.clone() |
| 231 | } |
| 232 | |
| 233 | fn text_system(&self) -> Arc<dyn PlatformTextSystem> { |
| 234 | self.text_system.clone() |
| 235 | } |
| 236 | |
| 237 | fn keyboard_layout(&self) -> Box<dyn PlatformKeyboardLayout> { |
| 238 | Box::new(TestKeyboardLayout) |
| 239 | } |
| 240 | |
| 241 | fn keyboard_mapper(&self) -> Rc<dyn PlatformKeyboardMapper> { |
| 242 | Rc::new(DummyKeyboardMapper) |
| 243 | } |
| 244 | |
| 245 | fn on_keyboard_layout_change(&self, _: Box<dyn FnMut()>) {} |
| 246 | |
| 247 | fn run(&self, _on_finish_launching: Box<dyn FnOnce()>) { |
| 248 | unimplemented!() |
| 249 | } |
| 250 | |
| 251 | fn quit(&self) {} |
| 252 | |
| 253 | fn restart(&self, _: Option<PathBuf>) { |
| 254 | // |
| 255 | } |
| 256 | |
| 257 | fn activate(&self, _ignoring_other_apps: bool) { |
| 258 | // |
| 259 | } |
| 260 | |
| 261 | fn hide(&self) { |
| 262 | unimplemented!() |
| 263 | } |
| 264 | |
| 265 | fn hide_other_apps(&self) { |
| 266 | unimplemented!() |
| 267 | } |
| 268 | |
| 269 | fn unhide_other_apps(&self) { |
| 270 | unimplemented!() |
| 271 | } |
| 272 | |
| 273 | fn displays(&self) -> Vec<std::rc::Rc<dyn crate::PlatformDisplay>> { |
| 274 | vec![self.active_display.clone()] |
| 275 | } |
| 276 | |
| 277 | fn primary_display(&self) -> Option<std::rc::Rc<dyn crate::PlatformDisplay>> { |
| 278 | Some(self.active_display.clone()) |
| 279 | } |
| 280 | |
| 281 | #[cfg(feature = "screen-capture")] |
| 282 | fn is_screen_capture_supported(&self) -> bool { |
| 283 | true |
| 284 | } |
| 285 | |
| 286 | #[cfg(feature = "screen-capture")] |
| 287 | fn screen_capture_sources( |
| 288 | &self, |
| 289 | ) -> oneshot::Receiver<Result<Vec<Rc<dyn ScreenCaptureSource>>>> { |
| 290 | let (mut tx, rx) = oneshot::channel(); |
| 291 | tx.send(Ok(self |
| 292 | .screen_capture_sources |
| 293 | .borrow() |
| 294 | .iter() |
| 295 | .map(|source| Rc::new(source.clone()) as Rc<dyn ScreenCaptureSource>) |
| 296 | .collect())) |
| 297 | .ok(); |
| 298 | rx |
| 299 | } |
| 300 | |
| 301 | fn active_window(&self) -> Option<crate::AnyWindowHandle> { |
| 302 | self.active_window |
| 303 | .borrow() |
| 304 | .as_ref() |
| 305 | .map(|window| window.0.lock().handle) |
| 306 | } |
| 307 | |
| 308 | fn open_window( |
| 309 | &self, |
| 310 | handle: AnyWindowHandle, |
| 311 | params: WindowParams, |
| 312 | ) -> anyhow::Result<Box<dyn crate::PlatformWindow>> { |
| 313 | let window = TestWindow::new( |
| 314 | handle, |
| 315 | params, |
| 316 | self.weak.clone(), |
| 317 | self.active_display.clone(), |
| 318 | ); |
| 319 | Ok(Box::new(window)) |
| 320 | } |
| 321 | |
| 322 | fn window_appearance(&self) -> WindowAppearance { |
| 323 | WindowAppearance::Light |
| 324 | } |
| 325 | |
| 326 | fn open_url(&self, url: &str) { |
| 327 | *self.opened_url.borrow_mut() = Some(url.to_string()) |
| 328 | } |
| 329 | |
| 330 | fn on_open_urls(&self, _callback: Box<dyn FnMut(Vec<String>)>) { |
| 331 | unimplemented!() |
| 332 | } |
| 333 | |
| 334 | fn prompt_for_paths( |
| 335 | &self, |
| 336 | _options: crate::PathPromptOptions, |
| 337 | ) -> oneshot::Receiver<Result<Option<Vec<std::path::PathBuf>>>> { |
| 338 | unimplemented!() |
| 339 | } |
| 340 | |
| 341 | fn prompt_for_new_path( |
| 342 | &self, |
| 343 | directory: &std::path::Path, |
| 344 | _suggested_name: Option<&str>, |
| 345 | ) -> oneshot::Receiver<Result<Option<std::path::PathBuf>>> { |
| 346 | let (tx, rx) = oneshot::channel(); |
| 347 | self.background_executor() |
| 348 | .set_waiting_hint(Some(format!("PROMPT FOR PATH: {:?}", directory))); |
| 349 | self.prompts |
| 350 | .borrow_mut() |
| 351 | .new_path |
| 352 | .push_back((directory.to_path_buf(), tx)); |
| 353 | rx |
| 354 | } |
| 355 | |
| 356 | fn can_select_mixed_files_and_dirs(&self) -> bool { |
| 357 | true |
| 358 | } |
| 359 | |
| 360 | fn reveal_path(&self, _path: &std::path::Path) { |
| 361 | unimplemented!() |
| 362 | } |
| 363 | |
| 364 | fn on_quit(&self, _callback: Box<dyn FnMut()>) {} |
| 365 | |
| 366 | fn on_reopen(&self, _callback: Box<dyn FnMut()>) { |
| 367 | unimplemented!() |
| 368 | } |
| 369 | |
| 370 | fn set_menus(&self, _menus: Vec<crate::Menu>, _keymap: &Keymap) {} |
| 371 | fn set_dock_menu(&self, _menu: Vec<crate::MenuItem>, _keymap: &Keymap) {} |
| 372 | |
| 373 | fn add_recent_document(&self, _paths: &Path) {} |
| 374 | |
| 375 | fn on_app_menu_action(&self, _callback: Box<dyn FnMut(&dyn crate::Action)>) {} |
| 376 | |
| 377 | fn on_will_open_app_menu(&self, _callback: Box<dyn FnMut()>) {} |
| 378 | |
| 379 | fn on_validate_app_menu_command(&self, _callback: Box<dyn FnMut(&dyn crate::Action) -> bool>) {} |
| 380 | |
| 381 | fn app_path(&self) -> Result<std::path::PathBuf> { |
| 382 | unimplemented!() |
| 383 | } |
| 384 | |
| 385 | fn path_for_auxiliary_executable(&self, _name: &str) -> Result<std::path::PathBuf> { |
| 386 | unimplemented!() |
| 387 | } |
| 388 | |
| 389 | fn set_cursor_style(&self, style: crate::CursorStyle) { |
| 390 | *self.active_cursor.lock() = style; |
| 391 | } |
| 392 | |
| 393 | fn should_auto_hide_scrollbars(&self) -> bool { |
| 394 | false |
| 395 | } |
| 396 | |
| 397 | #[cfg(any(target_os = "linux", target_os = "freebsd"))] |
| 398 | fn write_to_primary(&self, item: ClipboardItem) { |
| 399 | *self.current_primary_item.lock() = Some(item); |
| 400 | } |
| 401 | |
| 402 | fn write_to_clipboard(&self, item: ClipboardItem) { |
| 403 | *self.current_clipboard_item.lock() = Some(item); |
| 404 | } |
| 405 | |
| 406 | #[cfg(any(target_os = "linux", target_os = "freebsd"))] |
| 407 | fn read_from_primary(&self) -> Option<ClipboardItem> { |
| 408 | self.current_primary_item.lock().clone() |
| 409 | } |
| 410 | |
| 411 | fn read_from_clipboard(&self) -> Option<ClipboardItem> { |
| 412 | self.current_clipboard_item.lock().clone() |
| 413 | } |
| 414 | |
| 415 | fn write_credentials(&self, _url: &str, _username: &str, _password: &[u8]) -> Task<Result<()>> { |
| 416 | Task::ready(Ok(())) |
| 417 | } |
| 418 | |
| 419 | fn read_credentials(&self, _url: &str) -> Task<Result<Option<(String, Vec<u8>)>>> { |
| 420 | Task::ready(Ok(None)) |
| 421 | } |
| 422 | |
| 423 | fn delete_credentials(&self, _url: &str) -> Task<Result<()>> { |
| 424 | Task::ready(Ok(())) |
| 425 | } |
| 426 | |
| 427 | fn register_url_scheme(&self, _: &str) -> Task<anyhow::Result<()>> { |
| 428 | unimplemented!() |
| 429 | } |
| 430 | |
| 431 | fn open_with_system(&self, _path: &Path) { |
| 432 | unimplemented!() |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | impl TestScreenCaptureSource { |
| 437 | /// Create a fake screen capture source, for testing. |
| 438 | pub fn new() -> Self { |
| 439 | Self {} |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | #[cfg(target_os = "windows")] |
| 444 | impl Drop for TestPlatform { |
| 445 | fn drop(&mut self) { |
| 446 | unsafe { |
| 447 | std::mem::ManuallyDrop::drop(&mut self.bitmap_factory); |
| 448 | windows::Win32::System::Ole::OleUninitialize(); |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | struct TestKeyboardLayout; |
| 454 | |
| 455 | impl PlatformKeyboardLayout for TestKeyboardLayout { |
| 456 | fn id(&self) -> &str { |
| 457 | "zed.keyboard.example" |
| 458 | } |
| 459 | |
| 460 | fn name(&self) -> &str { |
| 461 | "zed.keyboard.example" |
| 462 | } |
| 463 | } |