Support macOS Sequoia titlebar double-click action (#30468)

Closes #16527

Release Notes:

- Added MacOS titlebar double-click action

---

Unfortunately, Apple doesn't seem to make the "Fill" API public or
documented anywhere.

Co-authored-by: Mikayla Maki <mikayla@zed.dev>
This commit is contained in:
Chung Wei Leong 2025-05-31 01:13:50 +08:00 committed by GitHub
parent f9f4be1fc4
commit 0ee900e8fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 59 additions and 1 deletions

View file

@ -19,6 +19,7 @@ use cocoa::{
foundation::{
NSArray, NSAutoreleasePool, NSDictionary, NSFastEnumeration, NSInteger, NSNotFound,
NSOperatingSystemVersion, NSPoint, NSProcessInfo, NSRect, NSSize, NSString, NSUInteger,
NSUserDefaults,
},
};
use core_graphics::display::{CGDirectDisplayID, CGPoint, CGRect};
@ -1177,6 +1178,49 @@ impl PlatformWindow for MacWindow {
})
.detach()
}
fn titlebar_double_click(&self) {
let this = self.0.lock();
let window = this.native_window;
this.executor
.spawn(async move {
unsafe {
let defaults: id = NSUserDefaults::standardUserDefaults();
let domain = NSString::alloc(nil).init_str("NSGlobalDomain");
let key = NSString::alloc(nil).init_str("AppleActionOnDoubleClick");
let dict: id = msg_send![defaults, persistentDomainForName: domain];
let action: id = if !dict.is_null() {
msg_send![dict, objectForKey: key]
} else {
nil
};
let action_str = if !action.is_null() {
CStr::from_ptr(NSString::UTF8String(action)).to_string_lossy()
} else {
"".into()
};
match action_str.as_ref() {
"Minimize" => {
window.miniaturize_(nil);
}
"Maximize" => {
window.zoom_(nil);
}
"Fill" => {
// There is no documented API for "Fill" action, so we'll just zoom the window
window.zoom_(nil);
}
_ => {
window.zoom_(nil);
}
}
}
})
.detach();
}
}
impl rwh::HasWindowHandle for MacWindow {