Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127public enum SnapGeometryError: Error, Sendable, Equatable { case invalidGeometry case actionNeedsContext}
public enum SnapGeometry { public static func frame( for action: SnapAction, current: Rect, visibleFrame: Rect, gaps: Gaps = Gaps(), reservedEdges: Insets = Insets(), cycle: Int = 0 ) throws -> Rect { guard valid(current), valid(visibleFrame), valid(gaps.outer), valid(reservedEdges), gaps.inner.isFinite, cycle >= 0 else { throw SnapGeometryError.invalidGeometry } let area = visibleFrame.inset(by: reservedEdges).inset(by: gaps.outer) let gap = max(0, gaps.inner) let fraction = [0.5, 2.0 / 3, 1.0 / 3][cycle % 3] let horizontalGap = min(gap, area.size.width) let verticalGap = min(gap, area.size.height) let result: Rect switch action { case .leftHalf: result = region(area, x: 0...fraction, y: 0...1, gapX: horizontalGap, gapY: 0) case .rightHalf: result = region(area, x: (1 - fraction)...1, y: 0...1, gapX: horizontalGap, gapY: 0) case .topHalf: result = region(area, x: 0...1, y: 0...fraction, gapX: 0, gapY: verticalGap) case .bottomHalf: result = region(area, x: 0...1, y: (1 - fraction)...1, gapX: 0, gapY: verticalGap) case .topLeft: result = region(area, x: 0...0.5, y: 0...0.5, gapX: horizontalGap, gapY: verticalGap) case .topRight: result = region(area, x: 0.5...1, y: 0...0.5, gapX: horizontalGap, gapY: verticalGap) case .bottomLeft: result = region(area, x: 0...0.5, y: 0.5...1, gapX: horizontalGap, gapY: verticalGap) case .bottomRight: result = region(area, x: 0.5...1, y: 0.5...1, gapX: horizontalGap, gapY: verticalGap) case .leftThird: result = region(area, x: 0...(1.0 / 3), y: 0...1, gapX: horizontalGap, gapY: 0) case .centerThird: result = region(area, x: (1.0 / 3)...(2.0 / 3), y: 0...1, gapX: horizontalGap, gapY: 0) case .rightThird: result = region(area, x: (2.0 / 3)...1, y: 0...1, gapX: horizontalGap, gapY: 0) case .leftTwoThirds: result = region(area, x: 0...(2.0 / 3), y: 0...1, gapX: horizontalGap, gapY: 0) case .rightTwoThirds: result = region(area, x: (1.0 / 3)...1, y: 0...1, gapX: horizontalGap, gapY: 0) case .center: result = centered(size: current.size, in: area) case .almostMaximize: result = centered(size: Size(width: area.size.width * 0.9, height: area.size.height * 0.9), in: area) case .maximize: result = area case .grow, .shrink: let factor = action == .grow ? 1.0 : -1.0 let size = Size( width: current.size.width + factor * area.size.width * 0.1, height: current.size.height + factor * area.size.height * 0.1) let width = min(area.size.width, max(0, size.width)) let height = min(area.size.height, max(0, size.height)) result = clamped( Rect( x: current.center.xCoordinate - width / 2, y: current.center.yCoordinate - height / 2, width: width, height: height), in: area) case .restore, .nextDisplay, .previousDisplay: throw SnapGeometryError.actionNeedsContext } guard valid(result) else { throw SnapGeometryError.invalidGeometry } return result.roundedToWholePoints() }
public static func move(_ frame: Rect, from source: Rect, to destination: Rect) throws -> Rect { guard valid(frame), valid(source), valid(destination) else { throw SnapGeometryError.invalidGeometry } let width = min(frame.size.width, destination.size.width) let height = min(frame.size.height, destination.size.height) let horizontalPosition = relative(frame.minX - source.minX, available: source.size.width - frame.size.width) let verticalPosition = relative(frame.minY - source.minY, available: source.size.height - frame.size.height) let result = Rect( x: destination.minX + horizontalPosition * (destination.size.width - width), y: destination.minY + verticalPosition * (destination.size.height - height), width: width, height: height) guard valid(result) else { throw SnapGeometryError.invalidGeometry } return result.roundedToWholePoints() }
public static func restore( _ frame: Rect, recordedDisplayFrame: Rect?, visibleFrame: Rect, gaps: Gaps = Gaps(), reservedEdges: Insets = Insets() ) throws -> Rect { guard valid(frame), valid(visibleFrame), recordedDisplayFrame.map(valid) != false, valid(gaps.outer), valid(reservedEdges) else { throw SnapGeometryError.invalidGeometry } let area = visibleFrame.inset(by: reservedEdges).inset(by: gaps.outer) let offset = recordedDisplayFrame.map { Point(x: visibleFrame.minX - $0.minX, y: visibleFrame.minY - $0.minY) } ?? Point() let translated = Rect( x: frame.minX + offset.xCoordinate, y: frame.minY + offset.yCoordinate, width: min(frame.size.width, area.size.width), height: min(frame.size.height, area.size.height)) guard valid(translated) else { throw SnapGeometryError.invalidGeometry } return clamped(translated, in: area).roundedToWholePoints() }
private static func relative(_ position: Double, available: Double) -> Double { guard available > 0 else { return 0.5 } return min(1, max(0, position / available)) }
private static func valid(_ rect: Rect) -> Bool { rect.isValid && rect.maxX.isFinite && rect.maxY.isFinite }
private static func valid(_ insets: Insets) -> Bool { insets.top.isFinite && insets.left.isFinite && insets.bottom.isFinite && insets.right.isFinite }
private static func centered(size: Size, in area: Rect) -> Rect { let width = min(area.size.width, max(0, size.width)) let height = min(area.size.height, max(0, size.height)) return Rect( x: area.minX + (area.size.width - width) / 2, y: area.minY + (area.size.height - height) / 2, width: width, height: height) }
private static func clamped(_ frame: Rect, in area: Rect) -> Rect { Rect( x: min(area.maxX - frame.size.width, max(area.minX, frame.minX)), y: min(area.maxY - frame.size.height, max(area.minY, frame.minY)), width: frame.size.width, height: frame.size.height) }
private static func region( _ area: Rect, x horizontal: ClosedRange<Double>, y vertical: ClosedRange<Double>, gapX: Double, gapY: Double ) -> Rect { let left = area.minX + area.size.width * horizontal.lowerBound + (horizontal.lowerBound > 0 ? gapX / 2 : 0) let right = area.minX + area.size.width * horizontal.upperBound - (horizontal.upperBound < 1 ? gapX / 2 : 0) let top = area.minY + area.size.height * vertical.lowerBound + (vertical.lowerBound > 0 ? gapY / 2 : 0) let bottom = area.minY + area.size.height * vertical.upperBound - (vertical.upperBound < 1 ? gapY / 2 : 0) return Rect( x: min(area.maxX, left), y: min(area.maxY, top), width: max(0, right - left), height: max(0, bottom - top)) }}