Phase 9, MA1: crates/ghostty-macos (AppKit via objc2; empty stub on non-Apple targets) ports the foundation of Ghostty 1.3.1's Swift macOS app (ghostty/macos): main, AppDelegate (+Ghostty) with lifecycle, dock menu, open-file, notifications, shortcut sync and quit/reopen paths; MainMenu.xib as generated menu tables built into NSMenus; the Ghostty.App/Config/Surface wrappers driving libghostty in-process via ghostty-lib; the generic helpers; scripts/macos-bundle.sh to assemble Ghostty.app (dry-run on Linux). Feature areas are stubbed for MA2-MA5. Design and type mapping: docs/rewrite/notes/macos-app-design.md (App Intents is a Swift-only framework and cannot be ported). Compile-checked only (no macOS here): just check-macos passes for aarch64/x86_64-apple-darwin with -D warnings; 125 platform-independent tests run on Linux. Test Plan: - just check-macos - cargo test -p ghostty-macos
128 lines
3.4 KiB
Rust
128 lines
3.4 KiB
Rust
//! The platform independent parts of `Helpers/Extensions/NSScreen+Extension.swift`
|
|
//! (and the Swift `NSScreenTests`).
|
|
|
|
use crate::helpers::geometry::{Point, Rect, Size};
|
|
|
|
/// `NSScreen.origin(fromTopLeftOffsetX:offsetY:windowSize:)`: converts
|
|
/// top-left offset coordinates to bottom-left origin coordinates for window
|
|
/// positioning, relative to the screen's `visibleFrame`.
|
|
#[must_use]
|
|
pub fn origin_from_top_left_offset(
|
|
visible_frame: Rect,
|
|
x: f64,
|
|
y: f64,
|
|
window_size: Size,
|
|
) -> Point {
|
|
// Convert top-left coordinates to bottom-left origin
|
|
Point {
|
|
x: visible_frame.min_x() + x,
|
|
y: visible_frame.max_y() - y - window_size.height,
|
|
}
|
|
}
|
|
|
|
/// The geometry part of `NSScreen.hasDock` (after the autohide default check).
|
|
#[must_use]
|
|
pub fn has_visible_dock(
|
|
visible_frame: Rect,
|
|
frame: Rect,
|
|
menu_height: f64,
|
|
notch_inset: f64,
|
|
) -> bool {
|
|
// If our visible width is less than the frame we assume its the dock.
|
|
if visible_frame.width() < frame.width() {
|
|
return true;
|
|
}
|
|
|
|
// We need to see if our visible frame height is less than the full
|
|
// screen height minus the menu and notch and such.
|
|
let boundary_area_padding = 5.0;
|
|
visible_frame.height() < (frame.height() - menu_height.max(notch_inset) - boundary_area_padding)
|
|
}
|
|
|
|
/// `NSScreen.hasNotch`: a top safe area inset is assumed to mean a notch.
|
|
#[must_use]
|
|
pub fn has_notch(safe_area_top: f64) -> bool {
|
|
safe_area_top > 0.0
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
const WIN: Size = Size {
|
|
width: 400.0,
|
|
height: 300.0,
|
|
};
|
|
|
|
#[test]
|
|
fn positive_coordinate_conversion() {
|
|
let o = origin_from_top_left_offset(Rect::new(0.0, 100.0, 1000.0, 800.0), 15.0, 15.0, WIN);
|
|
assert_eq!((o.x, o.y), (15.0, 585.0));
|
|
}
|
|
|
|
#[test]
|
|
fn zero_coordinates() {
|
|
let o = origin_from_top_left_offset(Rect::new(0.0, 100.0, 1000.0, 800.0), 0.0, 0.0, WIN);
|
|
assert_eq!((o.x, o.y), (0.0, 600.0));
|
|
}
|
|
|
|
#[test]
|
|
fn offset_screen() {
|
|
let o = origin_from_top_left_offset(
|
|
Rect::new(1440.0, 0.0, 1920.0, 1080.0),
|
|
100.0,
|
|
50.0,
|
|
Size {
|
|
width: 600.0,
|
|
height: 400.0,
|
|
},
|
|
);
|
|
assert_eq!((o.x, o.y), (1540.0, 630.0));
|
|
}
|
|
|
|
#[test]
|
|
fn large_coordinates() {
|
|
let o = origin_from_top_left_offset(Rect::new(0.0, 0.0, 1920.0, 1080.0), 500.0, 200.0, WIN);
|
|
assert_eq!((o.x, o.y), (500.0, 580.0));
|
|
}
|
|
|
|
#[test]
|
|
fn dock_detection() {
|
|
let frame = Rect::new(0.0, 0.0, 1000.0, 800.0);
|
|
// side dock
|
|
assert!(has_visible_dock(
|
|
Rect::new(70.0, 0.0, 930.0, 776.0),
|
|
frame,
|
|
24.0,
|
|
0.0
|
|
));
|
|
// bottom dock
|
|
assert!(has_visible_dock(
|
|
Rect::new(0.0, 70.0, 1000.0, 706.0),
|
|
frame,
|
|
24.0,
|
|
0.0
|
|
));
|
|
// no dock, only menu bar
|
|
assert!(!has_visible_dock(
|
|
Rect::new(0.0, 0.0, 1000.0, 776.0),
|
|
frame,
|
|
24.0,
|
|
0.0
|
|
));
|
|
// notch inset larger than the menu
|
|
assert!(!has_visible_dock(
|
|
Rect::new(0.0, 0.0, 1000.0, 760.0),
|
|
frame,
|
|
24.0,
|
|
38.0
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn notch() {
|
|
assert!(has_notch(32.0));
|
|
assert!(!has_notch(0.0));
|
|
}
|
|
}
|