1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-10 17:08:39 +00:00

incapsulate button image handling

This commit is contained in:
Serg 2018-05-12 10:49:16 +07:00
parent b73f8865a9
commit 9e1fcb9a35
3 changed files with 18 additions and 23 deletions

View File

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

View File

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

View File

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