macos: prevent moveFocus from being an infinite loop (#2905)

Fixes #2900

It's possible for moveFocus to infinite loop if the surface view we're
trying to move focus to NEVER gets attached to a window. This can happen
if the window is destroyed.

I think this issue should be more systemically fixed so it can't happen
but this workaround for now prevents moveFocus from being an infinite
loop source for the time being.
This commit is contained in:
Mitchell Hashimoto
2024-12-08 11:15:06 -08:00
committed by GitHub

View File

@ -429,12 +429,34 @@ extension Ghostty {
/// will lose focus. There has to be some nice SwiftUI-native way to fix this but I can't /// will lose focus. There has to be some nice SwiftUI-native way to fix this but I can't
/// figure it out so we're going to do this hacky thing to bring focus back to the terminal /// figure it out so we're going to do this hacky thing to bring focus back to the terminal
/// that should have it. /// that should have it.
static func moveFocus(to: SurfaceView, from: SurfaceView? = nil) { static func moveFocus(
DispatchQueue.main.async { to: SurfaceView,
from: SurfaceView? = nil,
delay: TimeInterval? = nil
) {
// The whole delay machinery is a bit of a hack to work around a
// situation where the window is destroyed and the surface view
// will never be attached to a window. Realistically, we should
// handle this upstream but we also don't want this function to be
// a source of infinite loops.
// Our max delay before we give up
let maxDelay: TimeInterval = 0.5
guard (delay ?? 0) < maxDelay else { return }
// We start at a 50 millisecond delay and do a doubling backoff
let nextDelay: TimeInterval = if let delay {
delay * 2
} else {
// 100 milliseconds
0.05
}
let work: DispatchWorkItem = .init {
// If the callback runs before the surface is attached to a view // If the callback runs before the surface is attached to a view
// then the window will be nil. We just reschedule in that case. // then the window will be nil. We just reschedule in that case.
guard let window = to.window else { guard let window = to.window else {
moveFocus(to: to, from: from) moveFocus(to: to, from: from, delay: nextDelay)
return return
} }
@ -448,5 +470,12 @@ extension Ghostty {
window.makeFirstResponder(to) window.makeFirstResponder(to)
} }
let queue = DispatchQueue.main
if let delay {
queue.asyncAfter(deadline: .now() + delay, execute: work)
} else {
queue.async(execute: work)
}
} }
} }