Merge pull request #2466 from johnseth97/NSToolbar-Deprication-Warning-Fix

Ns-toolbar depreciation warning fix
This commit is contained in:
Mitchell Hashimoto
2024-10-21 08:57:38 -04:00
committed by GitHub

View File

@ -43,17 +43,17 @@ class TerminalToolbar: NSToolbar, NSToolbarDelegate {
item.view = self.titleTextField item.view = self.titleTextField
item.visibilityPriority = .user item.visibilityPriority = .user
// NSToolbarItem.minSize and NSToolbarItem.maxSize are deprecated, and make big ugly // This ensures the title text field doesn't disappear when shrinking the view
// warnings in Xcode when you use them, but I cannot for the life of me figure out self.titleTextField.translatesAutoresizingMaskIntoConstraints = false
// how to get this to work with constraints. The behavior isn't the same, instead of self.titleTextField.setContentHuggingPriority(.defaultLow, for: .horizontal)
// shrinking the item and clipping the subview, it hides the item as soon as the self.titleTextField.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
// 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 :/ // Add constraints to the toolbar item's view
// NSLayoutConstraint.activate([
// If someone can fix this so we don't have to use deprecated properties: Please do. // Set the height constraint to match the toolbar's height
item.minSize = NSSize(width: 32, height: 1) self.titleTextField.heightAnchor.constraint(equalToConstant: 22), // Adjust as needed
item.maxSize = NSSize(width: 1024, height: self.titleTextField.intrinsicContentSize.height) ])
item.isEnabled = true item.isEnabled = true
case .resetZoom: case .resetZoom:
item = NSToolbarItem(itemIdentifier: .resetZoom) item = NSToolbarItem(itemIdentifier: .resetZoom)
@ -73,23 +73,27 @@ class TerminalToolbar: NSToolbar, NSToolbarDelegate {
// getting smaller than the max size so starts clipping. Lucky for us, two of the // getting smaller than the max size so starts clipping. Lucky for us, two of the
// built-in spacers plus the un-zoom button item seems to exactly match the space // built-in spacers plus the un-zoom button item seems to exactly match the space
// on the left that's reserved for the window buttons. // on the left that's reserved for the window buttons.
return [.titleText, .flexibleSpace, .space, .space, .resetZoom] return [.flexibleSpace, .titleText, .flexibleSpace]
} }
} }
/// A label that expands to fit whatever text you put in it and horizontally centers itself in the current window. /// A label that expands to fit whatever text you put in it and horizontally centers itself in the current window.
fileprivate class CenteredDynamicLabel: NSTextField { fileprivate class CenteredDynamicLabel: NSTextField {
override func viewDidMoveToSuperview() { override func viewDidMoveToSuperview() {
// Truncate the title when it gets too long, cutting it off with an ellipsis. // Configure the text field
isEditable = false
isBordered = false
drawsBackground = false
alignment = .center
lineBreakMode = .byTruncatingTail
cell?.truncatesLastVisibleLine = true cell?.truncatesLastVisibleLine = true
cell?.lineBreakMode = .byCharWrapping
// Use Auto Layout
// Make the text field as small as possible while fitting its text. translatesAutoresizingMaskIntoConstraints = false
setContentHuggingPriority(.required, for: .horizontal)
cell?.alignment = .center // Set content hugging and compression resistance priorities
setContentHuggingPriority(.defaultLow, for: .horizontal)
// We've changed some alignment settings, make sure the layout is updated immediately. setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
needsLayout = true
} }
} }