mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 09:28:38 +00:00
incapsulate button image handling
This commit is contained in:
parent
b73f8865a9
commit
9e1fcb9a35
@ -63,10 +63,17 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
|
|||||||
|
|
||||||
var attributedTitle: NSAttributedString {
|
var attributedTitle: NSAttributedString {
|
||||||
didSet {
|
didSet {
|
||||||
|
self.button?.imagePosition = attributedTitle.length > 0 ? .imageLeading : .imageOnly
|
||||||
self.button?.attributedTitle = attributedTitle
|
self.button?.attributedTitle = attributedTitle
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var image: NSImage? {
|
||||||
|
didSet {
|
||||||
|
button.image = image
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private func reinstallButton() {
|
private func reinstallButton() {
|
||||||
let title = button.attributedTitle
|
let title = button.attributedTitle
|
||||||
let image = button.image
|
let image = button.image
|
||||||
@ -80,7 +87,10 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
|
|||||||
button.isBordered = isBordered
|
button.isBordered = isBordered
|
||||||
button.bezelStyle = isBordered ? .rounded : .inline
|
button.bezelStyle = isBordered ? .rounded : .inline
|
||||||
}
|
}
|
||||||
|
button.imageScaling = .scaleProportionallyDown
|
||||||
|
button.imageHugsTitle = true
|
||||||
button.attributedTitle = title
|
button.attributedTitle = title
|
||||||
|
self.button?.imagePosition = title.length > 0 ? .imageLeading : .imageOnly
|
||||||
button.image = image
|
button.image = image
|
||||||
self.view = button
|
self.view = button
|
||||||
|
|
||||||
|
|||||||
@ -236,16 +236,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
widthBarItem.setWidth(value: value)
|
widthBarItem.setWidth(value: value)
|
||||||
}
|
}
|
||||||
if case .image(let source)? = item.additionalParameters[.image], let item = barItem as? CustomButtonTouchBarItem {
|
if case .image(let source)? = item.additionalParameters[.image], let item = barItem as? CustomButtonTouchBarItem {
|
||||||
let button = item.button!
|
item.image = source.image
|
||||||
button.imageScaling = .scaleProportionallyDown
|
|
||||||
button.imagePosition = .imageLeading
|
|
||||||
|
|
||||||
if (item.title == "") {
|
|
||||||
button.imagePosition = .imageOnly
|
|
||||||
}
|
|
||||||
|
|
||||||
button.imageHugsTitle = true
|
|
||||||
button.image = source.image
|
|
||||||
}
|
}
|
||||||
return barItem
|
return barItem
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,9 +22,6 @@ class InputSourceBarItem: CustomButtonTouchBarItem {
|
|||||||
|
|
||||||
self.button.cell?.action = #selector(switchInputSource)
|
self.button.cell?.action = #selector(switchInputSource)
|
||||||
self.button.action = #selector(switchInputSource)
|
self.button.action = #selector(switchInputSource)
|
||||||
|
|
||||||
self.button.frame.size = buttonSize
|
|
||||||
self.button.bounds.size = buttonSize
|
|
||||||
}
|
}
|
||||||
|
|
||||||
required init?(coder: NSCoder) {
|
required init?(coder: NSCoder) {
|
||||||
@ -45,19 +42,16 @@ class InputSourceBarItem: CustomButtonTouchBarItem {
|
|||||||
|
|
||||||
var iconImage: NSImage? = nil
|
var iconImage: NSImage? = nil
|
||||||
|
|
||||||
if let imageURL = currentSource.iconImageURL {
|
if let imageURL = currentSource.iconImageURL,
|
||||||
if let image = NSImage(contentsOf: imageURL) {
|
let image = NSImage(contentsOf: imageURL) {
|
||||||
iconImage = image
|
iconImage = image
|
||||||
}
|
} else if let iconRef = currentSource.iconRef {
|
||||||
}
|
|
||||||
|
|
||||||
if iconImage == nil, let iconRef = currentSource.iconRef {
|
|
||||||
iconImage = NSImage(iconRef: iconRef)
|
iconImage = NSImage(iconRef: iconRef)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (iconImage != nil) {
|
if let iconImage = iconImage {
|
||||||
self.button.cell?.image = iconImage
|
iconImage.size = buttonSize
|
||||||
self.button.cell?.image?.size = buttonSize
|
self.image = iconImage
|
||||||
self.title = ""
|
self.title = ""
|
||||||
} else {
|
} else {
|
||||||
self.title = currentSource.name
|
self.title = currentSource.name
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user