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

handy method to open particular preset file, expose bug with wrong preset reload on group close

This commit is contained in:
Serg 2018-06-03 00:49:48 +07:00
parent d03b16eb89
commit 958f93f440
3 changed files with 19 additions and 25 deletions

View File

@ -71,17 +71,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
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, longAction: .none, additionalParameters: [:])]
TouchBarController.shared.touchbarNeedRefresh = true;
TouchBarController.shared.createAndUpdatePreset(newJsonItems: jsonItems)
}
if dialog.runModal() == .OK, let path = dialog.url?.path {
TouchBarController.shared.reloadPreset(path: path)
}
}
@ -123,7 +114,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
func reloadOnDefaultConfigChanged() {
let file = NSURL.fileURL(withPath: NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!.appending("/MTMR/items.json"))
let file = NSURL.fileURL(withPath: standardConfigPath)
let fd = open(file.path, O_EVTONLY)
@ -132,11 +123,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
self.fileSystemSource?.setEventHandler(handler: {
print("Config changed, reloading...")
DispatchQueue.main.async {
let jsonData = file.path.fileData
let jsonItems = jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])]
TouchBarController.shared.touchbarNeedRefresh = true;
TouchBarController.shared.createAndUpdatePreset(newJsonItems: jsonItems)
TouchBarController.shared.reloadPreset(path: file.path)
}
})

View File

@ -13,6 +13,9 @@ struct ExactItem {
let presetItem: BarItemDefinition
}
let appSupportDirectory = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!.appending("/MTMR")
let standardConfigPath = appSupportDirectory.appending("/items.json")
extension ItemType {
var identifierBase: String {
@ -93,7 +96,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
return (item: .staticButton(title: ""), action: .custom(closure: { [weak self] in
guard let `self` = self else { return }
self.touchbarNeedRefresh = true
self.createAndUpdatePreset(newJsonItems: self.standardConfig() ?? [])
self.reloadStandardConfig() //fixme current config, not standard one!
}), longAction: .none, parameters: [.width: .width(30), .image: .image(source: (NSImage(named: .stopProgressFreestandingTemplate))!)])
}
@ -105,7 +108,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didTerminateApplicationNotification, object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didActivateApplicationNotification, object: nil)
createAndUpdatePreset(newJsonItems: standardConfig() ?? [])
reloadPreset(path: standardConfigPath)
}
func createAndUpdatePreset(newJsonItems: [BarItemDefinition]) {
@ -151,18 +154,21 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
}
}
func standardConfig() -> [BarItemDefinition]? {
let appSupportDirectory = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!.appending("/MTMR")
let presetPath = appSupportDirectory.appending("/items.json")
func reloadStandardConfig() {
let presetPath = standardConfigPath
if !FileManager.default.fileExists(atPath: presetPath),
let defaultPreset = Bundle.main.path(forResource: "defaultPreset", ofType: "json") {
try? FileManager.default.createDirectory(atPath: appSupportDirectory, withIntermediateDirectories: true, attributes: nil)
try? FileManager.default.copyItem(atPath: defaultPreset, toPath: presetPath)
}
let jsonData = presetPath.fileData
reloadPreset(path: presetPath)
}
return jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])]
func reloadPreset(path: String) {
let items = path.fileData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])]
touchbarNeedRefresh = true
createAndUpdatePreset(newJsonItems: items)
}
func loadItemDefinitions(jsonItems: [BarItemDefinition]) {

View File

@ -3,3 +3,4 @@
* try view controllers on `NSCustomTouchBarItem` instead of subclassing item itself
* try move away from enums when parse preset enums are hard to extend
* find better way to hide bar items
* extract bar items creating from TouchBarController to separate class, cover with tests