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:
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