Merge pull request #384 from mitchellh/macos12-split-focus

macos: hacks for split focus to work correctly on macos 12
This commit is contained in:
Mitchell Hashimoto
2023-09-02 09:18:51 -07:00
committed by GitHub
3 changed files with 30 additions and 2 deletions

View File

@ -322,6 +322,19 @@ extension Ghostty {
if previous != view {
_ = previous.resignFirstResponder()
}
// On newer versions of macOS everything above works great so we're done.
if #available(macOS 13, *) { return }
// On macOS 12, splits do not properly gain focus. I don't know why, but
// it seems like the `focused` SwiftUI method doesn't work. We use
// NotificationCenter as a blunt force instrument to make it work.
if #available(macOS 12, *) {
NotificationCenter.default.post(
name: Notification.didBecomeFocusedSurface,
object: view
)
}
}
}
}

View File

@ -91,6 +91,10 @@ extension Ghostty.Notification {
/// Toggle fullscreen of current window
static let ghosttyToggleFullscreen = Notification.Name("com.mitchellh.ghostty.toggleFullscreen")
static let NonNativeFullscreenKey = ghosttyToggleFullscreen.rawValue
/// Notification that a surface is becoming focused. This is only sent on macOS 12 to
/// work around bugs. macOS 13+ should use the ".focused()" attribute.
static let didBecomeFocusedSurface = Notification.Name("com.mitchellh.ghostty.didBecomeFocusedSurface")
}
// Make the input enum hashable.

View File

@ -54,7 +54,8 @@ extension Ghostty {
// resize callback.
GeometryReader { geo in
// We use these notifications to determine when the window our surface is
// attached to is or is not focused.
// attached to is or is not focused.
let pubBecomeFocused = NotificationCenter.default.publisher(for: Notification.didBecomeFocusedSurface, object: surfaceView)
let pubBecomeKey = NotificationCenter.default.publisher(for: NSWindow.didBecomeKeyNotification)
let pubResign = NotificationCenter.default.publisher(for: NSWindow.didResignKeyNotification)
@ -74,6 +75,16 @@ extension Ghostty {
windowFocus = false
}
}
.onReceive(pubBecomeFocused) { notification in
// We only want to run this on older macOS versions where the .focused
// method doesn't work properly. See the dispatch of this notification
// for more information.
if #available(macOS 13, *) { return }
DispatchQueue.main.async {
surfaceFocus = true
}
}
.onAppear() {
// Welcome to the SwiftUI bug house of horrors. On macOS 12 (at least
// 12.5.1, didn't test other versions), the order in which the view
@ -119,7 +130,7 @@ extension Ghostty {
/// The view to render for the terminal surface.
let view: SurfaceView
/// This should be set to true wen the surface has focus. This is up to the parent because
/// This should be set to true when the surface has focus. This is up to the parent because
/// focus is also defined by window focus. It is important this is set correctly since if it is
/// false then the surface will idle at almost 0% CPU.
let hasFocus: Bool