From e35bd431f4245a42c35935d8e8c74c59dfd40b28 Mon Sep 17 00:00:00 2001 From: Pranav Mangal Date: Thu, 12 Dec 2024 14:58:42 +0530 Subject: [PATCH] Move title change timer to SurfaceView and call it from Ghostty.App instead of terminal controllers --- .../Terminal/BaseTerminalController.swift | 11 ++-------- .../Terminal/TerminalController.swift | 10 +--------- macos/Sources/Ghostty/Ghostty.App.swift | 20 ++++++++++++------- .../Sources/Ghostty/SurfaceView_AppKit.swift | 4 ++++ 4 files changed, 20 insertions(+), 25 deletions(-) diff --git a/macos/Sources/Features/Terminal/BaseTerminalController.swift b/macos/Sources/Features/Terminal/BaseTerminalController.swift index 5f4f357b6..68c243004 100644 --- a/macos/Sources/Features/Terminal/BaseTerminalController.swift +++ b/macos/Sources/Features/Terminal/BaseTerminalController.swift @@ -53,10 +53,6 @@ class BaseTerminalController: NSWindowController, /// Fullscreen state management. private(set) var fullscreenStyle: FullscreenStyle? - - var titleChangeDelay: TimeInterval = 0.075 - - private var titleChangeTimer: Timer? /// Event monitor (see individual events for why) private var eventMonitor: Any? = nil @@ -264,12 +260,9 @@ class BaseTerminalController: NSWindowController, func titleDidChange(to: String) { guard let window else { return } - titleChangeTimer?.invalidate() + // Set the main window title + window.title = to - // Set the main window title after a small delay to prevent flicker - titleChangeTimer = Timer.scheduledTimer(withTimeInterval: titleChangeDelay, repeats: false) { _ in - window.title = to - } } func pwdDidChange(to: URL?) { diff --git a/macos/Sources/Features/Terminal/TerminalController.swift b/macos/Sources/Features/Terminal/TerminalController.swift index edb797890..67e7259f3 100644 --- a/macos/Sources/Features/Terminal/TerminalController.swift +++ b/macos/Sources/Features/Terminal/TerminalController.swift @@ -26,8 +26,6 @@ class TerminalController: BaseTerminalController { /// The notification cancellable for focused surface property changes. private var surfaceAppearanceCancellables: Set = [] - - private var toolbarTitleChangeTimer: Timer? init(_ ghostty: Ghostty.App, withBaseConfig base: Ghostty.SurfaceConfiguration? = nil, @@ -548,13 +546,7 @@ class TerminalController: BaseTerminalController { // a custom view instead, we need to re-hide it. window.titleVisibility = .hidden } - - toolbarTitleChangeTimer?.invalidate() - - // Set the toolbar title after a small delay to prevent flicker - toolbarTitleChangeTimer = Timer.scheduledTimer(withTimeInterval: titleChangeDelay, repeats: false) { _ in - toolbar.titleText = to - } + toolbar.titleText = to } } diff --git a/macos/Sources/Ghostty/Ghostty.App.swift b/macos/Sources/Ghostty/Ghostty.App.swift index 9056e692a..c8e3cc476 100644 --- a/macos/Sources/Ghostty/Ghostty.App.swift +++ b/macos/Sources/Ghostty/Ghostty.App.swift @@ -947,15 +947,21 @@ extension Ghostty { guard let surface = target.target.surface else { return } guard let surfaceView = self.surfaceView(from: surface) else { return } guard let title = String(cString: v.title!, encoding: .utf8) else { return } - - // We must set this in a dispatchqueue to avoid a deadlock on startup on some - // versions of macOS. I unfortunately didn't document the exact versions so - // I don't know when its safe to remove this. - DispatchQueue.main.async { - surfaceView.title = title + + surfaceView.titleChangeTimer?.invalidate() + + surfaceView.titleChangeTimer = Timer.scheduledTimer( + withTimeInterval: surfaceView.titleChangeDelay, + repeats: false + ) { _ in + // We must set this in a dispatchqueue to avoid a deadlock on startup on some + // versions of macOS. I unfortunately didn't document the exact versions so + // I don't know when its safe to remove this. + DispatchQueue.main.async { + surfaceView.title = title + } } - default: assertionFailure() } diff --git a/macos/Sources/Ghostty/SurfaceView_AppKit.swift b/macos/Sources/Ghostty/SurfaceView_AppKit.swift index 7e861a229..c426e9e07 100644 --- a/macos/Sources/Ghostty/SurfaceView_AppKit.swift +++ b/macos/Sources/Ghostty/SurfaceView_AppKit.swift @@ -13,6 +13,10 @@ extension Ghostty { // 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 = "👻" + + // A small delay that is introduced before a title change to avoid flickers + var titleChangeDelay: TimeInterval = 0.075 + var titleChangeTimer: Timer? // The current pwd of the surface as defined by the pty. This can be // changed with escape codes.