1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-11 17:38:38 +00:00

+ open preset from dialog (not replacing default file)

This commit is contained in:
ad 2018-04-19 18:01:16 +03:00
parent adaed36fbd
commit 3a57269510
2 changed files with 39 additions and 6 deletions

View File

@ -39,10 +39,35 @@ class AppDelegate: NSObject, NSApplicationDelegate {
TouchBarController.shared.createAndUpdatePreset()
}
@objc func openPreset(_ sender: Any?) {
let dialog = NSOpenPanel();
dialog.title = "Choose a items.json file";
dialog.showsResizeIndicator = true;
dialog.showsHiddenFiles = true;
dialog.canChooseDirectories = false;
dialog.canCreateDirectories = false;
dialog.allowsMultipleSelection = false;
dialog.allowedFileTypes = ["json"];
if (dialog.runModal() == NSApplication.ModalResponse.OK) {
let result = dialog.url
if (result != nil) {
let path = result!.path
let jsonData = path.fileData
let jsonItems = jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, additionalParameters: [:])]
TouchBarController.shared.createAndUpdatePreset(jsonItems: jsonItems)
}
}
}
func createMenu() {
let menu = NSMenu()
menu.addItem(withTitle: "Preferences", action: #selector(openPrefereces(_:)), keyEquivalent: ",")
menu.addItem(withTitle: "Reload Preset", action: #selector(updatePreset(_:)), keyEquivalent: "r")
menu.addItem(withTitle: "Open Preset", action: #selector(openPreset(_:)), keyEquivalent: "O")
menu.addItem(NSMenuItem.separator())
menu.addItem(withTitle: "Quit", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")
statusItem.menu = menu

View File

@ -60,14 +60,18 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
createAndUpdatePreset()
}
func createAndUpdatePreset() {
func createAndUpdatePreset(jsonItems: [BarItemDefinition]? = nil) {
var jsonItems = jsonItems
self.itemDefinitions = [:]
self.items = [:]
self.leftIdentifiers = []
self.centerItems = []
self.rightIdentifiers = []
loadItemDefinitions()
if (jsonItems == nil) {
jsonItems = readConfig()
}
loadItemDefinitions(jsonItems: jsonItems!)
createItems()
centerItems = self.itemDefinitions.compactMap { (identifier, definition) -> NSTouchBarItem? in
return definition.align == .center ? items[identifier] : nil
@ -78,7 +82,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
self.presentTouchBar()
}
func loadItemDefinitions() {
func readConfig() -> [BarItemDefinition]? {
let appSupportDirectory = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!.appending("/MTMR")
let presetPath = appSupportDirectory.appending("/items.json")
if !FileManager.default.fileExists(atPath: presetPath),
@ -86,9 +90,13 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
try? FileManager.default.createDirectory(atPath: appSupportDirectory, withIntermediateDirectories: true, attributes: nil)
try? FileManager.default.copyItem(atPath: defaultPreset, toPath: presetPath)
}
let jsonData = presetPath.fileData
let jsonItems = jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, additionalParameters: [:])]
let jsonData = presetPath.fileData
return jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, additionalParameters: [:])]
}
func loadItemDefinitions(jsonItems: [BarItemDefinition]) {
for item in jsonItems {
let identifierString = item.type.identifierBase.appending(UUID().uuidString)
let identifier = NSTouchBarItem.Identifier(identifierString)