Skip to content

Commit

Permalink
implement regex filter for dock items
Browse files Browse the repository at this point in the history
  • Loading branch information
ReDetection committed Oct 27, 2019
1 parent e9a7b6d commit 6920664
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
18 changes: 4 additions & 14 deletions MTMR/ItemsParsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -383,6 +371,7 @@ enum ItemType: Decodable {
case restTime
case flip
case autoResize
case filter
case disableMarquee
}

Expand Down Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions MTMR/TouchBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 15 additions & 9 deletions MTMR/Widgets/AppScrubberTouchBarItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand All @@ -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)
Expand All @@ -43,6 +45,7 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem {
applications = launchedApplications()
applications += getDockPersistentAppsList()
reloadData()
softReloadItems()
updateSize()
}

Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ To close a group, use the button:
```js
{
"type": "dock",
"filter": "(^Xcode$)|(Safari)|(.*player)",
"autoResize": true
},
```
Expand Down

0 comments on commit 6920664

Please sign in to comment.