From 5e2bed62b304c2b9b6c1325adfa836b90715d34f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 19 Oct 2023 20:48:38 -0700 Subject: [PATCH] macos: terminal inspector control --- macos/Sources/Ghostty/InspectorView.swift | 60 ++++++++++++++++++++--- macos/Sources/Ghostty/SurfaceView.swift | 5 +- 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/macos/Sources/Ghostty/InspectorView.swift b/macos/Sources/Ghostty/InspectorView.swift index 97952352b..9f1590825 100644 --- a/macos/Sources/Ghostty/InspectorView.swift +++ b/macos/Sources/Ghostty/InspectorView.swift @@ -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) + } } } diff --git a/macos/Sources/Ghostty/SurfaceView.swift b/macos/Sources/Ghostty/SurfaceView.swift index e4a89ec2e..63596299c 100644 --- a/macos/Sources/Ghostty/SurfaceView.swift +++ b/macos/Sources/Ghostty/SurfaceView.swift @@ -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