Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132import Foundationimport ParcelAnimationimport ParcelCoreimport ParcelPlatform
extension ParcelEngine { func prepareActivity(_ pending: [Pending], previous: State) { planningActivity.removeAll(keepingCapacity: true) guard activity?.isEnabled == true else { return } for item in pending { let command: Command? let key: WorkspaceKey? switch item.event { case .commandIssued(let value): command = value key = previous.activeWorkspace case .workspaceCommandIssued(let value, let workspace): command = value key = workspace case .commandForWindow(let window, let value, let workspace): command = value key = workspace ?? previous.workspace(containing: window) default: command = nil key = item.activitySource == .mouse ? previous.activeWorkspace : nil } var keys = Set(key.map { [$0] } ?? []) let source: WindowID? switch item.event { case .commandIssued, .workspaceCommandIssued: source = previous.focusedWindow case .commandForWindow(let window, _, _): source = window case .windowDropped(let window, let target, _): source = window if let targetKey = previous.workspace(containing: target) { keys.insert(targetKey) } case .gestureCommitted(let gesture): switch gesture { case .swapTiles(let first, let second): source = first.visible if let targetKey = previous.workspace(containing: second.visible) { keys.insert(targetKey) } case .fullHeight(let window), .resizeTile(let window, _), .moveToDisplay(let window, _), .floatingFrame(let window, _, _): source = window case .drop(let window, let workspace, _, _): source = window keys.insert(workspace) } case .windowAppeared(_, let workspace, _): source = nil keys.insert(workspace) default: source = nil } if let source { if let key = previous.workspace(containing: source) { keys.insert(key) } if let key = snapshot.state.workspace(containing: source) { keys.insert(key) } } let feature = command.flatMap(Self.activityFeature) ?? Self.activityFeature(item.event) let context = ActivityContext(source: item.activitySource, feature: feature) for window in snapshot.state.frames.keys where snapshot.state.workspace(containing: window).map(keys.contains) == true { planningActivity[window] = context } } }
func bindActivity(_ plan: FramePlan, window: WindowID) { guard activity?.isEnabled == true else { return } let previous = activityPlacements[window] let collectionGeneration = activity?.generation ?? 0 let retained = previous.flatMap { $0.collectionGeneration == collectionGeneration ? $0 : nil } let context = planningActivity[window] ?? previous?.context ?? ActivityContext(source: liveResizeWindows.contains(window) ? .mouse : .other, feature: nil) activityPlacements[window] = ActivityPlacement( collectionGeneration: collectionGeneration, generation: plan.request.generation, revision: plan.revision, target: Rect(plan.request.target), context: context, initial: retained?.initial) }
func capturesActivityBaseline(_ window: WindowID, sent: MotionFrame) -> Bool { guard let placement = activityPlacements[window] else { return false } return placement.generation == sent.generation && placement.revision == sent.revision && placement.initial == nil }
func acceptActivityBaseline(_ observed: WindowFrame, sent: MotionFrame, collectionGeneration: UInt64?) { // Replanning can finish before the first write replies; its baseline must still belong to this collection. guard let placement = activityPlacements[observed.window], placement.collectionGeneration == collectionGeneration, placement.generation == sent.generation, sent.revision <= placement.revision, placement.initial == nil else { return } activityPlacements[observed.window]?.initial = observed.frame }
func acceptActivityReadback(_ observed: WindowFrame, sent: MotionFrame) { guard sent.isFinal, !liveResizeWindows.contains(observed.window) || pendingMouseCommit?.window == observed.window, let placement = activityPlacements[observed.window], placement.generation == sent.generation, placement.revision == sent.revision, placement.target == targets[observed.window] || pendingMouseCommit?.window == observed.window, observed.frame.matches(placement.target) || observed.frame.fitsInside(placement.target), snapshot.state.windows[observed.window]?.isFloating == true || observed.frame.fitsInside(placement.target) else { return } activityPlacements[observed.window] = nil guard placement.collectionGeneration == activity?.generation else { return } guard let initial = placement.initial, initial != observed.frame else { return } activity?.record(placements: 1, source: placement.context.source, feature: placement.context.feature) }
static func activityFeature(_ event: Event) -> ActivityFeature? { switch event { case .tileEdgeResized, .tileEdgeReset: .sizes case .windowDropped(_, _, true), .gestureCommitted(.drop(_, _, _, .stack)): .stack default: nil } }
static func activityFeature(_ command: Command) -> ActivityFeature? { switch command { case .toggleFocus: .focus case .makePrimary: .makePrimary case .toggleFloat, .tileWindow: .float case .stack, .unstack, .focusNextInStack, .focusPreviousInStack: .stack case .setRatio, .nudgeRatio, .resetSizes: .sizes case .retile: .retile default: nil } }}