From 2efe851cda266c38f3a2022db6738df72ce72140 Mon Sep 17 00:00:00 2001 From: Lukas <134181853+bo2themax@users.noreply.github.com> Date: Sun, 12 Apr 2026 12:12:49 +0200 Subject: [PATCH] macOS: move `readonly` to `OSSurfaceView` --- .../Ghostty/Surface View/OSSurfaceView.swift | 23 +++++++++++++++++++ .../Surface View/SurfaceView_AppKit.swift | 13 ----------- .../Surface View/SurfaceView_UIKit.swift | 3 --- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/macos/Sources/Ghostty/Surface View/OSSurfaceView.swift b/macos/Sources/Ghostty/Surface View/OSSurfaceView.swift index 628869f8b..d4d6b2d24 100644 --- a/macos/Sources/Ghostty/Surface View/OSSurfaceView.swift +++ b/macos/Sources/Ghostty/Surface View/OSSurfaceView.swift @@ -42,14 +42,37 @@ extension Ghostty { // structure because I'm lazy. @Published var surfaceSize: ghostty_surface_size_s? + /// True when the surface is in readonly mode. + @Published private(set) var readonly: Bool = false + init(id: UUID?, frame: CGRect) { self.id = id ?? UUID() super.init(frame: frame) + + // Before we initialize the surface we want to register our notifications + // so there is no window where we can't receive them. + let center = NotificationCenter.default + center.addObserver( + self, + selector: #selector(ghosttyDidChangeReadonly(_:)), + name: .ghosttyDidChangeReadonly, + object: self, + ) } required init?(coder: NSCoder) { fatalError("init(coder:) is not supported for this view") } + + deinit { + NotificationCenter.default + .removeObserver(self) + } + + @objc private func ghosttyDidChangeReadonly(_ notification: Foundation.Notification) { + guard let value = notification.userInfo?[Foundation.Notification.Name.ReadonlyKey] as? Bool else { return } + readonly = value + } } } diff --git a/macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift b/macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift index 9b81e1e39..6f060e0b1 100644 --- a/macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift +++ b/macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift @@ -99,9 +99,6 @@ extension Ghostty { /// True when the bell is active. This is set inactive on focus or event. @Published private(set) var bell: Bool = false - /// True when the surface is in readonly mode. - @Published private(set) var readonly: Bool = false - /// True when the surface should show a highlight effect (e.g., when presented via goto_split). @Published private(set) var highlighted: Bool = false @@ -324,11 +321,6 @@ extension Ghostty { selector: #selector(ghosttyBellDidRing(_:)), name: .ghosttyBellDidRing, object: self) - center.addObserver( - self, - selector: #selector(ghosttyDidChangeReadonly(_:)), - name: .ghosttyDidChangeReadonly, - object: self) center.addObserver( self, selector: #selector(windowDidChangeScreen), @@ -747,11 +739,6 @@ extension Ghostty { bell = true } - @objc private func ghosttyDidChangeReadonly(_ notification: SwiftUI.Notification) { - guard let value = notification.userInfo?[SwiftUI.Notification.Name.ReadonlyKey] as? Bool else { return } - readonly = value - } - @objc private func windowDidChangeScreen(notification: SwiftUI.Notification) { guard let window = self.window else { return } guard let object = notification.object as? NSWindow, window == object else { return } diff --git a/macos/Sources/Ghostty/Surface View/SurfaceView_UIKit.swift b/macos/Sources/Ghostty/Surface View/SurfaceView_UIKit.swift index e448b59e3..7c961d0f3 100644 --- a/macos/Sources/Ghostty/Surface View/SurfaceView_UIKit.swift +++ b/macos/Sources/Ghostty/Surface View/SurfaceView_UIKit.swift @@ -14,9 +14,6 @@ extension Ghostty { // The current search state. When non-nil, the search overlay should be shown. @Published var searchState: SearchState? - /// True when the surface is in readonly mode. - @Published private(set) var readonly: Bool = false - /// True when the surface should show a highlight effect (e.g., when presented via goto_split). @Published private(set) var highlighted: Bool = false