macos: regain window focus on inspector toggle

Fixes #734

I don't know much about SwiftUI, but here's why I think this works.

- Moving the `inspectorVisible` logic to an `onChange` ensures the view
  has at least seen that change.
- The dispatch to the main thread is still necessary to ensure the view
  hierarchy has completely updated after `inspectorVisible`.

Note that this fix is to correctly regain focus. We still lose window
focus very briefly.
This commit is contained in:
Robbie Vanbrabant
2024-06-14 18:52:27 +01:00
parent 4e4b6ea6de
commit 693a599084

View File

@ -37,6 +37,19 @@ extension Ghostty {
}
}
.onReceive(pubInspector) { onControlInspector($0) }
.onChange(of: surfaceView.inspectorVisible) { inspectorVisible in
// When we show the inspector, we want to focus on the inspector.
// When we hide the inspector, we want to move focus back to the surface.
if (inspectorVisible) {
// We need to delay this until SwiftUI shows the inspector.
DispatchQueue.main.async {
_ = surfaceView.resignFirstResponder()
inspectorFocus = true
}
} else {
Ghostty.moveFocus(to: surfaceView)
}
}
}
private func onControlInspector(_ notification: SwiftUI.Notification) {
@ -57,18 +70,6 @@ extension Ghostty {
default:
return
}
// When we show the inspector, we want to focus on the inspector.
// When we hide the inspector, we want to move focus back to the surface.
if (surfaceView.inspectorVisible) {
// We need to delay this until SwiftUI shows the inspector.
DispatchQueue.main.async {
_ = surfaceView.resignFirstResponder()
inspectorFocus = true
}
} else {
Ghostty.moveFocus(to: surfaceView)
}
}
}