1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-10 00:58:37 +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
(
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

View File

@ -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)

View File

@ -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)

View File

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