From 582df33083cb56d9a6c5dce85a81d3026d672bcc Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Sun, 5 Nov 2023 20:09:38 -0600 Subject: [PATCH] macos: use Ghostty.AppState as @EnvironmentObject This is a small cleanup change. This provides the Ghostty.AppState object to any view that needs it within the TerminalSplit view hierarchy without needing to explicitly define EnvironmentKeys. --- .../Features/Terminal/TerminalView.swift | 2 -- macos/Sources/Ghostty/AppState.swift | 32 ------------------- macos/Sources/Ghostty/SurfaceView.swift | 8 ++--- 3 files changed, 4 insertions(+), 38 deletions(-) diff --git a/macos/Sources/Features/Terminal/TerminalView.swift b/macos/Sources/Features/Terminal/TerminalView.swift index 6da810690..7596c343a 100644 --- a/macos/Sources/Features/Terminal/TerminalView.swift +++ b/macos/Sources/Features/Terminal/TerminalView.swift @@ -85,8 +85,6 @@ struct TerminalView: View { Ghostty.TerminalSplit(node: $viewModel.surfaceTree) .environmentObject(ghostty) - .ghosttyApp(ghostty.app!) - .ghosttyConfig(ghostty.config!) .focused($focused) .onAppear { self.focused = true } .onChange(of: focusedSurface) { newValue in diff --git a/macos/Sources/Ghostty/AppState.swift b/macos/Sources/Ghostty/AppState.swift index d966d6900..c4e5cc2b7 100644 --- a/macos/Sources/Ghostty/AppState.swift +++ b/macos/Sources/Ghostty/AppState.swift @@ -582,35 +582,3 @@ extension Ghostty { } } } - -// MARK: AppState Environment Keys - -private struct GhosttyAppKey: EnvironmentKey { - static let defaultValue: ghostty_app_t? = nil -} - -private struct GhosttyConfigKey: EnvironmentKey { - static let defaultValue: ghostty_config_t? = nil -} - -extension EnvironmentValues { - var ghosttyApp: ghostty_app_t? { - get { self[GhosttyAppKey.self] } - set { self[GhosttyAppKey.self] = newValue } - } - - var ghosttyConfig: ghostty_config_t? { - get { self[GhosttyConfigKey.self] } - set { self[GhosttyConfigKey.self] = newValue } - } -} - -extension View { - func ghosttyApp(_ app: ghostty_app_t?) -> some View { - environment(\.ghosttyApp, app) - } - - func ghosttyConfig(_ config: ghostty_config_t?) -> some View { - environment(\.ghosttyConfig, config) - } -} diff --git a/macos/Sources/Ghostty/SurfaceView.swift b/macos/Sources/Ghostty/SurfaceView.swift index 97a347aab..d14602c5d 100644 --- a/macos/Sources/Ghostty/SurfaceView.swift +++ b/macos/Sources/Ghostty/SurfaceView.swift @@ -4,11 +4,11 @@ import GhosttyKit extension Ghostty { /// Render a terminal for the active app in the environment. struct Terminal: View { - @Environment(\.ghosttyApp) private var app + @EnvironmentObject private var ghostty: Ghostty.AppState @FocusedValue(\.ghosttySurfaceTitle) private var surfaceTitle: String? var body: some View { - if let app = self.app { + if let app = self.ghostty.app { SurfaceForApp(app) { surfaceView in SurfaceWrapper(surfaceView: surfaceView) } @@ -48,7 +48,7 @@ extension Ghostty { // Maintain whether our window has focus (is key) or not @State private var windowFocus: Bool = true - @Environment(\.ghosttyConfig) private var ghostty_config + @EnvironmentObject private var ghostty: Ghostty.AppState // This is true if the terminal is considered "focused". The terminal is focused if // it is both individually focused and the containing window is key. @@ -58,7 +58,7 @@ extension Ghostty { private var unfocusedOpacity: Double { var opacity: Double = 0.85 let key = "unfocused-split-opacity" - _ = ghostty_config_get(ghostty_config, &opacity, key, UInt(key.count)) + _ = ghostty_config_get(ghostty.config, &opacity, key, UInt(key.count)) return 1 - opacity }