mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 17:38:38 +00:00
preset working
This commit is contained in:
parent
d0efdef8c2
commit
46f35f4737
@ -1,5 +1,13 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
extension Data {
|
||||||
|
|
||||||
|
func barItemDefinitions() -> [BarItemDefinition]? {
|
||||||
|
return try? JSONDecoder().decode([BarItemDefinition].self, from: self)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
struct BarItemDefinition: Decodable {
|
struct BarItemDefinition: Decodable {
|
||||||
let type: ItemType
|
let type: ItemType
|
||||||
let action: ActionType
|
let action: ActionType
|
||||||
@ -8,17 +16,20 @@ struct BarItemDefinition: Decodable {
|
|||||||
case type
|
case type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
init(type: ItemType, action: ActionType) {
|
||||||
|
self.type = type
|
||||||
|
self.action = action
|
||||||
|
}
|
||||||
|
|
||||||
init(from decoder: Decoder) throws {
|
init(from decoder: Decoder) throws {
|
||||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
let type = try container.decode(String.self, forKey: .type)
|
let type = try container.decode(String.self, forKey: .type)
|
||||||
let parametersDecoder = SupportedTypesHolder.sharedInstance.lookup(by: type)
|
let parametersDecoder = SupportedTypesHolder.sharedInstance.lookup(by: type)
|
||||||
if let result = try? parametersDecoder(decoder),
|
if let result = try? parametersDecoder(decoder),
|
||||||
case let (itemType, action) = result {
|
case let (itemType, action) = result {
|
||||||
self.type = itemType
|
self.init(type: itemType, action: action)
|
||||||
self.action = action
|
|
||||||
} else {
|
} else {
|
||||||
self.type = .staticButton(title: "unknown")
|
self.init(type: .staticButton(title: "unknown"), action: .none)
|
||||||
self.action = .none
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -8,39 +8,59 @@
|
|||||||
|
|
||||||
import Cocoa
|
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 {
|
class TouchBarController: NSObject, NSTouchBarDelegate {
|
||||||
|
|
||||||
static let shared = TouchBarController()
|
static let shared = TouchBarController()
|
||||||
|
|
||||||
let touchBar = NSTouchBar()
|
let touchBar = NSTouchBar()
|
||||||
|
|
||||||
|
var items: [NSTouchBarItem.Identifier: BarItemDefinition] = [:]
|
||||||
|
|
||||||
private override init() {
|
private override init() {
|
||||||
super.init()
|
super.init()
|
||||||
|
|
||||||
|
loadItems()
|
||||||
|
|
||||||
touchBar.delegate = self
|
touchBar.delegate = self
|
||||||
touchBar.defaultItemIdentifiers = [
|
touchBar.defaultItemIdentifiers = Array(items.keys)
|
||||||
.escButton,
|
|
||||||
.dismissButton,
|
|
||||||
|
|
||||||
.brightDown,
|
|
||||||
.brightUp,
|
|
||||||
|
|
||||||
.flexibleSpace,
|
|
||||||
|
|
||||||
.prev,
|
|
||||||
.play,
|
|
||||||
.next,
|
|
||||||
|
|
||||||
.sleep,
|
|
||||||
.weather,
|
|
||||||
|
|
||||||
.volumeDown,
|
|
||||||
.volumeUp,
|
|
||||||
.battery,
|
|
||||||
.time,
|
|
||||||
]
|
|
||||||
self.presentTouchBar()
|
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() {
|
func setupControlStripPresence() {
|
||||||
DFRSystemModalShowsCloseBoxWhenFrontMost(false)
|
DFRSystemModalShowsCloseBoxWhenFrontMost(false)
|
||||||
let item = NSCustomTouchBarItem(identifier: .controlStripItem)
|
let item = NSCustomTouchBarItem(identifier: .controlStripItem)
|
||||||
@ -69,6 +89,19 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
|
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 {
|
switch identifier {
|
||||||
case .escButton:
|
case .escButton:
|
||||||
return CustomButtonTouchBarItem(identifier: identifier, title: "esc", key: ESCKeyPress())
|
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