From d03b16eb89bd29d37530102e0baba85b9548b286 Mon Sep 17 00:00:00 2001 From: Serg Date: Sun, 3 Jun 2018 00:28:27 +0700 Subject: [PATCH 1/3] simplify logic --- MTMR/AppDelegate.swift | 4 ---- MTMR/TouchBarController.swift | 23 +++++++++-------------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/MTMR/AppDelegate.swift b/MTMR/AppDelegate.swift index 547f58a..bb6c3d9 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() diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 4c78d1a..cea56f0 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -56,7 +56,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate { var touchBar: NSTouchBar! - var jsonItems: [BarItemDefinition]? + var jsonItems: [BarItemDefinition] = [] var itemDefinitions: [NSTouchBarItem.Identifier: BarItemDefinition] = [:] var items: [NSTouchBarItem.Identifier: NSTouchBarItem] = [:] var leftIdentifiers: [NSTouchBarItem.Identifier] = [] @@ -91,8 +91,9 @@ 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.touchbarNeedRefresh = true + self.createAndUpdatePreset(newJsonItems: self.standardConfig() ?? []) }), longAction: .none, parameters: [.width: .width(30), .image: .image(source: (NSImage(named: .stopProgressFreestandingTemplate))!)]) } @@ -104,28 +105,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() + createAndUpdatePreset(newJsonItems: standardConfig() ?? []) } - 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,7 +151,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate { } } - func readConfig() -> [BarItemDefinition]? { + func standardConfig() -> [BarItemDefinition]? { let appSupportDirectory = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!.appending("/MTMR") let presetPath = appSupportDirectory.appending("/items.json") if !FileManager.default.fileExists(atPath: presetPath), From 958f93f4409220db769272afaedc6867d697dd64 Mon Sep 17 00:00:00 2001 From: Serg Date: Sun, 3 Jun 2018 00:49:48 +0700 Subject: [PATCH 2/3] handy method to open particular preset file, expose bug with wrong preset reload on group close --- MTMR/AppDelegate.swift | 21 ++++----------------- MTMR/TouchBarController.swift | 22 ++++++++++++++-------- TECHNICAL_DEBT.md | 1 + 3 files changed, 19 insertions(+), 25 deletions(-) diff --git a/MTMR/AppDelegate.swift b/MTMR/AppDelegate.swift index bb6c3d9..f767fcf 100644 --- a/MTMR/AppDelegate.swift +++ b/MTMR/AppDelegate.swift @@ -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) } }) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index cea56f0..bde2f65 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 { @@ -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 - - return jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])] + reloadPreset(path: presetPath) + } + + 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]) { 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 From ec228fdddef666e964a98cf2c46480a278b15f7d Mon Sep 17 00:00:00 2001 From: Serg Date: Sun, 3 Jun 2018 10:30:03 +0700 Subject: [PATCH 3/3] fix wrong preset on close group --- MTMR/TouchBarController.swift | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index bde2f65..6aa8a60 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -59,6 +59,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate { var touchBar: NSTouchBar! + fileprivate var lastPresetPath = "" var jsonItems: [BarItemDefinition] = [] var itemDefinitions: [NSTouchBarItem.Identifier: BarItemDefinition] = [:] var items: [NSTouchBarItem.Identifier: NSTouchBarItem] = [:] @@ -95,8 +96,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate { SupportedTypesHolder.sharedInstance.register(typename: "close") { _ in return (item: .staticButton(title: ""), action: .custom(closure: { [weak self] in guard let `self` = self else { return } - self.touchbarNeedRefresh = true - self.reloadStandardConfig() //fixme current config, not standard one! + self.reloadPreset(path: self.lastPresetPath) }), longAction: .none, parameters: [.width: .width(30), .image: .image(source: (NSImage(named: .stopProgressFreestandingTemplate))!)]) } @@ -108,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) - reloadPreset(path: standardConfigPath) + reloadStandardConfig() } func createAndUpdatePreset(newJsonItems: [BarItemDefinition]) { @@ -166,6 +166,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate { } 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)