From 9e1fcb9a35bdfbc719c813cb453cae7503953b0e Mon Sep 17 00:00:00 2001 From: Serg Date: Sat, 12 May 2018 10:49:16 +0700 Subject: [PATCH] incapsulate button image handling --- MTMR/CustomButtonTouchBarItem.swift | 10 ++++++++++ MTMR/TouchBarController.swift | 11 +---------- MTMR/Widgets/InputSourceBarItem.swift | 20 +++++++------------- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/MTMR/CustomButtonTouchBarItem.swift b/MTMR/CustomButtonTouchBarItem.swift index 5804842..09f5eb8 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -63,10 +63,17 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat var attributedTitle: NSAttributedString { didSet { + self.button?.imagePosition = attributedTitle.length > 0 ? .imageLeading : .imageOnly self.button?.attributedTitle = attributedTitle } } + var image: NSImage? { + didSet { + button.image = image + } + } + private func reinstallButton() { let title = button.attributedTitle let image = button.image @@ -80,7 +87,10 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat button.isBordered = isBordered button.bezelStyle = isBordered ? .rounded : .inline } + button.imageScaling = .scaleProportionallyDown + button.imageHugsTitle = true button.attributedTitle = title + self.button?.imagePosition = title.length > 0 ? .imageLeading : .imageOnly button.image = image self.view = button diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index a396add..f4cea6b 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -236,16 +236,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate { widthBarItem.setWidth(value: value) } if case .image(let source)? = item.additionalParameters[.image], let item = barItem as? CustomButtonTouchBarItem { - let button = item.button! - button.imageScaling = .scaleProportionallyDown - button.imagePosition = .imageLeading - - if (item.title == "") { - button.imagePosition = .imageOnly - } - - button.imageHugsTitle = true - button.image = source.image + item.image = source.image } return barItem } diff --git a/MTMR/Widgets/InputSourceBarItem.swift b/MTMR/Widgets/InputSourceBarItem.swift index 1d9f49d..be809c8 100644 --- a/MTMR/Widgets/InputSourceBarItem.swift +++ b/MTMR/Widgets/InputSourceBarItem.swift @@ -22,9 +22,6 @@ class InputSourceBarItem: CustomButtonTouchBarItem { self.button.cell?.action = #selector(switchInputSource) self.button.action = #selector(switchInputSource) - - self.button.frame.size = buttonSize - self.button.bounds.size = buttonSize } required init?(coder: NSCoder) { @@ -45,19 +42,16 @@ class InputSourceBarItem: CustomButtonTouchBarItem { var iconImage: NSImage? = nil - if let imageURL = currentSource.iconImageURL { - if let image = NSImage(contentsOf: imageURL) { - iconImage = image - } - } - - if iconImage == nil, let iconRef = currentSource.iconRef { + if let imageURL = currentSource.iconImageURL, + let image = NSImage(contentsOf: imageURL) { + iconImage = image + } else if let iconRef = currentSource.iconRef { iconImage = NSImage(iconRef: iconRef) } - if (iconImage != nil) { - self.button.cell?.image = iconImage - self.button.cell?.image?.size = buttonSize + if let iconImage = iconImage { + iconImage.size = buttonSize + self.image = iconImage self.title = "" } else { self.title = currentSource.name