From 8d395c094b353cdb86032cc6b43cd263da39e827 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 2 Mar 2025 13:26:21 -0800 Subject: [PATCH] macos: set title of terminal window immediately if configured Fixes #5934 for macOS If a `title` config is set, this change sets the title immediately on windowDidLoad to ensure that the window appears with the correct title right away. If there is any reason to set another title, the `set_title` apprt action will come on another event loop tick (due to our usage of notifications) but that's okay since that's already how it works. This is just to say that setting this here won't break any shell integration or anything. --- .../Features/Terminal/BaseTerminalController.swift | 11 +++++++++++ macos/Sources/Ghostty/Ghostty.Config.swift | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/macos/Sources/Features/Terminal/BaseTerminalController.swift b/macos/Sources/Features/Terminal/BaseTerminalController.swift index edf3662b9..3b4b1a2ef 100644 --- a/macos/Sources/Features/Terminal/BaseTerminalController.swift +++ b/macos/Sources/Features/Terminal/BaseTerminalController.swift @@ -413,6 +413,14 @@ class BaseTerminalController: NSWindowController, override func windowDidLoad() { guard let window else { return } + // If there is a hardcoded title in the configuration, we set that + // immediately. Future `set_title` apprt actions will override this + // if necessary but this ensures our window loads with the proper + // title immediately rather than on another event loop tick (see #5934) + if let title = derivedConfig.title { + window.title = title + } + // We always initialize our fullscreen style to native if we can because // initialization sets up some state (i.e. observers). If its set already // somehow we don't do this. @@ -617,17 +625,20 @@ class BaseTerminalController: NSWindowController, } private struct DerivedConfig { + let title: String? let macosTitlebarProxyIcon: Ghostty.MacOSTitlebarProxyIcon let windowStepResize: Bool let focusFollowsMouse: Bool init() { + self.title = nil self.macosTitlebarProxyIcon = .visible self.windowStepResize = false self.focusFollowsMouse = false } init(_ config: Ghostty.Config) { + self.title = config.title self.macosTitlebarProxyIcon = config.macosTitlebarProxyIcon self.windowStepResize = config.windowStepResize self.focusFollowsMouse = config.focusFollowsMouse diff --git a/macos/Sources/Ghostty/Ghostty.Config.swift b/macos/Sources/Ghostty/Ghostty.Config.swift index 10e5446cc..20a43aa2b 100644 --- a/macos/Sources/Ghostty/Ghostty.Config.swift +++ b/macos/Sources/Ghostty/Ghostty.Config.swift @@ -132,6 +132,15 @@ extension Ghostty { return v } + var title: String? { + guard let config = self.config else { return nil } + var v: UnsafePointer? = nil + let key = "title" + guard ghostty_config_get(config, &v, key, UInt(key.count)) else { return nil } + guard let ptr = v else { return nil } + return String(cString: ptr) + } + var windowSaveState: String { guard let config = self.config else { return "" } var v: UnsafePointer? = nil