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

Merge pull request #93 from ReDetection/cleanup-and-fix-close-group

Fix close group and cleanup
This commit is contained in:
Anton Palgunov 2018-06-04 11:19:51 +01:00 committed by GitHub
commit dc94857ac4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 40 deletions

View File

@ -37,10 +37,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
task.launch() task.launch()
} }
// @objc func updatePreset(_ sender: Any?) {
// TouchBarController.shared.createAndUpdatePreset()
// }
@objc func toggleControlStrip(_ sender: Any?) { @objc func toggleControlStrip(_ sender: Any?) {
TouchBarController.shared.controlStripState = !TouchBarController.shared.controlStripState TouchBarController.shared.controlStripState = !TouchBarController.shared.controlStripState
createMenu() createMenu()
@ -75,17 +71,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
dialog.allowedFileTypes = ["json"] dialog.allowedFileTypes = ["json"]
dialog.directoryURL = NSURL.fileURL(withPath: NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!.appending("/MTMR"), isDirectory: true) dialog.directoryURL = NSURL.fileURL(withPath: NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!.appending("/MTMR"), isDirectory: true)
if (dialog.runModal() == NSApplication.ModalResponse.OK) { if dialog.runModal() == .OK, let path = dialog.url?.path {
let result = dialog.url TouchBarController.shared.reloadPreset(path: path)
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)
}
} }
} }
@ -127,7 +114,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
} }
func reloadOnDefaultConfigChanged() { 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) let fd = open(file.path, O_EVTONLY)
@ -136,11 +123,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
self.fileSystemSource?.setEventHandler(handler: { self.fileSystemSource?.setEventHandler(handler: {
print("Config changed, reloading...") print("Config changed, reloading...")
DispatchQueue.main.async { DispatchQueue.main.async {
let jsonData = file.path.fileData TouchBarController.shared.reloadPreset(path: file.path)
let jsonItems = jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])]
TouchBarController.shared.touchbarNeedRefresh = true;
TouchBarController.shared.createAndUpdatePreset(newJsonItems: jsonItems)
} }
}) })

View File

@ -13,6 +13,9 @@ struct ExactItem {
let presetItem: BarItemDefinition let presetItem: BarItemDefinition
} }
let appSupportDirectory = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!.appending("/MTMR")
let standardConfigPath = appSupportDirectory.appending("/items.json")
extension ItemType { extension ItemType {
var identifierBase: String { var identifierBase: String {
@ -56,7 +59,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
var touchBar: NSTouchBar! var touchBar: NSTouchBar!
var jsonItems: [BarItemDefinition]? fileprivate var lastPresetPath = ""
var jsonItems: [BarItemDefinition] = []
var itemDefinitions: [NSTouchBarItem.Identifier: BarItemDefinition] = [:] var itemDefinitions: [NSTouchBarItem.Identifier: BarItemDefinition] = [:]
var items: [NSTouchBarItem.Identifier: NSTouchBarItem] = [:] var items: [NSTouchBarItem.Identifier: NSTouchBarItem] = [:]
var leftIdentifiers: [NSTouchBarItem.Identifier] = [] var leftIdentifiers: [NSTouchBarItem.Identifier] = []
@ -91,8 +95,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
SupportedTypesHolder.sharedInstance.register(typename: "close") { _ in SupportedTypesHolder.sharedInstance.register(typename: "close") { _ in
return (item: .staticButton(title: ""), action: .custom(closure: { [weak self] in return (item: .staticButton(title: ""), action: .custom(closure: { [weak self] in
self?.touchbarNeedRefresh = true guard let `self` = self else { return }
self?.createAndUpdatePreset() self.reloadPreset(path: self.lastPresetPath)
}), longAction: .none, parameters: [.width: .width(30), .image: .image(source: (NSImage(named: .stopProgressFreestandingTemplate))!)]) }), 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.didTerminateApplicationNotification, object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didActivateApplicationNotification, 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 { if let oldBar = self.touchBar {
NSTouchBar.minimizeSystemModalFunctionBar(oldBar) NSTouchBar.minimizeSystemModalFunctionBar(oldBar)
} }
self.touchBar = NSTouchBar() self.touchBar = NSTouchBar()
if (newJsonItems != nil) { self.jsonItems = newJsonItems
self.jsonItems = newJsonItems
}
self.itemDefinitions = [:] self.itemDefinitions = [:]
self.items = [:] self.items = [:]
self.leftIdentifiers = [] self.leftIdentifiers = []
self.centerItems = [] self.centerItems = []
self.rightIdentifiers = [] self.rightIdentifiers = []
if (self.jsonItems == nil) { loadItemDefinitions(jsonItems: self.jsonItems)
self.jsonItems = readConfig()
}
loadItemDefinitions(jsonItems: self.jsonItems!)
createItems() createItems()
centerItems = centerIdentifiers.compactMap({ (identifier) -> NSTouchBarItem? in centerItems = centerIdentifiers.compactMap({ (identifier) -> NSTouchBarItem? in
@ -156,18 +154,22 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
} }
} }
func readConfig() -> [BarItemDefinition]? { func reloadStandardConfig() {
let appSupportDirectory = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!.appending("/MTMR") let presetPath = standardConfigPath
let presetPath = appSupportDirectory.appending("/items.json")
if !FileManager.default.fileExists(atPath: presetPath), if !FileManager.default.fileExists(atPath: presetPath),
let defaultPreset = Bundle.main.path(forResource: "defaultPreset", ofType: "json") { let defaultPreset = Bundle.main.path(forResource: "defaultPreset", ofType: "json") {
try? FileManager.default.createDirectory(atPath: appSupportDirectory, withIntermediateDirectories: true, attributes: nil) try? FileManager.default.createDirectory(atPath: appSupportDirectory, withIntermediateDirectories: true, attributes: nil)
try? FileManager.default.copyItem(atPath: defaultPreset, toPath: presetPath) 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) {
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]) { func loadItemDefinitions(jsonItems: [BarItemDefinition]) {

View File

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