From 98d5a6eb0e24e2c0060c4a86509e5e3b144fd227 Mon Sep 17 00:00:00 2001 From: Toxblh Date: Wed, 11 Apr 2018 17:03:38 +0100 Subject: [PATCH] add staticImageButton for buttons with custom image --- MTMR/ItemsParsing.swift | 22 +++++++++++++++++----- MTMR/TouchBarController.swift | 4 ++++ MTMR/TouchBarItems.swift | 18 +++++++++++++++++- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index a131243..fc45042 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -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) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 4dbe5d6..87ada61 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -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): diff --git a/MTMR/TouchBarItems.swift b/MTMR/TouchBarItems.swift index f3506d2..89b41c1 100644 --- a/MTMR/TouchBarItems.swift +++ b/MTMR/TouchBarItems.swift @@ -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") }