EPOD Window Bar: a window strip above the macOS Dock
EPOD Window Bar is a macOS agent app. It shows a thin strip above the Dock with one tab per window of the frontmost application. One click raises the window. Build from source with XcodeGen and Xcode. Signing uses Config.xcconfig (ad-hoc by default; a local Local.xcconfig can set a team ID). The app needs Accessibility and Screen Recording permissions, and runs in a reduced mode without Screen Recording. See README.md and docs/ for details.
This commit is contained in:
80
Sources/Tracking/WorkspaceEvents.swift
Normal file
80
Sources/Tracking/WorkspaceEvents.swift
Normal file
@@ -0,0 +1,80 @@
|
||||
import Foundation
|
||||
import AppKit
|
||||
|
||||
// WorkspaceEvents subscribes to the NSWorkspace notifications and the screen
|
||||
// parameter notification (spec section 7.4). The type turns each notification
|
||||
// into a callback. The type is main-actor isolated.
|
||||
@MainActor
|
||||
final class WorkspaceEvents {
|
||||
|
||||
var onFrontmostChanged: ((pid_t) -> Void)?
|
||||
var onAppLaunched: ((NSRunningApplication) -> Void)?
|
||||
var onAppTerminated: ((pid_t) -> Void)?
|
||||
var onActiveSpaceChanged: (() -> Void)?
|
||||
var onScreenParametersChanged: (() -> Void)?
|
||||
|
||||
private var tokens: [NSObjectProtocol] = []
|
||||
|
||||
// start subscribes to the notifications.
|
||||
func start() {
|
||||
let workspaceCenter = NSWorkspace.shared.notificationCenter
|
||||
|
||||
tokens.append(workspaceCenter.addObserver(
|
||||
forName: NSWorkspace.didActivateApplicationNotification,
|
||||
object: nil, queue: .main) { [weak self] note in
|
||||
MainActor.assumeIsolated {
|
||||
guard let app = Self.app(from: note) else { return }
|
||||
self?.onFrontmostChanged?(app.processIdentifier)
|
||||
}
|
||||
})
|
||||
|
||||
tokens.append(workspaceCenter.addObserver(
|
||||
forName: NSWorkspace.didLaunchApplicationNotification,
|
||||
object: nil, queue: .main) { [weak self] note in
|
||||
MainActor.assumeIsolated {
|
||||
guard let app = Self.app(from: note) else { return }
|
||||
self?.onAppLaunched?(app)
|
||||
}
|
||||
})
|
||||
|
||||
tokens.append(workspaceCenter.addObserver(
|
||||
forName: NSWorkspace.didTerminateApplicationNotification,
|
||||
object: nil, queue: .main) { [weak self] note in
|
||||
MainActor.assumeIsolated {
|
||||
guard let app = Self.app(from: note) else { return }
|
||||
self?.onAppTerminated?(app.processIdentifier)
|
||||
}
|
||||
})
|
||||
|
||||
tokens.append(workspaceCenter.addObserver(
|
||||
forName: NSWorkspace.activeSpaceDidChangeNotification,
|
||||
object: nil, queue: .main) { [weak self] _ in
|
||||
MainActor.assumeIsolated {
|
||||
self?.onActiveSpaceChanged?()
|
||||
}
|
||||
})
|
||||
|
||||
tokens.append(NotificationCenter.default.addObserver(
|
||||
forName: NSApplication.didChangeScreenParametersNotification,
|
||||
object: nil, queue: .main) { [weak self] _ in
|
||||
MainActor.assumeIsolated {
|
||||
self?.onScreenParametersChanged?()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// stop removes the subscriptions.
|
||||
func stop() {
|
||||
let workspaceCenter = NSWorkspace.shared.notificationCenter
|
||||
for token in tokens {
|
||||
workspaceCenter.removeObserver(token)
|
||||
NotificationCenter.default.removeObserver(token)
|
||||
}
|
||||
tokens.removeAll()
|
||||
}
|
||||
|
||||
// app reads the NSRunningApplication from the notification user info.
|
||||
private static func app(from note: Notification) -> NSRunningApplication? {
|
||||
return note.userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user