macos: minor edits

This commit is contained in:
Mitchell Hashimoto
2024-01-31 15:43:35 -08:00
parent 308f8cce36
commit 4a93181b79
3 changed files with 41 additions and 38 deletions

View File

@ -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) {

View File

@ -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]
}
}

View File

@ -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) {