From 6920664fad600d7122cf4edca4e5f45142711274 Mon Sep 17 00:00:00 2001 From: Serg Date: Sun, 27 Oct 2019 15:29:52 +0700 Subject: [PATCH] implement regex filter for dock items --- MTMR/ItemsParsing.swift | 18 ++++------------ MTMR/TouchBarController.swift | 14 ++++++++++--- MTMR/Widgets/AppScrubberTouchBarItem.swift | 24 ++++++++++++++-------- README.md | 1 + 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index ac43767..ab9b5ae 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -211,18 +211,6 @@ class SupportedTypesHolder { ) }, - "dock": { decoder in - enum CodingKeys: String, CodingKey { case autoResize } - let container = try decoder.container(keyedBy: CodingKeys.self) - let autoResize = try container.decodeIfPresent(Bool.self, forKey: .autoResize) ?? false - return ( - item: .dock(autoResize: autoResize), - action: .none, - longAction: .none, - parameters: [:] - ) - }, - "inputsource": { _ in ( item: .inputsource, @@ -346,7 +334,7 @@ enum ItemType: Decodable { case shellScriptTitledButton(source: SourceProtocol, refreshInterval: Double) case timeButton(formatTemplate: String, timeZone: String?, locale: String?) case battery - case dock(autoResize: Bool) + case dock(autoResize: Bool, filter: String?) case volume case brightness(refreshInterval: Double) case weather(interval: Double, units: String, api_key: String, icon_type: String) @@ -383,6 +371,7 @@ enum ItemType: Decodable { case restTime case flip case autoResize + case filter case disableMarquee } @@ -437,7 +426,8 @@ enum ItemType: Decodable { case .dock: let autoResize = try container.decodeIfPresent(Bool.self, forKey: .autoResize) ?? false - self = .dock(autoResize: autoResize) + let filterRegexString = try container.decodeIfPresent(String.self, forKey: .filter) + self = .dock(autoResize: autoResize, filter: filterRegexString) case .volume: self = .volume diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 941711d..6ff9a5e 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -29,7 +29,7 @@ extension ItemType { return "com.toxblh.mtmr.timeButton." case .battery: return "com.toxblh.mtmr.battery." - case .dock(autoResize: _): + case .dock(autoResize: _, filter: _): return "com.toxblh.mtmr.dock" case .volume: return "com.toxblh.mtmr.volume" @@ -248,8 +248,16 @@ class TouchBarController: NSObject, NSTouchBarDelegate { barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template, timeZone: timeZone, locale: locale) case .battery: barItem = BatteryBarItem(identifier: identifier) - case let .dock(autoResize: autoResize): - barItem = AppScrubberTouchBarItem(identifier: identifier, autoResize: autoResize) + case let .dock(autoResize: autoResize, filter: regexString): + if let regexString = regexString { + guard let regex = try? NSRegularExpression(pattern: regexString, options: []) else { + barItem = CustomButtonTouchBarItem(identifier: identifier, title: "Bad regex") + break + } + barItem = AppScrubberTouchBarItem(identifier: identifier, autoResize: autoResize, filter: regex) + } else { + barItem = AppScrubberTouchBarItem(identifier: identifier, autoResize: autoResize) + } case .volume: if case let .image(source)? = item.additionalParameters[.image] { barItem = VolumeViewController(identifier: identifier, image: source.image) diff --git a/MTMR/Widgets/AppScrubberTouchBarItem.swift b/MTMR/Widgets/AppScrubberTouchBarItem.swift index 95b341b..adc5541 100644 --- a/MTMR/Widgets/AppScrubberTouchBarItem.swift +++ b/MTMR/Widgets/AppScrubberTouchBarItem.swift @@ -11,6 +11,7 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem { private var scrollView = NSScrollView() private var autoResize: Bool = false private var widthConstraint: NSLayoutConstraint? + private let filter: NSRegularExpression? private var persistentAppIdentifiers: [String] = [] private var runningAppsIdentifiers: [String] = [] @@ -22,9 +23,10 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem { private var applications: [DockItem] = [] private var items: [DockBarItem] = [] - init(identifier: NSTouchBarItem.Identifier, autoResize: Bool = false) { + init(identifier: NSTouchBarItem.Identifier, autoResize: Bool = false, filter: NSRegularExpression? = nil) { + self.filter = filter super.init(identifier: identifier) - self.autoResize = autoResize //todo + self.autoResize = autoResize view = scrollView NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(hardReloadItems), name: NSWorkspace.didLaunchApplicationNotification, object: nil) @@ -43,6 +45,7 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem { applications = launchedApplications() applications += getDockPersistentAppsList() reloadData() + softReloadItems() updateSize() } @@ -67,8 +70,7 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem { } func reloadData() { - let frontMostAppId = self.frontmostApplicationIdentifier - items = applications.map { self.createAppButton(for: $0, isFrontmost: $0.bundleIdentifier == frontMostAppId) } + items = applications.map { self.createAppButton(for: $0) } let stackView = NSStackView(views: items.compactMap { $0.view }) stackView.spacing = 1 stackView.orientation = .horizontal @@ -77,8 +79,8 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem { stackView.scroll(visibleRect.origin) } - public func createAppButton(for app: DockItem, isFrontmost: Bool) -> DockBarItem { - let item = DockBarItem(app, isRunning: runningAppsIdentifiers.contains(app.bundleIdentifier), isFrontmost: isFrontmost) + public func createAppButton(for app: DockItem) -> DockBarItem { + let item = DockBarItem(app) item.isBordered = false item.tapClosure = { [weak self] in self?.switchToApp(app: app) @@ -134,7 +136,12 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem { for app in NSWorkspace.shared.runningApplications { guard app.activationPolicy == NSApplication.ActivationPolicy.regular else { continue } guard let bundleIdentifier = app.bundleIdentifier else { continue } - + if let filter = self.filter, + let name = app.localizedName, + filter.numberOfMatches(in: name, options: [], range: NSRange(location: 0, length: name.count)) == 0 { + continue + } + runningAppsIdentifiers.append(bundleIdentifier) let dockItem = DockItem(bundleIdentifier: bundleIdentifier, icon: app.icon ?? getIcon(forBundleIdentifier: bundleIdentifier), pid: app.processIdentifier) @@ -200,11 +207,10 @@ class DockBarItem: CustomButtonTouchBarItem { } } - init(_ app: DockItem, isRunning: Bool, isFrontmost: Bool) { + init(_ app: DockItem) { self.dockItem = app super.init(identifier: .init(app.bundleIdentifier), title: "") dotView.wantsLayer = true - self.isRunning = isRunning image = app.icon image?.size = NSSize(width: iconWidth, height: iconWidth) diff --git a/README.md b/README.md index c895537..2bab332 100644 --- a/README.md +++ b/README.md @@ -288,6 +288,7 @@ To close a group, use the button: ```js { "type": "dock", + "filter": "(^Xcode$)|(Safari)|(.*player)", "autoResize": true }, ```