From 6854d09a8d48e78ea13ca832e148e09f2ddb7415 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 5 Mar 2023 13:41:35 -0800 Subject: [PATCH] macos: get rid of delegates on our surface view --- macos/Sources/TerminalSurface.swift | 40 ++--------------------------- macos/Sources/TerminalView.swift | 7 +++-- 2 files changed, 5 insertions(+), 42 deletions(-) diff --git a/macos/Sources/TerminalSurface.swift b/macos/Sources/TerminalSurface.swift index d1817988b..1064b0e9f 100644 --- a/macos/Sources/TerminalSurface.swift +++ b/macos/Sources/TerminalSurface.swift @@ -33,62 +33,26 @@ struct TerminalSurface: NSViewRepresentable { /// The best approach is to wrap this view in a GeometryReader and pass in the geo.size. let size: CGSize - /// This is set to the title of the surface as defined by the pty. Callers should use this to - /// set the appropriate title of the window/tab/split/etc. if they care. - @Binding var title: String - func makeNSView(context: Context) -> TerminalSurfaceView { // We need the view as part of the state to be created previously because // the view is sent to the Ghostty API so that it can manipulate it // directly since we draw on a render thread. - view.delegate = context.coordinator return view; } func updateNSView(_ view: TerminalSurfaceView, context: Context) { - view.delegate = context.coordinator view.focusDidChange(hasFocus) view.sizeDidChange(size) } - - func makeCoordinator() -> Coordinator { - return Coordinator(self) - } - - class Coordinator : TerminalSurfaceDelegate { - let view: TerminalSurface - - init(_ view: TerminalSurface) { - self.view = view - } - - func titleDidChange(to: String) { - view.title = to - } - } -} - -/// We use the delegate pattern to receive notifications about important state changes in the surface. -protocol TerminalSurfaceDelegate: AnyObject { - func titleDidChange(to: String) } /// The actual NSView implementation for the terminal surface. class TerminalSurfaceView: NSView, NSTextInputClient, ObservableObject { - // This can be set to receive event callbacks. - weak var delegate: TerminalSurfaceDelegate? - // The current title of the surface as defined by the pty. This can be // changed with escape codes. This is public because the callbacks go // to the app level and it is set from there. - @Published var title: String = "" { - didSet { - if let delegate = self.delegate { - delegate.titleDidChange(to: title) - } - } - } - + @Published var title: String = "" + private(set) var surface: ghostty_surface_t? var error: Error? = nil diff --git a/macos/Sources/TerminalView.swift b/macos/Sources/TerminalView.swift index 6db2ff349..4475aa70b 100644 --- a/macos/Sources/TerminalView.swift +++ b/macos/Sources/TerminalView.swift @@ -3,11 +3,10 @@ import GhosttyKit struct TerminalView: View { // The surface to create a view for - let surfaceView: TerminalSurfaceView + @ObservedObject var surfaceView: TerminalSurfaceView @FocusState private var surfaceFocus: Bool @Environment(\.isKeyWindow) private var isKeyWindow: Bool - @State private var title: String = "Ghostty" // 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. @@ -23,9 +22,9 @@ struct TerminalView: View { // is up to date. See TerminalSurfaceView for why we don't use the NSView // resize callback. GeometryReader { geo in - TerminalSurface(view: surfaceView, hasFocus: hasFocus, size: geo.size, title: $title) + TerminalSurface(view: surfaceView, hasFocus: hasFocus, size: geo.size) .focused($surfaceFocus) - .navigationTitle(title) + .navigationTitle(surfaceView.title) } } }