macos: terminal inspector control

This commit is contained in:
Mitchell Hashimoto
2023-10-19 20:48:38 -07:00
parent 29bbcbbf92
commit 5e2bed62b3
2 changed files with 57 additions and 8 deletions

View File

@ -10,14 +10,60 @@ extension Ghostty {
@ObservedObject var surfaceView: SurfaceView
var isSplit: Bool = false
// Maintain whether our view has focus or not
@FocusState private var inspectorFocus: Bool
var body: some View {
SplitView(.vertical, left: {
SurfaceWrapper(surfaceView: surfaceView, isSplit: isSplit)
}, right: {
InspectorViewRepresentable(surfaceView: surfaceView)
.focusedValue(\.ghosttySurfaceTitle, surfaceView.title)
.focusedValue(\.ghosttySurfaceView, surfaceView)
})
let center = NotificationCenter.default
let pubInspector = center.publisher(for: Notification.didControlInspector, object: surfaceView)
ZStack {
if (!surfaceView.inspectorVisible) {
SurfaceWrapper(surfaceView: surfaceView, isSplit: isSplit)
} else {
SplitView(.vertical, left: {
SurfaceWrapper(surfaceView: surfaceView, isSplit: isSplit)
}, right: {
InspectorViewRepresentable(surfaceView: surfaceView)
.focused($inspectorFocus)
.focusedValue(\.ghosttySurfaceTitle, surfaceView.title)
.focusedValue(\.ghosttySurfaceView, surfaceView)
})
}
}
.onReceive(pubInspector) { onControlInspector($0) }
}
private func onControlInspector(_ notification: SwiftUI.Notification) {
// Determine our mode
guard let modeAny = notification.userInfo?["mode"] else { return }
guard let mode = modeAny as? ghostty_inspector_mode_e else { return }
switch (mode) {
case GHOSTTY_INSPECTOR_TOGGLE:
surfaceView.inspectorVisible = !surfaceView.inspectorVisible
case GHOSTTY_INSPECTOR_SHOW:
surfaceView.inspectorVisible = true
case GHOSTTY_INSPECTOR_HIDE:
surfaceView.inspectorVisible = false
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)
}
}
}

View File

@ -41,7 +41,7 @@ extension Ghostty {
// True if this surface is part of a split view. This is important to know so
// we know whether to dim the surface out of focus.
var isSplit: Bool = false
// Maintain whether our view has focus or not
@FocusState private var surfaceFocus: Bool
@ -252,6 +252,9 @@ extension Ghostty {
return ghostty_surface_inspector(surface)
}
// True if the inspector should be visible
@Published var inspectorVisible: Bool = false
private(set) var surface: ghostty_surface_t?
var error: Error? = nil