mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-10 17:08:39 +00:00
add staticImageButton for buttons with custom image
This commit is contained in:
parent
ff58329691
commit
98d5a6eb0e
@ -44,11 +44,11 @@ class SupportedTypesHolder {
|
||||
"escape": { _ in return (item: .staticButton(title: "esc"), action: .keyPress(keycode: 53)) },
|
||||
"brightnessUp": { _ in return (item: .staticButton(title: "🔆"), action: .keyPress(keycode: 113)) },
|
||||
"brightnessDown": { _ in return (item: .staticButton(title: "🔅"), action: .keyPress(keycode: 107)) },
|
||||
"volumeDown": { _ in return (item: .staticButton(title: "🔉"), action: .hidKey(keycode: NX_KEYTYPE_SOUND_DOWN)) },
|
||||
"volumeUp": { _ in return (item: .staticButton(title: "🔊"), action: .hidKey(keycode: NX_KEYTYPE_SOUND_UP)) },
|
||||
"previous": { _ in return (item: .staticButton(title: "⏪"), action: .hidKey(keycode: NX_KEYTYPE_PREVIOUS)) },
|
||||
"play": { _ in return (item: .staticButton(title: "⏯"), action: .hidKey(keycode: NX_KEYTYPE_PLAY)) },
|
||||
"next": { _ in return (item: .staticButton(title: "⏩"), action: .hidKey(keycode: NX_KEYTYPE_NEXT)) },
|
||||
"volumeDown": { _ in return (item: .staticImageButton(title: "", image: NSImage(named: .touchBarVolumeDownTemplate)!), action: .hidKey(keycode: NX_KEYTYPE_SOUND_DOWN)) },
|
||||
"volumeUp": { _ in return (item: .staticImageButton(title: "", image: NSImage(named: .touchBarVolumeUpTemplate)!), action: .hidKey(keycode: NX_KEYTYPE_SOUND_UP)) },
|
||||
"previous": { _ in return (item: .staticImageButton(title: "", image: NSImage(named: .touchBarRewindTemplate)!), action: .hidKey(keycode: NX_KEYTYPE_PREVIOUS)) },
|
||||
"play": { _ in return (item: .staticImageButton(title: "", image: NSImage(named: .touchBarPlayPauseTemplate)!), action: .hidKey(keycode: NX_KEYTYPE_PLAY)) },
|
||||
"next": { _ in return (item: .staticImageButton(title: "", image: NSImage(named: .touchBarFastForwardTemplate)!), action: .hidKey(keycode: NX_KEYTYPE_NEXT)) },
|
||||
"weather": { decoder in
|
||||
enum CodingKeys: String, CodingKey { case refreshInterval }
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
@ -91,6 +91,7 @@ class SupportedTypesHolder {
|
||||
|
||||
enum ItemType: Decodable {
|
||||
case staticButton(title: String)
|
||||
case staticImageButton(title: String, image: NSImage)
|
||||
case appleScriptTitledButton(source: String, refreshInterval: Double)
|
||||
case timeButton(formatTemplate: String)
|
||||
case flexSpace()
|
||||
@ -101,10 +102,12 @@ enum ItemType: Decodable {
|
||||
case titleAppleScript
|
||||
case refreshInterval
|
||||
case formatTemplate
|
||||
case image
|
||||
}
|
||||
|
||||
enum ItemTypeRaw: String, Decodable {
|
||||
case staticButton
|
||||
case staticImageButton
|
||||
case appleScriptTitledButton
|
||||
case timeButton
|
||||
case flexSpace
|
||||
@ -118,6 +121,15 @@ enum ItemType: Decodable {
|
||||
let source = try container.decode(String.self, forKey: .titleAppleScript)
|
||||
let interval = try container.decode(Double.self, forKey: .refreshInterval)
|
||||
self = .appleScriptTitledButton(source: try String(contentsOfFile: source), refreshInterval: interval)
|
||||
case .staticImageButton:
|
||||
let title = try container.decode(String.self, forKey: .title)
|
||||
let imageRaw = try container.decode(String.self, forKey: .image)
|
||||
if let decodedImageData = Data(base64Encoded: imageRaw, options: .ignoreUnknownCharacters) {
|
||||
let decImage = NSImage(data: decodedImageData)!
|
||||
self = .staticImageButton(title: title, image: decImage)
|
||||
} else {
|
||||
self = .staticButton(title: title)
|
||||
}
|
||||
case .staticButton:
|
||||
let title = try container.decode(String.self, forKey: .title)
|
||||
self = .staticButton(title: title)
|
||||
|
||||
@ -19,6 +19,8 @@ extension ItemType {
|
||||
switch self {
|
||||
case .staticButton(title: _):
|
||||
return "com.toxblh.mtmr.staticButton."
|
||||
case .staticImageButton(title: _):
|
||||
return "com.toxblh.mtmr.staticImageButton."
|
||||
case .appleScriptTitledButton(source: _):
|
||||
return "com.toxblh.mtmr.appleScriptButton."
|
||||
case .timeButton(formatTemplate: _):
|
||||
@ -100,6 +102,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
||||
switch item.type {
|
||||
case .staticButton(title: let title):
|
||||
barItem = CustomButtonTouchBarItem(identifier: identifier, title: title, onTap: action)
|
||||
case .staticImageButton(title: let title, image: let image):
|
||||
barItem = CustomButtonTouchBarItem(identifier: identifier, title: title, onTap: action, image: image)
|
||||
case .appleScriptTitledButton(source: let source, refreshInterval: let interval):
|
||||
barItem = AppleScriptTouchBarItem(identifier: identifier, appleScript: source, interval: interval)
|
||||
case .timeButton(formatTemplate: let template):
|
||||
|
||||
@ -18,9 +18,25 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem {
|
||||
init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping () -> ()) {
|
||||
self.tapClosure = callback
|
||||
super.init(identifier: identifier)
|
||||
self.view = NSButton(title: title, target: self, action: #selector(didTapped))
|
||||
var button = NSButton(title: "", target: nil, action: nil)
|
||||
button = NSButton(title: title, target: self, action: #selector(didTapped))
|
||||
self.view = button
|
||||
}
|
||||
|
||||
init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping () -> (), image: NSImage?) {
|
||||
self.tapClosure = callback
|
||||
super.init(identifier: identifier)
|
||||
var button = NSButton(title: "", target: nil, action: nil)
|
||||
if ((image) != nil) {
|
||||
button = NSButton(title: title, image: image!, target: self, action: #selector(didTapped))
|
||||
} else {
|
||||
button = NSButton(title: title, target: self, action: #selector(didTapped))
|
||||
}
|
||||
self.view = button
|
||||
button.bezelColor = .clear
|
||||
}
|
||||
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user