diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index fffbee4..eea90e8 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -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 { diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 8672f62..4decea7 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -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()) diff --git a/MTMR/defaultPreset.json b/MTMR/defaultPreset.json new file mode 100644 index 0000000..d01eb2f --- /dev/null +++ b/MTMR/defaultPreset.json @@ -0,0 +1,4 @@ +[ + { "type": "brightnessUp" } + +]