| 1 | use std::cell::{Cell, RefCell}; |
| 2 | use std::ffi::OsString; |
| 3 | use std::path::{Path, PathBuf}; |
| 4 | use std::rc::{Rc, Weak}; |
| 5 | use std::sync::Arc; |
| 6 | |
| 7 | use futures_channel::oneshot; |
| 8 | use gpui_kit::*; |
| 9 | use raw_window_handle::{ |
| 10 | DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, WindowHandle, |
| 11 | }; |
| 12 | |
| 13 | use crate::appearance::DEFAULT_ZOOM; |
| 14 | |
| 15 | struct Shared { |
| 16 | factor: Cell<f32>, |
| 17 | executor: ForegroundExecutor, |
| 18 | windows: RefCell<Vec<Weak<WindowState>>>, |
| 19 | } |
| 20 | |
| 21 | thread_local! { |
| 22 | static SHARED: RefCell<Option<Rc<Shared>>> = const { RefCell::new(None) }; |
| 23 | } |
| 24 | |
| 25 | pub fn factor() -> f32 { |
| 26 | SHARED.with(|shared| { |
| 27 | shared |
| 28 | .borrow() |
| 29 | .as_ref() |
| 30 | .map_or(DEFAULT_ZOOM, |shared| shared.factor.get()) |
| 31 | }) |
| 32 | } |
| 33 | |
| 34 | pub fn set_factor(factor: f32) { |
| 35 | let Some(shared) = SHARED.with(|shared| shared.borrow().clone()) else { |
| 36 | return; |
| 37 | }; |
| 38 | if (shared.factor.get() - factor).abs() < f32::EPSILON { |
| 39 | return; |
| 40 | } |
| 41 | shared.factor.set(factor); |
| 42 | let windows: Vec<Rc<WindowState>> = shared |
| 43 | .windows |
| 44 | .borrow_mut() |
| 45 | .drain(..) |
| 46 | .filter_map(|window| window.upgrade()) |
| 47 | .collect(); |
| 48 | shared |
| 49 | .windows |
| 50 | .borrow_mut() |
| 51 | .extend(windows.iter().map(Rc::downgrade)); |
| 52 | shared |
| 53 | .executor |
| 54 | .spawn(async move { |
| 55 | for window in windows { |
| 56 | window.relayout(); |
| 57 | } |
| 58 | }) |
| 59 | .detach(); |
| 60 | } |
| 61 | |
| 62 | pub fn wrap(inner: Rc<dyn Platform>, initial_factor: f32) -> Rc<dyn Platform> { |
| 63 | let shared = Rc::new(Shared { |
| 64 | factor: Cell::new(initial_factor), |
| 65 | executor: inner.foreground_executor(), |
| 66 | windows: RefCell::new(Vec::new()), |
| 67 | }); |
| 68 | SHARED.with(|slot| *slot.borrow_mut() = Some(shared.clone())); |
| 69 | Rc::new(ZoomPlatform { inner, shared }) |
| 70 | } |
| 71 | |
| 72 | pub fn zooms(params: &WindowParams) -> bool { |
| 73 | matches!(params.kind, WindowKind::Normal) && params.is_resizable |
| 74 | } |
| 75 | |
| 76 | struct WindowState { |
| 77 | shared: Rc<Shared>, |
| 78 | zoomed: bool, |
| 79 | platform_size: Cell<Size<Pixels>>, |
| 80 | platform_scale: Cell<f32>, |
| 81 | on_resize: RefCell<Option<Box<dyn FnMut(Size<Pixels>, f32)>>>, |
| 82 | } |
| 83 | |
| 84 | impl WindowState { |
| 85 | fn zoom(&self) -> f32 { |
| 86 | if self.zoomed { |
| 87 | self.shared.factor.get() |
| 88 | } else { |
| 89 | DEFAULT_ZOOM |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | fn relayout(&self) { |
| 94 | let zoom = self.zoom(); |
| 95 | let size = self.platform_size.get(); |
| 96 | let scale = self.platform_scale.get(); |
| 97 | if let Ok(mut callback) = self.on_resize.try_borrow_mut() |
| 98 | && let Some(callback) = callback.as_mut() |
| 99 | { |
| 100 | callback(scale_size(size, 1. / zoom), scale * zoom); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | fn scale_point(point: Point<Pixels>, factor: f32) -> Point<Pixels> { |
| 106 | gpui_kit::point(point.x * factor, point.y * factor) |
| 107 | } |
| 108 | |
| 109 | fn scale_size(size: Size<Pixels>, factor: f32) -> Size<Pixels> { |
| 110 | gpui_kit::size(size.width * factor, size.height * factor) |
| 111 | } |
| 112 | |
| 113 | fn scale_bounds(bounds: Bounds<Pixels>, factor: f32) -> Bounds<Pixels> { |
| 114 | Bounds { |
| 115 | origin: scale_point(bounds.origin, factor), |
| 116 | size: scale_size(bounds.size, factor), |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | fn unzoom_input(input: PlatformInput, zoom: f32) -> PlatformInput { |
| 121 | if (zoom - DEFAULT_ZOOM).abs() < f32::EPSILON { |
| 122 | return input; |
| 123 | } |
| 124 | let factor = 1. / zoom; |
| 125 | let scale = |point: Point<Pixels>| scale_point(point, factor); |
| 126 | match input { |
| 127 | PlatformInput::MouseDown(mut event) => { |
| 128 | event.position = scale(event.position); |
| 129 | PlatformInput::MouseDown(event) |
| 130 | } |
| 131 | PlatformInput::MouseUp(mut event) => { |
| 132 | event.position = scale(event.position); |
| 133 | PlatformInput::MouseUp(event) |
| 134 | } |
| 135 | PlatformInput::MousePressure(mut event) => { |
| 136 | event.position = scale(event.position); |
| 137 | PlatformInput::MousePressure(event) |
| 138 | } |
| 139 | PlatformInput::MouseMove(mut event) => { |
| 140 | event.position = scale(event.position); |
| 141 | PlatformInput::MouseMove(event) |
| 142 | } |
| 143 | PlatformInput::MouseExited(mut event) => { |
| 144 | event.position = scale(event.position); |
| 145 | PlatformInput::MouseExited(event) |
| 146 | } |
| 147 | PlatformInput::ScrollWheel(mut event) => { |
| 148 | event.position = scale(event.position); |
| 149 | if let ScrollDelta::Pixels(delta) = event.delta { |
| 150 | event.delta = ScrollDelta::Pixels(scale(delta)); |
| 151 | } |
| 152 | PlatformInput::ScrollWheel(event) |
| 153 | } |
| 154 | PlatformInput::Pinch(mut event) => { |
| 155 | event.position = scale(event.position); |
| 156 | PlatformInput::Pinch(event) |
| 157 | } |
| 158 | PlatformInput::LongPress(mut event) => { |
| 159 | event.start_position = scale(event.start_position); |
| 160 | event.position = scale(event.position); |
| 161 | PlatformInput::LongPress(event) |
| 162 | } |
| 163 | PlatformInput::TouchDrag(mut event) => { |
| 164 | event.start_position = scale(event.start_position); |
| 165 | event.position = scale(event.position); |
| 166 | PlatformInput::TouchDrag(event) |
| 167 | } |
| 168 | PlatformInput::Touch(mut event) => { |
| 169 | event.position = scale(event.position); |
| 170 | event.predicted_position = event.predicted_position.map(scale); |
| 171 | PlatformInput::Touch(event) |
| 172 | } |
| 173 | PlatformInput::FileDrop(event) => PlatformInput::FileDrop(match event { |
| 174 | FileDropEvent::Entered { position, paths } => FileDropEvent::Entered { |
| 175 | position: scale(position), |
| 176 | paths, |
| 177 | }, |
| 178 | FileDropEvent::Pending { position } => FileDropEvent::Pending { |
| 179 | position: scale(position), |
| 180 | }, |
| 181 | FileDropEvent::Submit { position } => FileDropEvent::Submit { |
| 182 | position: scale(position), |
| 183 | }, |
| 184 | other => other, |
| 185 | }), |
| 186 | other @ (PlatformInput::KeyDown(_) |
| 187 | | PlatformInput::KeyUp(_) |
| 188 | | PlatformInput::ModifiersChanged(_)) => other, |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | struct ZoomPlatform { |
| 193 | inner: Rc<dyn Platform>, |
| 194 | shared: Rc<Shared>, |
| 195 | } |
| 196 | |
| 197 | impl Platform for ZoomPlatform { |
| 198 | fn background_executor(&self) -> BackgroundExecutor { |
| 199 | self.inner.background_executor() |
| 200 | } |
| 201 | |
| 202 | fn foreground_executor(&self) -> ForegroundExecutor { |
| 203 | self.inner.foreground_executor() |
| 204 | } |
| 205 | |
| 206 | fn text_system(&self) -> Arc<dyn PlatformTextSystem> { |
| 207 | self.inner.text_system() |
| 208 | } |
| 209 | |
| 210 | fn run(&self, on_finish_launching: Box<dyn 'static + FnOnce()>) { |
| 211 | self.inner.run(on_finish_launching) |
| 212 | } |
| 213 | |
| 214 | fn quit(&self) { |
| 215 | self.inner.quit() |
| 216 | } |
| 217 | |
| 218 | fn set_initial_windowing(&self, request: WindowingRequest) { |
| 219 | self.inner.set_initial_windowing(request) |
| 220 | } |
| 221 | |
| 222 | fn request_windowing(&self, request: WindowingRequest) -> Task<Result<()>> { |
| 223 | self.inner.request_windowing(request) |
| 224 | } |
| 225 | |
| 226 | fn restart(&self, binary_path: Option<PathBuf>, arguments: Vec<OsString>) { |
| 227 | self.inner.restart(binary_path, arguments) |
| 228 | } |
| 229 | |
| 230 | fn activate(&self, ignoring_other_apps: bool) { |
| 231 | self.inner.activate(ignoring_other_apps) |
| 232 | } |
| 233 | |
| 234 | fn set_activation_policy(&self, policy: ActivationPolicy) { |
| 235 | self.inner.set_activation_policy(policy) |
| 236 | } |
| 237 | |
| 238 | fn hide(&self) { |
| 239 | self.inner.hide() |
| 240 | } |
| 241 | |
| 242 | fn hide_other_apps(&self) { |
| 243 | self.inner.hide_other_apps() |
| 244 | } |
| 245 | |
| 246 | fn unhide_other_apps(&self) { |
| 247 | self.inner.unhide_other_apps() |
| 248 | } |
| 249 | |
| 250 | fn displays(&self) -> Vec<Rc<dyn PlatformDisplay>> { |
| 251 | self.inner.displays() |
| 252 | } |
| 253 | |
| 254 | fn primary_display(&self) -> Option<Rc<dyn PlatformDisplay>> { |
| 255 | self.inner.primary_display() |
| 256 | } |
| 257 | |
| 258 | fn active_window(&self) -> Option<AnyWindowHandle> { |
| 259 | self.inner.active_window() |
| 260 | } |
| 261 | |
| 262 | fn window_stack(&self) -> Option<Vec<AnyWindowHandle>> { |
| 263 | self.inner.window_stack() |
| 264 | } |
| 265 | |
| 266 | fn is_screen_capture_supported(&self) -> bool { |
| 267 | self.inner.is_screen_capture_supported() |
| 268 | } |
| 269 | |
| 270 | fn open_window( |
| 271 | &self, |
| 272 | handle: AnyWindowHandle, |
| 273 | options: WindowParams, |
| 274 | ) -> Result<Box<dyn PlatformWindow>> { |
| 275 | let zoomed = zooms(&options); |
| 276 | let inner = self.inner.open_window(handle, options)?; |
| 277 | let state = Rc::new(WindowState { |
| 278 | shared: self.shared.clone(), |
| 279 | zoomed, |
| 280 | platform_size: Cell::new(inner.content_size()), |
| 281 | platform_scale: Cell::new(inner.scale_factor()), |
| 282 | on_resize: RefCell::new(None), |
| 283 | }); |
| 284 | self.shared.windows.borrow_mut().push(Rc::downgrade(&state)); |
| 285 | Ok(Box::new(ZoomWindow { inner, state })) |
| 286 | } |
| 287 | |
| 288 | fn window_appearance(&self) -> WindowAppearance { |
| 289 | self.inner.window_appearance() |
| 290 | } |
| 291 | |
| 292 | fn set_window_appearance(&self, appearance: Option<WindowAppearance>) { |
| 293 | self.inner.set_window_appearance(appearance) |
| 294 | } |
| 295 | |
| 296 | fn button_layout(&self) -> Option<WindowButtonLayout> { |
| 297 | self.inner.button_layout() |
| 298 | } |
| 299 | |
| 300 | fn open_url(&self, url: &str) { |
| 301 | self.inner.open_url(url) |
| 302 | } |
| 303 | |
| 304 | fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>) { |
| 305 | self.inner.on_open_urls(callback) |
| 306 | } |
| 307 | |
| 308 | fn register_url_scheme(&self, url: &str) -> Task<Result<()>> { |
| 309 | self.inner.register_url_scheme(url) |
| 310 | } |
| 311 | |
| 312 | fn prompt_for_paths( |
| 313 | &self, |
| 314 | options: PathPromptOptions, |
| 315 | ) -> oneshot::Receiver<Result<Option<Vec<PathBuf>>>> { |
| 316 | self.inner.prompt_for_paths(options) |
| 317 | } |
| 318 | |
| 319 | fn prompt_for_new_path( |
| 320 | &self, |
| 321 | directory: &Path, |
| 322 | suggested_name: Option<&str>, |
| 323 | ) -> oneshot::Receiver<Result<Option<PathBuf>>> { |
| 324 | self.inner.prompt_for_new_path(directory, suggested_name) |
| 325 | } |
| 326 | |
| 327 | fn can_select_mixed_files_and_dirs(&self) -> bool { |
| 328 | self.inner.can_select_mixed_files_and_dirs() |
| 329 | } |
| 330 | |
| 331 | fn reveal_path(&self, path: &Path) { |
| 332 | self.inner.reveal_path(path) |
| 333 | } |
| 334 | |
| 335 | fn open_with_system(&self, path: &Path) { |
| 336 | self.inner.open_with_system(path) |
| 337 | } |
| 338 | |
| 339 | fn on_quit(&self, callback: Box<dyn FnMut() -> bool>) { |
| 340 | self.inner.on_quit(callback) |
| 341 | } |
| 342 | |
| 343 | fn on_reopen(&self, callback: Box<dyn FnMut()>) { |
| 344 | self.inner.on_reopen(callback) |
| 345 | } |
| 346 | |
| 347 | fn on_system_sleep(&self, callback: Box<dyn FnMut()>) { |
| 348 | self.inner.on_system_sleep(callback) |
| 349 | } |
| 350 | |
| 351 | fn on_system_wake(&self, callback: Box<dyn FnMut()>) { |
| 352 | self.inner.on_system_wake(callback) |
| 353 | } |
| 354 | |
| 355 | fn on_app_lifecycle(&self, callback: Box<dyn FnMut(AppLifecyclePhase)>) { |
| 356 | self.inner.on_app_lifecycle(callback) |
| 357 | } |
| 358 | |
| 359 | fn on_memory_warning(&self, callback: Box<dyn FnMut()>) { |
| 360 | self.inner.on_memory_warning(callback) |
| 361 | } |
| 362 | |
| 363 | fn gestures(&self) -> Option<Rc<dyn PlatformGestures>> { |
| 364 | self.inner.gestures() |
| 365 | } |
| 366 | |
| 367 | fn set_menus(&self, menus: Vec<Menu>, keymap: &Keymap) { |
| 368 | self.inner.set_menus(menus, keymap) |
| 369 | } |
| 370 | |
| 371 | fn get_menus(&self) -> Option<Vec<OwnedMenu>> { |
| 372 | self.inner.get_menus() |
| 373 | } |
| 374 | |
| 375 | fn set_dock_menu(&self, menu: Vec<MenuItem>, keymap: &Keymap) { |
| 376 | self.inner.set_dock_menu(menu, keymap) |
| 377 | } |
| 378 | |
| 379 | fn perform_dock_menu_action(&self, action: usize) { |
| 380 | self.inner.perform_dock_menu_action(action) |
| 381 | } |
| 382 | |
| 383 | fn add_recent_document(&self, path: &Path) { |
| 384 | self.inner.add_recent_document(path) |
| 385 | } |
| 386 | |
| 387 | fn on_app_menu_action(&self, callback: Box<dyn FnMut(&dyn Action)>) { |
| 388 | self.inner.on_app_menu_action(callback) |
| 389 | } |
| 390 | |
| 391 | fn on_will_open_app_menu(&self, callback: Box<dyn FnMut()>) { |
| 392 | self.inner.on_will_open_app_menu(callback) |
| 393 | } |
| 394 | |
| 395 | fn on_validate_app_menu_command(&self, callback: Box<dyn FnMut(&dyn Action) -> bool>) { |
| 396 | self.inner.on_validate_app_menu_command(callback) |
| 397 | } |
| 398 | |
| 399 | fn thermal_state(&self) -> ThermalState { |
| 400 | self.inner.thermal_state() |
| 401 | } |
| 402 | |
| 403 | fn on_thermal_state_change(&self, callback: Box<dyn FnMut()>) { |
| 404 | self.inner.on_thermal_state_change(callback) |
| 405 | } |
| 406 | |
| 407 | fn prevent_idle_sleep(&self, reason: &str) -> Task<Result<ActivityGuard>> { |
| 408 | self.inner.prevent_idle_sleep(reason) |
| 409 | } |
| 410 | |
| 411 | fn set_app_identity(&self, identifier: &str, name: &str) { |
| 412 | self.inner.set_app_identity(identifier, name) |
| 413 | } |
| 414 | |
| 415 | fn show_system_notification(&self, notification: SystemNotification) { |
| 416 | self.inner.show_system_notification(notification) |
| 417 | } |
| 418 | |
| 419 | fn dismiss_system_notification(&self, tag: &str) { |
| 420 | self.inner.dismiss_system_notification(tag) |
| 421 | } |
| 422 | |
| 423 | fn on_system_notification_response( |
| 424 | &self, |
| 425 | callback: Box<dyn FnMut(SystemNotificationResponse)>, |
| 426 | ) { |
| 427 | self.inner.on_system_notification_response(callback) |
| 428 | } |
| 429 | |
| 430 | fn compositor_name(&self) -> &'static str { |
| 431 | self.inner.compositor_name() |
| 432 | } |
| 433 | |
| 434 | fn graphical_environment(&self) -> Option<GraphicalEnvironment> { |
| 435 | self.inner.graphical_environment() |
| 436 | } |
| 437 | |
| 438 | fn app_path(&self) -> Result<PathBuf> { |
| 439 | self.inner.app_path() |
| 440 | } |
| 441 | |
| 442 | fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf> { |
| 443 | self.inner.path_for_auxiliary_executable(name) |
| 444 | } |
| 445 | |
| 446 | fn set_cursor_style(&self, style: CursorStyle) { |
| 447 | self.inner.set_cursor_style(style) |
| 448 | } |
| 449 | |
| 450 | fn hide_cursor_until_mouse_moves(&self) { |
| 451 | self.inner.hide_cursor_until_mouse_moves() |
| 452 | } |
| 453 | |
| 454 | fn is_cursor_visible(&self) -> bool { |
| 455 | self.inner.is_cursor_visible() |
| 456 | } |
| 457 | |
| 458 | fn should_auto_hide_scrollbars(&self) -> bool { |
| 459 | self.inner.should_auto_hide_scrollbars() |
| 460 | } |
| 461 | |
| 462 | fn read_from_clipboard(&self) -> Option<ClipboardItem> { |
| 463 | self.inner.read_from_clipboard() |
| 464 | } |
| 465 | |
| 466 | fn write_to_clipboard(&self, item: ClipboardItem) { |
| 467 | self.inner.write_to_clipboard(item) |
| 468 | } |
| 469 | |
| 470 | fn read_from_clipboard_async(&self) -> Task<Result<Option<ClipboardItem>, ClipboardReadError>> { |
| 471 | self.inner.read_from_clipboard_async() |
| 472 | } |
| 473 | |
| 474 | #[cfg(any(target_os = "linux", target_os = "freebsd"))] |
| 475 | fn read_from_primary(&self) -> Option<ClipboardItem> { |
| 476 | self.inner.read_from_primary() |
| 477 | } |
| 478 | |
| 479 | #[cfg(any(target_os = "linux", target_os = "freebsd"))] |
| 480 | fn write_to_primary(&self, item: ClipboardItem) { |
| 481 | self.inner.write_to_primary(item) |
| 482 | } |
| 483 | |
| 484 | #[cfg(target_os = "macos")] |
| 485 | fn read_from_find_pasteboard(&self) -> Option<ClipboardItem> { |
| 486 | self.inner.read_from_find_pasteboard() |
| 487 | } |
| 488 | |
| 489 | #[cfg(target_os = "macos")] |
| 490 | fn write_to_find_pasteboard(&self, item: ClipboardItem) { |
| 491 | self.inner.write_to_find_pasteboard(item) |
| 492 | } |
| 493 | |
| 494 | fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Task<Result<()>> { |
| 495 | self.inner.write_credentials(url, username, password) |
| 496 | } |
| 497 | |
| 498 | fn read_credentials(&self, url: &str) -> Task<Result<Option<(String, Vec<u8>)>>> { |
| 499 | self.inner.read_credentials(url) |
| 500 | } |
| 501 | |
| 502 | fn delete_credentials(&self, url: &str) -> Task<Result<()>> { |
| 503 | self.inner.delete_credentials(url) |
| 504 | } |
| 505 | |
| 506 | fn keyboard_layout(&self) -> Box<dyn PlatformKeyboardLayout> { |
| 507 | self.inner.keyboard_layout() |
| 508 | } |
| 509 | |
| 510 | fn keyboard_mapper(&self) -> Rc<dyn PlatformKeyboardMapper> { |
| 511 | self.inner.keyboard_mapper() |
| 512 | } |
| 513 | |
| 514 | fn on_keyboard_layout_change(&self, callback: Box<dyn FnMut()>) { |
| 515 | self.inner.on_keyboard_layout_change(callback) |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | struct ZoomWindow { |
| 520 | inner: Box<dyn PlatformWindow>, |
| 521 | state: Rc<WindowState>, |
| 522 | } |
| 523 | |
| 524 | impl HasWindowHandle for ZoomWindow { |
| 525 | fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> { |
| 526 | self.inner.window_handle() |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | impl HasDisplayHandle for ZoomWindow { |
| 531 | fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> { |
| 532 | self.inner.display_handle() |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | impl PlatformWindow for ZoomWindow { |
| 537 | fn bounds(&self) -> Bounds<Pixels> { |
| 538 | self.inner.bounds() |
| 539 | } |
| 540 | |
| 541 | fn is_maximized(&self) -> bool { |
| 542 | self.inner.is_maximized() |
| 543 | } |
| 544 | |
| 545 | fn window_bounds(&self) -> WindowBounds { |
| 546 | self.inner.window_bounds() |
| 547 | } |
| 548 | |
| 549 | fn content_size(&self) -> Size<Pixels> { |
| 550 | scale_size(self.inner.content_size(), 1. / self.state.zoom()) |
| 551 | } |
| 552 | |
| 553 | fn on_visual_viewport_changed(&self, callback: Box<dyn FnMut()>) { |
| 554 | self.inner.on_visual_viewport_changed(callback) |
| 555 | } |
| 556 | |
| 557 | fn prepare_frame(&self) -> bool { |
| 558 | self.inner.prepare_frame() |
| 559 | } |
| 560 | |
| 561 | fn resize(&mut self, size: Size<Pixels>) { |
| 562 | let zoom = self.state.zoom(); |
| 563 | self.inner.resize(scale_size(size, zoom)) |
| 564 | } |
| 565 | |
| 566 | fn scale_factor(&self) -> f32 { |
| 567 | self.inner.scale_factor() * self.state.zoom() |
| 568 | } |
| 569 | |
| 570 | fn appearance(&self) -> WindowAppearance { |
| 571 | self.inner.appearance() |
| 572 | } |
| 573 | |
| 574 | fn display(&self) -> Option<Rc<dyn PlatformDisplay>> { |
| 575 | self.inner.display() |
| 576 | } |
| 577 | |
| 578 | fn mouse_position(&self) -> Point<Pixels> { |
| 579 | scale_point(self.inner.mouse_position(), 1. / self.state.zoom()) |
| 580 | } |
| 581 | |
| 582 | fn modifiers(&self) -> Modifiers { |
| 583 | self.inner.modifiers() |
| 584 | } |
| 585 | |
| 586 | fn capslock(&self) -> Capslock { |
| 587 | self.inner.capslock() |
| 588 | } |
| 589 | |
| 590 | fn set_input_handler(&mut self, input_handler: PlatformInputHandler) { |
| 591 | self.inner.set_input_handler(input_handler) |
| 592 | } |
| 593 | |
| 594 | fn take_input_handler(&mut self) -> Option<PlatformInputHandler> { |
| 595 | self.inner.take_input_handler() |
| 596 | } |
| 597 | |
| 598 | fn set_text_input_configuration(&mut self, configuration: TextInputConfiguration) { |
| 599 | self.inner.set_text_input_configuration(configuration) |
| 600 | } |
| 601 | |
| 602 | fn prompt( |
| 603 | &self, |
| 604 | level: PromptLevel, |
| 605 | msg: &str, |
| 606 | detail: Option<&str>, |
| 607 | answers: &[PromptButton], |
| 608 | ) -> Option<oneshot::Receiver<usize>> { |
| 609 | self.inner.prompt(level, msg, detail, answers) |
| 610 | } |
| 611 | |
| 612 | fn activate(&self) { |
| 613 | self.inner.activate() |
| 614 | } |
| 615 | |
| 616 | fn request_attention(&self) { |
| 617 | self.inner.request_attention() |
| 618 | } |
| 619 | |
| 620 | fn is_active(&self) -> bool { |
| 621 | self.inner.is_active() |
| 622 | } |
| 623 | |
| 624 | fn visibility(&self) -> WindowVisibility { |
| 625 | self.inner.visibility() |
| 626 | } |
| 627 | |
| 628 | fn is_hovered(&self) -> bool { |
| 629 | self.inner.is_hovered() |
| 630 | } |
| 631 | |
| 632 | fn background_appearance(&self) -> WindowBackgroundAppearance { |
| 633 | self.inner.background_appearance() |
| 634 | } |
| 635 | |
| 636 | fn set_title(&mut self, title: &str) { |
| 637 | self.inner.set_title(title) |
| 638 | } |
| 639 | |
| 640 | fn set_background_appearance(&self, background_appearance: WindowBackgroundAppearance) { |
| 641 | self.inner.set_background_appearance(background_appearance) |
| 642 | } |
| 643 | |
| 644 | fn minimize(&self) { |
| 645 | self.inner.minimize() |
| 646 | } |
| 647 | |
| 648 | fn zoom(&self) { |
| 649 | self.inner.zoom() |
| 650 | } |
| 651 | |
| 652 | fn toggle_fullscreen(&self) { |
| 653 | self.inner.toggle_fullscreen() |
| 654 | } |
| 655 | |
| 656 | fn is_fullscreen(&self) -> bool { |
| 657 | self.inner.is_fullscreen() |
| 658 | } |
| 659 | |
| 660 | fn frame_waker(&self) -> Option<Rc<dyn Fn()>> { |
| 661 | self.inner.frame_waker() |
| 662 | } |
| 663 | |
| 664 | fn on_request_frame(&self, callback: Box<dyn FnMut(RequestFrameOptions)>) { |
| 665 | self.inner.on_request_frame(callback) |
| 666 | } |
| 667 | |
| 668 | fn on_input(&self, mut callback: Box<dyn FnMut(PlatformInput) -> DispatchEventResult>) { |
| 669 | let state = self.state.clone(); |
| 670 | self.inner.on_input(Box::new(move |input| { |
| 671 | callback(unzoom_input(input, state.zoom())) |
| 672 | })) |
| 673 | } |
| 674 | |
| 675 | fn on_active_status_change(&self, callback: Box<dyn FnMut(bool)>) { |
| 676 | self.inner.on_active_status_change(callback) |
| 677 | } |
| 678 | |
| 679 | fn on_visibility_change(&self, callback: Box<dyn FnMut(WindowVisibility)>) { |
| 680 | self.inner.on_visibility_change(callback) |
| 681 | } |
| 682 | |
| 683 | fn on_hover_status_change(&self, callback: Box<dyn FnMut(bool)>) { |
| 684 | self.inner.on_hover_status_change(callback) |
| 685 | } |
| 686 | |
| 687 | fn on_resize(&self, callback: Box<dyn FnMut(Size<Pixels>, f32)>) { |
| 688 | *self.state.on_resize.borrow_mut() = Some(callback); |
| 689 | let state = self.state.clone(); |
| 690 | self.inner.on_resize(Box::new(move |size, scale| { |
| 691 | state.platform_size.set(size); |
| 692 | state.platform_scale.set(scale); |
| 693 | state.relayout(); |
| 694 | })) |
| 695 | } |
| 696 | |
| 697 | fn on_moved(&self, callback: Box<dyn FnMut()>) { |
| 698 | self.inner.on_moved(callback) |
| 699 | } |
| 700 | |
| 701 | fn on_should_close(&self, callback: Box<dyn FnMut() -> bool>) { |
| 702 | self.inner.on_should_close(callback) |
| 703 | } |
| 704 | |
| 705 | fn on_hit_test_window_control(&self, callback: Box<dyn FnMut() -> Option<WindowControlArea>>) { |
| 706 | self.inner.on_hit_test_window_control(callback) |
| 707 | } |
| 708 | |
| 709 | fn on_close(&self, callback: Box<dyn FnOnce()>) { |
| 710 | self.inner.on_close(callback) |
| 711 | } |
| 712 | |
| 713 | fn on_appearance_changed(&self, callback: Box<dyn FnMut()>) { |
| 714 | self.inner.on_appearance_changed(callback) |
| 715 | } |
| 716 | |
| 717 | fn on_button_layout_changed(&self, callback: Box<dyn FnMut()>) { |
| 718 | self.inner.on_button_layout_changed(callback) |
| 719 | } |
| 720 | |
| 721 | fn draw(&self, scene: &Scene) { |
| 722 | self.inner.draw(scene) |
| 723 | } |
| 724 | |
| 725 | fn schedule_frame(&self) { |
| 726 | self.inner.schedule_frame() |
| 727 | } |
| 728 | |
| 729 | fn sprite_atlas(&self) -> Arc<dyn PlatformAtlas> { |
| 730 | self.inner.sprite_atlas() |
| 731 | } |
| 732 | |
| 733 | fn is_subpixel_rendering_supported(&self) -> bool { |
| 734 | self.inner.is_subpixel_rendering_supported() |
| 735 | } |
| 736 | |
| 737 | fn get_title(&self) -> String { |
| 738 | self.inner.get_title() |
| 739 | } |
| 740 | |
| 741 | fn show_character_palette(&self) { |
| 742 | self.inner.show_character_palette() |
| 743 | } |
| 744 | |
| 745 | fn titlebar_double_click(&self, is_resizable: bool, is_minimizable: bool) { |
| 746 | self.inner |
| 747 | .titlebar_double_click(is_resizable, is_minimizable) |
| 748 | } |
| 749 | |
| 750 | fn native_window_state(&self) -> Option<Vec<u8>> { |
| 751 | self.inner.native_window_state() |
| 752 | } |
| 753 | |
| 754 | fn restore_native_window_state(&self, state: &[u8]) { |
| 755 | self.inner.restore_native_window_state(state) |
| 756 | } |
| 757 | |
| 758 | #[cfg(target_os = "windows")] |
| 759 | fn get_raw_handle(&self) -> windows::Win32::Foundation::HWND { |
| 760 | self.inner.get_raw_handle() |
| 761 | } |
| 762 | |
| 763 | fn inner_window_bounds(&self) -> WindowBounds { |
| 764 | self.inner.inner_window_bounds() |
| 765 | } |
| 766 | |
| 767 | fn request_decorations(&self, decorations: WindowDecorations) { |
| 768 | self.inner.request_decorations(decorations) |
| 769 | } |
| 770 | |
| 771 | fn show_window_menu(&self, position: Point<Pixels>) { |
| 772 | self.inner |
| 773 | .show_window_menu(scale_point(position, self.state.zoom())) |
| 774 | } |
| 775 | |
| 776 | fn start_window_move(&self) { |
| 777 | self.inner.start_window_move() |
| 778 | } |
| 779 | |
| 780 | fn can_start_external_drag(&self) -> bool { |
| 781 | self.inner.can_start_external_drag() |
| 782 | } |
| 783 | |
| 784 | fn start_external_drag(&self, payload: &ExternalDragPayload) -> bool { |
| 785 | self.inner.start_external_drag(payload) |
| 786 | } |
| 787 | |
| 788 | fn start_window_resize(&self, edge: ResizeEdge) { |
| 789 | self.inner.start_window_resize(edge) |
| 790 | } |
| 791 | |
| 792 | fn set_exclusive_zone(&self, zone: Pixels) { |
| 793 | self.inner.set_exclusive_zone(zone * self.state.zoom()) |
| 794 | } |
| 795 | |
| 796 | fn set_input_region(&self, region: Option<&[Bounds<Pixels>]>) { |
| 797 | let zoom = self.state.zoom(); |
| 798 | let scaled: Option<Vec<Bounds<Pixels>>> = region.map(|region| { |
| 799 | region |
| 800 | .iter() |
| 801 | .map(|bounds| scale_bounds(*bounds, zoom)) |
| 802 | .collect() |
| 803 | }); |
| 804 | self.inner.set_input_region(scaled.as_deref()) |
| 805 | } |
| 806 | |
| 807 | fn window_decorations(&self) -> Decorations { |
| 808 | self.inner.window_decorations() |
| 809 | } |
| 810 | |
| 811 | fn set_app_id(&mut self, app_id: &str) { |
| 812 | self.inner.set_app_id(app_id) |
| 813 | } |
| 814 | |
| 815 | fn map_window(&mut self) -> Result<()> { |
| 816 | self.inner.map_window() |
| 817 | } |
| 818 | |
| 819 | fn window_controls(&self) -> WindowControls { |
| 820 | self.inner.window_controls() |
| 821 | } |
| 822 | |
| 823 | fn set_client_inset(&self, inset: Pixels) { |
| 824 | self.inner.set_client_inset(inset * self.state.zoom()) |
| 825 | } |
| 826 | |
| 827 | fn gpu_specs(&self) -> Option<GpuSpecs> { |
| 828 | self.inner.gpu_specs() |
| 829 | } |
| 830 | |
| 831 | fn update_ime_position(&self, bounds: Bounds<Pixels>) { |
| 832 | self.inner |
| 833 | .update_ime_position(scale_bounds(bounds, self.state.zoom())) |
| 834 | } |
| 835 | |
| 836 | fn insets(&self) -> WindowInsets { |
| 837 | self.inner.insets() |
| 838 | } |
| 839 | |
| 840 | fn on_insets_changed(&self, callback: Box<dyn FnMut(WindowInsets)>) { |
| 841 | self.inner.on_insets_changed(callback) |
| 842 | } |
| 843 | |
| 844 | fn text_input_state_changed(&self, change: TextInputStateChange) { |
| 845 | self.inner.text_input_state_changed(change) |
| 846 | } |
| 847 | |
| 848 | fn play_system_bell(&self) { |
| 849 | self.inner.play_system_bell() |
| 850 | } |
| 851 | |
| 852 | fn a11y_init(&self, callbacks: A11yCallbacks) { |
| 853 | self.inner.a11y_init(callbacks) |
| 854 | } |
| 855 | |
| 856 | fn a11y_tree_update(&self, tree_update: accesskit::TreeUpdate) { |
| 857 | self.inner.a11y_tree_update(tree_update) |
| 858 | } |
| 859 | |
| 860 | fn a11y_update_window_bounds(&self) { |
| 861 | self.inner.a11y_update_window_bounds() |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | #[cfg(test)] |
| 866 | mod tests { |
| 867 | use gpui_kit::{ |
| 868 | FileDropEvent, Modifiers, MouseButton, MouseDownEvent, PlatformInput, ScrollDelta, |
| 869 | ScrollWheelEvent, TouchPhase, point, px, |
| 870 | }; |
| 871 | |
| 872 | use super::unzoom_input; |
| 873 | |
| 874 | #[test] |
| 875 | fn pointer_positions_move_into_the_zoomed_layout() { |
| 876 | let down = PlatformInput::MouseDown(MouseDownEvent { |
| 877 | button: MouseButton::Left, |
| 878 | position: point(px(300.), px(150.)), |
| 879 | modifiers: Modifiers::default(), |
| 880 | click_count: 1, |
| 881 | first_mouse: false, |
| 882 | }); |
| 883 | let PlatformInput::MouseDown(event) = unzoom_input(down, 1.5) else { |
| 884 | panic!("mouse down stays a mouse down"); |
| 885 | }; |
| 886 | assert_eq!(event.position, point(px(200.), px(100.))); |
| 887 | } |
| 888 | |
| 889 | #[test] |
| 890 | fn precise_scrolling_follows_the_finger_line_scrolling_keeps_its_lines() { |
| 891 | let wheel = |delta| { |
| 892 | PlatformInput::ScrollWheel(ScrollWheelEvent { |
| 893 | position: point(px(100.), px(100.)), |
| 894 | delta, |
| 895 | modifiers: Modifiers::default(), |
| 896 | touch_phase: TouchPhase::Moved, |
| 897 | }) |
| 898 | }; |
| 899 | let PlatformInput::ScrollWheel(pixels) = |
| 900 | unzoom_input(wheel(ScrollDelta::Pixels(point(px(0.), px(40.)))), 2.) |
| 901 | else { |
| 902 | panic!("scroll stays a scroll"); |
| 903 | }; |
| 904 | assert_eq!(pixels.position, point(px(50.), px(50.))); |
| 905 | assert!( |
| 906 | matches!(pixels.delta, ScrollDelta::Pixels(delta) if delta == point(px(0.), px(20.))) |
| 907 | ); |
| 908 | let PlatformInput::ScrollWheel(lines) = |
| 909 | unzoom_input(wheel(ScrollDelta::Lines(point(0., 3.))), 2.) |
| 910 | else { |
| 911 | panic!("scroll stays a scroll"); |
| 912 | }; |
| 913 | assert!(matches!(lines.delta, ScrollDelta::Lines(delta) if delta == point(0., 3.))); |
| 914 | } |
| 915 | |
| 916 | #[test] |
| 917 | fn file_drops_land_where_the_pointer_is() { |
| 918 | let drop = PlatformInput::FileDrop(FileDropEvent::Submit { |
| 919 | position: point(px(90.), px(30.)), |
| 920 | }); |
| 921 | let PlatformInput::FileDrop(FileDropEvent::Submit { position }) = unzoom_input(drop, 1.25) |
| 922 | else { |
| 923 | panic!("drop stays a drop"); |
| 924 | }; |
| 925 | assert_eq!(position, point(px(72.), px(24.))); |
| 926 | } |
| 927 | } |