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:
116
Sources/Strip/StripController.swift
Normal file
116
Sources/Strip/StripController.swift
Normal file
@@ -0,0 +1,116 @@
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
// StripViewModel holds the state for the tab bar view. The view model is
|
||||
// observable. The strip controller sets the active window and the action
|
||||
// closures. The view reads the window store through the view model.
|
||||
@MainActor
|
||||
@Observable
|
||||
final class StripViewModel {
|
||||
|
||||
let store: WindowStore
|
||||
var activeWindowID: CGWindowID?
|
||||
var availableWidth: CGFloat = 800
|
||||
|
||||
@ObservationIgnored var onActivate: ((CGWindowID) -> Void)?
|
||||
@ObservationIgnored var onMinimize: ((CGWindowID) -> Void)?
|
||||
@ObservationIgnored var onClose: ((CGWindowID) -> Void)?
|
||||
|
||||
init(store: WindowStore) {
|
||||
self.store = store
|
||||
}
|
||||
|
||||
// appIcon returns the icon of the frontmost application.
|
||||
func appIcon() -> NSImage? {
|
||||
guard let pid = store.frontmostPID,
|
||||
let app = NSRunningApplication(processIdentifier: pid) else { return nil }
|
||||
return app.icon
|
||||
}
|
||||
|
||||
// appName returns the name of the frontmost application.
|
||||
func appName() -> String {
|
||||
guard let pid = store.frontmostPID,
|
||||
let app = NSRunningApplication(processIdentifier: pid) else { return "" }
|
||||
return app.localizedName ?? ""
|
||||
}
|
||||
}
|
||||
|
||||
// StripController owns the strip panel and the tab bar view. The controller
|
||||
// positions the strip and shows or hides the strip. The controller forwards
|
||||
// the tab actions through the closures. See spec sections 7.7, 7.8, 7.9.
|
||||
// The controller does not read AX and does not detect full screen. The
|
||||
// integration layer calls setVisible for the full-screen case (decision D7).
|
||||
@MainActor
|
||||
final class StripController {
|
||||
|
||||
private let panel: StripPanel
|
||||
private let model: StripViewModel
|
||||
private let settings: Settings
|
||||
private let hostingView: NSHostingView<TabBarView>
|
||||
|
||||
var onActivate: ((CGWindowID) -> Void)? {
|
||||
get { model.onActivate }
|
||||
set { model.onActivate = newValue }
|
||||
}
|
||||
var onMinimize: ((CGWindowID) -> Void)? {
|
||||
get { model.onMinimize }
|
||||
set { model.onMinimize = newValue }
|
||||
}
|
||||
var onClose: ((CGWindowID) -> Void)? {
|
||||
get { model.onClose }
|
||||
set { model.onClose = newValue }
|
||||
}
|
||||
|
||||
// activeWindowID is the focused window of the frontmost application. The
|
||||
// integration layer sets this value. The tab bar marks this tab as active.
|
||||
var activeWindowID: CGWindowID? {
|
||||
get { model.activeWindowID }
|
||||
set { model.activeWindowID = newValue }
|
||||
}
|
||||
|
||||
// stripFrame is the current frame of the strip panel. The constraint
|
||||
// service reads this frame for the limit line (spec section 7.10).
|
||||
var stripFrame: CGRect {
|
||||
return panel.frame
|
||||
}
|
||||
|
||||
init(store: WindowStore, settings: Settings) {
|
||||
self.settings = settings
|
||||
self.model = StripViewModel(store: store)
|
||||
|
||||
let screen = StripPositioner.dockScreen(NSScreen.screens, main: NSScreen.main) ?? NSScreen.main
|
||||
let frame = screen.map { StripPositioner.stripFrame(for: $0, height: CGFloat(settings.stripHeight)) }
|
||||
?? NSRect(x: 0, y: 0, width: 800, height: CGFloat(settings.stripHeight))
|
||||
|
||||
panel = StripPanel(contentRect: frame)
|
||||
hostingView = NSHostingView(rootView: TabBarView(model: model))
|
||||
hostingView.frame = NSRect(origin: .zero, size: frame.size)
|
||||
hostingView.autoresizingMask = [.width, .height]
|
||||
panel.contentView?.addSubview(hostingView)
|
||||
model.availableWidth = frame.width
|
||||
}
|
||||
|
||||
func start() {
|
||||
reposition()
|
||||
setVisible(true)
|
||||
}
|
||||
|
||||
// reposition recomputes the strip frame. The integration layer calls this
|
||||
// function on a screen change, a Dock change, and after wake from sleep.
|
||||
func reposition() {
|
||||
guard let screen = StripPositioner.dockScreen(NSScreen.screens, main: NSScreen.main) else { return }
|
||||
let frame = StripPositioner.stripFrame(for: screen, height: CGFloat(settings.stripHeight))
|
||||
panel.setFrame(frame, display: true)
|
||||
model.availableWidth = frame.width
|
||||
Log.strip.debug("Strip repositioned to \(String(describing: frame), privacy: .public)")
|
||||
}
|
||||
|
||||
// setVisible shows or hides the strip.
|
||||
func setVisible(_ visible: Bool) {
|
||||
if visible {
|
||||
panel.orderFrontRegardless()
|
||||
} else {
|
||||
panel.orderOut(nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
43
Sources/Strip/StripPanel.swift
Normal file
43
Sources/Strip/StripPanel.swift
Normal file
@@ -0,0 +1,43 @@
|
||||
import AppKit
|
||||
|
||||
// StripPanel is the always-visible strip above the Dock. The panel does not
|
||||
// take focus from the frontmost application. See spec section 7.7.
|
||||
final class StripPanel: NSPanel {
|
||||
|
||||
init(contentRect: NSRect) {
|
||||
super.init(contentRect: contentRect,
|
||||
styleMask: [.borderless, .nonactivatingPanel],
|
||||
backing: .buffered,
|
||||
defer: false)
|
||||
|
||||
becomesKeyOnlyIfNeeded = true
|
||||
hasShadow = false
|
||||
hidesOnDeactivate = false
|
||||
isReleasedWhenClosed = false
|
||||
isFloatingPanel = true
|
||||
isOpaque = false
|
||||
backgroundColor = .clear
|
||||
|
||||
// The strip joins all Spaces. The strip does not move. The strip does
|
||||
// not take part in the window cycle.
|
||||
collectionBehavior = [.canJoinAllSpaces, .stationary, .ignoresCycle]
|
||||
|
||||
// The strip is above normal windows but below the Dock. The Dock
|
||||
// magnification can then cover the strip.
|
||||
level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.dockWindow)) - 1)
|
||||
|
||||
// The background uses a visual effect view. The material is close to
|
||||
// the Dock.
|
||||
let effect = NSVisualEffectView(frame: NSRect(origin: .zero, size: contentRect.size))
|
||||
effect.material = .hudWindow
|
||||
effect.blendingMode = .behindWindow
|
||||
effect.state = .active
|
||||
effect.autoresizingMask = [.width, .height]
|
||||
contentView = effect
|
||||
}
|
||||
|
||||
// The panel must not become the key window. A click on the strip must not
|
||||
// take focus from the frontmost application.
|
||||
override var canBecomeKey: Bool { false }
|
||||
override var canBecomeMain: Bool { false }
|
||||
}
|
||||
52
Sources/Strip/StripPositioner.swift
Normal file
52
Sources/Strip/StripPositioner.swift
Normal file
@@ -0,0 +1,52 @@
|
||||
import AppKit
|
||||
|
||||
// StripPositioner finds the Dock screen and computes the strip frame.
|
||||
// See spec section 7.8. The core math takes plain values so a unit test can
|
||||
// check the logic without a real screen.
|
||||
enum StripPositioner {
|
||||
|
||||
// ScreenFrames holds the two frames of one screen.
|
||||
struct ScreenFrames: Equatable {
|
||||
let frame: CGRect
|
||||
let visibleFrame: CGRect
|
||||
}
|
||||
|
||||
// dockScreenIndex returns the index of the Dock screen. The Dock screen has
|
||||
// a visibleFrame.minY above the frame.minY. macOS reserves this space for
|
||||
// the Dock at the bottom. If no screen matches, the function returns the
|
||||
// main index.
|
||||
static func dockScreenIndex(screens: [ScreenFrames], mainIndex: Int) -> Int {
|
||||
for (index, screen) in screens.enumerated() {
|
||||
if screen.visibleFrame.minY > screen.frame.minY {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return mainIndex
|
||||
}
|
||||
|
||||
// stripFrame returns the strip frame for a visible frame and a height.
|
||||
// The strip sits at the bottom of the visible frame, full width.
|
||||
static func stripFrame(visibleFrame: CGRect, height: CGFloat) -> CGRect {
|
||||
return CGRect(x: visibleFrame.minX,
|
||||
y: visibleFrame.minY,
|
||||
width: visibleFrame.width,
|
||||
height: height)
|
||||
}
|
||||
|
||||
// dockScreen returns the Dock NSScreen. The function uses the same rule as
|
||||
// dockScreenIndex. If no screen matches, the function returns the main
|
||||
// screen, or the first screen.
|
||||
static func dockScreen(_ screens: [NSScreen], main: NSScreen?) -> NSScreen? {
|
||||
for screen in screens {
|
||||
if screen.visibleFrame.minY > screen.frame.minY {
|
||||
return screen
|
||||
}
|
||||
}
|
||||
return main ?? screens.first
|
||||
}
|
||||
|
||||
// stripFrame returns the strip frame for an NSScreen and a height.
|
||||
static func stripFrame(for screen: NSScreen, height: CGFloat) -> NSRect {
|
||||
return stripFrame(visibleFrame: screen.visibleFrame, height: height)
|
||||
}
|
||||
}
|
||||
143
Sources/Strip/TabBarView.swift
Normal file
143
Sources/Strip/TabBarView.swift
Normal file
@@ -0,0 +1,143 @@
|
||||
import SwiftUI
|
||||
import AppKit
|
||||
|
||||
// TabBarView draws the tab bar. The view is a SwiftUI view. The strip hosts
|
||||
// the view in an NSHostingView. The view observes the StripViewModel. See
|
||||
// spec section 7.9. The view uses system colors only. The view supports light
|
||||
// mode and dark mode.
|
||||
struct TabBarView: View {
|
||||
|
||||
let model: StripViewModel
|
||||
|
||||
@State private var hoveredID: CGWindowID?
|
||||
|
||||
var body: some View {
|
||||
let windows = model.store.frontmostWindows
|
||||
let appIcon = model.appIcon()
|
||||
let appName = model.appName()
|
||||
|
||||
HStack(spacing: 0) {
|
||||
// The app icon is on the left of all tabs.
|
||||
iconView(appIcon)
|
||||
.frame(width: 26)
|
||||
|
||||
if windows.isEmpty {
|
||||
// Empty state: the app name in muted text.
|
||||
Text(appName)
|
||||
.font(.system(size: 12))
|
||||
.foregroundStyle(.secondary)
|
||||
.padding(.leading, 4)
|
||||
Spacer(minLength: 0)
|
||||
} else {
|
||||
tabRow(windows)
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 4)
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private func iconView(_ icon: NSImage?) -> some View {
|
||||
if let icon = icon {
|
||||
Image(nsImage: icon)
|
||||
.resizable()
|
||||
.frame(width: 16, height: 16)
|
||||
} else {
|
||||
Image(systemName: "app")
|
||||
.frame(width: 16, height: 16)
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private func tabRow(_ windows: [WindowInfo]) -> some View {
|
||||
let activeIndex = windows.firstIndex { $0.id == model.activeWindowID }
|
||||
let layout = TabLayout.compute(availableWidth: model.availableWidth,
|
||||
windowCount: windows.count,
|
||||
activeIndex: activeIndex)
|
||||
|
||||
HStack(spacing: 3) {
|
||||
ForEach(layout.visibleIndices, id: \.self) { index in
|
||||
tabView(windows[index], width: layout.tabWidth)
|
||||
}
|
||||
if layout.showsOverflow {
|
||||
overflowMenu(windows, indices: layout.overflowIndices)
|
||||
}
|
||||
Spacer(minLength: 0)
|
||||
}
|
||||
.padding(.leading, 4)
|
||||
}
|
||||
|
||||
private func tabView(_ window: WindowInfo, width: CGFloat) -> some View {
|
||||
let isActive = window.id == model.activeWindowID
|
||||
let isHovered = window.id == hoveredID
|
||||
|
||||
return HStack(spacing: 3) {
|
||||
if window.isOnCurrentSpace == false {
|
||||
Image(systemName: "desktopcomputer")
|
||||
.font(.system(size: 9))
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
Text(window.title)
|
||||
.font(.system(size: 11))
|
||||
.italic(window.isMinimized)
|
||||
.lineLimit(1)
|
||||
.truncationMode(.middle)
|
||||
.foregroundStyle(textStyle(active: isActive, minimized: window.isMinimized))
|
||||
}
|
||||
.padding(.horizontal, 6)
|
||||
.frame(width: width, height: 22)
|
||||
.background(background(active: isActive, hovered: isHovered))
|
||||
.clipShape(RoundedRectangle(cornerRadius: 5))
|
||||
.help(window.title)
|
||||
.contentShape(Rectangle())
|
||||
.onTapGesture {
|
||||
model.onActivate?(window.id)
|
||||
}
|
||||
.onHover { inside in
|
||||
hoveredID = inside ? window.id : (hoveredID == window.id ? nil : hoveredID)
|
||||
}
|
||||
.contextMenu {
|
||||
Button("Minimize") { model.onMinimize?(window.id) }
|
||||
Button("Close") { model.onClose?(window.id) }
|
||||
}
|
||||
}
|
||||
|
||||
private func overflowMenu(_ windows: [WindowInfo], indices: [Int]) -> some View {
|
||||
Menu("+\(indices.count)") {
|
||||
ForEach(indices, id: \.self) { index in
|
||||
let window = windows[index]
|
||||
Button(action: { model.onActivate?(window.id) }) {
|
||||
Text(prefix(window) + window.title)
|
||||
}
|
||||
}
|
||||
}
|
||||
.menuStyle(.borderlessButton)
|
||||
.frame(width: 36, height: 22)
|
||||
.fixedSize()
|
||||
}
|
||||
|
||||
// prefix returns a text marker for the overflow menu. The marker shows the
|
||||
// state of the window.
|
||||
private func prefix(_ window: WindowInfo) -> String {
|
||||
if window.isMinimized { return "— " }
|
||||
if window.isOnCurrentSpace == false { return "□ " }
|
||||
return ""
|
||||
}
|
||||
|
||||
private func textStyle(active: Bool, minimized: Bool) -> HierarchicalShapeStyle {
|
||||
if active { return .primary }
|
||||
if minimized { return .tertiary }
|
||||
return .secondary
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private func background(active: Bool, hovered: Bool) -> some View {
|
||||
if active {
|
||||
Color.accentColor.opacity(0.25)
|
||||
} else if hovered {
|
||||
Color.primary.opacity(0.08)
|
||||
} else {
|
||||
Color.clear
|
||||
}
|
||||
}
|
||||
}
|
||||
127
Sources/Strip/TabLayout.swift
Normal file
127
Sources/Strip/TabLayout.swift
Normal file
@@ -0,0 +1,127 @@
|
||||
import CoreGraphics
|
||||
|
||||
// TabLayout is a pure layout function. The function has no AppKit dependency.
|
||||
// The unit tests check this function. See spec section 7.9.
|
||||
enum TabLayout {
|
||||
|
||||
// Constants holds the layout sizes. The default values come from the spec.
|
||||
struct Constants: Equatable {
|
||||
var maxTabWidth: CGFloat = 220
|
||||
var minTabWidth: CGFloat = 90
|
||||
var spacing: CGFloat = 3
|
||||
var overflowButtonWidth: CGFloat = 36
|
||||
var appIconArea: CGFloat = 26
|
||||
init() {}
|
||||
}
|
||||
|
||||
// Result holds the output of the layout.
|
||||
struct Result: Equatable {
|
||||
let visibleCount: Int
|
||||
let tabWidth: CGFloat
|
||||
let showsOverflow: Bool
|
||||
let overflowCount: Int
|
||||
let visibleIndices: [Int]
|
||||
let overflowIndices: [Int]
|
||||
}
|
||||
|
||||
// compute returns the layout for the tab bar.
|
||||
// availableWidth is the full width of the strip.
|
||||
// windowCount is the number of windows.
|
||||
// activeIndex is the index of the active window, or nil.
|
||||
static func compute(availableWidth: CGFloat,
|
||||
windowCount: Int,
|
||||
activeIndex: Int?,
|
||||
constants: Constants = Constants()) -> Result {
|
||||
|
||||
if windowCount <= 0 {
|
||||
return Result(visibleCount: 0, tabWidth: 0, showsOverflow: false,
|
||||
overflowCount: 0, visibleIndices: [], overflowIndices: [])
|
||||
}
|
||||
|
||||
// The app icon area is on the left of all tabs. The tabs use the rest.
|
||||
let tabsWidth = max(0, availableWidth - constants.appIconArea)
|
||||
|
||||
// fits returns true if n tabs fit at the minimum width. overflow adds
|
||||
// the +N button width.
|
||||
func fits(_ n: Int, overflow: Bool) -> Bool {
|
||||
guard n > 0 else { return overflow ? constants.overflowButtonWidth <= tabsWidth : true }
|
||||
var need = CGFloat(n) * constants.minTabWidth + CGFloat(n - 1) * constants.spacing
|
||||
if overflow {
|
||||
need += constants.overflowButtonWidth + constants.spacing
|
||||
}
|
||||
return need <= tabsWidth
|
||||
}
|
||||
|
||||
var showsOverflow = false
|
||||
var visibleCount = windowCount
|
||||
|
||||
if fits(windowCount, overflow: false) {
|
||||
visibleCount = windowCount
|
||||
showsOverflow = false
|
||||
} else {
|
||||
// The tabs do not all fit. Show as many as fit plus the +N button.
|
||||
showsOverflow = true
|
||||
var n = windowCount - 1
|
||||
while n > 0 && fits(n, overflow: true) == false {
|
||||
n -= 1
|
||||
}
|
||||
visibleCount = max(0, n)
|
||||
}
|
||||
|
||||
// All visible tabs have the same width. The width fills the free space.
|
||||
// The width stays inside the minimum and the maximum.
|
||||
let tabWidth: CGFloat
|
||||
if visibleCount > 0 {
|
||||
let reserved = showsOverflow ? (constants.overflowButtonWidth + constants.spacing) : 0
|
||||
let spacingTotal = CGFloat(visibleCount - 1) * constants.spacing
|
||||
let free = tabsWidth - reserved - spacingTotal
|
||||
var width = free / CGFloat(visibleCount)
|
||||
width = min(constants.maxTabWidth, max(constants.minTabWidth, width))
|
||||
tabWidth = width
|
||||
} else {
|
||||
tabWidth = 0
|
||||
}
|
||||
|
||||
let split = visibleIndices(windowCount: windowCount,
|
||||
visibleCount: visibleCount,
|
||||
activeIndex: activeIndex)
|
||||
|
||||
return Result(visibleCount: visibleCount,
|
||||
tabWidth: tabWidth,
|
||||
showsOverflow: showsOverflow,
|
||||
overflowCount: windowCount - visibleCount,
|
||||
visibleIndices: split.visible,
|
||||
overflowIndices: split.overflow)
|
||||
}
|
||||
|
||||
// visibleIndices returns the visible window indices and the overflow window
|
||||
// indices. The function keeps the natural order. The active window must
|
||||
// always be visible. If the active window is in the overflow, the function
|
||||
// swaps the active window with the last visible tab (spec 7.9, decision D5
|
||||
// keeps the order otherwise).
|
||||
static func visibleIndices(windowCount: Int,
|
||||
visibleCount: Int,
|
||||
activeIndex: Int?) -> (visible: [Int], overflow: [Int]) {
|
||||
guard windowCount > 0 else { return ([], []) }
|
||||
guard visibleCount > 0 else {
|
||||
return ([], Array(0..<windowCount))
|
||||
}
|
||||
guard visibleCount < windowCount else {
|
||||
return (Array(0..<windowCount), [])
|
||||
}
|
||||
|
||||
var visible = Array(0..<visibleCount)
|
||||
var overflow = Array(visibleCount..<windowCount)
|
||||
|
||||
if let active = activeIndex, overflow.contains(active) {
|
||||
let lastPos = visible.count - 1
|
||||
let moved = visible[lastPos]
|
||||
visible[lastPos] = active
|
||||
if let activePos = overflow.firstIndex(of: active) {
|
||||
overflow[activePos] = moved
|
||||
}
|
||||
overflow.sort()
|
||||
}
|
||||
return (visible, overflow)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user