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

implement regex filter for dock items

This commit is contained in:
Serg 2019-10-27 15:29:52 +07:00
parent e9a7b6d32a
commit 6920664fad
4 changed files with 31 additions and 26 deletions

View File

@ -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 "inputsource": { _ in
( (
item: .inputsource, item: .inputsource,
@ -346,7 +334,7 @@ enum ItemType: Decodable {
case shellScriptTitledButton(source: SourceProtocol, refreshInterval: Double) case shellScriptTitledButton(source: SourceProtocol, refreshInterval: Double)
case timeButton(formatTemplate: String, timeZone: String?, locale: String?) case timeButton(formatTemplate: String, timeZone: String?, locale: String?)
case battery case battery
case dock(autoResize: Bool) case dock(autoResize: Bool, filter: String?)
case volume case volume
case brightness(refreshInterval: Double) case brightness(refreshInterval: Double)
case weather(interval: Double, units: String, api_key: String, icon_type: String) case weather(interval: Double, units: String, api_key: String, icon_type: String)
@ -383,6 +371,7 @@ enum ItemType: Decodable {
case restTime case restTime
case flip case flip
case autoResize case autoResize
case filter
case disableMarquee case disableMarquee
} }
@ -437,7 +426,8 @@ enum ItemType: Decodable {
case .dock: case .dock:
let autoResize = try container.decodeIfPresent(Bool.self, forKey: .autoResize) ?? false 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: case .volume:
self = .volume self = .volume

View File

@ -29,7 +29,7 @@ extension ItemType {
return "com.toxblh.mtmr.timeButton." return "com.toxblh.mtmr.timeButton."
case .battery: case .battery:
return "com.toxblh.mtmr.battery." return "com.toxblh.mtmr.battery."
case .dock(autoResize: _): case .dock(autoResize: _, filter: _):
return "com.toxblh.mtmr.dock" return "com.toxblh.mtmr.dock"
case .volume: case .volume:
return "com.toxblh.mtmr.volume" return "com.toxblh.mtmr.volume"
@ -248,8 +248,16 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template, timeZone: timeZone, locale: locale) barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template, timeZone: timeZone, locale: locale)
case .battery: case .battery:
barItem = BatteryBarItem(identifier: identifier) barItem = BatteryBarItem(identifier: identifier)
case let .dock(autoResize: autoResize): case let .dock(autoResize: autoResize, filter: regexString):
barItem = AppScrubberTouchBarItem(identifier: identifier, autoResize: autoResize) 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: case .volume:
if case let .image(source)? = item.additionalParameters[.image] { if case let .image(source)? = item.additionalParameters[.image] {
barItem = VolumeViewController(identifier: identifier, image: source.image) barItem = VolumeViewController(identifier: identifier, image: source.image)

View File

@ -11,6 +11,7 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem {
private var scrollView = NSScrollView() private var scrollView = NSScrollView()
private var autoResize: Bool = false private var autoResize: Bool = false
private var widthConstraint: NSLayoutConstraint? private var widthConstraint: NSLayoutConstraint?
private let filter: NSRegularExpression?
private var persistentAppIdentifiers: [String] = [] private var persistentAppIdentifiers: [String] = []
private var runningAppsIdentifiers: [String] = [] private var runningAppsIdentifiers: [String] = []
@ -22,9 +23,10 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem {
private var applications: [DockItem] = [] private var applications: [DockItem] = []
private var items: [DockBarItem] = [] 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) super.init(identifier: identifier)
self.autoResize = autoResize //todo self.autoResize = autoResize
view = scrollView view = scrollView
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(hardReloadItems), name: NSWorkspace.didLaunchApplicationNotification, object: nil) NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(hardReloadItems), name: NSWorkspace.didLaunchApplicationNotification, object: nil)
@ -43,6 +45,7 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem {
applications = launchedApplications() applications = launchedApplications()
applications += getDockPersistentAppsList() applications += getDockPersistentAppsList()
reloadData() reloadData()
softReloadItems()
updateSize() updateSize()
} }
@ -67,8 +70,7 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem {
} }
func reloadData() { func reloadData() {
let frontMostAppId = self.frontmostApplicationIdentifier items = applications.map { self.createAppButton(for: $0) }
items = applications.map { self.createAppButton(for: $0, isFrontmost: $0.bundleIdentifier == frontMostAppId) }
let stackView = NSStackView(views: items.compactMap { $0.view }) let stackView = NSStackView(views: items.compactMap { $0.view })
stackView.spacing = 1 stackView.spacing = 1
stackView.orientation = .horizontal stackView.orientation = .horizontal
@ -77,8 +79,8 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem {
stackView.scroll(visibleRect.origin) stackView.scroll(visibleRect.origin)
} }
public func createAppButton(for app: DockItem, isFrontmost: Bool) -> DockBarItem { public func createAppButton(for app: DockItem) -> DockBarItem {
let item = DockBarItem(app, isRunning: runningAppsIdentifiers.contains(app.bundleIdentifier), isFrontmost: isFrontmost) let item = DockBarItem(app)
item.isBordered = false item.isBordered = false
item.tapClosure = { [weak self] in item.tapClosure = { [weak self] in
self?.switchToApp(app: app) self?.switchToApp(app: app)
@ -134,6 +136,11 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem {
for app in NSWorkspace.shared.runningApplications { for app in NSWorkspace.shared.runningApplications {
guard app.activationPolicy == NSApplication.ActivationPolicy.regular else { continue } guard app.activationPolicy == NSApplication.ActivationPolicy.regular else { continue }
guard let bundleIdentifier = app.bundleIdentifier 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) runningAppsIdentifiers.append(bundleIdentifier)
@ -200,11 +207,10 @@ class DockBarItem: CustomButtonTouchBarItem {
} }
} }
init(_ app: DockItem, isRunning: Bool, isFrontmost: Bool) { init(_ app: DockItem) {
self.dockItem = app self.dockItem = app
super.init(identifier: .init(app.bundleIdentifier), title: "") super.init(identifier: .init(app.bundleIdentifier), title: "")
dotView.wantsLayer = true dotView.wantsLayer = true
self.isRunning = isRunning
image = app.icon image = app.icon
image?.size = NSSize(width: iconWidth, height: iconWidth) image?.size = NSSize(width: iconWidth, height: iconWidth)

View File

@ -288,6 +288,7 @@ To close a group, use the button:
```js ```js
{ {
"type": "dock", "type": "dock",
"filter": "(^Xcode$)|(Safari)|(.*player)",
"autoResize": true "autoResize": true
}, },
``` ```