Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233public import AppKitpublic import ParcelConfigpublic import ParcelCore
struct StatusItemAnchor { let frame: CGRect? let isVisible: Bool var excludedFrames: [CGRect] = []
func isUsable(on screens: [CGRect]) -> Bool { guard isVisible, let frame, MenuBarPlacement.isValid(frame), !excludedFrames.contains(where: { $0.intersects(frame) }) else { return false } return screens.contains { MenuBarPlacement.isValid($0) && $0.contains(frame) } }}
enum MenuBarPlacement: Equatable { case statusItem case fallback(CGPoint) case unavailable
static func resolve( anchor: StatusItemAnchor, screens: [CGRect], activeVisibleFrame: CGRect? ) -> Self { if anchor.isUsable(on: screens) { return .statusItem } guard let frame = activeVisibleFrame, isValid(frame), screens.contains(where: { isValid($0) && $0.contains(frame) }) else { return .unavailable } return .fallback( CGPoint(x: frame.maxX - min(12, frame.width / 2), y: frame.maxY - min(12, frame.height / 2))) }
static func notchFrame(screen: CGRect, topInset: CGFloat, leftArea: CGRect?, rightArea: CGRect?) -> CGRect? { guard isValid(screen), topInset.isFinite, topInset > 0 else { return nil } let left = leftArea.flatMap { isValid($0) && screen.contains($0) ? $0.maxX : nil } ?? screen.minX let right = rightArea.flatMap { isValid($0) && screen.contains($0) ? $0.minX : nil } ?? screen.maxX guard right > left else { return nil } let height = min(topInset, screen.height) return CGRect(x: left, y: screen.maxY - height, width: right - left, height: height) }
static func preferredScreenIndex(active: Int?, main: Int?, pointer: CGPoint, frames: [CGRect]) -> Int? { func valid(_ index: Int?) -> Int? { index.flatMap { frames.indices.contains($0) && isValid(frames[$0]) ? $0 : nil } } return valid(active) ?? frames.indices.first { isValid(frames[$0]) && frames[$0].contains(pointer) } ?? valid(main) ?? frames.indices.first { isValid(frames[$0]) } }
static func isValid(_ frame: CGRect) -> Bool { frame.isFiniteAndPositive && frame.size.width > 0 && frame.size.height > 0 }}
public struct MenuWorkspaceActivation { public let pointer: CGPoint? public let statusScreen: NSScreen?
public init(pointer: CGPoint?, statusScreen: NSScreen?) { self.pointer = pointer self.statusScreen = statusScreen }}
public struct MenuWorkspace: Equatable, Sendable { public enum Unavailability: Error, Equatable, Sendable { case disconnected case unmanagedSpace case notRunning
var title: String { switch self { case .disconnected: MenuText.text("Display unavailable") case .unmanagedSpace: MenuText.text("Space not arranged") case .notRunning: MenuText.text("Tiling not started") } }
var detail: String { switch self { case .disconnected: MenuText.text("This display is unavailable. Reopen the menu on a connected display.") case .unmanagedSpace: MenuText.text("Parcel doesn’t arrange this Space (for example, a full-screen app).") case .notRunning: MenuText.text("Not arranging this screen — tiling has not started") } } }
public let key: WorkspaceKey public let snapshot: LayoutSnapshot public let displayName: String public let desktopNumber: Int? public let hasFocusedWindow: Bool public let hasMultipleDisplays: Bool public let disabledReasons: [ConfigCommand: String]
public init( key: WorkspaceKey, snapshot: LayoutSnapshot, displayName: String, desktopNumber: Int? = nil, hasFocusedWindow: Bool = false, hasMultipleDisplays: Bool = false, disabledReasons: [ConfigCommand: String] = [:] ) { self.key = key self.snapshot = snapshot self.displayName = displayName self.desktopNumber = desktopNumber self.hasFocusedWindow = hasFocusedWindow self.hasMultipleDisplays = hasMultipleDisplays self.disabledReasons = disabledReasons }
var title: String { guard let desktopNumber else { return displayName } return String( format: MenuText.text("%@ · Desktop %lld"), locale: .current, displayName, Int64(desktopNumber)) }
func disabledReason(for command: ConfigCommand) -> String? { if let reason = disabledReasons[command] { return reason } let needsFocus: Bool switch command { case .toggleFocus: needsFocus = !snapshot.isFocused case .toggleNoPeek: needsFocus = !snapshot.noPeek case .toggleCleanSpace: needsFocus = !snapshot.cleanSpace default: needsFocus = false } return needsFocus && !hasFocusedWindow ? MenuText.text("Focus a window on this display first.") : nil }
public static func target( at pointer: CGPoint?, statusDisplay: DisplayID? = nil, screens: [DisplayID: CGRect], state: State ) -> Result<WorkspaceKey, Unavailability> { let display: DisplayID? if let pointer { guard pointer.x.isFinite, pointer.y.isFinite else { return .failure(.disconnected) } let ordered = screens.keys.sorted() // Prefer the interior at shared edges; include the menu bar's outermost pixel only as a second pass. display = ordered.first { screens[$0]?.contains(pointer) == true } ?? ordered.first { guard let frame = screens[$0] else { return false } return pointer.x >= frame.minX && pointer.x < frame.maxX && pointer.y >= frame.minY && pointer.y <= frame.maxY } ?? ordered.first { guard let frame = screens[$0] else { return false } return pointer.x >= frame.minX && pointer.x <= frame.maxX && pointer.y >= frame.minY && pointer.y <= frame.maxY } } else { display = statusDisplay.flatMap { screens[$0] != nil && state.displays[$0] != nil ? $0 : nil } ?? state.activeWorkspace?.display } guard let display, screens[display] != nil, state.displays[display] != nil else { return .failure(.disconnected) } guard let space = state.activeSpaces[display] else { return .failure(.unmanagedSpace) } let key = WorkspaceKey(display: display, space: space) return state.workspaces[key] == nil ? .failure(.unmanagedSpace) : .success(key) }}
extension ConfigCommand { var targetsMenuWorkspace: Bool { switch self { case .layoutGrid, .layoutColumns, .layoutRows, .layoutSide, .layoutTree, .layoutBSP, .layoutAccordion, .setWindowCap, .ratioHalf, .ratioThreeFifths, .ratioTwoThirds, .ratioIncrease, .ratioDecrease, .resetSizes, .retile, .toggleFocus, .toggleNoPeek, .toggleCleanSpace: true default: false } }}
struct MenuLayoutPreview { static let maximumTileCount = 5
static func count(for layout: ConfigLayout, in snapshot: LayoutSnapshot) -> Int { let visibleCount = max(1, snapshot.tiles.count) let requested = snapshot.hasNoWindowLimit ? visibleCount : snapshot.windowCap.flatMap { $0 > 0 ? $0 : nil } ?? visibleCount let maximum = snapshot.hasNoWindowLimit ? maximumTileCount : layout.capabilities(in: snapshot.capabilityBounds).maximumWindowCount return max(1, min(requested, maximumTileCount, maximum)) }
static func snapshot( layout: ConfigLayout, count: Int, bounds: CGRect, ratio: Double, unlimited: Bool = false, capabilityBounds: CGRect? = nil ) -> LayoutSnapshot { let key = WorkspaceKey(display: DisplayID(rawValue: "preview"), space: SpaceID(rawValue: 1)) let ids = (1...max(1, min(count, maximumTileCount))).map { WindowID(rawValue: UInt32($0)) } guard let coreLayout = Layout(rawValue: layout.rawValue) else { preconditionFailure("Every configured layout needs a core layout") } var workspace = Workspace( key: key, layout: coreLayout, ratio: MainRatio(ratio), windowCap: unlimited ? Int.max : ids.count, order: ids) let frame = Rect(width: bounds.width, height: bounds.height) let fullBounds = capabilityBounds ?? bounds workspace.adapt(to: frame, fullFrame: Rect(width: fullBounds.width, height: fullBounds.height)) let windows = Dictionary( uniqueKeysWithValues: ids.map { ($0, ManagedWindow(id: $0, app: AppID(pid: 0, bundleID: "preview"))) }) let arranged = LayoutEngine.arrange(workspace, in: frame, windows: windows) return LayoutSnapshot( layout: layout, bounds: CGRect(origin: .zero, size: bounds.size), tiles: ids.compactMap { id in guard let tile = arranged.frames[id], tile.isValid, tile.size.width > 0, tile.size.height > 0 else { return nil } return LayoutTile( id: UInt64(id.rawValue), frame: CGRect(x: tile.minX, y: tile.minY, width: tile.size.width, height: tile.size.height), isFocused: id == ids.first) }, windowCap: unlimited ? Int.max : ids.count, ratio: ratio, capabilityBounds: CGRect(origin: .zero, size: fullBounds.size)) }}
enum MenuText { static func text(_ key: String) -> String { Bundle.module.localizedString(forKey: key, value: nil, table: "Menu") }}