window.rs

   1use crate::{
   2    BoolExt, DisplayLink, MacDisplay, NSRange, NSStringExt, TISCopyCurrentKeyboardInputSource,
   3    TISGetInputSourceProperty, events::platform_input_from_native,
   4    kTISPropertyInputSourceIsASCIICapable, kTISPropertyInputSourceType, kTISTypeKeyboardInputMode,
   5    ns_string, renderer,
   6};
   7#[cfg(any(test, feature = "test-support"))]
   8use anyhow::Result;
   9use block::ConcreteBlock;
  10use cocoa::{
  11    appkit::{
  12        NSAppKitVersionNumber, NSAppKitVersionNumber12_0, NSApplication, NSBackingStoreBuffered,
  13        NSColor, NSEvent, NSEventModifierFlags, NSFilenamesPboardType, NSPasteboard, NSScreen,
  14        NSView, NSViewHeightSizable, NSViewWidthSizable, NSVisualEffectMaterial,
  15        NSVisualEffectState, NSVisualEffectView, NSWindow, NSWindowButton,
  16        NSWindowCollectionBehavior, NSWindowOcclusionState, NSWindowOrderingMode,
  17        NSWindowStyleMask, NSWindowTitleVisibility,
  18    },
  19    base::{id, nil},
  20    foundation::{
  21        NSArray, NSAutoreleasePool, NSDictionary, NSFastEnumeration, NSInteger, NSNotFound,
  22        NSOperatingSystemVersion, NSPoint, NSProcessInfo, NSRect, NSSize, NSString, NSUInteger,
  23        NSUserDefaults,
  24    },
  25};
  26use dispatch2::DispatchQueue;
  27use gpui::{
  28    AnyWindowHandle, BackgroundExecutor, Bounds, Capslock, ExternalPaths, FileDropEvent,
  29    ForegroundExecutor, KeyDownEvent, Keystroke, Modifiers, ModifiersChangedEvent, MouseButton,
  30    MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, PlatformAtlas, PlatformDisplay,
  31    PlatformInput, PlatformInputHandler, PlatformWindow, Point, PromptButton, PromptLevel,
  32    RequestFrameOptions, SharedString, Size, SystemWindowTab, WindowAppearance,
  33    WindowBackgroundAppearance, WindowBounds, WindowControlArea, WindowKind, WindowParams, point,
  34    px, size,
  35};
  36#[cfg(any(test, feature = "test-support"))]
  37use image::RgbaImage;
  38
  39use core_foundation::base::{CFRelease, CFTypeRef};
  40use core_foundation_sys::base::CFEqual;
  41use core_foundation_sys::number::{CFBooleanGetValue, CFBooleanRef};
  42use core_graphics::display::{CGDirectDisplayID, CGPoint, CGRect};
  43use ctor::ctor;
  44use futures::channel::oneshot;
  45use objc::{
  46    class,
  47    declare::ClassDecl,
  48    msg_send,
  49    runtime::{BOOL, Class, NO, Object, Protocol, Sel, YES},
  50    sel, sel_impl,
  51};
  52use parking_lot::Mutex;
  53use raw_window_handle as rwh;
  54use smallvec::SmallVec;
  55use std::{
  56    cell::Cell,
  57    ffi::{CStr, c_void},
  58    mem,
  59    ops::Range,
  60    path::PathBuf,
  61    ptr::{self, NonNull},
  62    rc::Rc,
  63    sync::{
  64        Arc, Weak,
  65        atomic::{AtomicBool, Ordering},
  66    },
  67    time::Duration,
  68};
  69use util::ResultExt;
  70
  71const WINDOW_STATE_IVAR: &str = "windowState";
  72
  73static mut WINDOW_CLASS: *const Class = ptr::null();
  74static mut PANEL_CLASS: *const Class = ptr::null();
  75static mut VIEW_CLASS: *const Class = ptr::null();
  76static mut BLURRED_VIEW_CLASS: *const Class = ptr::null();
  77
  78#[allow(non_upper_case_globals)]
  79const NSWindowStyleMaskNonactivatingPanel: NSWindowStyleMask =
  80    NSWindowStyleMask::from_bits_retain(1 << 7);
  81// WindowLevel const value ref: https://docs.rs/core-graphics2/0.4.1/src/core_graphics2/window_level.rs.html
  82#[allow(non_upper_case_globals)]
  83const NSNormalWindowLevel: NSInteger = 0;
  84#[allow(non_upper_case_globals)]
  85const NSFloatingWindowLevel: NSInteger = 3;
  86#[allow(non_upper_case_globals)]
  87const NSPopUpWindowLevel: NSInteger = 101;
  88#[allow(non_upper_case_globals)]
  89const NSTrackingMouseEnteredAndExited: NSUInteger = 0x01;
  90#[allow(non_upper_case_globals)]
  91const NSTrackingMouseMoved: NSUInteger = 0x02;
  92#[allow(non_upper_case_globals)]
  93const NSTrackingActiveAlways: NSUInteger = 0x80;
  94#[allow(non_upper_case_globals)]
  95const NSTrackingInVisibleRect: NSUInteger = 0x200;
  96#[allow(non_upper_case_globals)]
  97const NSWindowAnimationBehaviorUtilityWindow: NSInteger = 4;
  98#[allow(non_upper_case_globals)]
  99const NSViewLayerContentsRedrawDuringViewResize: NSInteger = 2;
 100// https://developer.apple.com/documentation/appkit/nsdragoperation
 101type NSDragOperation = NSUInteger;
 102#[allow(non_upper_case_globals)]
 103const NSDragOperationNone: NSDragOperation = 0;
 104#[allow(non_upper_case_globals)]
 105const NSDragOperationCopy: NSDragOperation = 1;
 106#[derive(PartialEq)]
 107pub enum UserTabbingPreference {
 108    Never,
 109    Always,
 110    InFullScreen,
 111}
 112
 113#[link(name = "CoreGraphics", kind = "framework")]
 114unsafe extern "C" {
 115    // Widely used private APIs; Apple uses them for their Terminal.app.
 116    fn CGSMainConnectionID() -> id;
 117    fn CGSSetWindowBackgroundBlurRadius(
 118        connection_id: id,
 119        window_id: NSInteger,
 120        radius: i64,
 121    ) -> i32;
 122}
 123
 124#[ctor]
 125unsafe fn build_classes() {
 126    unsafe {
 127        WINDOW_CLASS = build_window_class("GPUIWindow", class!(NSWindow));
 128        PANEL_CLASS = build_window_class("GPUIPanel", class!(NSPanel));
 129        VIEW_CLASS = {
 130            let mut decl = ClassDecl::new("GPUIView", class!(NSView)).unwrap();
 131            decl.add_ivar::<*mut c_void>(WINDOW_STATE_IVAR);
 132            unsafe {
 133                decl.add_method(sel!(dealloc), dealloc_view as extern "C" fn(&Object, Sel));
 134
 135                decl.add_method(
 136                    sel!(performKeyEquivalent:),
 137                    handle_key_equivalent as extern "C" fn(&Object, Sel, id) -> BOOL,
 138                );
 139                decl.add_method(
 140                    sel!(keyDown:),
 141                    handle_key_down as extern "C" fn(&Object, Sel, id),
 142                );
 143                decl.add_method(
 144                    sel!(keyUp:),
 145                    handle_key_up as extern "C" fn(&Object, Sel, id),
 146                );
 147                decl.add_method(
 148                    sel!(mouseDown:),
 149                    handle_view_event as extern "C" fn(&Object, Sel, id),
 150                );
 151                decl.add_method(
 152                    sel!(mouseUp:),
 153                    handle_view_event as extern "C" fn(&Object, Sel, id),
 154                );
 155                decl.add_method(
 156                    sel!(rightMouseDown:),
 157                    handle_view_event as extern "C" fn(&Object, Sel, id),
 158                );
 159                decl.add_method(
 160                    sel!(rightMouseUp:),
 161                    handle_view_event as extern "C" fn(&Object, Sel, id),
 162                );
 163                decl.add_method(
 164                    sel!(otherMouseDown:),
 165                    handle_view_event as extern "C" fn(&Object, Sel, id),
 166                );
 167                decl.add_method(
 168                    sel!(otherMouseUp:),
 169                    handle_view_event as extern "C" fn(&Object, Sel, id),
 170                );
 171                decl.add_method(
 172                    sel!(mouseMoved:),
 173                    handle_view_event as extern "C" fn(&Object, Sel, id),
 174                );
 175                decl.add_method(
 176                    sel!(pressureChangeWithEvent:),
 177                    handle_view_event as extern "C" fn(&Object, Sel, id),
 178                );
 179                decl.add_method(
 180                    sel!(mouseExited:),
 181                    handle_view_event as extern "C" fn(&Object, Sel, id),
 182                );
 183                decl.add_method(
 184                    sel!(magnifyWithEvent:),
 185                    handle_view_event as extern "C" fn(&Object, Sel, id),
 186                );
 187                decl.add_method(
 188                    sel!(mouseDragged:),
 189                    handle_view_event as extern "C" fn(&Object, Sel, id),
 190                );
 191                decl.add_method(
 192                    sel!(scrollWheel:),
 193                    handle_view_event as extern "C" fn(&Object, Sel, id),
 194                );
 195                decl.add_method(
 196                    sel!(swipeWithEvent:),
 197                    handle_view_event as extern "C" fn(&Object, Sel, id),
 198                );
 199                decl.add_method(
 200                    sel!(flagsChanged:),
 201                    handle_view_event as extern "C" fn(&Object, Sel, id),
 202                );
 203
 204                decl.add_method(
 205                    sel!(makeBackingLayer),
 206                    make_backing_layer as extern "C" fn(&Object, Sel) -> id,
 207                );
 208
 209                decl.add_protocol(Protocol::get("CALayerDelegate").unwrap());
 210                decl.add_method(
 211                    sel!(viewDidChangeBackingProperties),
 212                    view_did_change_backing_properties as extern "C" fn(&Object, Sel),
 213                );
 214                decl.add_method(
 215                    sel!(setFrameSize:),
 216                    set_frame_size as extern "C" fn(&Object, Sel, NSSize),
 217                );
 218                decl.add_method(
 219                    sel!(displayLayer:),
 220                    display_layer as extern "C" fn(&Object, Sel, id),
 221                );
 222
 223                decl.add_protocol(Protocol::get("NSTextInputClient").unwrap());
 224                decl.add_method(
 225                    sel!(validAttributesForMarkedText),
 226                    valid_attributes_for_marked_text as extern "C" fn(&Object, Sel) -> id,
 227                );
 228                decl.add_method(
 229                    sel!(hasMarkedText),
 230                    has_marked_text as extern "C" fn(&Object, Sel) -> BOOL,
 231                );
 232                decl.add_method(
 233                    sel!(markedRange),
 234                    marked_range as extern "C" fn(&Object, Sel) -> NSRange,
 235                );
 236                decl.add_method(
 237                    sel!(selectedRange),
 238                    selected_range as extern "C" fn(&Object, Sel) -> NSRange,
 239                );
 240                decl.add_method(
 241                    sel!(firstRectForCharacterRange:actualRange:),
 242                    first_rect_for_character_range
 243                        as extern "C" fn(&Object, Sel, NSRange, id) -> NSRect,
 244                );
 245                decl.add_method(
 246                    sel!(insertText:replacementRange:),
 247                    insert_text as extern "C" fn(&Object, Sel, id, NSRange),
 248                );
 249                decl.add_method(
 250                    sel!(setMarkedText:selectedRange:replacementRange:),
 251                    set_marked_text as extern "C" fn(&Object, Sel, id, NSRange, NSRange),
 252                );
 253                decl.add_method(sel!(unmarkText), unmark_text as extern "C" fn(&Object, Sel));
 254                decl.add_method(
 255                    sel!(attributedSubstringForProposedRange:actualRange:),
 256                    attributed_substring_for_proposed_range
 257                        as extern "C" fn(&Object, Sel, NSRange, *mut c_void) -> id,
 258                );
 259                decl.add_method(
 260                    sel!(viewDidChangeEffectiveAppearance),
 261                    view_did_change_effective_appearance as extern "C" fn(&Object, Sel),
 262                );
 263
 264                // Suppress beep on keystrokes with modifier keys.
 265                decl.add_method(
 266                    sel!(doCommandBySelector:),
 267                    do_command_by_selector as extern "C" fn(&Object, Sel, Sel),
 268                );
 269
 270                decl.add_method(
 271                    sel!(acceptsFirstMouse:),
 272                    accepts_first_mouse as extern "C" fn(&Object, Sel, id) -> BOOL,
 273                );
 274
 275                decl.add_method(
 276                    sel!(characterIndexForPoint:),
 277                    character_index_for_point as extern "C" fn(&Object, Sel, NSPoint) -> u64,
 278                );
 279            }
 280            decl.register()
 281        };
 282        BLURRED_VIEW_CLASS = {
 283            let mut decl = ClassDecl::new("BlurredView", class!(NSVisualEffectView)).unwrap();
 284            unsafe {
 285                decl.add_method(
 286                    sel!(initWithFrame:),
 287                    blurred_view_init_with_frame as extern "C" fn(&Object, Sel, NSRect) -> id,
 288                );
 289                decl.add_method(
 290                    sel!(updateLayer),
 291                    blurred_view_update_layer as extern "C" fn(&Object, Sel),
 292                );
 293                decl.register()
 294            }
 295        };
 296    }
 297}
 298
 299pub(crate) fn convert_mouse_position(position: NSPoint, window_height: Pixels) -> Point<Pixels> {
 300    point(
 301        px(position.x as f32),
 302        // macOS screen coordinates are relative to bottom left
 303        window_height - px(position.y as f32),
 304    )
 305}
 306
 307unsafe fn build_window_class(name: &'static str, superclass: &Class) -> *const Class {
 308    unsafe {
 309        let mut decl = ClassDecl::new(name, superclass).unwrap();
 310        decl.add_ivar::<*mut c_void>(WINDOW_STATE_IVAR);
 311        decl.add_method(sel!(dealloc), dealloc_window as extern "C" fn(&Object, Sel));
 312
 313        decl.add_method(
 314            sel!(canBecomeMainWindow),
 315            yes as extern "C" fn(&Object, Sel) -> BOOL,
 316        );
 317        decl.add_method(
 318            sel!(canBecomeKeyWindow),
 319            yes as extern "C" fn(&Object, Sel) -> BOOL,
 320        );
 321        decl.add_method(
 322            sel!(windowDidResize:),
 323            window_did_resize as extern "C" fn(&Object, Sel, id),
 324        );
 325        decl.add_method(
 326            sel!(windowDidChangeOcclusionState:),
 327            window_did_change_occlusion_state as extern "C" fn(&Object, Sel, id),
 328        );
 329        decl.add_method(
 330            sel!(windowWillEnterFullScreen:),
 331            window_will_enter_fullscreen as extern "C" fn(&Object, Sel, id),
 332        );
 333        decl.add_method(
 334            sel!(windowWillExitFullScreen:),
 335            window_will_exit_fullscreen as extern "C" fn(&Object, Sel, id),
 336        );
 337        decl.add_method(
 338            sel!(windowDidMove:),
 339            window_did_move as extern "C" fn(&Object, Sel, id),
 340        );
 341        decl.add_method(
 342            sel!(windowDidChangeScreen:),
 343            window_did_change_screen as extern "C" fn(&Object, Sel, id),
 344        );
 345        decl.add_method(
 346            sel!(windowDidBecomeKey:),
 347            window_did_change_key_status as extern "C" fn(&Object, Sel, id),
 348        );
 349        decl.add_method(
 350            sel!(windowDidResignKey:),
 351            window_did_change_key_status as extern "C" fn(&Object, Sel, id),
 352        );
 353        decl.add_method(
 354            sel!(windowShouldClose:),
 355            window_should_close as extern "C" fn(&Object, Sel, id) -> BOOL,
 356        );
 357
 358        decl.add_method(sel!(close), close_window as extern "C" fn(&Object, Sel));
 359
 360        decl.add_method(
 361            sel!(draggingEntered:),
 362            dragging_entered as extern "C" fn(&Object, Sel, id) -> NSDragOperation,
 363        );
 364        decl.add_method(
 365            sel!(draggingUpdated:),
 366            dragging_updated as extern "C" fn(&Object, Sel, id) -> NSDragOperation,
 367        );
 368        decl.add_method(
 369            sel!(draggingExited:),
 370            dragging_exited as extern "C" fn(&Object, Sel, id),
 371        );
 372        decl.add_method(
 373            sel!(performDragOperation:),
 374            perform_drag_operation as extern "C" fn(&Object, Sel, id) -> BOOL,
 375        );
 376        decl.add_method(
 377            sel!(concludeDragOperation:),
 378            conclude_drag_operation as extern "C" fn(&Object, Sel, id),
 379        );
 380
 381        decl.add_method(
 382            sel!(addTitlebarAccessoryViewController:),
 383            add_titlebar_accessory_view_controller as extern "C" fn(&Object, Sel, id),
 384        );
 385
 386        decl.add_method(
 387            sel!(moveTabToNewWindow:),
 388            move_tab_to_new_window as extern "C" fn(&Object, Sel, id),
 389        );
 390
 391        decl.add_method(
 392            sel!(mergeAllWindows:),
 393            merge_all_windows as extern "C" fn(&Object, Sel, id),
 394        );
 395
 396        decl.add_method(
 397            sel!(selectNextTab:),
 398            select_next_tab as extern "C" fn(&Object, Sel, id),
 399        );
 400
 401        decl.add_method(
 402            sel!(selectPreviousTab:),
 403            select_previous_tab as extern "C" fn(&Object, Sel, id),
 404        );
 405
 406        decl.add_method(
 407            sel!(toggleTabBar:),
 408            toggle_tab_bar as extern "C" fn(&Object, Sel, id),
 409        );
 410
 411        decl.register()
 412    }
 413}
 414
 415struct MacWindowState {
 416    handle: AnyWindowHandle,
 417    foreground_executor: ForegroundExecutor,
 418    background_executor: BackgroundExecutor,
 419    native_window: id,
 420    native_view: NonNull<Object>,
 421    blurred_view: Option<id>,
 422    background_appearance: WindowBackgroundAppearance,
 423    display_link: Option<DisplayLink>,
 424    renderer: renderer::Renderer,
 425    request_frame_callback: Option<Box<dyn FnMut(RequestFrameOptions)>>,
 426    event_callback: Option<Box<dyn FnMut(PlatformInput) -> gpui::DispatchEventResult>>,
 427    activate_callback: Option<Box<dyn FnMut(bool)>>,
 428    resize_callback: Option<Box<dyn FnMut(Size<Pixels>, f32)>>,
 429    moved_callback: Option<Box<dyn FnMut()>>,
 430    should_close_callback: Option<Box<dyn FnMut() -> bool>>,
 431    close_callback: Option<Box<dyn FnOnce()>>,
 432    appearance_changed_callback: Option<Box<dyn FnMut()>>,
 433    input_handler: Option<PlatformInputHandler>,
 434    last_key_equivalent: Option<KeyDownEvent>,
 435    synthetic_drag_counter: usize,
 436    traffic_light_position: Option<Point<Pixels>>,
 437    transparent_titlebar: bool,
 438    previous_modifiers_changed_event: Option<PlatformInput>,
 439    keystroke_for_do_command: Option<Keystroke>,
 440    do_command_handled: Option<bool>,
 441    external_files_dragged: bool,
 442    // Whether the next left-mouse click is also the focusing click.
 443    first_mouse: bool,
 444    fullscreen_restore_bounds: Bounds<Pixels>,
 445    move_tab_to_new_window_callback: Option<Box<dyn FnMut()>>,
 446    merge_all_windows_callback: Option<Box<dyn FnMut()>>,
 447    select_next_tab_callback: Option<Box<dyn FnMut()>>,
 448    select_previous_tab_callback: Option<Box<dyn FnMut()>>,
 449    toggle_tab_bar_callback: Option<Box<dyn FnMut()>>,
 450    activated_least_once: bool,
 451    closed: Arc<AtomicBool>,
 452    // The parent window if this window is a sheet (Dialog kind)
 453    sheet_parent: Option<id>,
 454}
 455
 456impl MacWindowState {
 457    fn move_traffic_light(&self) {
 458        if let Some(traffic_light_position) = self.traffic_light_position {
 459            if self.is_fullscreen() {
 460                // Moving traffic lights while fullscreen doesn't work,
 461                // see https://github.com/zed-industries/zed/issues/4712
 462                return;
 463            }
 464
 465            let titlebar_height = self.titlebar_height();
 466
 467            unsafe {
 468                let close_button: id = msg_send![
 469                    self.native_window,
 470                    standardWindowButton: NSWindowButton::NSWindowCloseButton
 471                ];
 472                let min_button: id = msg_send![
 473                    self.native_window,
 474                    standardWindowButton: NSWindowButton::NSWindowMiniaturizeButton
 475                ];
 476                let zoom_button: id = msg_send![
 477                    self.native_window,
 478                    standardWindowButton: NSWindowButton::NSWindowZoomButton
 479                ];
 480
 481                let mut close_button_frame: CGRect = msg_send![close_button, frame];
 482                let mut min_button_frame: CGRect = msg_send![min_button, frame];
 483                let mut zoom_button_frame: CGRect = msg_send![zoom_button, frame];
 484                let mut origin = point(
 485                    traffic_light_position.x,
 486                    titlebar_height
 487                        - traffic_light_position.y
 488                        - px(close_button_frame.size.height as f32),
 489                );
 490                let button_spacing =
 491                    px((min_button_frame.origin.x - close_button_frame.origin.x) as f32);
 492
 493                close_button_frame.origin = CGPoint::new(origin.x.into(), origin.y.into());
 494                let _: () = msg_send![close_button, setFrame: close_button_frame];
 495                origin.x += button_spacing;
 496
 497                min_button_frame.origin = CGPoint::new(origin.x.into(), origin.y.into());
 498                let _: () = msg_send![min_button, setFrame: min_button_frame];
 499                origin.x += button_spacing;
 500
 501                zoom_button_frame.origin = CGPoint::new(origin.x.into(), origin.y.into());
 502                let _: () = msg_send![zoom_button, setFrame: zoom_button_frame];
 503                origin.x += button_spacing;
 504            }
 505        }
 506    }
 507
 508    fn start_display_link(&mut self) {
 509        self.stop_display_link();
 510        unsafe {
 511            if !self
 512                .native_window
 513                .occlusionState()
 514                .contains(NSWindowOcclusionState::NSWindowOcclusionStateVisible)
 515            {
 516                return;
 517            }
 518        }
 519        let display_id = unsafe { display_id_for_screen(self.native_window.screen()) };
 520        if let Some(mut display_link) =
 521            DisplayLink::new(display_id, self.native_view.as_ptr() as *mut c_void, step).log_err()
 522        {
 523            display_link.start().log_err();
 524            self.display_link = Some(display_link);
 525        }
 526    }
 527
 528    fn stop_display_link(&mut self) {
 529        self.display_link = None;
 530    }
 531
 532    fn is_maximized(&self) -> bool {
 533        fn rect_to_size(rect: NSRect) -> Size<Pixels> {
 534            let NSSize { width, height } = rect.size;
 535            size(width.into(), height.into())
 536        }
 537
 538        unsafe {
 539            let bounds = self.bounds();
 540            let screen_size = rect_to_size(self.native_window.screen().visibleFrame());
 541            bounds.size == screen_size
 542        }
 543    }
 544
 545    fn is_fullscreen(&self) -> bool {
 546        unsafe {
 547            let style_mask = self.native_window.styleMask();
 548            style_mask.contains(NSWindowStyleMask::NSFullScreenWindowMask)
 549        }
 550    }
 551
 552    fn bounds(&self) -> Bounds<Pixels> {
 553        let mut window_frame = unsafe { NSWindow::frame(self.native_window) };
 554        let screen = unsafe { NSWindow::screen(self.native_window) };
 555        if screen == nil {
 556            return Bounds::new(point(px(0.), px(0.)), gpui::DEFAULT_WINDOW_SIZE);
 557        }
 558        let screen_frame = unsafe { NSScreen::frame(screen) };
 559
 560        // Flip the y coordinate to be top-left origin
 561        window_frame.origin.y =
 562            screen_frame.size.height - window_frame.origin.y - window_frame.size.height;
 563
 564        Bounds::new(
 565            point(
 566                px((window_frame.origin.x - screen_frame.origin.x) as f32),
 567                px((window_frame.origin.y + screen_frame.origin.y) as f32),
 568            ),
 569            size(
 570                px(window_frame.size.width as f32),
 571                px(window_frame.size.height as f32),
 572            ),
 573        )
 574    }
 575
 576    fn content_size(&self) -> Size<Pixels> {
 577        let NSSize { width, height, .. } =
 578            unsafe { NSView::frame(self.native_window.contentView()) }.size;
 579        size(px(width as f32), px(height as f32))
 580    }
 581
 582    fn scale_factor(&self) -> f32 {
 583        get_scale_factor(self.native_window)
 584    }
 585
 586    fn titlebar_height(&self) -> Pixels {
 587        unsafe {
 588            let frame = NSWindow::frame(self.native_window);
 589            let content_layout_rect: CGRect = msg_send![self.native_window, contentLayoutRect];
 590            px((frame.size.height - content_layout_rect.size.height) as f32)
 591        }
 592    }
 593
 594    fn window_bounds(&self) -> WindowBounds {
 595        if self.is_fullscreen() {
 596            WindowBounds::Fullscreen(self.fullscreen_restore_bounds)
 597        } else {
 598            WindowBounds::Windowed(self.bounds())
 599        }
 600    }
 601}
 602
 603unsafe impl Send for MacWindowState {}
 604
 605pub(crate) struct MacWindow(Arc<Mutex<MacWindowState>>);
 606
 607impl MacWindow {
 608    pub fn open(
 609        handle: AnyWindowHandle,
 610        WindowParams {
 611            bounds,
 612            titlebar,
 613            kind,
 614            is_movable,
 615            is_resizable,
 616            is_minimizable,
 617            focus,
 618            show,
 619            display_id,
 620            window_min_size,
 621            tabbing_identifier,
 622        }: WindowParams,
 623        foreground_executor: ForegroundExecutor,
 624        background_executor: BackgroundExecutor,
 625        renderer_context: renderer::Context,
 626    ) -> Self {
 627        unsafe {
 628            let pool = NSAutoreleasePool::new(nil);
 629
 630            let allows_automatic_window_tabbing = tabbing_identifier.is_some();
 631            if allows_automatic_window_tabbing {
 632                let () = msg_send![class!(NSWindow), setAllowsAutomaticWindowTabbing: YES];
 633            } else {
 634                let () = msg_send![class!(NSWindow), setAllowsAutomaticWindowTabbing: NO];
 635            }
 636
 637            let mut style_mask;
 638            if let Some(titlebar) = titlebar.as_ref() {
 639                style_mask =
 640                    NSWindowStyleMask::NSClosableWindowMask | NSWindowStyleMask::NSTitledWindowMask;
 641
 642                if is_resizable {
 643                    style_mask |= NSWindowStyleMask::NSResizableWindowMask;
 644                }
 645
 646                if is_minimizable {
 647                    style_mask |= NSWindowStyleMask::NSMiniaturizableWindowMask;
 648                }
 649
 650                if titlebar.appears_transparent {
 651                    style_mask |= NSWindowStyleMask::NSFullSizeContentViewWindowMask;
 652                }
 653            } else {
 654                style_mask = NSWindowStyleMask::NSTitledWindowMask
 655                    | NSWindowStyleMask::NSFullSizeContentViewWindowMask;
 656            }
 657
 658            let native_window: id = match kind {
 659                WindowKind::Normal => {
 660                    msg_send![WINDOW_CLASS, alloc]
 661                }
 662                WindowKind::PopUp => {
 663                    style_mask |= NSWindowStyleMaskNonactivatingPanel;
 664                    msg_send![PANEL_CLASS, alloc]
 665                }
 666                WindowKind::Floating | WindowKind::Dialog => {
 667                    msg_send![PANEL_CLASS, alloc]
 668                }
 669            };
 670
 671            let display = display_id
 672                .and_then(MacDisplay::find_by_id)
 673                .unwrap_or_else(MacDisplay::primary);
 674
 675            let mut target_screen = nil;
 676            let mut screen_frame = None;
 677
 678            let screens = NSScreen::screens(nil);
 679            let count: u64 = cocoa::foundation::NSArray::count(screens);
 680            for i in 0..count {
 681                let screen = cocoa::foundation::NSArray::objectAtIndex(screens, i);
 682                let frame = NSScreen::frame(screen);
 683                let display_id = display_id_for_screen(screen);
 684                if display_id == display.0 {
 685                    screen_frame = Some(frame);
 686                    target_screen = screen;
 687                }
 688            }
 689
 690            let screen_frame = screen_frame.unwrap_or_else(|| {
 691                let screen = NSScreen::mainScreen(nil);
 692                target_screen = screen;
 693                NSScreen::frame(screen)
 694            });
 695
 696            let window_rect = NSRect::new(
 697                NSPoint::new(
 698                    screen_frame.origin.x + bounds.origin.x.as_f32() as f64,
 699                    screen_frame.origin.y
 700                        + (display.bounds().size.height - bounds.origin.y).as_f32() as f64,
 701                ),
 702                NSSize::new(
 703                    bounds.size.width.as_f32() as f64,
 704                    bounds.size.height.as_f32() as f64,
 705                ),
 706            );
 707
 708            let native_window = native_window.initWithContentRect_styleMask_backing_defer_screen_(
 709                window_rect,
 710                style_mask,
 711                NSBackingStoreBuffered,
 712                NO,
 713                target_screen,
 714            );
 715            assert!(!native_window.is_null());
 716            let () = msg_send![
 717                native_window,
 718                registerForDraggedTypes:
 719                    NSArray::arrayWithObject(nil, NSFilenamesPboardType)
 720            ];
 721            let () = msg_send![
 722                native_window,
 723                setReleasedWhenClosed: NO
 724            ];
 725
 726            let content_view = native_window.contentView();
 727            let native_view: id = msg_send![VIEW_CLASS, alloc];
 728            let native_view = NSView::initWithFrame_(native_view, NSView::bounds(content_view));
 729            assert!(!native_view.is_null());
 730
 731            let mut window = Self(Arc::new(Mutex::new(MacWindowState {
 732                handle,
 733                foreground_executor,
 734                background_executor,
 735                native_window,
 736                native_view: NonNull::new_unchecked(native_view),
 737                blurred_view: None,
 738                background_appearance: WindowBackgroundAppearance::Opaque,
 739                display_link: None,
 740                renderer: renderer::new_renderer(
 741                    renderer_context,
 742                    native_window as *mut _,
 743                    native_view as *mut _,
 744                    bounds.size.map(|pixels| pixels.as_f32()),
 745                    false,
 746                ),
 747                request_frame_callback: None,
 748                event_callback: None,
 749                activate_callback: None,
 750                resize_callback: None,
 751                moved_callback: None,
 752                should_close_callback: None,
 753                close_callback: None,
 754                appearance_changed_callback: None,
 755                input_handler: None,
 756                last_key_equivalent: None,
 757                synthetic_drag_counter: 0,
 758                traffic_light_position: titlebar
 759                    .as_ref()
 760                    .and_then(|titlebar| titlebar.traffic_light_position),
 761                transparent_titlebar: titlebar
 762                    .as_ref()
 763                    .is_none_or(|titlebar| titlebar.appears_transparent),
 764                previous_modifiers_changed_event: None,
 765                keystroke_for_do_command: None,
 766                do_command_handled: None,
 767                external_files_dragged: false,
 768                first_mouse: false,
 769                fullscreen_restore_bounds: Bounds::default(),
 770                move_tab_to_new_window_callback: None,
 771                merge_all_windows_callback: None,
 772                select_next_tab_callback: None,
 773                select_previous_tab_callback: None,
 774                toggle_tab_bar_callback: None,
 775                activated_least_once: false,
 776                closed: Arc::new(AtomicBool::new(false)),
 777                sheet_parent: None,
 778            })));
 779
 780            (*native_window).set_ivar(
 781                WINDOW_STATE_IVAR,
 782                Arc::into_raw(window.0.clone()) as *const c_void,
 783            );
 784            native_window.setDelegate_(native_window);
 785            (*native_view).set_ivar(
 786                WINDOW_STATE_IVAR,
 787                Arc::into_raw(window.0.clone()) as *const c_void,
 788            );
 789
 790            if let Some(title) = titlebar
 791                .as_ref()
 792                .and_then(|t| t.title.as_ref().map(AsRef::as_ref))
 793            {
 794                window.set_title(title);
 795            }
 796
 797            native_window.setMovable_(is_movable as BOOL);
 798
 799            if let Some(window_min_size) = window_min_size {
 800                native_window.setContentMinSize_(NSSize {
 801                    width: window_min_size.width.to_f64(),
 802                    height: window_min_size.height.to_f64(),
 803                });
 804            }
 805
 806            if titlebar.is_none_or(|titlebar| titlebar.appears_transparent) {
 807                native_window.setTitlebarAppearsTransparent_(YES);
 808                native_window.setTitleVisibility_(NSWindowTitleVisibility::NSWindowTitleHidden);
 809            }
 810
 811            native_view.setAutoresizingMask_(NSViewWidthSizable | NSViewHeightSizable);
 812            native_view.setWantsBestResolutionOpenGLSurface_(YES);
 813
 814            // From winit crate: On Mojave, views automatically become layer-backed shortly after
 815            // being added to a native_window. Changing the layer-backedness of a view breaks the
 816            // association between the view and its associated OpenGL context. To work around this,
 817            // on we explicitly make the view layer-backed up front so that AppKit doesn't do it
 818            // itself and break the association with its context.
 819            native_view.setWantsLayer(YES);
 820            let _: () = msg_send![
 821            native_view,
 822            setLayerContentsRedrawPolicy: NSViewLayerContentsRedrawDuringViewResize
 823            ];
 824
 825            content_view.addSubview_(native_view.autorelease());
 826            native_window.makeFirstResponder_(native_view);
 827
 828            let app: id = NSApplication::sharedApplication(nil);
 829            let main_window: id = msg_send![app, mainWindow];
 830            let mut sheet_parent = None;
 831
 832            match kind {
 833                WindowKind::Normal | WindowKind::Floating => {
 834                    if kind == WindowKind::Floating {
 835                        // Let the window float keep above normal windows.
 836                        native_window.setLevel_(NSFloatingWindowLevel);
 837                    } else {
 838                        native_window.setLevel_(NSNormalWindowLevel);
 839                    }
 840                    native_window.setAcceptsMouseMovedEvents_(YES);
 841
 842                    if let Some(tabbing_identifier) = tabbing_identifier {
 843                        let tabbing_id = ns_string(tabbing_identifier.as_str());
 844                        let _: () = msg_send![native_window, setTabbingIdentifier: tabbing_id];
 845                    } else {
 846                        let _: () = msg_send![native_window, setTabbingIdentifier:nil];
 847                    }
 848                }
 849                WindowKind::PopUp => {
 850                    // Use a tracking area to allow receiving MouseMoved events even when
 851                    // the window or application aren't active, which is often the case
 852                    // e.g. for notification windows.
 853                    let tracking_area: id = msg_send![class!(NSTrackingArea), alloc];
 854                    let _: () = msg_send![
 855                        tracking_area,
 856                        initWithRect: NSRect::new(NSPoint::new(0., 0.), NSSize::new(0., 0.))
 857                        options: NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveAlways | NSTrackingInVisibleRect
 858                        owner: native_view
 859                        userInfo: nil
 860                    ];
 861                    let _: () =
 862                        msg_send![native_view, addTrackingArea: tracking_area.autorelease()];
 863
 864                    native_window.setLevel_(NSPopUpWindowLevel);
 865                    let _: () = msg_send![
 866                        native_window,
 867                        setAnimationBehavior: NSWindowAnimationBehaviorUtilityWindow
 868                    ];
 869                    native_window.setCollectionBehavior_(
 870                        NSWindowCollectionBehavior::NSWindowCollectionBehaviorCanJoinAllSpaces |
 871                        NSWindowCollectionBehavior::NSWindowCollectionBehaviorFullScreenAuxiliary
 872                    );
 873                }
 874                WindowKind::Dialog => {
 875                    if !main_window.is_null() {
 876                        let parent = {
 877                            let active_sheet: id = msg_send![main_window, attachedSheet];
 878                            if active_sheet.is_null() {
 879                                main_window
 880                            } else {
 881                                active_sheet
 882                            }
 883                        };
 884                        let _: () =
 885                            msg_send![parent, beginSheet: native_window completionHandler: nil];
 886                        sheet_parent = Some(parent);
 887                    }
 888                }
 889            }
 890
 891            if allows_automatic_window_tabbing
 892                && !main_window.is_null()
 893                && main_window != native_window
 894            {
 895                let main_window_is_fullscreen = main_window
 896                    .styleMask()
 897                    .contains(NSWindowStyleMask::NSFullScreenWindowMask);
 898                let user_tabbing_preference = Self::get_user_tabbing_preference()
 899                    .unwrap_or(UserTabbingPreference::InFullScreen);
 900                let should_add_as_tab = user_tabbing_preference == UserTabbingPreference::Always
 901                    || user_tabbing_preference == UserTabbingPreference::InFullScreen
 902                        && main_window_is_fullscreen;
 903
 904                if should_add_as_tab {
 905                    let main_window_can_tab: BOOL =
 906                        msg_send![main_window, respondsToSelector: sel!(addTabbedWindow:ordered:)];
 907                    let main_window_visible: BOOL = msg_send![main_window, isVisible];
 908
 909                    if main_window_can_tab == YES && main_window_visible == YES {
 910                        let _: () = msg_send![main_window, addTabbedWindow: native_window ordered: NSWindowOrderingMode::NSWindowAbove];
 911
 912                        // Ensure the window is visible immediately after adding the tab, since the tab bar is updated with a new entry at this point.
 913                        // Note: Calling orderFront here can break fullscreen mode (makes fullscreen windows exit fullscreen), so only do this if the main window is not fullscreen.
 914                        if !main_window_is_fullscreen {
 915                            let _: () = msg_send![native_window, orderFront: nil];
 916                        }
 917                    }
 918                }
 919            }
 920
 921            if focus && show {
 922                native_window.makeKeyAndOrderFront_(nil);
 923            } else if show {
 924                native_window.orderFront_(nil);
 925            }
 926
 927            // Set the initial position of the window to the specified origin.
 928            // Although we already specified the position using `initWithContentRect_styleMask_backing_defer_screen_`,
 929            // the window position might be incorrect if the main screen (the screen that contains the window that has focus)
 930            //  is different from the primary screen.
 931            NSWindow::setFrameTopLeftPoint_(native_window, window_rect.origin);
 932            {
 933                let mut window_state = window.0.lock();
 934                window_state.move_traffic_light();
 935                window_state.sheet_parent = sheet_parent;
 936            }
 937
 938            pool.drain();
 939
 940            window
 941        }
 942    }
 943
 944    pub fn active_window() -> Option<AnyWindowHandle> {
 945        unsafe {
 946            let app = NSApplication::sharedApplication(nil);
 947            let main_window: id = msg_send![app, mainWindow];
 948            if main_window.is_null() {
 949                return None;
 950            }
 951
 952            if msg_send![main_window, isKindOfClass: WINDOW_CLASS] {
 953                let handle = get_window_state(&*main_window).lock().handle;
 954                Some(handle)
 955            } else {
 956                None
 957            }
 958        }
 959    }
 960
 961    pub fn ordered_windows() -> Vec<AnyWindowHandle> {
 962        unsafe {
 963            let app = NSApplication::sharedApplication(nil);
 964            let windows: id = msg_send![app, orderedWindows];
 965            let count: NSUInteger = msg_send![windows, count];
 966
 967            let mut window_handles = Vec::new();
 968            for i in 0..count {
 969                let window: id = msg_send![windows, objectAtIndex:i];
 970                if msg_send![window, isKindOfClass: WINDOW_CLASS] {
 971                    let handle = get_window_state(&*window).lock().handle;
 972                    window_handles.push(handle);
 973                }
 974            }
 975
 976            window_handles
 977        }
 978    }
 979
 980    pub fn get_user_tabbing_preference() -> Option<UserTabbingPreference> {
 981        unsafe {
 982            let defaults: id = NSUserDefaults::standardUserDefaults();
 983            let domain = ns_string("NSGlobalDomain");
 984            let key = ns_string("AppleWindowTabbingMode");
 985
 986            let dict: id = msg_send![defaults, persistentDomainForName: domain];
 987            let value: id = if !dict.is_null() {
 988                msg_send![dict, objectForKey: key]
 989            } else {
 990                nil
 991            };
 992
 993            let value_str = if !value.is_null() {
 994                CStr::from_ptr(NSString::UTF8String(value)).to_string_lossy()
 995            } else {
 996                "".into()
 997            };
 998
 999            match value_str.as_ref() {
1000                "manual" => Some(UserTabbingPreference::Never),
1001                "always" => Some(UserTabbingPreference::Always),
1002                _ => Some(UserTabbingPreference::InFullScreen),
1003            }
1004        }
1005    }
1006}
1007
1008impl Drop for MacWindow {
1009    fn drop(&mut self) {
1010        let mut this = self.0.lock();
1011        this.renderer.destroy();
1012        let window = this.native_window;
1013        let sheet_parent = this.sheet_parent.take();
1014        this.display_link.take();
1015        unsafe {
1016            this.native_window.setDelegate_(nil);
1017        }
1018        this.input_handler.take();
1019        this.foreground_executor
1020            .spawn(async move {
1021                unsafe {
1022                    if let Some(parent) = sheet_parent {
1023                        let _: () = msg_send![parent, endSheet: window];
1024                    }
1025                    window.close();
1026                    window.autorelease();
1027                }
1028            })
1029            .detach();
1030    }
1031}
1032
1033/// Calls `f` if the window is not closed.
1034///
1035/// This should be used when spawning foreground tasks interacting with the
1036/// window, as some messages will end hard faulting if dispatched to no longer
1037/// valid window handles.
1038fn if_window_not_closed(closed: Arc<AtomicBool>, f: impl FnOnce()) {
1039    if !closed.load(Ordering::Acquire) {
1040        f();
1041    }
1042}
1043
1044impl PlatformWindow for MacWindow {
1045    fn bounds(&self) -> Bounds<Pixels> {
1046        self.0.as_ref().lock().bounds()
1047    }
1048
1049    fn window_bounds(&self) -> WindowBounds {
1050        self.0.as_ref().lock().window_bounds()
1051    }
1052
1053    fn is_maximized(&self) -> bool {
1054        self.0.as_ref().lock().is_maximized()
1055    }
1056
1057    fn content_size(&self) -> Size<Pixels> {
1058        self.0.as_ref().lock().content_size()
1059    }
1060
1061    fn resize(&mut self, size: Size<Pixels>) {
1062        let this = self.0.lock();
1063        let window = this.native_window;
1064        let closed = this.closed.clone();
1065        this.foreground_executor
1066            .spawn(async move {
1067                if_window_not_closed(closed, || unsafe {
1068                    window.setContentSize_(NSSize {
1069                        width: size.width.as_f32() as f64,
1070                        height: size.height.as_f32() as f64,
1071                    });
1072                })
1073            })
1074            .detach();
1075    }
1076
1077    fn merge_all_windows(&self) {
1078        let native_window = self.0.lock().native_window;
1079        extern "C" fn merge_windows_async(context: *mut std::ffi::c_void) {
1080            unsafe {
1081                let native_window = context as id;
1082                let _: () = msg_send![native_window, mergeAllWindows:nil];
1083            }
1084        }
1085
1086        unsafe {
1087            DispatchQueue::main()
1088                .exec_async_f(native_window as *mut std::ffi::c_void, merge_windows_async);
1089        }
1090    }
1091
1092    fn move_tab_to_new_window(&self) {
1093        let native_window = self.0.lock().native_window;
1094        extern "C" fn move_tab_async(context: *mut std::ffi::c_void) {
1095            unsafe {
1096                let native_window = context as id;
1097                let _: () = msg_send![native_window, moveTabToNewWindow:nil];
1098                let _: () = msg_send![native_window, makeKeyAndOrderFront: nil];
1099            }
1100        }
1101
1102        unsafe {
1103            DispatchQueue::main()
1104                .exec_async_f(native_window as *mut std::ffi::c_void, move_tab_async);
1105        }
1106    }
1107
1108    fn toggle_window_tab_overview(&self) {
1109        let native_window = self.0.lock().native_window;
1110        unsafe {
1111            let _: () = msg_send![native_window, toggleTabOverview:nil];
1112        }
1113    }
1114
1115    fn set_tabbing_identifier(&self, tabbing_identifier: Option<String>) {
1116        let native_window = self.0.lock().native_window;
1117        unsafe {
1118            let allows_automatic_window_tabbing = tabbing_identifier.is_some();
1119            if allows_automatic_window_tabbing {
1120                let () = msg_send![class!(NSWindow), setAllowsAutomaticWindowTabbing: YES];
1121            } else {
1122                let () = msg_send![class!(NSWindow), setAllowsAutomaticWindowTabbing: NO];
1123            }
1124
1125            if let Some(tabbing_identifier) = tabbing_identifier {
1126                let tabbing_id = ns_string(tabbing_identifier.as_str());
1127                let _: () = msg_send![native_window, setTabbingIdentifier: tabbing_id];
1128            } else {
1129                let _: () = msg_send![native_window, setTabbingIdentifier:nil];
1130            }
1131        }
1132    }
1133
1134    fn scale_factor(&self) -> f32 {
1135        self.0.as_ref().lock().scale_factor()
1136    }
1137
1138    fn appearance(&self) -> WindowAppearance {
1139        unsafe {
1140            let appearance: id = msg_send![self.0.lock().native_window, effectiveAppearance];
1141            crate::window_appearance::window_appearance_from_native(appearance)
1142        }
1143    }
1144
1145    fn display(&self) -> Option<Rc<dyn PlatformDisplay>> {
1146        unsafe {
1147            let screen = self.0.lock().native_window.screen();
1148            if screen.is_null() {
1149                return None;
1150            }
1151            let device_description: id = msg_send![screen, deviceDescription];
1152            let screen_number: id =
1153                NSDictionary::valueForKey_(device_description, ns_string("NSScreenNumber"));
1154
1155            let screen_number: u32 = msg_send![screen_number, unsignedIntValue];
1156
1157            Some(Rc::new(MacDisplay(screen_number)))
1158        }
1159    }
1160
1161    fn mouse_position(&self) -> Point<Pixels> {
1162        let position = unsafe {
1163            self.0
1164                .lock()
1165                .native_window
1166                .mouseLocationOutsideOfEventStream()
1167        };
1168        convert_mouse_position(position, self.content_size().height)
1169    }
1170
1171    fn modifiers(&self) -> Modifiers {
1172        unsafe {
1173            let modifiers: NSEventModifierFlags = msg_send![class!(NSEvent), modifierFlags];
1174
1175            let control = modifiers.contains(NSEventModifierFlags::NSControlKeyMask);
1176            let alt = modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask);
1177            let shift = modifiers.contains(NSEventModifierFlags::NSShiftKeyMask);
1178            let command = modifiers.contains(NSEventModifierFlags::NSCommandKeyMask);
1179            let function = modifiers.contains(NSEventModifierFlags::NSFunctionKeyMask);
1180
1181            Modifiers {
1182                control,
1183                alt,
1184                shift,
1185                platform: command,
1186                function,
1187            }
1188        }
1189    }
1190
1191    fn capslock(&self) -> Capslock {
1192        unsafe {
1193            let modifiers: NSEventModifierFlags = msg_send![class!(NSEvent), modifierFlags];
1194
1195            Capslock {
1196                on: modifiers.contains(NSEventModifierFlags::NSAlphaShiftKeyMask),
1197            }
1198        }
1199    }
1200
1201    fn set_input_handler(&mut self, input_handler: PlatformInputHandler) {
1202        self.0.as_ref().lock().input_handler = Some(input_handler);
1203    }
1204
1205    fn take_input_handler(&mut self) -> Option<PlatformInputHandler> {
1206        self.0.as_ref().lock().input_handler.take()
1207    }
1208
1209    fn prompt(
1210        &self,
1211        level: PromptLevel,
1212        msg: &str,
1213        detail: Option<&str>,
1214        answers: &[PromptButton],
1215    ) -> Option<oneshot::Receiver<usize>> {
1216        // macOs applies overrides to modal window buttons after they are added.
1217        // Two most important for this logic are:
1218        // * Buttons with "Cancel" title will be displayed as the last buttons in the modal
1219        // * Last button added to the modal via `addButtonWithTitle` stays focused
1220        // * Focused buttons react on "space"/" " keypresses
1221        // * Usage of `keyEquivalent`, `makeFirstResponder` or `setInitialFirstResponder` does not change the focus
1222        //
1223        // See also https://developer.apple.com/documentation/appkit/nsalert/1524532-addbuttonwithtitle#discussion
1224        // ```
1225        // By default, the first button has a key equivalent of Return,
1226        // any button with a title of “Cancel” has a key equivalent of Escape,
1227        // and any button with the title “Don’t Save” has a key equivalent of Command-D (but only if it’s not the first button).
1228        // ```
1229        //
1230        // To avoid situations when the last element added is "Cancel" and it gets the focus
1231        // (hence stealing both ESC and Space shortcuts), we find and add one non-Cancel button
1232        // last, so it gets focus and a Space shortcut.
1233        // This way, "Save this file? Yes/No/Cancel"-ish modals will get all three buttons mapped with a key.
1234        let latest_non_cancel_label = answers
1235            .iter()
1236            .enumerate()
1237            .rev()
1238            .find(|(_, label)| !label.is_cancel())
1239            .filter(|&(label_index, _)| label_index > 0);
1240
1241        unsafe {
1242            let alert: id = msg_send![class!(NSAlert), alloc];
1243            let alert: id = msg_send![alert, init];
1244            let alert_style = match level {
1245                PromptLevel::Info => 1,
1246                PromptLevel::Warning => 0,
1247                PromptLevel::Critical => 2,
1248            };
1249            let _: () = msg_send![alert, setAlertStyle: alert_style];
1250            let _: () = msg_send![alert, setMessageText: ns_string(msg)];
1251            if let Some(detail) = detail {
1252                let _: () = msg_send![alert, setInformativeText: ns_string(detail)];
1253            }
1254
1255            for (ix, answer) in answers
1256                .iter()
1257                .enumerate()
1258                .filter(|&(ix, _)| Some(ix) != latest_non_cancel_label.map(|(ix, _)| ix))
1259            {
1260                let button: id = msg_send![alert, addButtonWithTitle: ns_string(answer.label())];
1261                let _: () = msg_send![button, setTag: ix as NSInteger];
1262
1263                if answer.is_cancel() {
1264                    // Bind Escape Key to Cancel Button
1265                    if let Some(key) = std::char::from_u32(crate::events::ESCAPE_KEY as u32) {
1266                        let _: () =
1267                            msg_send![button, setKeyEquivalent: ns_string(&key.to_string())];
1268                    }
1269                }
1270            }
1271            if let Some((ix, answer)) = latest_non_cancel_label {
1272                let button: id = msg_send![alert, addButtonWithTitle: ns_string(answer.label())];
1273                let _: () = msg_send![button, setTag: ix as NSInteger];
1274            }
1275
1276            let (done_tx, done_rx) = oneshot::channel();
1277            let done_tx = Cell::new(Some(done_tx));
1278            let block = ConcreteBlock::new(move |answer: NSInteger| {
1279                let _: () = msg_send![alert, release];
1280                if let Some(done_tx) = done_tx.take() {
1281                    let _ = done_tx.send(answer.try_into().unwrap());
1282                }
1283            });
1284            let block = block.copy();
1285            let lock = self.0.lock();
1286            let native_window = lock.native_window;
1287            let closed = lock.closed.clone();
1288            let executor = lock.foreground_executor.clone();
1289            executor
1290                .spawn(async move {
1291                    if !closed.load(Ordering::Acquire) {
1292                        let _: () = msg_send![
1293                            alert,
1294                            beginSheetModalForWindow: native_window
1295                            completionHandler: block
1296                        ];
1297                    } else {
1298                        let _: () = msg_send![alert, release];
1299                    }
1300                })
1301                .detach();
1302
1303            Some(done_rx)
1304        }
1305    }
1306
1307    fn activate(&self) {
1308        let lock = self.0.lock();
1309        let window = lock.native_window;
1310        let closed = lock.closed.clone();
1311        let executor = lock.foreground_executor.clone();
1312        executor
1313            .spawn(async move {
1314                if !closed.load(Ordering::Acquire) {
1315                    unsafe {
1316                        let _: () = msg_send![window, makeKeyAndOrderFront: nil];
1317                    }
1318                }
1319            })
1320            .detach();
1321    }
1322
1323    fn is_active(&self) -> bool {
1324        unsafe { self.0.lock().native_window.isKeyWindow() == YES }
1325    }
1326
1327    // is_hovered is unused on macOS. See Window::is_window_hovered.
1328    fn is_hovered(&self) -> bool {
1329        false
1330    }
1331
1332    fn set_title(&mut self, title: &str) {
1333        unsafe {
1334            let app = NSApplication::sharedApplication(nil);
1335            let window = self.0.lock().native_window;
1336            let title = ns_string(title);
1337            let _: () = msg_send![app, changeWindowsItem:window title:title filename:false];
1338            let _: () = msg_send![window, setTitle: title];
1339            self.0.lock().move_traffic_light();
1340        }
1341    }
1342
1343    fn get_title(&self) -> String {
1344        unsafe {
1345            let title: id = msg_send![self.0.lock().native_window, title];
1346            if title.is_null() {
1347                "".to_string()
1348            } else {
1349                title.to_str().to_string()
1350            }
1351        }
1352    }
1353
1354    fn set_app_id(&mut self, _app_id: &str) {}
1355
1356    fn set_background_appearance(&self, background_appearance: WindowBackgroundAppearance) {
1357        let mut this = self.0.as_ref().lock();
1358        this.background_appearance = background_appearance;
1359
1360        let opaque = background_appearance == WindowBackgroundAppearance::Opaque;
1361        this.renderer.update_transparency(!opaque);
1362
1363        unsafe {
1364            this.native_window.setOpaque_(opaque as BOOL);
1365            let background_color = if opaque {
1366                NSColor::colorWithSRGBRed_green_blue_alpha_(nil, 0f64, 0f64, 0f64, 1f64)
1367            } else {
1368                // Not using `+[NSColor clearColor]` to avoid broken shadow.
1369                NSColor::colorWithSRGBRed_green_blue_alpha_(nil, 0f64, 0f64, 0f64, 0.0001)
1370            };
1371            this.native_window.setBackgroundColor_(background_color);
1372
1373            if NSAppKitVersionNumber < NSAppKitVersionNumber12_0 {
1374                // Whether `-[NSVisualEffectView respondsToSelector:@selector(_updateProxyLayer)]`.
1375                // On macOS Catalina/Big Sur `NSVisualEffectView` doesn’t own concrete sublayers
1376                // but uses a `CAProxyLayer`. Use the legacy WindowServer API.
1377                let blur_radius = if background_appearance == WindowBackgroundAppearance::Blurred {
1378                    80
1379                } else {
1380                    0
1381                };
1382
1383                let window_number = this.native_window.windowNumber();
1384                CGSSetWindowBackgroundBlurRadius(CGSMainConnectionID(), window_number, blur_radius);
1385            } else {
1386                // On newer macOS `NSVisualEffectView` manages the effect layer directly. Using it
1387                // could have a better performance (it downsamples the backdrop) and more control
1388                // over the effect layer.
1389                if background_appearance != WindowBackgroundAppearance::Blurred {
1390                    if let Some(blur_view) = this.blurred_view {
1391                        NSView::removeFromSuperview(blur_view);
1392                        this.blurred_view = None;
1393                    }
1394                } else if this.blurred_view.is_none() {
1395                    let content_view = this.native_window.contentView();
1396                    let frame = NSView::bounds(content_view);
1397                    let mut blur_view: id = msg_send![BLURRED_VIEW_CLASS, alloc];
1398                    blur_view = NSView::initWithFrame_(blur_view, frame);
1399                    blur_view.setAutoresizingMask_(NSViewWidthSizable | NSViewHeightSizable);
1400
1401                    let _: () = msg_send![
1402                        content_view,
1403                        addSubview: blur_view
1404                        positioned: NSWindowOrderingMode::NSWindowBelow
1405                        relativeTo: nil
1406                    ];
1407                    this.blurred_view = Some(blur_view.autorelease());
1408                }
1409            }
1410        }
1411    }
1412
1413    fn background_appearance(&self) -> WindowBackgroundAppearance {
1414        self.0.as_ref().lock().background_appearance
1415    }
1416
1417    fn is_subpixel_rendering_supported(&self) -> bool {
1418        false
1419    }
1420
1421    fn set_edited(&mut self, edited: bool) {
1422        unsafe {
1423            let window = self.0.lock().native_window;
1424            msg_send![window, setDocumentEdited: edited as BOOL]
1425        }
1426
1427        // Changing the document edited state resets the traffic light position,
1428        // so we have to move it again.
1429        self.0.lock().move_traffic_light();
1430    }
1431
1432    fn show_character_palette(&self) {
1433        let this = self.0.lock();
1434        let window = this.native_window;
1435        this.foreground_executor
1436            .spawn(async move {
1437                unsafe {
1438                    let app = NSApplication::sharedApplication(nil);
1439                    let _: () = msg_send![app, orderFrontCharacterPalette: window];
1440                }
1441            })
1442            .detach();
1443    }
1444
1445    fn minimize(&self) {
1446        let window = self.0.lock().native_window;
1447        unsafe {
1448            window.miniaturize_(nil);
1449        }
1450    }
1451
1452    fn zoom(&self) {
1453        let this = self.0.lock();
1454        let window = this.native_window;
1455        let closed = this.closed.clone();
1456        this.foreground_executor
1457            .spawn(async move {
1458                if_window_not_closed(closed, || unsafe {
1459                    window.zoom_(nil);
1460                })
1461            })
1462            .detach();
1463    }
1464
1465    fn toggle_fullscreen(&self) {
1466        let this = self.0.lock();
1467        let window = this.native_window;
1468        let closed = this.closed.clone();
1469        this.foreground_executor
1470            .spawn(async move {
1471                if_window_not_closed(closed, || unsafe {
1472                    window.toggleFullScreen_(nil);
1473                })
1474            })
1475            .detach();
1476    }
1477
1478    fn is_fullscreen(&self) -> bool {
1479        let this = self.0.lock();
1480        let window = this.native_window;
1481
1482        unsafe {
1483            window
1484                .styleMask()
1485                .contains(NSWindowStyleMask::NSFullScreenWindowMask)
1486        }
1487    }
1488
1489    fn on_request_frame(&self, callback: Box<dyn FnMut(RequestFrameOptions)>) {
1490        self.0.as_ref().lock().request_frame_callback = Some(callback);
1491    }
1492
1493    fn on_input(&self, callback: Box<dyn FnMut(PlatformInput) -> gpui::DispatchEventResult>) {
1494        self.0.as_ref().lock().event_callback = Some(callback);
1495    }
1496
1497    fn on_active_status_change(&self, callback: Box<dyn FnMut(bool)>) {
1498        self.0.as_ref().lock().activate_callback = Some(callback);
1499    }
1500
1501    fn on_hover_status_change(&self, _: Box<dyn FnMut(bool)>) {}
1502
1503    fn on_resize(&self, callback: Box<dyn FnMut(Size<Pixels>, f32)>) {
1504        self.0.as_ref().lock().resize_callback = Some(callback);
1505    }
1506
1507    fn on_moved(&self, callback: Box<dyn FnMut()>) {
1508        self.0.as_ref().lock().moved_callback = Some(callback);
1509    }
1510
1511    fn on_should_close(&self, callback: Box<dyn FnMut() -> bool>) {
1512        self.0.as_ref().lock().should_close_callback = Some(callback);
1513    }
1514
1515    fn on_close(&self, callback: Box<dyn FnOnce()>) {
1516        self.0.as_ref().lock().close_callback = Some(callback);
1517    }
1518
1519    fn on_hit_test_window_control(&self, _callback: Box<dyn FnMut() -> Option<WindowControlArea>>) {
1520    }
1521
1522    fn on_appearance_changed(&self, callback: Box<dyn FnMut()>) {
1523        self.0.lock().appearance_changed_callback = Some(callback);
1524    }
1525
1526    fn tabbed_windows(&self) -> Option<Vec<SystemWindowTab>> {
1527        unsafe {
1528            let windows: id = msg_send![self.0.lock().native_window, tabbedWindows];
1529            if windows.is_null() {
1530                return None;
1531            }
1532
1533            let count: NSUInteger = msg_send![windows, count];
1534            let mut result = Vec::new();
1535            for i in 0..count {
1536                let window: id = msg_send![windows, objectAtIndex:i];
1537                if msg_send![window, isKindOfClass: WINDOW_CLASS] {
1538                    let handle = get_window_state(&*window).lock().handle;
1539                    let title: id = msg_send![window, title];
1540                    let title = SharedString::from(title.to_str().to_string());
1541
1542                    result.push(SystemWindowTab::new(title, handle));
1543                }
1544            }
1545
1546            Some(result)
1547        }
1548    }
1549
1550    fn tab_bar_visible(&self) -> bool {
1551        unsafe {
1552            let tab_group: id = msg_send![self.0.lock().native_window, tabGroup];
1553            if tab_group.is_null() {
1554                false
1555            } else {
1556                let tab_bar_visible: BOOL = msg_send![tab_group, isTabBarVisible];
1557                tab_bar_visible == YES
1558            }
1559        }
1560    }
1561
1562    fn on_move_tab_to_new_window(&self, callback: Box<dyn FnMut()>) {
1563        self.0.as_ref().lock().move_tab_to_new_window_callback = Some(callback);
1564    }
1565
1566    fn on_merge_all_windows(&self, callback: Box<dyn FnMut()>) {
1567        self.0.as_ref().lock().merge_all_windows_callback = Some(callback);
1568    }
1569
1570    fn on_select_next_tab(&self, callback: Box<dyn FnMut()>) {
1571        self.0.as_ref().lock().select_next_tab_callback = Some(callback);
1572    }
1573
1574    fn on_select_previous_tab(&self, callback: Box<dyn FnMut()>) {
1575        self.0.as_ref().lock().select_previous_tab_callback = Some(callback);
1576    }
1577
1578    fn on_toggle_tab_bar(&self, callback: Box<dyn FnMut()>) {
1579        self.0.as_ref().lock().toggle_tab_bar_callback = Some(callback);
1580    }
1581
1582    fn draw(&self, scene: &gpui::Scene) {
1583        let mut this = self.0.lock();
1584        this.renderer.draw(scene);
1585    }
1586
1587    fn sprite_atlas(&self) -> Arc<dyn PlatformAtlas> {
1588        self.0.lock().renderer.sprite_atlas().clone()
1589    }
1590
1591    fn gpu_specs(&self) -> Option<gpui::GpuSpecs> {
1592        None
1593    }
1594
1595    fn update_ime_position(&self, _bounds: Bounds<Pixels>) {
1596        let executor = self.0.lock().foreground_executor.clone();
1597        executor
1598            .spawn(async move {
1599                unsafe {
1600                    let input_context: id =
1601                        msg_send![class!(NSTextInputContext), currentInputContext];
1602                    if input_context.is_null() {
1603                        return;
1604                    }
1605                    let _: () = msg_send![input_context, invalidateCharacterCoordinates];
1606                }
1607            })
1608            .detach()
1609    }
1610
1611    fn titlebar_double_click(&self) {
1612        let this = self.0.lock();
1613        let window = this.native_window;
1614        let closed = this.closed.clone();
1615        this.foreground_executor
1616            .spawn(async move {
1617                if_window_not_closed(closed, || {
1618                    unsafe {
1619                        let defaults: id = NSUserDefaults::standardUserDefaults();
1620                        let domain = ns_string("NSGlobalDomain");
1621                        let key = ns_string("AppleActionOnDoubleClick");
1622
1623                        let dict: id = msg_send![defaults, persistentDomainForName: domain];
1624                        let action: id = if !dict.is_null() {
1625                            msg_send![dict, objectForKey: key]
1626                        } else {
1627                            nil
1628                        };
1629
1630                        let action_str = if !action.is_null() {
1631                            CStr::from_ptr(NSString::UTF8String(action)).to_string_lossy()
1632                        } else {
1633                            "".into()
1634                        };
1635
1636                        match action_str.as_ref() {
1637                            "None" => {
1638                                // "Do Nothing" selected, so do no action
1639                            }
1640                            "Minimize" => {
1641                                window.miniaturize_(nil);
1642                            }
1643                            "Maximize" => {
1644                                window.zoom_(nil);
1645                            }
1646                            "Fill" => {
1647                                // There is no documented API for "Fill" action, so we'll just zoom the window
1648                                window.zoom_(nil);
1649                            }
1650                            _ => {
1651                                window.zoom_(nil);
1652                            }
1653                        }
1654                    }
1655                })
1656            })
1657            .detach();
1658    }
1659
1660    fn start_window_move(&self) {
1661        let this = self.0.lock();
1662        let window = this.native_window;
1663
1664        unsafe {
1665            let app = NSApplication::sharedApplication(nil);
1666            let event: id = msg_send![app, currentEvent];
1667            let _: () = msg_send![window, performWindowDragWithEvent: event];
1668        }
1669    }
1670
1671    #[cfg(any(test, feature = "test-support"))]
1672    fn render_to_image(&self, scene: &gpui::Scene) -> Result<RgbaImage> {
1673        let mut this = self.0.lock();
1674        this.renderer.render_to_image(scene)
1675    }
1676}
1677
1678impl rwh::HasWindowHandle for MacWindow {
1679    fn window_handle(&self) -> Result<rwh::WindowHandle<'_>, rwh::HandleError> {
1680        // SAFETY: The AppKitWindowHandle is a wrapper around a pointer to an NSView
1681        unsafe {
1682            Ok(rwh::WindowHandle::borrow_raw(rwh::RawWindowHandle::AppKit(
1683                rwh::AppKitWindowHandle::new(self.0.lock().native_view.cast()),
1684            )))
1685        }
1686    }
1687}
1688
1689impl rwh::HasDisplayHandle for MacWindow {
1690    fn display_handle(&self) -> Result<rwh::DisplayHandle<'_>, rwh::HandleError> {
1691        // SAFETY: This is a no-op on macOS
1692        unsafe {
1693            Ok(rwh::DisplayHandle::borrow_raw(
1694                rwh::AppKitDisplayHandle::new().into(),
1695            ))
1696        }
1697    }
1698}
1699
1700fn get_scale_factor(native_window: id) -> f32 {
1701    let factor = unsafe {
1702        let screen: id = msg_send![native_window, screen];
1703        if screen.is_null() {
1704            return 2.0;
1705        }
1706        NSScreen::backingScaleFactor(screen) as f32
1707    };
1708
1709    // We are not certain what triggers this, but it seems that sometimes
1710    // this method would return 0 (https://github.com/zed-industries/zed/issues/6412)
1711    // It seems most likely that this would happen if the window has no screen
1712    // (if it is off-screen), though we'd expect to see viewDidChangeBackingProperties before
1713    // it was rendered for real.
1714    // Regardless, attempt to avoid the issue here.
1715    if factor == 0.0 { 2. } else { factor }
1716}
1717
1718unsafe fn get_window_state(object: &Object) -> Arc<Mutex<MacWindowState>> {
1719    unsafe {
1720        let raw: *mut c_void = *object.get_ivar(WINDOW_STATE_IVAR);
1721        let rc1 = Arc::from_raw(raw as *mut Mutex<MacWindowState>);
1722        let rc2 = rc1.clone();
1723        mem::forget(rc1);
1724        rc2
1725    }
1726}
1727
1728unsafe fn drop_window_state(object: &Object) {
1729    unsafe {
1730        let raw: *mut c_void = *object.get_ivar(WINDOW_STATE_IVAR);
1731        Arc::from_raw(raw as *mut Mutex<MacWindowState>);
1732    }
1733}
1734
1735extern "C" fn yes(_: &Object, _: Sel) -> BOOL {
1736    YES
1737}
1738
1739extern "C" fn dealloc_window(this: &Object, _: Sel) {
1740    unsafe {
1741        drop_window_state(this);
1742        let _: () = msg_send![super(this, class!(NSWindow)), dealloc];
1743    }
1744}
1745
1746extern "C" fn dealloc_view(this: &Object, _: Sel) {
1747    unsafe {
1748        drop_window_state(this);
1749        let _: () = msg_send![super(this, class!(NSView)), dealloc];
1750    }
1751}
1752
1753extern "C" fn handle_key_equivalent(this: &Object, _: Sel, native_event: id) -> BOOL {
1754    handle_key_event(this, native_event, true)
1755}
1756
1757extern "C" fn handle_key_down(this: &Object, _: Sel, native_event: id) {
1758    handle_key_event(this, native_event, false);
1759}
1760
1761extern "C" fn handle_key_up(this: &Object, _: Sel, native_event: id) {
1762    handle_key_event(this, native_event, false);
1763}
1764
1765// Things to test if you're modifying this method:
1766//  U.S. layout:
1767//   - The IME consumes characters like 'j' and 'k', which makes paging through `less` in
1768//     the terminal behave incorrectly by default. This behavior should be patched by our
1769//     IME integration
1770//   - `alt-t` should open the tasks menu
1771//   - In vim mode, this keybinding should work:
1772//     ```
1773//        {
1774//          "context": "Editor && vim_mode == insert",
1775//          "bindings": {"j j": "vim::NormalBefore"}
1776//        }
1777//     ```
1778//     and typing 'j k' in insert mode with this keybinding should insert the two characters
1779//  Brazilian layout:
1780//   - `" space` should create an unmarked quote
1781//   - `" backspace` should delete the marked quote
1782//   - `" "`should create an unmarked quote and a second marked quote
1783//   - `" up` should insert a quote, unmark it, and move up one line
1784//   - `" cmd-down` should insert a quote, unmark it, and move to the end of the file
1785//   - `cmd-ctrl-space` and clicking on an emoji should type it
1786//  Czech (QWERTY) layout:
1787//   - in vim mode `option-4`  should go to end of line (same as $)
1788//  Japanese (Romaji) layout:
1789//   - type `a i left down up enter enter` should create an unmarked text "愛"
1790//   - In vim mode with `jj` bound to `vim::NormalBefore` in insert mode, typing 'j i' with
1791//     Japanese IME should produce "じ" (ji), not "jい"
1792
1793/// Returns true if the current keyboard input source is a composition-based IME
1794/// (e.g. Japanese Hiragana, Korean, Chinese Pinyin) that produces non-ASCII output.
1795///
1796/// This checks two properties:
1797/// 1. The source type is `kTISTypeKeyboardInputMode` (an IME input mode, not a plain
1798///    keyboard layout). This excludes non-ASCII layouts like Armenian and Ukrainian
1799///    that map keys directly without composition.
1800/// 2. The source is not ASCII-capable, which excludes modes like Japanese Romaji that
1801///    produce ASCII characters and should allow multi-stroke keybindings like `jj`.
1802unsafe fn is_ime_input_source_active() -> bool {
1803    unsafe {
1804        let source = TISCopyCurrentKeyboardInputSource();
1805        if source.is_null() {
1806            return false;
1807        }
1808
1809        let source_type =
1810            TISGetInputSourceProperty(source, kTISPropertyInputSourceType as *const c_void);
1811        let is_input_mode = !source_type.is_null()
1812            && CFEqual(
1813                source_type as CFTypeRef,
1814                kTISTypeKeyboardInputMode as CFTypeRef,
1815            ) != 0;
1816
1817        let is_ascii = TISGetInputSourceProperty(
1818            source,
1819            kTISPropertyInputSourceIsASCIICapable as *const c_void,
1820        );
1821        let is_ascii_capable = !is_ascii.is_null() && CFBooleanGetValue(is_ascii as CFBooleanRef);
1822
1823        CFRelease(source as CFTypeRef);
1824
1825        is_input_mode && !is_ascii_capable
1826    }
1827}
1828
1829extern "C" fn handle_key_event(this: &Object, native_event: id, key_equivalent: bool) -> BOOL {
1830    let window_state = unsafe { get_window_state(this) };
1831    let mut lock = window_state.as_ref().lock();
1832
1833    let window_height = lock.content_size().height;
1834    let event = unsafe { platform_input_from_native(native_event, Some(window_height)) };
1835
1836    let Some(event) = event else {
1837        return NO;
1838    };
1839
1840    let run_callback = |event: PlatformInput| -> BOOL {
1841        let mut callback = window_state.as_ref().lock().event_callback.take();
1842        let handled: BOOL = if let Some(callback) = callback.as_mut() {
1843            !callback(event).propagate as BOOL
1844        } else {
1845            NO
1846        };
1847        window_state.as_ref().lock().event_callback = callback;
1848        handled
1849    };
1850
1851    match event {
1852        PlatformInput::KeyDown(key_down_event) => {
1853            // For certain keystrokes, macOS will first dispatch a "key equivalent" event.
1854            // If that event isn't handled, it will then dispatch a "key down" event. GPUI
1855            // makes no distinction between these two types of events, so we need to ignore
1856            // the "key down" event if we've already just processed its "key equivalent" version.
1857            if key_equivalent {
1858                lock.last_key_equivalent = Some(key_down_event.clone());
1859            } else if lock.last_key_equivalent.take().as_ref() == Some(&key_down_event) {
1860                return NO;
1861            }
1862
1863            drop(lock);
1864
1865            let is_composing =
1866                with_input_handler(this, |input_handler| input_handler.marked_text_range())
1867                    .flatten()
1868                    .is_some();
1869
1870            // If we're composing, send the key to the input handler first;
1871            // otherwise we only send to the input handler if we don't have a matching binding.
1872            // The input handler may call `do_command_by_selector` if it doesn't know how to handle
1873            // a key. If it does so, it will return YES so we won't send the key twice.
1874            // We also do this for non-printing keys (like arrow keys and escape) as the IME menu
1875            // may need them even if there is no marked text;
1876            // however we skip keys with control or the input handler adds control-characters to the buffer.
1877            // and keys with function, as the input handler swallows them.
1878            // and keys with platform (Cmd), so that Cmd+key events (e.g. Cmd+`) are not
1879            // consumed by the IME on non-QWERTY / dead-key layouts.
1880            // We also send printable keys to the IME first when an IME input source (e.g. Japanese,
1881            // Korean, Chinese) is active and the input handler accepts text input. This prevents
1882            // multi-stroke keybindings like `jj` from intercepting keys that the IME should compose
1883            // (e.g. typing 'ji' should produce 'じ', not 'jい'). If the IME doesn't handle the key,
1884            // it calls `doCommandBySelector:` which routes it back to keybinding matching.
1885            let is_ime_printable_key = !is_composing
1886                && key_down_event
1887                    .keystroke
1888                    .key_char
1889                    .as_ref()
1890                    .is_some_and(|key_char| key_char.chars().all(|c| !c.is_control()))
1891                && !key_down_event.keystroke.modifiers.control
1892                && !key_down_event.keystroke.modifiers.function
1893                && !key_down_event.keystroke.modifiers.platform
1894                && unsafe { is_ime_input_source_active() }
1895                && with_input_handler(this, |input_handler| {
1896                    input_handler.query_prefers_ime_for_printable_keys()
1897                })
1898                .unwrap_or(false);
1899
1900            if is_composing
1901                || is_ime_printable_key
1902                || (key_down_event.keystroke.key_char.is_none()
1903                    && !key_down_event.keystroke.modifiers.control
1904                    && !key_down_event.keystroke.modifiers.function
1905                    && !key_down_event.keystroke.modifiers.platform)
1906            {
1907                {
1908                    let mut lock = window_state.as_ref().lock();
1909                    lock.keystroke_for_do_command = Some(key_down_event.keystroke.clone());
1910                    lock.do_command_handled.take();
1911                    drop(lock);
1912                }
1913
1914                let handled: BOOL = unsafe {
1915                    let input_context: id = msg_send![this, inputContext];
1916                    msg_send![input_context, handleEvent: native_event]
1917                };
1918                window_state.as_ref().lock().keystroke_for_do_command.take();
1919                if let Some(handled) = window_state.as_ref().lock().do_command_handled.take() {
1920                    return handled as BOOL;
1921                } else if handled == YES {
1922                    return YES;
1923                }
1924
1925                let handled = run_callback(PlatformInput::KeyDown(key_down_event));
1926                return handled;
1927            }
1928
1929            let handled = run_callback(PlatformInput::KeyDown(key_down_event.clone()));
1930            if handled == YES {
1931                return YES;
1932            }
1933
1934            if key_down_event.is_held
1935                && let Some(key_char) = key_down_event.keystroke.key_char.as_ref()
1936            {
1937                let handled = with_input_handler(this, |input_handler| {
1938                    if !input_handler.apple_press_and_hold_enabled() {
1939                        input_handler.replace_text_in_range(None, key_char);
1940                        return YES;
1941                    }
1942                    NO
1943                });
1944                if handled == Some(YES) {
1945                    return YES;
1946                }
1947            }
1948
1949            // Don't send key equivalents to the input handler if there are key modifiers other
1950            // than Function key, or macOS shortcuts like cmd-` will stop working.
1951            if key_equivalent && key_down_event.keystroke.modifiers != Modifiers::function() {
1952                return NO;
1953            }
1954
1955            unsafe {
1956                let input_context: id = msg_send![this, inputContext];
1957                msg_send![input_context, handleEvent: native_event]
1958            }
1959        }
1960
1961        PlatformInput::KeyUp(_) => {
1962            drop(lock);
1963            run_callback(event)
1964        }
1965
1966        _ => NO,
1967    }
1968}
1969
1970extern "C" fn handle_view_event(this: &Object, _: Sel, native_event: id) {
1971    let window_state = unsafe { get_window_state(this) };
1972    let weak_window_state = Arc::downgrade(&window_state);
1973    let mut lock = window_state.as_ref().lock();
1974    let window_height = lock.content_size().height;
1975    let event = unsafe { platform_input_from_native(native_event, Some(window_height)) };
1976
1977    if let Some(mut event) = event {
1978        match &mut event {
1979            PlatformInput::MouseDown(
1980                event @ MouseDownEvent {
1981                    button: MouseButton::Left,
1982                    modifiers: Modifiers { control: true, .. },
1983                    ..
1984                },
1985            ) => {
1986                // On mac, a ctrl-left click should be handled as a right click.
1987                *event = MouseDownEvent {
1988                    button: MouseButton::Right,
1989                    modifiers: Modifiers {
1990                        control: false,
1991                        ..event.modifiers
1992                    },
1993                    click_count: 1,
1994                    ..*event
1995                };
1996            }
1997
1998            // Handles focusing click.
1999            PlatformInput::MouseDown(
2000                event @ MouseDownEvent {
2001                    button: MouseButton::Left,
2002                    ..
2003                },
2004            ) if (lock.first_mouse) => {
2005                *event = MouseDownEvent {
2006                    first_mouse: true,
2007                    ..*event
2008                };
2009                lock.first_mouse = false;
2010            }
2011
2012            // Because we map a ctrl-left_down to a right_down -> right_up let's ignore
2013            // the ctrl-left_up to avoid having a mismatch in button down/up events if the
2014            // user is still holding ctrl when releasing the left mouse button
2015            PlatformInput::MouseUp(
2016                event @ MouseUpEvent {
2017                    button: MouseButton::Left,
2018                    modifiers: Modifiers { control: true, .. },
2019                    ..
2020                },
2021            ) => {
2022                *event = MouseUpEvent {
2023                    button: MouseButton::Right,
2024                    modifiers: Modifiers {
2025                        control: false,
2026                        ..event.modifiers
2027                    },
2028                    click_count: 1,
2029                    ..*event
2030                };
2031            }
2032
2033            _ => {}
2034        };
2035
2036        match &event {
2037            PlatformInput::MouseDown(_) => {
2038                drop(lock);
2039                unsafe {
2040                    let input_context: id = msg_send![this, inputContext];
2041                    msg_send![input_context, handleEvent: native_event]
2042                }
2043                lock = window_state.as_ref().lock();
2044            }
2045            PlatformInput::MouseMove(
2046                event @ MouseMoveEvent {
2047                    pressed_button: Some(_),
2048                    ..
2049                },
2050            ) => {
2051                // Synthetic drag is used for selecting long buffer contents while buffer is being scrolled.
2052                // External file drag and drop is able to emit its own synthetic mouse events which will conflict
2053                // with these ones.
2054                if !lock.external_files_dragged {
2055                    lock.synthetic_drag_counter += 1;
2056                    let executor = lock.foreground_executor.clone();
2057                    executor
2058                        .spawn(synthetic_drag(
2059                            weak_window_state,
2060                            lock.synthetic_drag_counter,
2061                            event.clone(),
2062                            lock.background_executor.clone(),
2063                        ))
2064                        .detach();
2065                }
2066            }
2067
2068            PlatformInput::MouseUp(MouseUpEvent { .. }) => {
2069                lock.synthetic_drag_counter += 1;
2070            }
2071
2072            PlatformInput::ModifiersChanged(ModifiersChangedEvent {
2073                modifiers,
2074                capslock,
2075            }) => {
2076                // Only raise modifiers changed event when they have actually changed
2077                if let Some(PlatformInput::ModifiersChanged(ModifiersChangedEvent {
2078                    modifiers: prev_modifiers,
2079                    capslock: prev_capslock,
2080                })) = &lock.previous_modifiers_changed_event
2081                    && prev_modifiers == modifiers
2082                    && prev_capslock == capslock
2083                {
2084                    return;
2085                }
2086
2087                lock.previous_modifiers_changed_event = Some(event.clone());
2088            }
2089
2090            _ => {}
2091        }
2092
2093        if let Some(mut callback) = lock.event_callback.take() {
2094            drop(lock);
2095            callback(event);
2096            window_state.lock().event_callback = Some(callback);
2097        }
2098    }
2099}
2100
2101extern "C" fn window_did_change_occlusion_state(this: &Object, _: Sel, _: id) {
2102    let window_state = unsafe { get_window_state(this) };
2103    let lock = &mut *window_state.lock();
2104    unsafe {
2105        if lock
2106            .native_window
2107            .occlusionState()
2108            .contains(NSWindowOcclusionState::NSWindowOcclusionStateVisible)
2109        {
2110            lock.move_traffic_light();
2111            lock.start_display_link();
2112        } else {
2113            lock.stop_display_link();
2114        }
2115    }
2116}
2117
2118extern "C" fn window_did_resize(this: &Object, _: Sel, _: id) {
2119    let window_state = unsafe { get_window_state(this) };
2120    window_state.as_ref().lock().move_traffic_light();
2121}
2122
2123extern "C" fn window_will_enter_fullscreen(this: &Object, _: Sel, _: id) {
2124    let window_state = unsafe { get_window_state(this) };
2125    let mut lock = window_state.as_ref().lock();
2126    lock.fullscreen_restore_bounds = lock.bounds();
2127
2128    let min_version = NSOperatingSystemVersion::new(15, 3, 0);
2129
2130    if is_macos_version_at_least(min_version) {
2131        unsafe {
2132            lock.native_window.setTitlebarAppearsTransparent_(NO);
2133        }
2134    }
2135}
2136
2137extern "C" fn window_will_exit_fullscreen(this: &Object, _: Sel, _: id) {
2138    let window_state = unsafe { get_window_state(this) };
2139    let lock = window_state.as_ref().lock();
2140
2141    let min_version = NSOperatingSystemVersion::new(15, 3, 0);
2142
2143    if is_macos_version_at_least(min_version) && lock.transparent_titlebar {
2144        unsafe {
2145            lock.native_window.setTitlebarAppearsTransparent_(YES);
2146        }
2147    }
2148}
2149
2150pub(crate) fn is_macos_version_at_least(version: NSOperatingSystemVersion) -> bool {
2151    unsafe { NSProcessInfo::processInfo(nil).isOperatingSystemAtLeastVersion(version) }
2152}
2153
2154extern "C" fn window_did_move(this: &Object, _: Sel, _: id) {
2155    let window_state = unsafe { get_window_state(this) };
2156    let mut lock = window_state.as_ref().lock();
2157    if let Some(mut callback) = lock.moved_callback.take() {
2158        drop(lock);
2159        callback();
2160        window_state.lock().moved_callback = Some(callback);
2161    }
2162}
2163
2164// Update the window scale factor and drawable size, and call the resize callback if any.
2165fn update_window_scale_factor(window_state: &Arc<Mutex<MacWindowState>>) {
2166    let mut lock = window_state.as_ref().lock();
2167    let scale_factor = lock.scale_factor();
2168    let size = lock.content_size();
2169    let drawable_size = size.to_device_pixels(scale_factor);
2170    if let Some(layer) = lock.renderer.layer() {
2171        unsafe {
2172            let _: () = msg_send![
2173                layer,
2174                setContentsScale: scale_factor as f64
2175            ];
2176        }
2177    }
2178
2179    lock.renderer.update_drawable_size(drawable_size);
2180
2181    if let Some(mut callback) = lock.resize_callback.take() {
2182        let content_size = lock.content_size();
2183        let scale_factor = lock.scale_factor();
2184        drop(lock);
2185        callback(content_size, scale_factor);
2186        window_state.as_ref().lock().resize_callback = Some(callback);
2187    };
2188}
2189
2190extern "C" fn window_did_change_screen(this: &Object, _: Sel, _: id) {
2191    let window_state = unsafe { get_window_state(this) };
2192    let mut lock = window_state.as_ref().lock();
2193    lock.start_display_link();
2194    drop(lock);
2195    update_window_scale_factor(&window_state);
2196}
2197
2198extern "C" fn window_did_change_key_status(this: &Object, selector: Sel, _: id) {
2199    let window_state = unsafe { get_window_state(this) };
2200    let lock = window_state.lock();
2201    let is_active = unsafe { lock.native_window.isKeyWindow() == YES };
2202
2203    // When opening a pop-up while the application isn't active, Cocoa sends a spurious
2204    // `windowDidBecomeKey` message to the previous key window even though that window
2205    // isn't actually key. This causes a bug if the application is later activated while
2206    // the pop-up is still open, making it impossible to activate the previous key window
2207    // even if the pop-up gets closed. The only way to activate it again is to de-activate
2208    // the app and re-activate it, which is a pretty bad UX.
2209    // The following code detects the spurious event and invokes `resignKeyWindow`:
2210    // in theory, we're not supposed to invoke this method manually but it balances out
2211    // the spurious `becomeKeyWindow` event and helps us work around that bug.
2212    if selector == sel!(windowDidBecomeKey:) && !is_active {
2213        let native_window = lock.native_window;
2214        drop(lock);
2215        unsafe {
2216            let _: () = msg_send![native_window, resignKeyWindow];
2217        }
2218        return;
2219    }
2220
2221    let executor = lock.foreground_executor.clone();
2222    drop(lock);
2223
2224    // When a window becomes active, trigger an immediate synchronous frame request to prevent
2225    // tab flicker when switching between windows in native tabs mode.
2226    //
2227    // This is only done on subsequent activations (not the first) to ensure the initial focus
2228    // path is properly established. Without this guard, the focus state would remain unset until
2229    // the first mouse click, causing keybindings to be non-functional.
2230    if selector == sel!(windowDidBecomeKey:) && is_active {
2231        let window_state = unsafe { get_window_state(this) };
2232        let mut lock = window_state.lock();
2233
2234        if lock.activated_least_once {
2235            if let Some(mut callback) = lock.request_frame_callback.take() {
2236                lock.renderer.set_presents_with_transaction(true);
2237                lock.stop_display_link();
2238                drop(lock);
2239                callback(Default::default());
2240
2241                let mut lock = window_state.lock();
2242                lock.request_frame_callback = Some(callback);
2243                lock.renderer.set_presents_with_transaction(false);
2244                lock.start_display_link();
2245            }
2246        } else {
2247            lock.activated_least_once = true;
2248        }
2249    }
2250
2251    executor
2252        .spawn(async move {
2253            let mut lock = window_state.as_ref().lock();
2254            if is_active {
2255                lock.move_traffic_light();
2256            }
2257
2258            if let Some(mut callback) = lock.activate_callback.take() {
2259                drop(lock);
2260                callback(is_active);
2261                window_state.lock().activate_callback = Some(callback);
2262            };
2263        })
2264        .detach();
2265}
2266
2267extern "C" fn window_should_close(this: &Object, _: Sel, _: id) -> BOOL {
2268    let window_state = unsafe { get_window_state(this) };
2269    let mut lock = window_state.as_ref().lock();
2270    if let Some(mut callback) = lock.should_close_callback.take() {
2271        drop(lock);
2272        let should_close = callback();
2273        window_state.lock().should_close_callback = Some(callback);
2274        should_close as BOOL
2275    } else {
2276        YES
2277    }
2278}
2279
2280extern "C" fn close_window(this: &Object, _: Sel) {
2281    unsafe {
2282        let close_callback = {
2283            let window_state = get_window_state(this);
2284            let mut lock = window_state.as_ref().lock();
2285            lock.closed.store(true, Ordering::Release);
2286            lock.close_callback.take()
2287        };
2288
2289        if let Some(callback) = close_callback {
2290            callback();
2291        }
2292
2293        let _: () = msg_send![super(this, class!(NSWindow)), close];
2294    }
2295}
2296
2297extern "C" fn make_backing_layer(this: &Object, _: Sel) -> id {
2298    let window_state = unsafe { get_window_state(this) };
2299    let window_state = window_state.as_ref().lock();
2300    window_state.renderer.layer_ptr() as id
2301}
2302
2303extern "C" fn view_did_change_backing_properties(this: &Object, _: Sel) {
2304    let window_state = unsafe { get_window_state(this) };
2305    update_window_scale_factor(&window_state);
2306}
2307
2308extern "C" fn set_frame_size(this: &Object, _: Sel, size: NSSize) {
2309    fn convert(value: NSSize) -> Size<Pixels> {
2310        Size {
2311            width: px(value.width as f32),
2312            height: px(value.height as f32),
2313        }
2314    }
2315
2316    let window_state = unsafe { get_window_state(this) };
2317    let mut lock = window_state.as_ref().lock();
2318
2319    let new_size = convert(size);
2320    let old_size = unsafe {
2321        let old_frame: NSRect = msg_send![this, frame];
2322        convert(old_frame.size)
2323    };
2324
2325    if old_size == new_size {
2326        return;
2327    }
2328
2329    unsafe {
2330        let _: () = msg_send![super(this, class!(NSView)), setFrameSize: size];
2331    }
2332
2333    let scale_factor = lock.scale_factor();
2334    let drawable_size = new_size.to_device_pixels(scale_factor);
2335    lock.renderer.update_drawable_size(drawable_size);
2336
2337    if let Some(mut callback) = lock.resize_callback.take() {
2338        let content_size = lock.content_size();
2339        let scale_factor = lock.scale_factor();
2340        drop(lock);
2341        callback(content_size, scale_factor);
2342        window_state.lock().resize_callback = Some(callback);
2343    };
2344}
2345
2346extern "C" fn display_layer(this: &Object, _: Sel, _: id) {
2347    let window_state = unsafe { get_window_state(this) };
2348    let mut lock = window_state.lock();
2349    if let Some(mut callback) = lock.request_frame_callback.take() {
2350        lock.renderer.set_presents_with_transaction(true);
2351        lock.stop_display_link();
2352        drop(lock);
2353        callback(Default::default());
2354
2355        let mut lock = window_state.lock();
2356        lock.request_frame_callback = Some(callback);
2357        lock.renderer.set_presents_with_transaction(false);
2358        lock.start_display_link();
2359    }
2360}
2361
2362extern "C" fn step(view: *mut c_void) {
2363    let view = view as id;
2364    let window_state = unsafe { get_window_state(&*view) };
2365    let mut lock = window_state.lock();
2366
2367    if let Some(mut callback) = lock.request_frame_callback.take() {
2368        drop(lock);
2369        callback(Default::default());
2370        window_state.lock().request_frame_callback = Some(callback);
2371    }
2372}
2373
2374extern "C" fn valid_attributes_for_marked_text(_: &Object, _: Sel) -> id {
2375    unsafe { msg_send![class!(NSArray), array] }
2376}
2377
2378extern "C" fn has_marked_text(this: &Object, _: Sel) -> BOOL {
2379    let has_marked_text_result =
2380        with_input_handler(this, |input_handler| input_handler.marked_text_range()).flatten();
2381
2382    has_marked_text_result.is_some() as BOOL
2383}
2384
2385extern "C" fn marked_range(this: &Object, _: Sel) -> NSRange {
2386    let marked_range_result =
2387        with_input_handler(this, |input_handler| input_handler.marked_text_range()).flatten();
2388
2389    marked_range_result.map_or(NSRange::invalid(), |range| range.into())
2390}
2391
2392extern "C" fn selected_range(this: &Object, _: Sel) -> NSRange {
2393    let selected_range_result = with_input_handler(this, |input_handler| {
2394        input_handler.selected_text_range(false)
2395    })
2396    .flatten();
2397
2398    selected_range_result.map_or(NSRange::invalid(), |selection| selection.range.into())
2399}
2400
2401extern "C" fn first_rect_for_character_range(
2402    this: &Object,
2403    _: Sel,
2404    range: NSRange,
2405    _: id,
2406) -> NSRect {
2407    let frame = get_frame(this);
2408    with_input_handler(this, |input_handler| {
2409        input_handler.bounds_for_range(range.to_range()?)
2410    })
2411    .flatten()
2412    .map_or(
2413        NSRect::new(NSPoint::new(0., 0.), NSSize::new(0., 0.)),
2414        |bounds| {
2415            NSRect::new(
2416                NSPoint::new(
2417                    frame.origin.x + bounds.origin.x.as_f32() as f64,
2418                    frame.origin.y + frame.size.height
2419                        - bounds.origin.y.as_f32() as f64
2420                        - bounds.size.height.as_f32() as f64,
2421                ),
2422                NSSize::new(
2423                    bounds.size.width.as_f32() as f64,
2424                    bounds.size.height.as_f32() as f64,
2425                ),
2426            )
2427        },
2428    )
2429}
2430
2431fn get_frame(this: &Object) -> NSRect {
2432    unsafe {
2433        let state = get_window_state(this);
2434        let lock = state.lock();
2435        let mut frame = NSWindow::frame(lock.native_window);
2436        let content_layout_rect: CGRect = msg_send![lock.native_window, contentLayoutRect];
2437        let style_mask: NSWindowStyleMask = msg_send![lock.native_window, styleMask];
2438        if !style_mask.contains(NSWindowStyleMask::NSFullSizeContentViewWindowMask) {
2439            frame.origin.y -= frame.size.height - content_layout_rect.size.height;
2440        }
2441        frame
2442    }
2443}
2444
2445extern "C" fn insert_text(this: &Object, _: Sel, text: id, replacement_range: NSRange) {
2446    unsafe {
2447        let is_attributed_string: BOOL =
2448            msg_send![text, isKindOfClass: [class!(NSAttributedString)]];
2449        let text: id = if is_attributed_string == YES {
2450            msg_send![text, string]
2451        } else {
2452            text
2453        };
2454
2455        let text = text.to_str();
2456        let replacement_range = replacement_range.to_range();
2457        with_input_handler(this, |input_handler| {
2458            input_handler.replace_text_in_range(replacement_range, text)
2459        });
2460    }
2461}
2462
2463extern "C" fn set_marked_text(
2464    this: &Object,
2465    _: Sel,
2466    text: id,
2467    selected_range: NSRange,
2468    replacement_range: NSRange,
2469) {
2470    unsafe {
2471        let is_attributed_string: BOOL =
2472            msg_send![text, isKindOfClass: [class!(NSAttributedString)]];
2473        let text: id = if is_attributed_string == YES {
2474            msg_send![text, string]
2475        } else {
2476            text
2477        };
2478        let selected_range = selected_range.to_range();
2479        let replacement_range = replacement_range.to_range();
2480        let text = text.to_str();
2481        with_input_handler(this, |input_handler| {
2482            input_handler.replace_and_mark_text_in_range(replacement_range, text, selected_range)
2483        });
2484    }
2485}
2486extern "C" fn unmark_text(this: &Object, _: Sel) {
2487    with_input_handler(this, |input_handler| input_handler.unmark_text());
2488}
2489
2490extern "C" fn attributed_substring_for_proposed_range(
2491    this: &Object,
2492    _: Sel,
2493    range: NSRange,
2494    actual_range: *mut c_void,
2495) -> id {
2496    with_input_handler(this, |input_handler| {
2497        let range = range.to_range()?;
2498        if range.is_empty() {
2499            return None;
2500        }
2501        let mut adjusted: Option<Range<usize>> = None;
2502
2503        let selected_text = input_handler.text_for_range(range.clone(), &mut adjusted)?;
2504        if let Some(adjusted) = adjusted
2505            && adjusted != range
2506        {
2507            unsafe { (actual_range as *mut NSRange).write(NSRange::from(adjusted)) };
2508        }
2509        unsafe {
2510            let string: id = msg_send![class!(NSAttributedString), alloc];
2511            let string: id = msg_send![string, initWithString: ns_string(&selected_text)];
2512            Some(string)
2513        }
2514    })
2515    .flatten()
2516    .unwrap_or(nil)
2517}
2518
2519// We ignore which selector it asks us to do because the user may have
2520// bound the shortcut to something else.
2521extern "C" fn do_command_by_selector(this: &Object, _: Sel, _: Sel) {
2522    let state = unsafe { get_window_state(this) };
2523    let mut lock = state.as_ref().lock();
2524    let keystroke = lock.keystroke_for_do_command.take();
2525    let mut event_callback = lock.event_callback.take();
2526    drop(lock);
2527
2528    if let Some((keystroke, callback)) = keystroke.zip(event_callback.as_mut()) {
2529        let handled = (callback)(PlatformInput::KeyDown(KeyDownEvent {
2530            keystroke,
2531            is_held: false,
2532            prefer_character_input: false,
2533        }));
2534        state.as_ref().lock().do_command_handled = Some(!handled.propagate);
2535    }
2536
2537    state.as_ref().lock().event_callback = event_callback;
2538}
2539
2540extern "C" fn view_did_change_effective_appearance(this: &Object, _: Sel) {
2541    unsafe {
2542        let state = get_window_state(this);
2543        let mut lock = state.as_ref().lock();
2544        if let Some(mut callback) = lock.appearance_changed_callback.take() {
2545            drop(lock);
2546            callback();
2547            state.lock().appearance_changed_callback = Some(callback);
2548        }
2549    }
2550}
2551
2552extern "C" fn accepts_first_mouse(this: &Object, _: Sel, _: id) -> BOOL {
2553    let window_state = unsafe { get_window_state(this) };
2554    let mut lock = window_state.as_ref().lock();
2555    lock.first_mouse = true;
2556    YES
2557}
2558
2559extern "C" fn character_index_for_point(this: &Object, _: Sel, position: NSPoint) -> u64 {
2560    let position = screen_point_to_gpui_point(this, position);
2561    with_input_handler(this, |input_handler| {
2562        input_handler.character_index_for_point(position)
2563    })
2564    .flatten()
2565    .map(|index| index as u64)
2566    .unwrap_or(NSNotFound as u64)
2567}
2568
2569fn screen_point_to_gpui_point(this: &Object, position: NSPoint) -> Point<Pixels> {
2570    let frame = get_frame(this);
2571    let window_x = position.x - frame.origin.x;
2572    let window_y = frame.size.height - (position.y - frame.origin.y);
2573
2574    point(px(window_x as f32), px(window_y as f32))
2575}
2576
2577extern "C" fn dragging_entered(this: &Object, _: Sel, dragging_info: id) -> NSDragOperation {
2578    let window_state = unsafe { get_window_state(this) };
2579    let position = drag_event_position(&window_state, dragging_info);
2580    let paths = external_paths_from_event(dragging_info);
2581    if let Some(event) = paths.map(|paths| FileDropEvent::Entered { position, paths })
2582        && send_file_drop_event(window_state, event)
2583    {
2584        return NSDragOperationCopy;
2585    }
2586    NSDragOperationNone
2587}
2588
2589extern "C" fn dragging_updated(this: &Object, _: Sel, dragging_info: id) -> NSDragOperation {
2590    let window_state = unsafe { get_window_state(this) };
2591    let position = drag_event_position(&window_state, dragging_info);
2592    if send_file_drop_event(window_state, FileDropEvent::Pending { position }) {
2593        NSDragOperationCopy
2594    } else {
2595        NSDragOperationNone
2596    }
2597}
2598
2599extern "C" fn dragging_exited(this: &Object, _: Sel, _: id) {
2600    let window_state = unsafe { get_window_state(this) };
2601    send_file_drop_event(window_state, FileDropEvent::Exited);
2602}
2603
2604extern "C" fn perform_drag_operation(this: &Object, _: Sel, dragging_info: id) -> BOOL {
2605    let window_state = unsafe { get_window_state(this) };
2606    let position = drag_event_position(&window_state, dragging_info);
2607    send_file_drop_event(window_state, FileDropEvent::Submit { position }).to_objc()
2608}
2609
2610fn external_paths_from_event(dragging_info: *mut Object) -> Option<ExternalPaths> {
2611    let mut paths = SmallVec::new();
2612    let pasteboard: id = unsafe { msg_send![dragging_info, draggingPasteboard] };
2613    let filenames = unsafe { NSPasteboard::propertyListForType(pasteboard, NSFilenamesPboardType) };
2614    if filenames == nil {
2615        return None;
2616    }
2617    for file in unsafe { filenames.iter() } {
2618        let path = unsafe {
2619            let f = NSString::UTF8String(file);
2620            CStr::from_ptr(f).to_string_lossy().into_owned()
2621        };
2622        paths.push(PathBuf::from(path))
2623    }
2624    Some(ExternalPaths(paths))
2625}
2626
2627extern "C" fn conclude_drag_operation(this: &Object, _: Sel, _: id) {
2628    let window_state = unsafe { get_window_state(this) };
2629    send_file_drop_event(window_state, FileDropEvent::Exited);
2630}
2631
2632async fn synthetic_drag(
2633    window_state: Weak<Mutex<MacWindowState>>,
2634    drag_id: usize,
2635    event: MouseMoveEvent,
2636    executor: BackgroundExecutor,
2637) {
2638    loop {
2639        executor.timer(Duration::from_millis(16)).await;
2640        if let Some(window_state) = window_state.upgrade() {
2641            let mut lock = window_state.lock();
2642            if lock.synthetic_drag_counter == drag_id {
2643                if let Some(mut callback) = lock.event_callback.take() {
2644                    drop(lock);
2645                    callback(PlatformInput::MouseMove(event.clone()));
2646                    window_state.lock().event_callback = Some(callback);
2647                }
2648            } else {
2649                break;
2650            }
2651        }
2652    }
2653}
2654
2655/// Sends the specified FileDropEvent using `PlatformInput::FileDrop` to the window
2656/// state and updates the window state according to the event passed.
2657fn send_file_drop_event(
2658    window_state: Arc<Mutex<MacWindowState>>,
2659    file_drop_event: FileDropEvent,
2660) -> bool {
2661    let external_files_dragged = match file_drop_event {
2662        FileDropEvent::Entered { .. } => Some(true),
2663        FileDropEvent::Exited => Some(false),
2664        _ => None,
2665    };
2666
2667    let mut lock = window_state.lock();
2668    if let Some(mut callback) = lock.event_callback.take() {
2669        drop(lock);
2670        callback(PlatformInput::FileDrop(file_drop_event));
2671        let mut lock = window_state.lock();
2672        lock.event_callback = Some(callback);
2673        if let Some(external_files_dragged) = external_files_dragged {
2674            lock.external_files_dragged = external_files_dragged;
2675        }
2676        true
2677    } else {
2678        false
2679    }
2680}
2681
2682fn drag_event_position(window_state: &Mutex<MacWindowState>, dragging_info: id) -> Point<Pixels> {
2683    let drag_location: NSPoint = unsafe { msg_send![dragging_info, draggingLocation] };
2684    convert_mouse_position(drag_location, window_state.lock().content_size().height)
2685}
2686
2687fn with_input_handler<F, R>(window: &Object, f: F) -> Option<R>
2688where
2689    F: FnOnce(&mut PlatformInputHandler) -> R,
2690{
2691    let window_state = unsafe { get_window_state(window) };
2692    let mut lock = window_state.as_ref().lock();
2693    if let Some(mut input_handler) = lock.input_handler.take() {
2694        drop(lock);
2695        let result = f(&mut input_handler);
2696        window_state.lock().input_handler = Some(input_handler);
2697        Some(result)
2698    } else {
2699        None
2700    }
2701}
2702
2703unsafe fn display_id_for_screen(screen: id) -> CGDirectDisplayID {
2704    unsafe {
2705        let device_description = NSScreen::deviceDescription(screen);
2706        let screen_number_key: id = ns_string("NSScreenNumber");
2707        let screen_number = device_description.objectForKey_(screen_number_key);
2708        let screen_number: NSUInteger = msg_send![screen_number, unsignedIntegerValue];
2709        screen_number as CGDirectDisplayID
2710    }
2711}
2712
2713extern "C" fn blurred_view_init_with_frame(this: &Object, _: Sel, frame: NSRect) -> id {
2714    unsafe {
2715        let view = msg_send![super(this, class!(NSVisualEffectView)), initWithFrame: frame];
2716        // Use a colorless semantic material. The default value `AppearanceBased`, though not
2717        // manually set, is deprecated.
2718        NSVisualEffectView::setMaterial_(view, NSVisualEffectMaterial::Selection);
2719        NSVisualEffectView::setState_(view, NSVisualEffectState::Active);
2720        view
2721    }
2722}
2723
2724extern "C" fn blurred_view_update_layer(this: &Object, _: Sel) {
2725    unsafe {
2726        let _: () = msg_send![super(this, class!(NSVisualEffectView)), updateLayer];
2727        let layer: id = msg_send![this, layer];
2728        if !layer.is_null() {
2729            remove_layer_background(layer);
2730        }
2731    }
2732}
2733
2734unsafe fn remove_layer_background(layer: id) {
2735    unsafe {
2736        let _: () = msg_send![layer, setBackgroundColor:nil];
2737
2738        let class_name: id = msg_send![layer, className];
2739        if class_name.isEqualToString("CAChameleonLayer") {
2740            // Remove the desktop tinting effect.
2741            let _: () = msg_send![layer, setHidden: YES];
2742            return;
2743        }
2744
2745        let filters: id = msg_send![layer, filters];
2746        if !filters.is_null() {
2747            // Remove the increased saturation.
2748            // The effect of a `CAFilter` or `CIFilter` is determined by its name, and the
2749            // `description` reflects its name and some parameters. Currently `NSVisualEffectView`
2750            // uses a `CAFilter` named "colorSaturate". If one day they switch to `CIFilter`, the
2751            // `description` will still contain "Saturat" ("... inputSaturation = ...").
2752            let test_string: id = ns_string("Saturat");
2753            let count = NSArray::count(filters);
2754            for i in 0..count {
2755                let description: id = msg_send![filters.objectAtIndex(i), description];
2756                let hit: BOOL = msg_send![description, containsString: test_string];
2757                if hit == NO {
2758                    continue;
2759                }
2760
2761                let all_indices = NSRange {
2762                    location: 0,
2763                    length: count,
2764                };
2765                let indices: id = msg_send![class!(NSMutableIndexSet), indexSet];
2766                let _: () = msg_send![indices, addIndexesInRange: all_indices];
2767                let _: () = msg_send![indices, removeIndex:i];
2768                let filtered: id = msg_send![filters, objectsAtIndexes: indices];
2769                let _: () = msg_send![layer, setFilters: filtered];
2770                break;
2771            }
2772        }
2773
2774        let sublayers: id = msg_send![layer, sublayers];
2775        if !sublayers.is_null() {
2776            let count = NSArray::count(sublayers);
2777            for i in 0..count {
2778                let sublayer = sublayers.objectAtIndex(i);
2779                remove_layer_background(sublayer);
2780            }
2781        }
2782    }
2783}
2784
2785extern "C" fn add_titlebar_accessory_view_controller(this: &Object, _: Sel, view_controller: id) {
2786    unsafe {
2787        let _: () = msg_send![super(this, class!(NSWindow)), addTitlebarAccessoryViewController: view_controller];
2788
2789        // Hide the native tab bar and set its height to 0, since we render our own.
2790        let accessory_view: id = msg_send![view_controller, view];
2791        let _: () = msg_send![accessory_view, setHidden: YES];
2792        let mut frame: NSRect = msg_send![accessory_view, frame];
2793        frame.size.height = 0.0;
2794        let _: () = msg_send![accessory_view, setFrame: frame];
2795    }
2796}
2797
2798extern "C" fn move_tab_to_new_window(this: &Object, _: Sel, _: id) {
2799    unsafe {
2800        let _: () = msg_send![super(this, class!(NSWindow)), moveTabToNewWindow:nil];
2801
2802        let window_state = get_window_state(this);
2803        let mut lock = window_state.as_ref().lock();
2804        if let Some(mut callback) = lock.move_tab_to_new_window_callback.take() {
2805            drop(lock);
2806            callback();
2807            window_state.lock().move_tab_to_new_window_callback = Some(callback);
2808        }
2809    }
2810}
2811
2812extern "C" fn merge_all_windows(this: &Object, _: Sel, _: id) {
2813    unsafe {
2814        let _: () = msg_send![super(this, class!(NSWindow)), mergeAllWindows:nil];
2815
2816        let window_state = get_window_state(this);
2817        let mut lock = window_state.as_ref().lock();
2818        if let Some(mut callback) = lock.merge_all_windows_callback.take() {
2819            drop(lock);
2820            callback();
2821            window_state.lock().merge_all_windows_callback = Some(callback);
2822        }
2823    }
2824}
2825
2826extern "C" fn select_next_tab(this: &Object, _sel: Sel, _id: id) {
2827    let window_state = unsafe { get_window_state(this) };
2828    let mut lock = window_state.as_ref().lock();
2829    if let Some(mut callback) = lock.select_next_tab_callback.take() {
2830        drop(lock);
2831        callback();
2832        window_state.lock().select_next_tab_callback = Some(callback);
2833    }
2834}
2835
2836extern "C" fn select_previous_tab(this: &Object, _sel: Sel, _id: id) {
2837    let window_state = unsafe { get_window_state(this) };
2838    let mut lock = window_state.as_ref().lock();
2839    if let Some(mut callback) = lock.select_previous_tab_callback.take() {
2840        drop(lock);
2841        callback();
2842        window_state.lock().select_previous_tab_callback = Some(callback);
2843    }
2844}
2845
2846extern "C" fn toggle_tab_bar(this: &Object, _sel: Sel, _id: id) {
2847    unsafe {
2848        let _: () = msg_send![super(this, class!(NSWindow)), toggleTabBar:nil];
2849
2850        let window_state = get_window_state(this);
2851        let mut lock = window_state.as_ref().lock();
2852        lock.move_traffic_light();
2853
2854        if let Some(mut callback) = lock.toggle_tab_bar_callback.take() {
2855            drop(lock);
2856            callback();
2857            window_state.lock().toggle_tab_bar_callback = Some(callback);
2858        }
2859    }
2860}