macos: get rid of delegates on our surface view

This commit is contained in:
Mitchell Hashimoto
2023-03-05 13:41:35 -08:00
parent 1fbbdd3fc7
commit 6854d09a8d
2 changed files with 5 additions and 42 deletions

View File

@ -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

View File

@ -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)
}
}
}