Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235public import ParcelConfigpublic import ParcelCoreimport ParcelEngine
public enum TourTrack: String, Codable, CaseIterable, Sendable { case keyboard, mouse }public enum TourStep: String, Codable, CaseIterable, Sendable { case focus, layout, swap, stack, select }public enum TourPhase: String, Codable, Sendable { case offered, running, paused, celebrating, completed }
public struct TourProgress: Codable, Equatable, Sendable { public let version: Int public var offered: Bool public var track: TourTrack public var step: Int public var phase: TourPhase
public init(offered: Bool = false, track: TourTrack = .keyboard, step: Int = 0, phase: TourPhase = .offered) { version = 1 self.offered = offered self.track = track self.step = step self.phase = phase }
public var isValid: Bool { version == 1 && (0..<TourStep.allCases.count).contains(step) }}
public enum TourInput: Equatable, Sendable { case keyboard(ConfigCommand) case mouse case chip(WindowID)}
public struct GuidedTour: Sendable { public private(set) var progress: TourProgress private var baseline: State? private var input: TourInput? private var expectedCommand: ConfigCommand? private var confirmedWindow: WindowID? public var step: TourStep { TourStep.allCases[progress.step] }
public init(progress: TourProgress = TourProgress()) { precondition(progress.isValid) self.progress = progress if progress.phase == .running || progress.phase == .celebrating { self.progress.phase = .paused } }
public mutating func offer() -> Bool { guard !progress.offered else { return false } progress.offered = true return true }
public mutating func start(track: TourTrack, state: State, configuration: ParcelConfiguration) { if progress.track != track || progress.phase == .completed { progress.step = 0 } progress.track = track progress.offered = true progress.phase = .running confirmedWindow = state.focusedWindow rebase(state, configuration: configuration) }
public mutating func confirmFocus(_ window: WindowID) { confirmedWindow = window }
public mutating func pause() { if progress.phase != .completed { progress.phase = .paused } input = nil baseline = nil }
public mutating func skip(state: State, configuration: ParcelConfiguration) { guard progress.phase == .running || progress.phase == .celebrating else { return } advance(state, configuration: configuration) }
public mutating func continueAfterWin(state: State, configuration: ParcelConfiguration) { guard progress.phase == .celebrating else { return } advance(state, configuration: configuration) }
public mutating func performed(_ input: TourInput) { guard progress.phase == .running else { return } switch (progress.track, input) { case (.keyboard, .keyboard(let command)) where command == expectedCommand: self.input = input case (.mouse, .mouse) where step != .select: self.input = input case (.mouse, .chip) where step == .select: self.input = input default: self.input = nil } }
@discardableResult public mutating func receive(_ state: State, configuration: ParcelConfiguration) -> Bool { guard progress.phase == .running, let old = baseline else { return false } guard old.activeWorkspace == state.activeWorkspace, let key = state.activeWorkspace, let before = old.workspaces[key], let after = state.workspaces[key], Set(before.order) == Set(after.order) else { rebase(state, configuration: configuration) return false } guard input != nil else { rebase(state, configuration: configuration) return false } if prepareStackIfNeeded(state, configuration: configuration) { return false } guard stepChanged(from: old, to: state, before: before, after: after), inputMatches(state, baseline: old, key: key, configuration: configuration) else { return false } progress.phase = .celebrating input = nil return true }
private mutating func prepareStackIfNeeded(_ state: State, configuration: ParcelConfiguration) -> Bool { guard step == .stack, case .keyboard(let command) = input, command != .stack, let baseline else { return false } let expected = Self.applying(command, to: baseline) if let key = state.activeWorkspace, state.focusedWindow == expected.focusedWindow, confirmedWindow == state.focusedWindow, state.workspaces[key]?.layout == expected.workspaces[key]?.layout, framesConfirmed(state) { rebase(state, configuration: configuration) } return true }
private func stepChanged(from old: State, to state: State, before: Workspace, after: Workspace) -> Bool { switch step { case .focus: return old.focusedWindow != state.focusedWindow && confirmedWindow == state.focusedWindow && state.focusedWindow.map { after.visibleWindows.contains($0) && state.windows[$0]?.isFloating == false } == true case .layout: return before.layout != after.layout && framesConfirmed(state) case .swap: return before.order != after.order && before.tree?.stacks.count == after.tree?.stacks.count && framesConfirmed(state) case .stack: return (after.tree?.stacks.count ?? 0) < (before.tree?.stacks.count ?? 0) && framesConfirmed(state) case .select: return before.tree?.stacks != after.tree?.stacks && confirmedWindow == state.focusedWindow && after.tree?.stacks.contains(where: { stack in stack.windows.count > 1 && stack.visible == state.focusedWindow && before.tree?.stacks.contains(where: { $0.windows == stack.windows && $0.visible != stack.visible }) == true }) == true } }
private func inputMatches(_ state: State, baseline: State, key: WorkspaceKey, configuration: ParcelConfiguration) -> Bool { if case .keyboard(let command) = input { let expected = Self.applying(command, to: baseline) switch step { case .focus, .select: guard state.focusedWindow == expected.focusedWindow else { return false } case .layout: return state.workspaces[key]?.layout == expected.workspaces[key]?.layout case .swap: return state.workspaces[key]?.order == expected.workspaces[key]?.order case .stack: return state.workspaces[key]?.tree?.stacks == expected.workspaces[key]?.tree?.stacks } } if progress.track == .mouse { guard configuration.mouse.enabled else { return false } if case .chip(let id) = input, state.focusedWindow != id { return false } } return true }
public func command(state: State, configuration: ParcelConfiguration) -> ConfigCommand? { guard progress.track == .keyboard else { return nil } let choices: [ConfigCommand] switch step { case .focus: choices = [.focusRight, .focusLeft, .focusDown, .focusUp] case .layout: choices = [.layoutColumns, .layoutRows, .layoutGrid, .layoutSide] case .swap: choices = [.swapRight, .swapLeft, .swapDown, .swapUp] case .stack: if state.activeWorkspace.flatMap({ state.workspaces[$0]?.layout }) == .columns { choices = [.stack, .focusLeft] } else { choices = [.stack, .layoutColumns, .focusLeft] } case .select: choices = [.stackNext, .stackPrevious] } return choices.first { candidate in guard configuration.keybindings[candidate] != nil else { return false } let (next, effects) = Self.result(candidate, state: state) guard next != state, next.activeWorkspace == state.activeWorkspace else { return false } if step == .stack, candidate == .layoutColumns, state.activeWorkspace.flatMap({ state.workspaces[$0]?.layout }) == .columns { return false } return !effects.contains { if case .report = $0 { return true } return false } } }
private static func result(_ command: ConfigCommand, state: State) -> (State, [Effect]) { do { return reduce(state, .commandIssued(try EngineCommandParser.command(command))) } catch { preconditionFailure("Invalid tour command: \(error)") } }
private static func applying(_ command: ConfigCommand, to state: State) -> State { result(command, state: state).0 }
public mutating func rebase(_ state: State, configuration: ParcelConfiguration) { baseline = state input = nil expectedCommand = command(state: state, configuration: configuration) }
private mutating func advance(_ state: State, configuration: ParcelConfiguration) { if progress.step == TourStep.allCases.count - 1 { progress.phase = .completed } else { progress.step += 1 progress.phase = .running } rebase(state, configuration: configuration) }
private func framesConfirmed(_ state: State) -> Bool { state.visibleWindows.allSatisfy { id in guard let target = state.frames[id], let actual = state.observedFrames[id] else { return false } return abs(target.origin.xCoordinate - actual.origin.xCoordinate) < 2 && abs(target.origin.yCoordinate - actual.origin.yCoordinate) < 2 && abs(target.size.width - actual.size.width) < 2 && abs(target.size.height - actual.size.height) < 2 } }}