From 20ed8277504fdd5556fe2173e6f57270226a362c Mon Sep 17 00:00:00 2001 From: Serg Date: Wed, 9 May 2018 12:23:02 +0700 Subject: [PATCH] fix that borderless buttons in scrollable area were not centered, fix wrong text color for borderless colored buttons --- MTMR/CustomButtonTouchBarItem.swift | 45 +++++++++++++++++++++++++---- MTMR/TouchBarController.swift | 12 ++------ 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/MTMR/CustomButtonTouchBarItem.swift b/MTMR/CustomButtonTouchBarItem.swift index a56a44c..c3ede21 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -21,12 +21,7 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat self.longTapClosure = callbackLong super.init(identifier: identifier) - button = NSButton(title: title, target: nil, action: nil) - button.cell = CustomButtonCell() - button.isBordered = true - button.bezelStyle = .rounded - button.title = title - self.view = button + installButton(titled: title, bordered: true, backgroundColor: nil) longClick = NSPressGestureRecognizer(target: self, action: #selector(handleGestureLong)) longClick.allowedTouchTypes = .direct @@ -44,6 +39,33 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat fatalError("init(coder:) has not been implemented") } + var isBordered: Bool = true { + didSet { + installButton(titled: self.button.title, bordered: isBordered, backgroundColor: backgroundColor) + } + } + + var backgroundColor: NSColor? { + didSet { + installButton(titled: self.button.title, bordered: isBordered, backgroundColor: backgroundColor) + } + } + + private func installButton(titled title: String, bordered: Bool, backgroundColor: NSColor?) { + button = CustomHeightButton(title: title, target: nil, action: nil) + let cell = CustomButtonCell() + button.cell = cell + if let color = backgroundColor { + cell.isBordered = true + button.bezelColor = color + cell.backgroundColor = color + } else { + button.isBordered = bordered + button.bezelStyle = bordered ? .rounded : .inline + } + button.title = title + self.view = button + } func gestureRecognizer(_ gestureRecognizer: NSGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: NSGestureRecognizer) -> Bool { if gestureRecognizer == singleClick && otherGestureRecognizer == longClick { @@ -84,6 +106,16 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat } } +class CustomHeightButton : NSButton { + + override var intrinsicContentSize: NSSize { + var size = super.intrinsicContentSize + size.height = 30 + return size + } + +} + class CustomButtonCell: NSButtonCell { init() { @@ -117,5 +149,6 @@ class CustomButtonCell: NSButtonCell { self.attributedTitle = attrTitle } + } diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index ca012ba..bef8cf8 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -212,18 +212,10 @@ class TouchBarController: NSObject, NSTouchBarDelegate { widthBarItem.setWidth(value: value) } if case .bordered(let bordered)? = item.additionalParameters[.bordered], let item = barItem as? CustomButtonTouchBarItem { - item.button.isBordered = bordered - item.button.bezelStyle = bordered ? .rounded : .inline + item.isBordered = bordered } if case .background(let color)? = item.additionalParameters[.background], let item = barItem as? CustomButtonTouchBarItem { - if item.button.cell?.isBordered == false { - let newCell = NSButtonCell() - newCell.title = item.button.title - item.button.cell = newCell - item.button.cell?.isBordered = true - } - item.button.bezelColor = color - (item.button.cell as? NSButtonCell)?.backgroundColor = color + item.backgroundColor = color } if case .image(let source)? = item.additionalParameters[.image], let item = barItem as? CustomButtonTouchBarItem { let button = item.button!