Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104import Testing
@testable import ParcelCore
private func interval(origin: Double, extent: Double, gap: Double, count: Int, index: Int) -> (Double, Double) { let length = (extent - gap * Double(count - 1)) / Double(count) let start = origin + Double(index) * (length + gap) return (start, start + length)}
private func expectedFrame(layout: Layout, count: Int, index: Int, area: Rect, gap: Double, ratio: Double) -> Rect { var horizontal = (area.minX, area.maxX) var vertical = (area.minY, area.maxY) switch layout { case .columns: horizontal = interval(origin: area.minX, extent: area.size.width, gap: gap, count: count, index: index) case .rows: vertical = interval(origin: area.minY, extent: area.size.height, gap: gap, count: count, index: index) case .side: if count > 1 { let main = (area.size.width - gap) * ratio if index == 0 { horizontal = (area.minX, area.minX + main) } else { horizontal = (area.minX + main + gap, area.maxX) vertical = interval( origin: area.minY, extent: area.size.height, gap: gap, count: count - 1, index: index - 1) } } case .grid: let columns = Int(Double(count).squareRoot().rounded(.up)) let rows = (count + columns - 1) / columns let row = index / columns let rowCount = min(columns, count - row * columns) horizontal = interval( origin: area.minX, extent: area.size.width, gap: gap, count: rowCount, index: index % columns) vertical = interval(origin: area.minY, extent: area.size.height, gap: gap, count: rows, index: row) case .tree, .bsp, .accordion: preconditionFailure("No expected pixel formula for \(layout)") } let left = horizontal.0.rounded() let top = vertical.0.rounded() return Rect(x: left, y: top, width: horizontal.1.rounded() - left, height: vertical.1.rounded() - top)}
@Test(arguments: [Layout.grid, .columns, .rows, .side], 1...8)func exactPixelsWithGapsAndReservedEdges(layout: Layout, count: Int) { var space = layoutTree(count, layout: layout, gaps: Gaps(inner: 12, outer: Insets(8))) space.reservedEdges = Insets(top: 24, left: 10, bottom: 6, right: 20) let screen = Rect(x: -1200, y: -800, width: 1200, height: 800) space.adapt(to: screen) let result = LayoutEngine.arrange(space, in: screen) let area = Rect(x: -1182, y: -768, width: 1154, height: 754) let frames = result.frames for index in 0..<count { #expect( frames[windowID(index + 1)] == expectedFrame(layout: layout, count: count, index: index, area: area, gap: 12, ratio: 0.5)) } for frame in frames.values { #expect(frame.minX >= area.minX && frame.maxX <= area.maxX) #expect(frame.minY >= area.minY && frame.maxY <= area.maxY) #expect(frame.minX == frame.minX.rounded()) #expect(frame.size.width >= 0 && frame.size.height >= 0) } if count == 1 { #expect(frames[windowID(1)] == area) } if layout == .columns && count > 1 { for number in 1..<count { if let first = frames[windowID(number)], let second = frames[windowID(number + 1)] { #expect(second.minX - first.maxX == 12) } } }}
@Test(arguments: [Layout.grid, .columns, .rows, .side], 1...8)func fractionalGlobalEdgesRoundExactlyOnce(layout: Layout, count: Int) { var space = layoutTree(count, layout: layout, gaps: Gaps(inner: 12.5)) let area = Rect(x: -1000.25, y: 30.75, width: 1153.5, height: 755.25) space.adapt(to: area) let result = LayoutEngine.arrange(space, in: area) for index in 0..<count { #expect( result.frames[windowID(index + 1)] == expectedFrame(layout: layout, count: count, index: index, area: area, gap: 12.5, ratio: 0.5)) }}
@Test(arguments: [MainRatio(0.2), .half, .threeFifths, .twoThirds, MainRatio(0.8), MainRatio(0.37)], 1...8)func everySideTileUsesRatioAndGaps(ratio: MainRatio, count: Int) { var space = Workspace( key: primaryKey, layout: .side, ratio: ratio, gaps: Gaps(inner: 10), windowCap: count, order: (1...count).map(windowID)) space.layout = .tree space.adapt(to: testScreen) let result = LayoutEngine.arrange(space, in: testScreen) for index in 0..<count { #expect( result.frames[windowID(index + 1)] == expectedFrame( layout: .side, count: count, index: index, area: testScreen, gap: 10, ratio: ratio.value)) }}