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

simplify logic

This commit is contained in:
Serg 2018-06-03 00:28:27 +07:00
parent ea573882a0
commit d03b16eb89
2 changed files with 9 additions and 18 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()

View File

@ -56,7 +56,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
var touchBar: NSTouchBar! var touchBar: NSTouchBar!
var jsonItems: [BarItemDefinition]? 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 +91,9 @@ 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.touchbarNeedRefresh = true
self.createAndUpdatePreset(newJsonItems: self.standardConfig() ?? [])
}), 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 +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.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() createAndUpdatePreset(newJsonItems: standardConfig() ?? [])
} }
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,7 +151,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
} }
} }
func readConfig() -> [BarItemDefinition]? { func standardConfig() -> [BarItemDefinition]? {
let appSupportDirectory = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!.appending("/MTMR") let appSupportDirectory = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!.appending("/MTMR")
let presetPath = appSupportDirectory.appending("/items.json") let presetPath = appSupportDirectory.appending("/items.json")
if !FileManager.default.fileExists(atPath: presetPath), if !FileManager.default.fileExists(atPath: presetPath),