diff --git a/MTMR/AppDelegate.swift b/MTMR/AppDelegate.swift index 547f58a..f767fcf 100644 --- a/MTMR/AppDelegate.swift +++ b/MTMR/AppDelegate.swift @@ -37,10 +37,6 @@ class AppDelegate: NSObject, NSApplicationDelegate { task.launch() } -// @objc func updatePreset(_ sender: Any?) { -// TouchBarController.shared.createAndUpdatePreset() -// } - @objc func toggleControlStrip(_ sender: Any?) { TouchBarController.shared.controlStripState = !TouchBarController.shared.controlStripState createMenu() @@ -75,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) } } @@ -127,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) @@ -136,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) } }) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 4c78d1a..6aa8a60 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -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 { @@ -56,7 +59,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate { var touchBar: NSTouchBar! - var jsonItems: [BarItemDefinition]? + fileprivate var lastPresetPath = "" + var jsonItems: [BarItemDefinition] = [] var itemDefinitions: [NSTouchBarItem.Identifier: BarItemDefinition] = [:] var items: [NSTouchBarItem.Identifier: NSTouchBarItem] = [:] var leftIdentifiers: [NSTouchBarItem.Identifier] = [] @@ -91,8 +95,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate { SupportedTypesHolder.sharedInstance.register(typename: "close") { _ in return (item: .staticButton(title: ""), action: .custom(closure: { [weak self] in - self?.touchbarNeedRefresh = true - self?.createAndUpdatePreset() + guard let `self` = self else { return } + self.reloadPreset(path: self.lastPresetPath) }), longAction: .none, parameters: [.width: .width(30), .image: .image(source: (NSImage(named: .stopProgressFreestandingTemplate))!)]) } @@ -104,28 +108,22 @@ 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() + reloadStandardConfig() } - func createAndUpdatePreset(newJsonItems: [BarItemDefinition]? = nil) { + func createAndUpdatePreset(newJsonItems: [BarItemDefinition]) { if let oldBar = self.touchBar { NSTouchBar.minimizeSystemModalFunctionBar(oldBar) } self.touchBar = NSTouchBar() - if (newJsonItems != nil) { - self.jsonItems = newJsonItems - } + self.jsonItems = newJsonItems self.itemDefinitions = [:] self.items = [:] self.leftIdentifiers = [] self.centerItems = [] self.rightIdentifiers = [] - if (self.jsonItems == nil) { - self.jsonItems = readConfig() - } - - loadItemDefinitions(jsonItems: self.jsonItems!) + loadItemDefinitions(jsonItems: self.jsonItems) createItems() centerItems = centerIdentifiers.compactMap({ (identifier) -> NSTouchBarItem? in @@ -156,18 +154,22 @@ class TouchBarController: NSObject, NSTouchBarDelegate { } } - func readConfig() -> [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 - - return jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])] + reloadPreset(path: presetPath) + } + + func reloadPreset(path: String) { + lastPresetPath = path + 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]) { diff --git a/TECHNICAL_DEBT.md b/TECHNICAL_DEBT.md index 8825331..94f77b3 100644 --- a/TECHNICAL_DEBT.md +++ b/TECHNICAL_DEBT.md @@ -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