From 2750d00317303365a4e568d07c13e91dfdf61f13 Mon Sep 17 00:00:00 2001 From: Serg Date: Tue, 10 Apr 2018 18:25:03 +0700 Subject: [PATCH] media keys implemented --- MTMR/ItemsParsing.swift | 14 +++++++------ MTMR/KeyPress.swift | 9 --------- MTMR/TouchBarController.swift | 38 ----------------------------------- MTMR/TouchBarItems.swift | 23 --------------------- MTMR/defaultPreset.json | 10 ++++++++- 5 files changed, 17 insertions(+), 77 deletions(-) diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index 43fbc5a..ea88c28 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -40,17 +40,19 @@ class SupportedTypesHolder { private var supportedTypes: [String: ParametersDecoder] = [ "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)) }, ] static let sharedInstance = SupportedTypesHolder() func lookup(by type: String) -> ParametersDecoder { - if let extraType = supportedTypes[type] { - return extraType - } else { - return { decoder in - return (item: try ItemType(from: decoder), action: try ActionType(from: decoder)) - } + return supportedTypes[type] ?? { decoder in + return (item: try ItemType(from: decoder), action: try ActionType(from: decoder)) } } diff --git a/MTMR/KeyPress.swift b/MTMR/KeyPress.swift index f8bd4a1..5421d97 100644 --- a/MTMR/KeyPress.swift +++ b/MTMR/KeyPress.swift @@ -29,15 +29,6 @@ extension KeyPress { } } -struct BrightnessUpPress: KeyPress { - let keyCode: CGKeyCode = 113 -} - -struct BrightnessDownPress: KeyPress { - let keyCode: CGKeyCode = 107 -} - - func doKey(_ key: Int, down: Bool) { let flags = NSEvent.ModifierFlags(rawValue: down ? 0xa00 : 0xb00) let data1 = (key << 16) | ((down ? 0xa : 0xb) << 8) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 28e4aa5..e8c2760 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -105,44 +105,6 @@ class TouchBarController: NSObject, NSTouchBarDelegate { return AppleScriptTouchBarItem(identifier: identifier, appleScript: source, interval: interval) } - switch identifier { - case .brightUp: - return CustomButtonTouchBarItem(identifier: identifier, title: "🔆", key: BrightnessUpPress()) - case .brightDown: - return CustomButtonTouchBarItem(identifier: identifier, title: "🔅", key: BrightnessDownPress()) - - case .volumeDown: - return CustomButtonTouchBarItem(identifier: identifier, title: "🔉", HIDKeycode: NX_KEYTYPE_SOUND_DOWN) - case .volumeUp: - return CustomButtonTouchBarItem(identifier: identifier, title: "🔊", HIDKeycode: NX_KEYTYPE_SOUND_UP) - - case .weather: - let url = Bundle.main.url(forResource: "weather", withExtension: "scpt")! - let script = try! String.init(contentsOf: url) - return AppleScriptTouchBarItem(identifier: identifier, appleScript: script, interval: 600) - - case .sleep: - let item = NSCustomTouchBarItem(identifier: identifier) - item.view = NSButton(title: "☕️", target: self, action: #selector(goToSleep)) - return item - - case .prev: - return CustomButtonTouchBarItem(identifier: identifier, title: "⏪", HIDKeycode: NX_KEYTYPE_PREVIOUS) - case .play: - return CustomButtonTouchBarItem(identifier: identifier, title: "⏯", HIDKeycode: NX_KEYTYPE_PLAY) - case .next: - return CustomButtonTouchBarItem(identifier: identifier, title: "⏩", HIDKeycode: NX_KEYTYPE_NEXT) - - case .battery: - let url = Bundle.main.url(forResource: "battery", withExtension: "scpt")! - let script = try! String.init(contentsOf: url) - return AppleScriptTouchBarItem(identifier: identifier, appleScript: script, interval: 60) - case .time: - return TimeTouchBarItem(identifier: identifier, formatTemplate: "HH:mm") - - default: - return nil - } } diff --git a/MTMR/TouchBarItems.swift b/MTMR/TouchBarItems.swift index a1adf35..f3506d2 100644 --- a/MTMR/TouchBarItems.swift +++ b/MTMR/TouchBarItems.swift @@ -9,29 +9,6 @@ import Cocoa extension NSTouchBarItem.Identifier { - static let escButton = NSTouchBarItem.Identifier("com.toxblh.mtmr.escButton") - static let dismissButton = NSTouchBarItem.Identifier("com.toxblh.mtmr.dismissButton") - - // Volume - static let volumeUp = NSTouchBarItem.Identifier("com.toxblh.mtmr.volumeUp") - static let volumeDown = NSTouchBarItem.Identifier("com.toxblh.mtmr.volumeDown") - - // Brightness - static let brightUp = NSTouchBarItem.Identifier("com.toxblh.mtmr.brightUp") - static let brightDown = NSTouchBarItem.Identifier("com.toxblh.mtmr.brightDown") - - // Music - static let prev = NSTouchBarItem.Identifier("com.toxblh.mtmr.prev") - static let next = NSTouchBarItem.Identifier("com.toxblh.mtmr.next") - static let play = NSTouchBarItem.Identifier("com.toxblh.mtmr.play") - - // Plugins - static let sleep = NSTouchBarItem.Identifier("com.toxblh.mtmr.sleep") - static let weather = NSTouchBarItem.Identifier("com.toxblh.mtmr.weather") - static let time = NSTouchBarItem.Identifier("com.toxblh.mtmr.time") - static let battery = NSTouchBarItem.Identifier("com.toxblh.mtmr.battery") - static let nowPlaying = NSTouchBarItem.Identifier("com.toxblh.mtmr.nowPlaying") - static let controlStripItem = NSTouchBarItem.Identifier("com.toxblh.mtmr.controlStrip") } diff --git a/MTMR/defaultPreset.json b/MTMR/defaultPreset.json index 23e8997..5bc3764 100644 --- a/MTMR/defaultPreset.json +++ b/MTMR/defaultPreset.json @@ -1,5 +1,13 @@ [ { "type": "escape" }, - { "type": "brightnessUp" }, { "type": "exitTouchbar" }, + { "type": "brightnessDown" }, + { "type": "brightnessUp" }, + { "type": "volumeDown" }, + { "type": "volumeUp" }, + { "type": "previous" }, + { "type": "play" }, + { "type": "next" }, + { "type": "volumeUp" }, + { "type": "volumeUp" }, ]