mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-12 09:58:38 +00:00
media keys implemented
This commit is contained in:
parent
4fb7cb9b99
commit
2750d00317
@ -40,19 +40,21 @@ class SupportedTypesHolder {
|
|||||||
private var supportedTypes: [String: ParametersDecoder] = [
|
private var supportedTypes: [String: ParametersDecoder] = [
|
||||||
"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)) },
|
||||||
|
"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()
|
static let sharedInstance = SupportedTypesHolder()
|
||||||
|
|
||||||
func lookup(by type: String) -> ParametersDecoder {
|
func lookup(by type: String) -> ParametersDecoder {
|
||||||
if let extraType = supportedTypes[type] {
|
return supportedTypes[type] ?? { decoder in
|
||||||
return extraType
|
|
||||||
} else {
|
|
||||||
return { decoder in
|
|
||||||
return (item: try ItemType(from: decoder), action: try ActionType(from: decoder))
|
return (item: try ItemType(from: decoder), action: try ActionType(from: decoder))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func register(typename: String, decoder: @escaping ParametersDecoder) {
|
func register(typename: String, decoder: @escaping ParametersDecoder) {
|
||||||
supportedTypes[typename] = decoder
|
supportedTypes[typename] = decoder
|
||||||
|
|||||||
@ -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) {
|
func doKey(_ key: Int, down: Bool) {
|
||||||
let flags = NSEvent.ModifierFlags(rawValue: down ? 0xa00 : 0xb00)
|
let flags = NSEvent.ModifierFlags(rawValue: down ? 0xa00 : 0xb00)
|
||||||
let data1 = (key << 16) | ((down ? 0xa : 0xb) << 8)
|
let data1 = (key << 16) | ((down ? 0xa : 0xb) << 8)
|
||||||
|
|||||||
@ -105,44 +105,6 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
return AppleScriptTouchBarItem(identifier: identifier, appleScript: source, interval: interval)
|
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -9,29 +9,6 @@
|
|||||||
import Cocoa
|
import Cocoa
|
||||||
|
|
||||||
extension NSTouchBarItem.Identifier {
|
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")
|
static let controlStripItem = NSTouchBarItem.Identifier("com.toxblh.mtmr.controlStrip")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,13 @@
|
|||||||
[
|
[
|
||||||
{ "type": "escape" },
|
{ "type": "escape" },
|
||||||
{ "type": "brightnessUp" },
|
|
||||||
{ "type": "exitTouchbar" },
|
{ "type": "exitTouchbar" },
|
||||||
|
{ "type": "brightnessDown" },
|
||||||
|
{ "type": "brightnessUp" },
|
||||||
|
{ "type": "volumeDown" },
|
||||||
|
{ "type": "volumeUp" },
|
||||||
|
{ "type": "previous" },
|
||||||
|
{ "type": "play" },
|
||||||
|
{ "type": "next" },
|
||||||
|
{ "type": "volumeUp" },
|
||||||
|
{ "type": "volumeUp" },
|
||||||
]
|
]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user