macos: move title setting into a function to better encapsulate

This commit is contained in:
Mitchell Hashimoto
2024-12-12 16:43:10 -08:00
parent e35bd431f4
commit 4fdf5eb12b
2 changed files with 23 additions and 20 deletions

View File

@@ -12,11 +12,7 @@ extension Ghostty {
// 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 = "👻"
// A small delay that is introduced before a title change to avoid flickers
var titleChangeDelay: TimeInterval = 0.075
var titleChangeTimer: Timer?
@Published private(set) var title: String = "👻"
// The current pwd of the surface as defined by the pty. This can be
// changed with escape codes.
@@ -114,6 +110,9 @@ extension Ghostty {
// This is set to non-null during keyDown to accumulate insertText contents
private var keyTextAccumulator: [String]? = nil
// A small delay that is introduced before a title change to avoid flickers
private var titleChangeTimer: Timer?
// We need to support being a first responder so that we can get input events
override var acceptsFirstResponder: Bool { return true }
@@ -343,6 +342,20 @@ extension Ghostty {
NSCursor.setHiddenUntilMouseMoves(!visible)
}
func setTitle(_ title: String) {
// This fixes an issue where very quick changes to the title could
// cause an unpleasant flickering. We set a timer so that we can
// coalesce rapid changes. The timer is short enough that it still
// feels "instant".
titleChangeTimer?.invalidate()
titleChangeTimer = Timer.scheduledTimer(
withTimeInterval: 0.075,
repeats: false
) { [weak self] _ in
self?.title = title
}
}
// MARK: - Notifications
@objc private func onUpdateRendererHealth(notification: SwiftUI.Notification) {