Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

28.6 KB
Raw Blame
// use std::ffi::c_void;

// use cocoa::{
//     base::{id, nil},
//     foundation::NSString,
// };
// use core_graphics::{
//     base::boolean_t,
//     display::{CFDictionaryRef, CGRect},
// };
// use objc::{class, msg_send, sel, sel_impl};

pub mod delegates;
mod sc_shareable_content;

pub use sc_shareable_content::*;

fn constrain_position_to_visible_top(
    position: tauri::PhysicalPosition<i32>,
    monitor_top: i32,
    scale_factor: f64,
    visible_frame_top_inset: f64,
    safe_area_top_inset: f64,
) -> Option<tauri::PhysicalPosition<i32>> {
    let top_inset = visible_frame_top_inset.max(safe_area_top_inset).max(0.0);
    let minimum_y = monitor_top.saturating_add((top_inset * scale_factor).round() as i32);

    (position.y < minimum_y).then(|| tauri::PhysicalPosition::new(position.x, minimum_y))
}

pub fn constrain_main_window_to_visible_top(
    window: &tauri::Window,
    position: tauri::PhysicalPosition<i32>,
) -> Option<tauri::PhysicalPosition<i32>> {
    use objc2::{runtime::NSObjectProtocol, sel};
    use objc2_app_kit::NSWindow;

    let monitor = window.current_monitor().ok().flatten()?;
    let ns_window = window.ns_window().ok()? as *const NSWindow;
    let screen = unsafe { (*ns_window).screen() }?;
    let frame = screen.frame();
    let visible_frame = screen.visibleFrame();
    let safe_area_top_inset = if screen.respondsToSelector(sel!(safeAreaInsets)) {
        unsafe { screen.safeAreaInsets().top }
    } else {
        0.0
    };
    // Tauri runtime 2.8 omits this macOS top offset from Monitor::work_area().position.
    let visible_frame_top_inset =
        frame.origin.y + frame.size.height - visible_frame.origin.y - visible_frame.size.height;

    constrain_position_to_visible_top(
        position,
        monitor.position().y,
        monitor.scale_factor(),
        visible_frame_top_inset,
        safe_area_top_inset,
    )
}

fn constrain_frame_to_visible_area(
    frame: objc2_foundation::NSRect,
    screen_frame: objc2_foundation::NSRect,
    visible_frame: objc2_foundation::NSRect,
    safe_area_top_inset: f64,
) -> Option<objc2_foundation::NSRect> {
    use objc2_foundation::{NSPoint, NSRect, NSSize};

    let minimum_x = visible_frame.origin.x;
    let minimum_y = visible_frame.origin.y;
    let maximum_x = minimum_x + visible_frame.size.width;
    let maximum_y = (minimum_y + visible_frame.size.height)
        .min(screen_frame.origin.y + screen_frame.size.height - safe_area_top_inset.max(0.0));
    if ![
        frame.origin.x,
        frame.origin.y,
        frame.size.width,
        frame.size.height,
        minimum_x,
        minimum_y,
        maximum_x,
        maximum_y,
    ]
    .into_iter()
    .all(f64::is_finite)
        || frame.size.width <= 0.0
        || frame.size.height <= 0.0
        || maximum_x <= minimum_x
        || maximum_y <= minimum_y
    {
        return None;
    }

    let width = frame.size.width.min(maximum_x - minimum_x);
    let height = frame.size.height.min(maximum_y - minimum_y);
    let constrained = NSRect::new(
        NSPoint::new(
            frame.origin.x.clamp(minimum_x, maximum_x - width),
            frame.origin.y.clamp(minimum_y, maximum_y - height),
        ),
        NSSize::new(width, height),
    );
    (constrained != frame).then_some(constrained)
}

pub fn constrain_main_window_to_visible_frame(window: &tauri::Window) -> Result<(), String> {
    use objc2::{MainThreadMarker, runtime::NSObjectProtocol, sel};
    use objc2_app_kit::{NSScreen, NSWindow};

    let main_thread =
        MainThreadMarker::new().ok_or("Main window bounds must be checked on the main thread")?;
    let ns_window = window.ns_window().map_err(|error| error.to_string())? as *const NSWindow;
    let ns_window = unsafe { ns_window.as_ref() }.ok_or("Main window is unavailable")?;
    let screen = ns_window
        .screen()
        .or_else(|| NSScreen::mainScreen(main_thread))
        .ok_or("Main window screen is unavailable")?;
    let safe_area_top_inset = if screen.respondsToSelector(sel!(safeAreaInsets)) {
        unsafe { screen.safeAreaInsets().top }
    } else {
        0.0
    };
    if let Some(frame) = constrain_frame_to_visible_area(
        ns_window.frame(),
        screen.frame(),
        screen.visibleFrame(),
        safe_area_top_inset,
    ) {
        ns_window.setFrame_display(frame, true);
    }
    Ok(())
}

pub fn set_window_level(window: tauri::Window, level: objc2_app_kit::NSWindowLevel) {
    let c_window = window.clone();
    _ = window.run_on_main_thread(move || unsafe {
        let Ok(ns_win) = c_window.ns_window() else {
            return;
        };
        let ns_win = ns_win as *const objc2_app_kit::NSWindow;
        (*ns_win).setLevel(level);
    });
}

pub fn set_window_opacity(window: tauri::Window, opacity: f64) {
    let opacity = opacity.clamp(0.45, 1.0);
    let c_window = window.clone();
    _ = window.run_on_main_thread(move || unsafe {
        use cocoa::base::id;
        use objc::{msg_send, sel, sel_impl};

        let Ok(ns_win) = c_window.ns_window() else {
            return;
        };
        let ns_win = ns_win as id;
        let _: () = msg_send![ns_win, setAlphaValue: opacity];
    });
}

pub fn apply_squircle_corners(window: &tauri::WebviewWindow, radius: f64) {
    use cocoa::base::{id, nil};
    use cocoa::foundation::NSString;
    use objc::{msg_send, sel, sel_impl};

    let Ok(ns_win) = window.ns_window() else {
        return;
    };

    unsafe {
        let ns_win = ns_win as id;
        let content_view: id = msg_send![ns_win, contentView];

        if content_view != nil {
            let _: () = msg_send![content_view, setWantsLayer: true];

            let layer: id = msg_send![content_view, layer];
            if layer != nil {
                let _: () = msg_send![layer, setCornerRadius: radius];
                let _: () = msg_send![layer, setMasksToBounds: true];

                let continuous = NSString::alloc(nil).init_str("continuous");
                let _: () = msg_send![layer, setCornerCurve: continuous];
            }
        }
    }
}

const TAURI_VIBRANCY_VIEW_TAG: isize = 91376254;
const LIQUID_GLASS_IDENTIFIER: &str = "so.cap.liquid-glass-background";
const NS_GLASS_EFFECT_VIEW_STYLE_REGULAR: isize = 0;

#[derive(Clone, Copy, PartialEq, Eq)]
enum LiquidGlassActivity {
    SystemManaged,
    AlwaysActive,
}

pub fn apply_main_window_liquid_glass_background(
    window: &tauri::Window,
    enabled: bool,
    radius: f64,
) -> Result<bool, String> {
    apply_liquid_glass_background_inner(window, enabled, radius, LiquidGlassActivity::SystemManaged)
}

unsafe fn remove_tagged_subview(container: cocoa::base::id, tag: isize) {
    use objc::{msg_send, sel, sel_impl};

    unsafe {
        let responds_to_view_with_tag: bool =
            msg_send![container, respondsToSelector: sel!(viewWithTag:)];
        if !responds_to_view_with_tag {
            return;
        }

        let view: cocoa::base::id = msg_send![container, viewWithTag: tag];
        if view != cocoa::base::nil {
            let _: () = msg_send![view, removeFromSuperview];
        }
    }
}

unsafe fn remove_liquid_glass_subviews(
    container: cocoa::base::id,
    identifier: cocoa::base::id,
    glass_class: *const objc::runtime::Class,
) {
    use cocoa::base::{id, nil};
    use objc::{msg_send, sel, sel_impl};

    unsafe {
        let subviews: id = msg_send![container, subviews];
        if subviews == nil {
            return;
        }

        let count: usize = msg_send![subviews, count];
        for index in (0..count).rev() {
            let view: id = msg_send![subviews, objectAtIndex: index];
            if view == nil {
                continue;
            }

            let is_glass_view: bool = msg_send![view, isKindOfClass: glass_class];
            if is_glass_view {
                let _: () = msg_send![view, removeFromSuperview];
                // Balance the `alloc` ownership claim taken when this view was
                // created in `apply_liquid_glass_background`. Without this,
                // removing it from the hierarchy only drops the superview's
                // retain, leaving it stuck at refcount 1 forever — a leaked
                // GPU/IOSurface-backed NSGlassEffectView on every re-apply.
                let _: () = msg_send![view, release];
                continue;
            }

            let responds_to_identifier: bool =
                msg_send![view, respondsToSelector: sel!(identifier)];
            if !responds_to_identifier {
                continue;
            }

            let view_identifier: id = msg_send![view, identifier];
            if view_identifier == nil {
                continue;
            }

            let matches_identifier: bool = msg_send![view_identifier, isEqualToString: identifier];
            if matches_identifier {
                let _: () = msg_send![view, removeFromSuperview];
            }
        }
    }
}

pub fn apply_liquid_glass_background(
    window: &tauri::Window,
    enabled: bool,
    radius: f64,
) -> Result<bool, String> {
    apply_liquid_glass_background_inner(window, enabled, radius, LiquidGlassActivity::AlwaysActive)
}

fn apply_liquid_glass_background_inner(
    window: &tauri::Window,
    enabled: bool,
    radius: f64,
    activity: LiquidGlassActivity,
) -> Result<bool, String> {
    use cocoa::{
        base::{id, nil},
        foundation::{NSRect, NSString},
    };
    use objc::{class, msg_send, runtime::Class, sel, sel_impl};

    unsafe {
        let ns_window = window
            .ns_window()
            .map_err(|error| format!("Failed to get native window handle: {error}"))?
            as id;
        let content_view: id = msg_send![ns_window, contentView];

        if content_view == nil {
            return Err("Window has no content view".into());
        }

        let Some(glass_class) = Class::get("NSGlassEffectView") else {
            // No NSGlassEffectView (pre-macOS-26) — the caller uses vibrancy.
            if enabled {
                crate::crash_sentinel::set_liquid_glass_outcome("unsupported");
            }
            return Ok(false);
        };
        let glass_class = glass_class as *const Class;
        let glass_identifier = NSString::alloc(nil).init_str(LIQUID_GLASS_IDENTIFIER);

        if !enabled {
            teardown_liquid_glass_ns(ns_window);
            return Ok(false);
        }

        remove_liquid_glass_subviews(content_view, glass_identifier, glass_class);

        // Clip the content view itself to the same continuous (squircle) curve we apply
        // to the NSGlassEffectView below. Without this, WebKit's CSS border-radius on
        // `.cap-window-shell` and the window's Core Animation corner are rasterised by
        // different engines and don't align pixel-perfectly, producing the visible inner
        // "second border" inside the rounded corner. This is plain Core Animation (no
        // private SPI / no WindowServer risk), so we apply it up front — it must clip
        // BOTH the glass view and the vibrancy fallback, otherwise the artifact returns
        // on the fallback path (most visibly on the larger-radius settings window).
        let _: () = msg_send![content_view, setWantsLayer: true];
        let content_layer: id = msg_send![content_view, layer];
        if content_layer != nil {
            let _: () = msg_send![content_layer, setCornerRadius: radius];
            let _: () = msg_send![content_layer, setMasksToBounds: true];
            let continuous = NSString::alloc(nil).init_str("continuous");
            let _: () = msg_send![content_layer, setCornerCurve: continuous];
        }

        let bounds: NSRect = msg_send![content_view, bounds];
        let glass_view: id = msg_send![glass_class, alloc];
        let glass_view: id = msg_send![glass_view, initWithFrame: bounds];

        if glass_view == nil {
            crate::crash_sentinel::set_liquid_glass_outcome("fallback");
            return Ok(false);
        }

        let responds_to_set_identifier: bool =
            msg_send![glass_view, respondsToSelector: sel!(setIdentifier:)];
        if responds_to_set_identifier {
            let _: () = msg_send![glass_view, setIdentifier: glass_identifier];
        }

        let responds_to_set_corner_radius: bool =
            msg_send![glass_view, respondsToSelector: sel!(setCornerRadius:)];
        if responds_to_set_corner_radius {
            let _: () = msg_send![glass_view, setCornerRadius: radius];
        }

        if activity == LiquidGlassActivity::SystemManaged {
            let _: () = msg_send![glass_view, setStyle: NS_GLASS_EFFECT_VIEW_STYLE_REGULAR];
        }

        // Pin the glass to its "always-active" representation so it keeps re-rendering
        // the live backdrop regardless of which window currently has key state. The
        // default for an NSVisualEffectView-derived view is FollowsWindowActiveState,
        // which dims the material whenever another Cap window (camera, settings, etc.)
        // becomes key and masks the backdrop reactivity that's the whole point of
        // Liquid Glass. The always-active state is private SPI, so we probe multiple
        // state knobs (`setState:`, `setActive:`) instead of assuming one inheritance
        // path. The main window does not enter this branch and remains system-managed.
        //
        // macOS 26.3 shipped an NSGlassEffectView that responds to neither selector,
        // so the pin silently fails. In that state the material can't be relied on
        // (it dims whenever another window becomes key), so abandon the private SPI
        // entirely and let the caller fall back to NSVisualEffectView vibrancy
        // (Ok(false)).
        if activity == LiquidGlassActivity::AlwaysActive
            && !force_glass_view_always_active(glass_view)
        {
            // Never entered the view hierarchy; balance the alloc and bail. The
            // content-layer squircle clip applied above is kept (plain Core Animation,
            // not a WindowServer/occlusion mutation) so the vibrancy fallback still gets
            // the aligned rounded corner.
            let _: () = msg_send![glass_view, release];
            crate::crash_sentinel::set_liquid_glass_outcome("fallback");
            return Ok(false);
        }

        remove_tagged_subview(content_view, TAURI_VIBRANCY_VIEW_TAG);

        let clear_color: id = msg_send![class!(NSColor), clearColor];
        let _: () = msg_send![ns_window, setOpaque: false];
        let _: () = msg_send![ns_window, setBackgroundColor: clear_color];

        // Deliberately DO NOT disable window/WKWebView occlusion detection here.
        // Earlier builds called the private `_setWindowOcclusionDetectionEnabled:` /
        // `_setWebViewWindowOcclusionDetectionEnabled:` SPI so the glass backdrop kept
        // sampling while Cap was unfocused, but an occlusion-suppressed surface is one
        // WindowServer can never quiesce — on macOS 26 that wedged the compositor when
        // the window was later hidden or the app quit (often after sleep/lid-close),
        // watchdog-stalling WindowServer and soft-restarting the whole login session.
        // Gating it on the always-active pin was not enough: the main window's
        // close-to-tray path hides the window without any teardown, so any future
        // macOS build where the pin succeeds would silently re-arm the wedge. The
        // only field-verified-safe configuration is to leave occlusion detection
        // alone entirely; the trade-off is merely cosmetic (backdrop sampling may
        // pause on the last frame while the app is deactivated).

        let _: () = msg_send![glass_view, setAutoresizingMask: 18usize];
        let _: () = msg_send![
            content_view,
            addSubview: glass_view
            positioned: -1isize
            relativeTo: nil
        ];

        if activity == LiquidGlassActivity::AlwaysActive {
            // Re-apply after the view enters the hierarchy: some private AppKit views
            // reset state machine fields on `viewDidMoveToWindow:`, so the post-add pass
            // is what actually sticks.
            force_glass_view_always_active(glass_view);
        }

        crate::crash_sentinel::set_liquid_glass_outcome("applied");
        Ok(true)
    }
}

/// Pin the glass material to its always-active representation. Returns whether the
/// view actually responded to a known pinning selector. A `false` return means this
/// macOS build's NSGlassEffectView SPI differs from what we understand (notably 26.3,
/// where it responds to neither `setState:` nor `setActive:`) and the material must
/// NOT be relied upon — see `apply_liquid_glass_background`.
unsafe fn force_glass_view_always_active(glass_view: cocoa::base::id) -> bool {
    use cocoa::base::id;
    use objc::{msg_send, sel, sel_impl};

    unsafe {
        let responds_to_set_state: bool =
            msg_send![glass_view, respondsToSelector: sel!(setState:)];
        if responds_to_set_state {
            // NSVisualEffectStateActive == 1
            let _: () = msg_send![glass_view, setState: 1isize];
            tracing::info!(target: "cap_desktop_lib::liquid_glass", "NSGlassEffectView responds to setState:");
        }

        let responds_to_set_active: bool =
            msg_send![glass_view, respondsToSelector: sel!(setActive:)];
        if responds_to_set_active {
            let _: () = msg_send![glass_view, setActive: true];
            tracing::info!(target: "cap_desktop_lib::liquid_glass", "NSGlassEffectView responds to setActive:");
        }

        if !responds_to_set_state && !responds_to_set_active {
            tracing::debug!(
                target: "cap_desktop_lib::liquid_glass",
                "NSGlassEffectView responds to neither setState: nor setActive: — \
                 cannot pin material to always-active; falling back to vibrancy"
            );
            return false;
        }

        // If the glass view internally wraps an NSVisualEffectView (rather than
        // subclassing it), the state property on the outer view won't propagate.
        // Walk one level of subviews and pin any visual-effect-like state we find.
        let subviews: id = msg_send![glass_view, subviews];
        if subviews != cocoa::base::nil {
            let count: usize = msg_send![subviews, count];
            for index in 0..count {
                let subview: id = msg_send![subviews, objectAtIndex: index];
                if subview == cocoa::base::nil {
                    continue;
                }

                let subview_responds_to_set_state: bool =
                    msg_send![subview, respondsToSelector: sel!(setState:)];
                if subview_responds_to_set_state {
                    let _: () = msg_send![subview, setState: 1isize];
                }
            }
        }

        true
    }
}

unsafe fn enable_window_occlusion_detection(ns_window: cocoa::base::id) {
    use objc::{msg_send, sel, sel_impl};

    // Defense-in-depth heal: current builds never disable occlusion detection, but
    // re-assert the OS default via whichever private SPI variant this AppKit build
    // responds to, so the OS can always quiesce the surface.
    unsafe {
        let responds: bool = msg_send![
            ns_window,
            respondsToSelector: sel!(_setWindowOcclusionDetectionEnabled:)
        ];
        if responds {
            let _: () = msg_send![ns_window, _setWindowOcclusionDetectionEnabled: true];
            return;
        }

        let responds: bool = msg_send![
            ns_window,
            respondsToSelector: sel!(setWindowOcclusionDetectionEnabled:)
        ];
        if responds {
            let _: () = msg_send![ns_window, setWindowOcclusionDetectionEnabled: true];
            return;
        }

        let responds: bool = msg_send![
            ns_window,
            respondsToSelector: sel!(_setOcclusionDetectionEnabled:)
        ];
        if responds {
            let _: () = msg_send![ns_window, _setOcclusionDetectionEnabled: true];
            return;
        }

        let responds: bool = msg_send![
            ns_window,
            respondsToSelector: sel!(setOcclusionDetectionEnabled:)
        ];
        if responds {
            let _: () = msg_send![ns_window, setOcclusionDetectionEnabled: true];
        }
    }
}

unsafe fn enable_webview_occlusion_detection(content_view: cocoa::base::id) {
    use cocoa::base::id;
    use objc::{msg_send, runtime::Class, sel, sel_impl};

    // Re-enumerate the live WKWebView subview (its identity/index can change between
    // apply and teardown) and restore occlusion detection, mirroring the disable path.
    unsafe {
        let Some(wkwebview_class) = Class::get("WKWebView") else {
            return;
        };
        let wkwebview_class = wkwebview_class as *const Class;

        let subviews: id = msg_send![content_view, subviews];
        if subviews == cocoa::base::nil {
            return;
        }

        let count: usize = msg_send![subviews, count];
        for index in 0..count {
            let subview: id = msg_send![subviews, objectAtIndex: index];
            if subview == cocoa::base::nil {
                continue;
            }

            let is_webview: bool = msg_send![subview, isKindOfClass: wkwebview_class];
            if !is_webview {
                continue;
            }

            let responds: bool = msg_send![
                subview,
                respondsToSelector: sel!(_setWebViewWindowOcclusionDetectionEnabled:)
            ];
            if responds {
                let _: () = msg_send![subview, _setWebViewWindowOcclusionDetectionEnabled: true];
                return;
            }

            let responds: bool = msg_send![
                subview,
                respondsToSelector: sel!(_setWindowOcclusionDetectionEnabled:)
            ];
            if responds {
                let _: () = msg_send![subview, _setWindowOcclusionDetectionEnabled: true];
                return;
            }

            return;
        }
    }
}

/// Reverse every WindowServer-visible mutation `apply_liquid_glass_background` makes:
/// remove the NSGlassEffectView, then restore window/WKWebView occlusion detection and
/// window opacity. MUST run on the AppKit main thread.
unsafe fn teardown_liquid_glass_ns(ns_window: cocoa::base::id) {
    use cocoa::{
        base::{id, nil},
        foundation::NSString,
    };
    use objc::{msg_send, runtime::Class, sel, sel_impl};

    unsafe {
        let content_view: id = msg_send![ns_window, contentView];
        if content_view == nil {
            return;
        }

        if let Some(glass_class) = Class::get("NSGlassEffectView") {
            let glass_identifier = NSString::alloc(nil).init_str(LIQUID_GLASS_IDENTIFIER);
            remove_liquid_glass_subviews(
                content_view,
                glass_identifier,
                glass_class as *const Class,
            );
        }

        enable_window_occlusion_detection(ns_window);
        enable_webview_occlusion_detection(content_view);
        let _: () = msg_send![ns_window, setOpaque: true];
    }
}

/// Tear down liquid glass on every webview window. MUST be called on the AppKit main
/// thread (e.g. from the `RunEvent::Exit` handler, which runs on main). Returns the
/// number of windows processed.
pub fn teardown_all_liquid_glass_on_main(app: &tauri::AppHandle) -> usize {
    use tauri::Manager;

    let mut count = 0usize;
    for (_label, window) in app.webview_windows() {
        if let Ok(ns_window) = window.ns_window() {
            unsafe {
                teardown_liquid_glass_ns(ns_window as cocoa::base::id);
            }
            count += 1;
        }
    }
    count
}

/// Dispatch liquid-glass teardown onto the main thread and await it. Safe to call
/// from a tokio worker during exit: the AppKit run loop is still pumping because the
/// macOS terminate handler returned NSTerminateCancel before spawning the async exit.
pub async fn teardown_all_liquid_glass(app: &tauri::AppHandle) -> Result<(), String> {
    let app_for_closure = app.clone();
    let (tx, rx) = tokio::sync::oneshot::channel();

    app.run_on_main_thread(move || {
        let count = teardown_all_liquid_glass_on_main(&app_for_closure);
        let _ = tx.send(count);
    })
    .map_err(|error| error.to_string())?;

    let count = rx
        .await
        .map_err(|_| "liquid glass teardown task was cancelled".to_string())?;
    tracing::info!(
        target: "cap_desktop_lib::liquid_glass",
        windows = count,
        "Tore down liquid glass before exit"
    );
    Ok(())
}

// pub fn get_ns_window_number(ns_window: *mut c_void) -> isize {
//     let ns_window = ns_window as *const objc2_app_kit::NSWindow;

//     unsafe { (*ns_window).windowNumber() }
// }

// #[link(name = "CoreGraphics", kind = "framework")]
// unsafe extern "C" {
//     pub fn CGRectMakeWithDictionaryRepresentation(
//         dict: CFDictionaryRef,
//         rect: *mut CGRect,
//     ) -> boolean_t;
// }

// /// Makes the background of the WKWebView layer transparent.
// /// This differs from Tauri's implementation as it does not change the window background which causes performance performance issues and artifacts when shadows are enabled on the window.
// /// Use Tauri's implementation to make the window itself transparent.
// pub fn make_webview_transparent(target: &tauri::WebviewWindow) -> tauri::Result<()> {
//     target.with_webview(|webview| unsafe {
//         let wkwebview = webview.inner() as id;
//         let no: id = msg_send![class!(NSNumber), numberWithBool:0];
//         // [https://developer.apple.com/documentation/webkit/webview/1408486-drawsbackground]
//         let _: id = msg_send![wkwebview, setValue:no forKey: NSString::alloc(nil).init_str("drawsBackground")];
//     })
// }

#[cfg(test)]
mod tests {
    use super::{constrain_frame_to_visible_area, constrain_position_to_visible_top};
    use objc2_foundation::{NSPoint, NSRect, NSSize};
    use tauri::PhysicalPosition;

    fn rect(x: f64, y: f64, width: f64, height: f64) -> NSRect {
        NSRect::new(NSPoint::new(x, y), NSSize::new(width, height))
    }

    #[test]
    fn fits_the_actual_resized_frame_inside_the_work_area() {
        let screen = rect(0.0, 0.0, 1440.0, 900.0);
        let visible = rect(0.0, 24.0, 1440.0, 851.0);
        let constrained = constrain_frame_to_visible_area(
            rect(1300.0, 800.0, 330.0, 395.0),
            screen,
            visible,
            0.0,
        )
        .unwrap();

        assert_eq!(constrained, rect(1110.0, 480.0, 330.0, 395.0));
        assert_eq!(
            constrain_frame_to_visible_area(constrained, screen, visible, 0.0),
            None
        );
    }

    #[test]
    fn fits_expanded_frames_below_a_notch_with_a_hidden_menu_bar() {
        let screen = rect(0.0, 0.0, 1440.0, 900.0);
        assert_eq!(
            constrain_frame_to_visible_area(rect(100.0, 650.0, 480.0, 700.0), screen, screen, 37.0,),
            Some(rect(100.0, 163.0, 480.0, 700.0))
        );
    }

    #[test]
    fn keeps_native_coordinates_on_a_display_above_and_left_of_primary() {
        assert_eq!(
            constrain_frame_to_visible_area(
                rect(-2000.0, 1800.0, 480.0, 700.0),
                rect(-1920.0, 900.0, 1920.0, 1080.0),
                rect(-1880.0, 900.0, 1880.0, 1050.0),
                37.0,
            ),
            Some(rect(-1880.0, 1243.0, 480.0, 700.0))
        );
    }

    #[test]
    fn fits_an_oversized_restored_frame_without_negative_clamp_ranges() {
        let visible = rect(40.0, 24.0, 1400.0, 851.0);
        assert_eq!(
            constrain_frame_to_visible_area(
                rect(-100.0, -100.0, 2000.0, 1200.0),
                rect(0.0, 0.0, 1440.0, 900.0),
                visible,
                0.0,
            ),
            Some(visible)
        );
    }

    #[test]
    fn moves_a_window_below_a_notched_visible_frame() {
        let position = PhysicalPosition::new(640, 0);

        assert_eq!(
            constrain_position_to_visible_top(position, 0, 2.0, 19.0, 18.0),
            Some(PhysicalPosition::new(640, 38))
        );
    }

    #[test]
    fn uses_the_safe_area_when_the_menu_bar_is_hidden() {
        let position = PhysicalPosition::new(200, 0);

        assert_eq!(
            constrain_position_to_visible_top(position, 0, 2.0, 0.0, 37.0),
            Some(PhysicalPosition::new(200, 74))
        );
    }

    #[test]
    fn preserves_negative_coordinates_on_a_monitor_above_the_primary() {
        let position = PhysicalPosition::new(-600, -1800);

        assert_eq!(
            constrain_position_to_visible_top(position, -1800, 1.5, 25.0, 0.0),
            Some(PhysicalPosition::new(-600, -1762))
        );
    }

    #[test]
    fn leaves_an_accessible_position_unchanged() {
        let position = PhysicalPosition::new(300, 100);

        assert_eq!(
            constrain_position_to_visible_top(position, 0, 2.0, 19.0, 18.0),
            None
        );
    }
}