1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-12 09:58:38 +00:00

add staticImageButton for buttons with custom image

This commit is contained in:
Toxblh 2018-04-11 17:03:38 +01:00
parent ff58329691
commit 98d5a6eb0e
3 changed files with 38 additions and 6 deletions

View File

@ -44,11 +44,11 @@ class SupportedTypesHolder {
"escape": { _ in return (item: .staticButton(title: "esc"), action: .keyPress(keycode: 53)) }, "escape": { _ in return (item: .staticButton(title: "esc"), action: .keyPress(keycode: 53)) },
"brightnessUp": { _ in return (item: .staticButton(title: "🔆"), action: .keyPress(keycode: 113)) }, "brightnessUp": { _ in return (item: .staticButton(title: "🔆"), action: .keyPress(keycode: 113)) },
"brightnessDown": { _ in return (item: .staticButton(title: "🔅"), action: .keyPress(keycode: 107)) }, "brightnessDown": { _ in return (item: .staticButton(title: "🔅"), action: .keyPress(keycode: 107)) },
"volumeDown": { _ in return (item: .staticButton(title: "🔉"), action: .hidKey(keycode: NX_KEYTYPE_SOUND_DOWN)) }, "volumeDown": { _ in return (item: .staticImageButton(title: "", image: NSImage(named: .touchBarVolumeDownTemplate)!), action: .hidKey(keycode: NX_KEYTYPE_SOUND_DOWN)) },
"volumeUp": { _ in return (item: .staticButton(title: "🔊"), action: .hidKey(keycode: NX_KEYTYPE_SOUND_UP)) }, "volumeUp": { _ in return (item: .staticImageButton(title: "", image: NSImage(named: .touchBarVolumeUpTemplate)!), action: .hidKey(keycode: NX_KEYTYPE_SOUND_UP)) },
"previous": { _ in return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_PREVIOUS)) }, "previous": { _ in return (item: .staticImageButton(title: "", image: NSImage(named: .touchBarRewindTemplate)!), action: .hidKey(keycode: NX_KEYTYPE_PREVIOUS)) },
"play": { _ in return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_PLAY)) }, "play": { _ in return (item: .staticImageButton(title: "", image: NSImage(named: .touchBarPlayPauseTemplate)!), action: .hidKey(keycode: NX_KEYTYPE_PLAY)) },
"next": { _ in return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_NEXT)) }, "next": { _ in return (item: .staticImageButton(title: "", image: NSImage(named: .touchBarFastForwardTemplate)!), action: .hidKey(keycode: NX_KEYTYPE_NEXT)) },
"weather": { decoder in "weather": { decoder in
enum CodingKeys: String, CodingKey { case refreshInterval } enum CodingKeys: String, CodingKey { case refreshInterval }
let container = try decoder.container(keyedBy: CodingKeys.self) let container = try decoder.container(keyedBy: CodingKeys.self)
@ -91,6 +91,7 @@ class SupportedTypesHolder {
enum ItemType: Decodable { enum ItemType: Decodable {
case staticButton(title: String) case staticButton(title: String)
case staticImageButton(title: String, image: NSImage)
case appleScriptTitledButton(source: String, refreshInterval: Double) case appleScriptTitledButton(source: String, refreshInterval: Double)
case timeButton(formatTemplate: String) case timeButton(formatTemplate: String)
case flexSpace() case flexSpace()
@ -101,10 +102,12 @@ enum ItemType: Decodable {
case titleAppleScript case titleAppleScript
case refreshInterval case refreshInterval
case formatTemplate case formatTemplate
case image
} }
enum ItemTypeRaw: String, Decodable { enum ItemTypeRaw: String, Decodable {
case staticButton case staticButton
case staticImageButton
case appleScriptTitledButton case appleScriptTitledButton
case timeButton case timeButton
case flexSpace case flexSpace
@ -118,6 +121,15 @@ enum ItemType: Decodable {
let source = try container.decode(String.self, forKey: .titleAppleScript) let source = try container.decode(String.self, forKey: .titleAppleScript)
let interval = try container.decode(Double.self, forKey: .refreshInterval) let interval = try container.decode(Double.self, forKey: .refreshInterval)
self = .appleScriptTitledButton(source: try String(contentsOfFile: source), refreshInterval: interval) 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: case .staticButton:
let title = try container.decode(String.self, forKey: .title) let title = try container.decode(String.self, forKey: .title)
self = .staticButton(title: title) self = .staticButton(title: title)

View File

@ -19,6 +19,8 @@ extension ItemType {
switch self { switch self {
case .staticButton(title: _): case .staticButton(title: _):
return "com.toxblh.mtmr.staticButton." return "com.toxblh.mtmr.staticButton."
case .staticImageButton(title: _):
return "com.toxblh.mtmr.staticImageButton."
case .appleScriptTitledButton(source: _): case .appleScriptTitledButton(source: _):
return "com.toxblh.mtmr.appleScriptButton." return "com.toxblh.mtmr.appleScriptButton."
case .timeButton(formatTemplate: _): case .timeButton(formatTemplate: _):
@ -100,6 +102,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
switch item.type { switch item.type {
case .staticButton(title: let title): case .staticButton(title: let title):
barItem = CustomButtonTouchBarItem(identifier: identifier, title: title, onTap: action) 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): case .appleScriptTitledButton(source: let source, refreshInterval: let interval):
barItem = AppleScriptTouchBarItem(identifier: identifier, appleScript: source, interval: interval) barItem = AppleScriptTouchBarItem(identifier: identifier, appleScript: source, interval: interval)
case .timeButton(formatTemplate: let template): case .timeButton(formatTemplate: let template):

View File

@ -18,9 +18,25 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem {
init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping () -> ()) { init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping () -> ()) {
self.tapClosure = callback self.tapClosure = callback
super.init(identifier: identifier) 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) { required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }