From 4d335a220f8ef52291ca1f61c82fc5df82b5863e Mon Sep 17 00:00:00 2001 From: Qwerasd Date: Sun, 4 Feb 2024 16:55:07 -0500 Subject: [PATCH] fix(macOS): Adjust custom toolbar title to fix clipping problems. --- .../Features/Terminal/TerminalToolbar.swift | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/macos/Sources/Features/Terminal/TerminalToolbar.swift b/macos/Sources/Features/Terminal/TerminalToolbar.swift index 5b4760642..ba73f3adc 100644 --- a/macos/Sources/Features/Terminal/TerminalToolbar.swift +++ b/macos/Sources/Features/Terminal/TerminalToolbar.swift @@ -4,7 +4,7 @@ import Cocoa // in order to accommodate the titlebar tabs feature. class TerminalToolbar: NSToolbar, NSToolbarDelegate { static private let identifier = NSToolbarItem.Identifier("TitleText") - private let titleTextField = NSTextField(labelWithString: "👻 Ghostty") + private let titleTextField = CenteredDynamicLabel(labelWithString: "👻 Ghostty") var titleText: String { get { @@ -34,8 +34,22 @@ class TerminalToolbar: NSToolbar, NSToolbarDelegate { guard itemIdentifier == Self.identifier else { return nil } let toolbarItem = NSToolbarItem(itemIdentifier: itemIdentifier) - toolbarItem.isEnabled = true toolbarItem.view = self.titleTextField + toolbarItem.visibilityPriority = .user + + // NSToolbarItem.minSize and NSToolbarItem.maxSize are deprecated, and make big ugly + // warnings in Xcode when you use them, but I cannot for the life of me figure out + // how to get this to work with constraints. The behavior isn't the same, instead of + // shrinking the item and clipping the subview, it hides the item as soon as the + // intrinsic size of the subview gets too big for the toolbar width, regardless of + // whether I have constraints set on its width, height, or both :/ + // + // If someone can fix this so we don't have to use deprecated properties: Please do. + toolbarItem.minSize = NSSize(width: 65, height: 1) + toolbarItem.maxSize = NSSize(width: 1024, height: self.titleTextField.intrinsicContentSize.height) + + toolbarItem.isEnabled = true + return toolbarItem } @@ -47,3 +61,15 @@ class TerminalToolbar: NSToolbar, NSToolbarDelegate { return [Self.identifier] } } + +class CenteredDynamicLabel: NSTextField { + override func viewDidMoveToSuperview() { + guard let superview = superview else { return } + widthAnchor.constraint(equalTo: superview.widthAnchor).isActive = true + centerXAnchor.constraint(equalTo: superview.centerXAnchor).isActive = true + + alignment = .center + + needsLayout = true + } +}