From 4a93181b79e19b6537f0f4399606da6d69f43983 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 31 Jan 2024 15:43:35 -0800 Subject: [PATCH] macos: minor edits --- .../Terminal/TerminalController.swift | 6 ++- .../Features/Terminal/TerminalToolbar.swift | 45 ++++++++++--------- .../Features/Terminal/TerminalWindow.swift | 28 +++++------- 3 files changed, 41 insertions(+), 38 deletions(-) diff --git a/macos/Sources/Features/Terminal/TerminalController.swift b/macos/Sources/Features/Terminal/TerminalController.swift index ab739a88f..37d6a516b 100644 --- a/macos/Sources/Features/Terminal/TerminalController.swift +++ b/macos/Sources/Features/Terminal/TerminalController.swift @@ -484,11 +484,13 @@ class TerminalController: NSWindowController, NSWindowDelegate, func titleDidChange(to: String) { guard let window = window as? TerminalWindow else { return } + // Set the main window title window.title = to // Custom toolbar-based title used when titlebar tabs are enabled. - guard let toolbar = window.toolbar as? TerminalToolbar else { return } - toolbar.setTitleText(to) + if let toolbar = window.toolbar as? TerminalToolbar { + toolbar.titleText = to + } } func cellSizeDidChange(to: NSSize) { diff --git a/macos/Sources/Features/Terminal/TerminalToolbar.swift b/macos/Sources/Features/Terminal/TerminalToolbar.swift index 685db8b53..3f095294a 100644 --- a/macos/Sources/Features/Terminal/TerminalToolbar.swift +++ b/macos/Sources/Features/Terminal/TerminalToolbar.swift @@ -1,44 +1,49 @@ +import Cocoa + // Custom NSToolbar subclass that displays a centered window title, // in order to accommodate the titlebar tabs feature. - -import Foundation -import Cocoa -import SwiftUI - class TerminalToolbar: NSToolbar, NSToolbarDelegate { - static private let TitleIdentifier = NSToolbarItem.Identifier("TitleText") - private let TitleTextField = NSTextField( - labelWithString: "👻 Ghostty" - ) - - func setTitleText(_ text: String) { - self.TitleTextField.stringValue = text - } + static private let identifier = NSToolbarItem.Identifier("TitleText") + private let titleTextField = NSTextField(labelWithString: "👻 Ghostty") + + var titleText: String { + get { + titleTextField.stringValue + } + + set { + titleTextField.stringValue = newValue + } + } override init(identifier: NSToolbar.Identifier) { super.init(identifier: identifier) + delegate = self + if #available(macOS 13.0, *) { - centeredItemIdentifiers.insert(Self.TitleIdentifier) + centeredItemIdentifiers.insert(Self.identifier) } else { - centeredItemIdentifier = Self.TitleIdentifier + centeredItemIdentifier = Self.identifier } } - func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? { - guard itemIdentifier == Self.TitleIdentifier else { return nil } + func toolbar(_ toolbar: NSToolbar, + itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, + willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? { + guard itemIdentifier == Self.identifier else { return nil } let toolbarItem = NSToolbarItem(itemIdentifier: itemIdentifier) toolbarItem.isEnabled = true - toolbarItem.view = self.TitleTextField + toolbarItem.view = self.titleTextField return toolbarItem } func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] { - return [Self.TitleIdentifier] + return [Self.identifier] } func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] { - return [Self.TitleIdentifier] + return [Self.identifier] } } diff --git a/macos/Sources/Features/Terminal/TerminalWindow.swift b/macos/Sources/Features/Terminal/TerminalWindow.swift index af0d303e4..13ffa8be9 100644 --- a/macos/Sources/Features/Terminal/TerminalWindow.swift +++ b/macos/Sources/Features/Terminal/TerminalWindow.swift @@ -44,10 +44,21 @@ class TerminalWindow: NSWindow { if (self.toolbar == nil) { self.toolbar = TerminalToolbar(identifier: "Toolbar") } + // We directly hide the view containing the title text because if we use the // `titleVisibility` property for this it prevents the window from hiding the // tab bar when we get down to a single tab. - self.hideTitleText() + if let toolbarTitleView = contentView?.superview?.subviews.first(where: { + $0.className == "NSTitlebarContainerView" + })?.subviews.first(where: { + $0.className == "NSTitlebarView" + })?.subviews.first(where: { + $0.className == "NSToolbarView" + })?.subviews.first(where: { + $0.className == "NSToolbarTitleView" + }) { + toolbarTitleView.isHidden = true + } } else { // "expanded" places the toolbar below the titlebar, so setting this style and // removing the toolbar ensures that the titlebar will be the default height. @@ -66,21 +77,6 @@ class TerminalWindow: NSWindow { titlebarContainer.layer?.backgroundColor = color } - // Directly hide the view containing the title text - func hideTitleText() { - guard let toolbarTitleView = contentView?.superview?.subviews.first(where: { - $0.className == "NSTitlebarContainerView" - })?.subviews.first(where: { - $0.className == "NSTitlebarView" - })?.subviews.first(where: { - $0.className == "NSToolbarView" - })?.subviews.first(where: { - $0.className == "NSToolbarTitleView" - }) else { return } - - toolbarTitleView.isHidden = true - } - // This is called by macOS for native tabbing in order to add the tab bar. We hook into // this, detect the tab bar being added, and override its behavior. override func addTitlebarAccessoryViewController(_ childViewController: NSTitlebarAccessoryViewController) {