Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222public import Foundationpublic import ParcelConfigpublic import ParcelCore
public typealias HoverBarConfiguration = ParcelConfig.HoverBarConfiguration
public struct HoverTarget: Equatable, Sendable { public let window: WindowID public let workspace: WorkspaceKey public let frame: CGRect public let displayFrame: CGRect public let windowFrame: CGRect public let isFloating: Bool public let isFocused: Bool public let stackControlsFrame: CGRect? public let stackWindows: [WindowID] public var presentationAnchor: CGPoint? public var explicitlyInvoked = false public var allowsPassive = true
public init( window: WindowID, workspace: WorkspaceKey, frame: CGRect, displayFrame: CGRect, isFloating: Bool = false, isFocused: Bool = false, stackControlsFrame: CGRect? = nil, windowFrame: CGRect? = nil, explicitlyInvoked: Bool = false, stackWindows: [WindowID] = [], presentationAnchor: CGPoint? = nil ) { self.window = window self.workspace = workspace self.frame = frame self.displayFrame = displayFrame self.windowFrame = windowFrame ?? frame self.isFloating = isFloating self.isFocused = isFocused self.stackControlsFrame = stackControlsFrame self.explicitlyInvoked = explicitlyInvoked self.stackWindows = stackWindows self.presentationAnchor = presentationAnchor }
public var tabRowHeight: CGFloat { stackWindows.count > 1 ? 38 : 0 }
public var controlsFrame: CGRect { guard windowFrame.isFiniteAndPositive, displayFrame.isFiniteAndPositive else { return .zero } let anchor = presentationAnchor ?? CGPoint(x: frame.midX, y: frame.midY) guard anchor.x.isFinite, anchor.y.isFinite else { return .zero } let width = min(explicitlyInvoked ? 520 : 390, displayFrame.width) let height = min(64 + tabRowHeight, displayFrame.height) return CGRect( x: min(max(anchor.x - width / 2, displayFrame.minX), displayFrame.maxX - width), y: min(max(anchor.y - 8, displayFrame.minY), displayFrame.maxY - height), width: width, height: height) }
public var actionFrame: CGRect { let bounds = controlsFrame let inset = min(tabRowHeight, bounds.height) return CGRect(x: bounds.minX, y: bounds.minY + inset, width: bounds.width, height: bounds.height - inset) }
public func retainingPresentation(of previous: HoverTarget) -> HoverTarget { var target = self target.presentationAnchor = previous.presentationAnchor return target }
var interactionFrame: CGRect { frame.union(controlsFrame).union(stackControlsFrame ?? frame) }
func containsControl(at point: CGPoint) -> Bool { controlsFrame.contains(point) || stackControlsFrame?.contains(point) == true }}
public struct HoverBarModel: Sendable { public enum Phase: Equatable, Sendable { case hidden, pending, visible } public private(set) var phase: Phase = .hidden public private(set) var target: HoverTarget? public private(set) var deadline: Double? private var lastTime = -Double.infinity private var preferences: HoverBarConfiguration?
public init() {}
public mutating func receive(_ target: HoverTarget?, at time: Double, configuration: HoverBarConfiguration) { precondition(configuration.isValid) guard time.isFinite, time >= lastTime else { hide() return } lastTime = time if preferences != configuration { hide() } preferences = configuration guard configuration.enabled, let target, target.frame.isFiniteAndPositive, target.displayFrame.isFiniteAndPositive, target.controlsFrame.isFiniteAndPositive, target.stackControlsFrame.map(\.isFiniteAndPositive) ?? true else { hide() return } var anchoredTarget = target if phase == .visible, let previous = self.target, previous.window == target.window, previous.workspace == target.workspace, previous.presentationAnchor != nil { anchoredTarget = target.retainingPresentation(of: previous) } if self.target?.window != target.window || self.target?.workspace != target.workspace || self.target?.frame != target.frame || self.target?.windowFrame != target.windowFrame || self.target?.displayFrame != target.displayFrame { phase = .pending deadline = time + configuration.delay } self.target = anchoredTarget if let deadline, time >= deadline { phase = .visible self.deadline = nil } }
public mutating func show(_ target: HoverTarget, configuration: HoverBarConfiguration) { precondition(configuration.isValid) guard target.frame.isFiniteAndPositive, target.displayFrame.isFiniteAndPositive, target.controlsFrame.isFiniteAndPositive, target.stackControlsFrame.map(\.isFiniteAndPositive) ?? true else { hide() return } preferences = configuration self.target = target phase = .visible deadline = nil }
public mutating func hide() { phase = .hidden target = nil deadline = nil }
public static func target(at point: CGPoint, state: State) -> HoverTarget? { guard point.x.isFinite, point.y.isFinite else { return nil } let visible = Set(state.visibleWindows) let floating = visible.filter { state.windows[$0]?.isFloating == true } .compactMap { target(for: $0, state: state) } .filter { $0.frame.contains(point) || $0.windowFrame.contains(point) } // Cached frames don't establish stacking order. guard floating.count <= 1 else { return nil } if let target = floating.first { guard target.allowsPassive, target.frame.contains(point) else { return nil } return target } let tiled = visible.filter { state.windows[$0]?.isFloating == false } .compactMap { target(for: $0, state: state) }.filter { $0.allowsPassive && $0.frame.contains(point) } return tiled.count == 1 ? tiled.first : nil }
public static func target(for window: WindowID, state: State) -> HoverTarget? { guard state.visibleWindows.contains(window), let managed = state.windows[window], let observed = state.observedFrames[window]?.learningRect, observed.isFiniteAndPositive, let key = state.workspace(containing: window), state.isActive(key), let screen = state.displays[key.display]?.frame.learningRect, screen.isFiniteAndPositive else { return nil } guard let rect = managed.isFloating ? observed : state.frames[window]?.learningRect, rect.isFiniteAndPositive else { return nil } let stackWindows = managed.isFloating ? [window] : state.workspaces[key]?.tree?.stacks.first { $0.windows.contains(window) }?.windows ?? [window] let focused = state.modes(for: key).focusedWindow guard managed.isFloating || focused == nil || focused == window else { return nil } let grip = managed.isFloating ? nil : StackGrip.groups(in: state).first { $0.visible == window } let chipRow = grip.flatMap { _ in StackChips.groups(in: StackGrip.observedGeometry(in: state), expanding: window) .first { $0.visible == window }?.frame } let stripWidth = min(120, rect.width / 3) var strip = CGRect( x: rect.midX - stripWidth / 2, y: rect.minY, width: stripWidth, height: min(20, rect.height)) if managed.isFloating { let left = max(rect.minX, screen.minX) let right = min(rect.maxX, screen.maxX) let width = min(stripWidth, right - left) if width > 0 { strip.origin.x = min(max(strip.minX, left), right - width) strip.size.width = width } } let neighbors = state.visibleWindows.filter { $0 != window }.compactMap { (state.windows[$0]?.isFloating == true ? state.observedFrames[$0] : state.frames[$0])?.learningRect } let gap = managed.isFloating ? HoverGripGeometry.floatingTopRegion(window: rect, display: screen) : HoverGripGeometry.topRegion( window: rect, display: screen, neighbors: neighbors, memberCount: stackWindows.count) var target = HoverTarget( window: window, workspace: key, frame: gap ?? strip, displayFrame: screen, isFloating: managed.isFloating, isFocused: focused == window, stackControlsFrame: chipRow, windowFrame: rect, stackWindows: stackWindows) target.allowsPassive = managed.isFloating || gap != nil return target }
public static func controlFrames(in frame: CGRect, count: Int = HoverControlID.allCases.count) -> [CGRect] { guard frame.isFiniteAndPositive, count > 0 else { return [] } let inset = min(4, frame.width / 16, frame.height / 4) let bounds = frame.insetBy(dx: inset, dy: inset) let width = bounds.width / Double(count) return (0..<count).map { CGRect(x: bounds.minX + Double($0) * width, y: bounds.minY, width: width, height: bounds.height) } }
public static func hitTest(_ point: CGPoint, target: HoverTarget) -> HoverControlID? { guard point.x.isFinite, point.y.isFinite else { return nil } let controls: [HoverControlID] = target.explicitlyInvoked ? HoverControlID.allCases : [.primary, .focus, .floating] return zip(controls, controlFrames(in: target.actionFrame, count: controls.count)).first { $0.1.contains(point) }?.0 }}