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

Merge pull request #25 from ad/openPresetFile

+ open preset from dialog (not replacing default file)
This commit is contained in:
Anton Palgunov 2018-04-19 16:19:45 +01:00 committed by GitHub
commit 03bcafd5ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 6 deletions

View File

@ -39,10 +39,36 @@ 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"]
dialog.directoryURL = NSURL.fileURL(withPath: NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!.appending("/MTMR"), isDirectory: true)
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

@ -64,14 +64,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
@ -81,8 +85,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
touchBar.defaultItemIdentifiers = self.leftIdentifiers + [.centerScrollArea] + self.rightIdentifiers
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),
@ -90,9 +94,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: [:])]
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)