mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-10 17:08:39 +00:00
preset working
This commit is contained in:
parent
d0efdef8c2
commit
46f35f4737
@ -1,5 +1,13 @@
|
||||
import Foundation
|
||||
|
||||
extension Data {
|
||||
|
||||
func barItemDefinitions() -> [BarItemDefinition]? {
|
||||
return try? JSONDecoder().decode([BarItemDefinition].self, from: self)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
struct BarItemDefinition: Decodable {
|
||||
let type: ItemType
|
||||
let action: ActionType
|
||||
@ -8,20 +16,23 @@ struct BarItemDefinition: Decodable {
|
||||
case type
|
||||
}
|
||||
|
||||
init(type: ItemType, action: ActionType) {
|
||||
self.type = type
|
||||
self.action = action
|
||||
}
|
||||
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
let type = try container.decode(String.self, forKey: .type)
|
||||
let parametersDecoder = SupportedTypesHolder.sharedInstance.lookup(by: type)
|
||||
if let result = try? parametersDecoder(decoder),
|
||||
case let (itemType, action) = result {
|
||||
self.type = itemType
|
||||
self.action = action
|
||||
self.init(type: itemType, action: action)
|
||||
} else {
|
||||
self.type = .staticButton(title: "unknown")
|
||||
self.action = .none
|
||||
self.init(type: .staticButton(title: "unknown"), action: .none)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
class SupportedTypesHolder {
|
||||
|
||||
@ -8,38 +8,58 @@
|
||||
|
||||
import Cocoa
|
||||
|
||||
struct ExactItem {
|
||||
let identifier: NSTouchBarItem.Identifier
|
||||
let presetItem: BarItemDefinition
|
||||
}
|
||||
|
||||
extension ItemType {
|
||||
|
||||
var identifierBase: String {
|
||||
switch self {
|
||||
case .staticButton(title: _):
|
||||
return "com.toxblh.mtmr.staticButton."
|
||||
case .appleScriptTitledButton(source: _):
|
||||
return "com.toxblh.mtmr.appleScriptButton."
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class TouchBarController: NSObject, NSTouchBarDelegate {
|
||||
|
||||
static let shared = TouchBarController()
|
||||
|
||||
let touchBar = NSTouchBar()
|
||||
|
||||
var items: [NSTouchBarItem.Identifier: BarItemDefinition] = [:]
|
||||
|
||||
private override init() {
|
||||
super.init()
|
||||
|
||||
loadItems()
|
||||
|
||||
touchBar.delegate = self
|
||||
touchBar.defaultItemIdentifiers = [
|
||||
.escButton,
|
||||
.dismissButton,
|
||||
|
||||
.brightDown,
|
||||
.brightUp,
|
||||
|
||||
.flexibleSpace,
|
||||
|
||||
.prev,
|
||||
.play,
|
||||
.next,
|
||||
|
||||
.sleep,
|
||||
.weather,
|
||||
|
||||
.volumeDown,
|
||||
.volumeUp,
|
||||
.battery,
|
||||
.time,
|
||||
]
|
||||
touchBar.defaultItemIdentifiers = Array(items.keys)
|
||||
self.presentTouchBar()
|
||||
}
|
||||
|
||||
func loadItems() {
|
||||
let appSupportDirectory = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!
|
||||
let presetPath = appSupportDirectory.appending("items.json")
|
||||
if !FileManager.default.fileExists(atPath: presetPath),
|
||||
let defaultPreset = Bundle.main.path(forResource: "defaultPreset", ofType: "json") {
|
||||
try? FileManager.default.copyItem(atPath: defaultPreset, toPath: presetPath)
|
||||
}
|
||||
let jsonData = try? Data(contentsOf: URL(fileURLWithPath: presetPath))
|
||||
let jsonItems = jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none)]
|
||||
|
||||
for item in jsonItems {
|
||||
let identifierString = item.type.identifierBase.appending(UUID().uuidString)
|
||||
let identifier = NSTouchBarItem.Identifier(identifierString)
|
||||
items[identifier] = item
|
||||
}
|
||||
}
|
||||
|
||||
func setupControlStripPresence() {
|
||||
DFRSystemModalShowsCloseBoxWhenFrontMost(false)
|
||||
@ -69,6 +89,19 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
||||
}
|
||||
|
||||
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
|
||||
guard let item = self.items[identifier] else {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch item.type {
|
||||
case .staticButton(title: let title):
|
||||
return CustomButtonTouchBarItem(identifier: identifier, title: title) { _ in
|
||||
|
||||
}
|
||||
case .appleScriptTitledButton(source: let source):
|
||||
return AppleScriptTouchBarItem(identifier: identifier, appleScript: source, interval: 30) //fixme interval
|
||||
}
|
||||
|
||||
switch identifier {
|
||||
case .escButton:
|
||||
return CustomButtonTouchBarItem(identifier: identifier, title: "esc", key: ESCKeyPress())
|
||||
|
||||
4
MTMR/defaultPreset.json
Normal file
4
MTMR/defaultPreset.json
Normal file
@ -0,0 +1,4 @@
|
||||
[
|
||||
{ "type": "brightnessUp" }
|
||||
|
||||
]
|
||||
Loading…
Reference in New Issue
Block a user