Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156import ParcelCore
struct MouseDropTarget { let event: Event let preview: WindowFrame}
enum MouseGestureGeometry { static func workspace(at point: Point, state: State) -> WorkspaceKey? { state.displays.keys.sorted().lazy.compactMap { display -> WorkspaceKey? in guard let screen = state.displays[display], screen.isValid, (screen.physicalFrame ?? screen.frame).contains(point), let space = state.activeSpaces[display] else { return nil } let key = WorkspaceKey(display: display, space: space) return state.isActive(key) && state.workspaces[key] != nil ? key : nil }.first }
static func drop( source: WindowID, at point: Point, intent: WindowDropIntent, state: State, wholeTiles: Bool = false ) -> MouseDropTarget? { guard let origin = state.workspace(containing: source), state.isActive(origin), let destination = workspace(at: point, state: state), let workspace = state.workspaces[destination], let sourceStack = state.workspaces[origin]?.tree?.stacks.first(where: { $0.windows.contains(source) }) else { return nil } let target = workspace.visibleWindows.first { window in !sourceStack.windows.contains(window) && state.windows[window]?.isFloating == false && state.frames[window]?.contains(point) == true } let event: Event if wholeTiles, destination == origin, let target, let targetStack = workspace.tree?.stacks.first(where: { $0.windows.contains(target) }) { let selectedState = ParcelCore.reduce(state, .focusChanged(source)).0 guard let movingStack = selectedState.workspaces[origin]?.tree?.stacks.first(where: { $0.windows.contains(source) }) else { return nil } event = .gestureCommitted(.swapTiles(source: movingStack, target: targetStack)) } else if destination == origin { guard let target else { return nil } event = .windowDropped(source: source, target: target, stacking: intent == .stack) } else { event = .gestureCommitted(.drop(source: source, workspace: destination, target: target, intent: intent)) } let (next, _) = ParcelCore.reduce(state, event) // A refused candidate still reaches validation, which surfaces its report once. let preview = next.frames[source] ?? target.flatMap { state.frames[$0] } ?? state.displays[destination.display]?.frame guard let preview else { return nil } let hasRoom = (workspace.tree?.tileCount ?? 0) < workspace.effectiveWindowCap let previewWindow = destination == origin || !hasRoom ? target ?? source : source let frame = wholeTiles ? preview : destination == origin ? target.flatMap { state.frames[$0] } ?? preview : preview return MouseDropTarget(event: event, preview: WindowFrame(window: previewWindow, frame: frame)) }
static func ratioDrop(source: WindowID, at point: Point, state: State, tolerance: Double) -> MouseDropTarget? { guard let key = state.workspace(containing: source), workspace(at: point, state: state) == key, let edge = state.tileEdges.lazy.filter({ $0.workspace == key && $0.affectedWindows.contains(source) && $0.contains(point, tolerance: tolerance) }).min(by: { abs(coordinate(point, axis: $0.axis) - $0.position) < abs(coordinate(point, axis: $1.axis) - $1.position) }) else { return nil } let event = Event.tileEdgeResized(edge, delta: coordinate(point, axis: edge.axis) - edge.position) let (next, _) = ParcelCore.reduce(state, event) guard let frame = next.frames[source] else { return nil } return MouseDropTarget(event: event, preview: WindowFrame(window: source, frame: frame)) }
static func snap(at point: Point, current: Rect, state: State, tolerance: Double) throws -> (WorkspaceKey, Rect)? { guard let key = workspace(at: point, state: state), let display = state.displays[key.display], let workspace = state.workspaces[key] else { return nil } let screen = display.physicalFrame ?? display.frame let neighbors = state.displays.values.filter { $0.id != display.id && $0.isValid } .map { $0.physicalFrame ?? $0.frame } let sharedLeft = neighbors.contains { abs($0.maxX - screen.minX) <= 1 && point.yCoordinate >= $0.minY && point.yCoordinate <= $0.maxY } let sharedRight = neighbors.contains { abs($0.minX - screen.maxX) <= 1 && point.yCoordinate >= $0.minY && point.yCoordinate <= $0.maxY } let sharedTop = neighbors.contains { abs($0.maxY - screen.minY) <= 1 && point.xCoordinate >= $0.minX && point.xCoordinate <= $0.maxX } let sharedBottom = neighbors.contains { abs($0.minY - screen.maxY) <= 1 && point.xCoordinate >= $0.minX && point.xCoordinate <= $0.maxX } let left = !sharedLeft && point.xCoordinate - screen.minX <= tolerance let right = !sharedRight && screen.maxX - point.xCoordinate <= tolerance let top = !sharedTop && point.yCoordinate - screen.minY <= tolerance let bottom = !sharedBottom && screen.maxY - point.yCoordinate <= tolerance let action: SnapAction switch (left, right, top, bottom) { case (true, _, true, _): action = .topLeft case (_, true, true, _): action = .topRight case (true, _, _, true): action = .bottomLeft case (_, true, _, true): action = .bottomRight case (true, _, _, _): action = .leftHalf case (_, true, _, _): action = .rightHalf case (_, _, true, _): action = .maximize case (_, _, _, true): action = .bottomHalf default: return nil } let frame = try SnapGeometry.frame( for: action, current: current, visibleFrame: display.frame, gaps: workspace.gaps, reservedEdges: workspace.reservedEdges) return (key, frame) }
static func directFrame(start: Rect, from anchor: Point, to point: Point, resizing: Bool, minimum: Size) -> Rect { let dx = point.xCoordinate - anchor.xCoordinate let dy = point.yCoordinate - anchor.yCoordinate guard resizing else { return Rect(x: start.minX + dx, y: start.minY + dy, width: start.size.width, height: start.size.height) } let fromLeft = anchor.xCoordinate < start.center.xCoordinate let fromTop = anchor.yCoordinate < start.center.yCoordinate let width = max(max(1, minimum.width), start.size.width + (fromLeft ? -dx : dx)) let height = max(max(1, minimum.height), start.size.height + (fromTop ? -dy : dy)) return Rect( x: fromLeft ? start.maxX - width : start.minX, y: fromTop ? start.maxY - height : start.minY, width: width, height: height) }
static func bounded(_ frame: Rect, state: State) -> Rect { guard frame.isValid, frame.maxX.isFinite, frame.maxY.isFinite else { return frame } var closest = frame var distance = Double.infinity for display in state.displays.values.sorted(by: { $0.id < $1.id }) where display.isValid { let bounds = display.frame let titleWidth = min(40, min(frame.size.width, bounds.size.width)) let titleHeight = min(28, min(frame.size.height, bounds.size.height)) let x = min(bounds.maxX - titleWidth, max(bounds.minX + titleWidth - frame.size.width, frame.minX)) let y = min(bounds.maxY - titleHeight, max(bounds.minY, frame.minY)) let dx = x - frame.minX let dy = y - frame.minY let candidateDistance = dx * dx + dy * dy if candidateDistance == 0 { return frame } if candidateDistance < distance { distance = candidateDistance closest = Rect(x: x, y: y, width: frame.size.width, height: frame.size.height) } } return closest }
private static func coordinate(_ point: Point, axis: Axis) -> Double { axis == .horizontal ? point.xCoordinate : point.yCoordinate }}