diff --git a/MTMR/AppDelegate.swift b/MTMR/AppDelegate.swift index 9838b67..051be3f 100644 --- a/MTMR/AppDelegate.swift +++ b/MTMR/AppDelegate.swift @@ -47,6 +47,22 @@ class AppDelegate: NSObject, NSApplicationDelegate { TouchBarController.shared.resetControlStrip() } + @objc func toggleBlackListedApp(_ sender: Any?) { + let appIdentifier = TouchBarController.shared.frontmostApplicationIdentifier + if appIdentifier != nil { + if let index = TouchBarController.shared.blacklistAppIdentifiers.index(of: appIdentifier!) { + TouchBarController.shared.blacklistAppIdentifiers.remove(at: index) + } else { + TouchBarController.shared.blacklistAppIdentifiers.append(appIdentifier!) + } + + UserDefaults.standard.set(TouchBarController.shared.blacklistAppIdentifiers, forKey: "com.toxblh.mtmr.blackListedApps") + UserDefaults.standard.synchronize() + + TouchBarController.shared.updateActiveApp() + } + } + @objc func openPreset(_ sender: Any?) { let dialog = NSOpenPanel(); @@ -78,6 +94,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { menu.addItem(withTitle: "Reload Preset", action: #selector(updatePreset(_:)), keyEquivalent: "r") menu.addItem(withTitle: "Open Preset", action: #selector(openPreset(_:)), keyEquivalent: "O") menu.addItem(withTitle: TouchBarController.shared.controlStripState ? "Hide Control Strip" : "Show Control Strip" , action: #selector(toggleControlStrip(_:)), keyEquivalent: "T") + menu.addItem(withTitle: "Toggle blackList current app" , action: #selector(toggleBlackListedApp(_:)), keyEquivalent: "B") menu.addItem(NSMenuItem.separator()) menu.addItem(withTitle: "Quit", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q") statusItem.menu = menu diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index e916a5f..fe13822 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -72,10 +72,26 @@ class TouchBarController: NSObject, NSTouchBarDelegate { } } + var blacklistAppIdentifiers: [String] = [] + var frontmostApplicationIdentifier: String? { + get { + guard let frontmostId = NSWorkspace.shared.frontmostApplication?.bundleIdentifier else { return nil } + return frontmostId + } + } + private override init() { super.init() SupportedTypesHolder.sharedInstance.register(typename: "exitTouchbar", item: .staticButton(title: "exit"), action: .custom(closure: { [weak self] in self?.dismissTouchBar()}), longAction: .none) + if let blackListed = UserDefaults.standard.stringArray(forKey: "com.toxblh.mtmr.blackListedApps") { + self.blacklistAppIdentifiers = blackListed + } + + NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didLaunchApplicationNotification, 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) + createAndUpdatePreset() } @@ -107,7 +123,20 @@ class TouchBarController: NSObject, NSTouchBarDelegate { touchBar.delegate = self touchBar.defaultItemIdentifiers = [] touchBar.defaultItemIdentifiers = self.leftIdentifiers + [centerScrollArea] + self.rightIdentifiers - self.presentTouchBar() + + self.updateActiveApp() + } + + @objc func activeApplicationChanged(_ n: Notification) { + updateActiveApp() + } + + func updateActiveApp() { + if self.blacklistAppIdentifiers.index(of: self.frontmostApplicationIdentifier!) != nil { + DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, false) + } else { + presentTouchBar() + } } func readConfig() -> [BarItemDefinition]? {