Move title change timer to SurfaceView and call it from Ghostty.App instead of terminal controllers

This commit is contained in:
Pranav Mangal
2024-12-12 14:58:42 +05:30
parent 146b1f2a1b
commit e35bd431f4
4 changed files with 20 additions and 25 deletions

View File

@ -54,10 +54,6 @@ class BaseTerminalController: NSWindowController,
/// Fullscreen state management. /// Fullscreen state management.
private(set) var fullscreenStyle: FullscreenStyle? private(set) var fullscreenStyle: FullscreenStyle?
var titleChangeDelay: TimeInterval = 0.075
private var titleChangeTimer: Timer?
/// Event monitor (see individual events for why) /// Event monitor (see individual events for why)
private var eventMonitor: Any? = nil private var eventMonitor: Any? = nil
@ -264,12 +260,9 @@ class BaseTerminalController: NSWindowController,
func titleDidChange(to: String) { func titleDidChange(to: String) {
guard let window else { return } guard let window else { return }
titleChangeTimer?.invalidate() // Set the main window title
// Set the main window title after a small delay to prevent flicker
titleChangeTimer = Timer.scheduledTimer(withTimeInterval: titleChangeDelay, repeats: false) { _ in
window.title = to window.title = to
}
} }
func pwdDidChange(to: URL?) { func pwdDidChange(to: URL?) {

View File

@ -27,8 +27,6 @@ class TerminalController: BaseTerminalController {
/// The notification cancellable for focused surface property changes. /// The notification cancellable for focused surface property changes.
private var surfaceAppearanceCancellables: Set<AnyCancellable> = [] private var surfaceAppearanceCancellables: Set<AnyCancellable> = []
private var toolbarTitleChangeTimer: Timer?
init(_ ghostty: Ghostty.App, init(_ ghostty: Ghostty.App,
withBaseConfig base: Ghostty.SurfaceConfiguration? = nil, withBaseConfig base: Ghostty.SurfaceConfiguration? = nil,
withSurfaceTree tree: Ghostty.SplitNode? = nil withSurfaceTree tree: Ghostty.SplitNode? = nil
@ -548,15 +546,9 @@ class TerminalController: BaseTerminalController {
// a custom view instead, we need to re-hide it. // a custom view instead, we need to re-hide it.
window.titleVisibility = .hidden 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
} }
} }
}
override func surfaceTreeDidChange() { override func surfaceTreeDidChange() {
// Whenever our surface tree changes in any way (new split, close split, etc.) // Whenever our surface tree changes in any way (new split, close split, etc.)

View File

@ -948,13 +948,19 @@ extension Ghostty {
guard let surfaceView = self.surfaceView(from: surface) else { return } guard let surfaceView = self.surfaceView(from: surface) else { return }
guard let title = String(cString: v.title!, encoding: .utf8) else { return } guard let title = String(cString: v.title!, encoding: .utf8) else { return }
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 // 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 // versions of macOS. I unfortunately didn't document the exact versions so
// I don't know when its safe to remove this. // I don't know when its safe to remove this.
DispatchQueue.main.async { DispatchQueue.main.async {
surfaceView.title = title surfaceView.title = title
} }
}
default: default:
assertionFailure() assertionFailure()

View File

@ -14,6 +14,10 @@ extension Ghostty {
// to the app level and it is set from there. // to the app level and it is set from there.
@Published var title: String = "👻" @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 // The current pwd of the surface as defined by the pty. This can be
// changed with escape codes. // changed with escape codes.
@Published var pwd: String? = nil @Published var pwd: String? = nil