Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129import Testing
@testable import ParcelCore
private struct TreeGenerator { var seed: UInt64
mutating func next(_ upperBound: Int) -> Int { seed = seed &* 6_364_136_223_846_793_005 &+ 1 return Int((seed >> 32) % UInt64(upperBound)) }
mutating func tree(_ windows: ArraySlice<WindowID>) -> TileTree { precondition(!windows.isEmpty) if windows.count == 1 { return .tile(WindowStack(windows[windows.startIndex])) } let count = next(windows.count - 1) + 1 let axis: Axis = next(2) == 0 ? .horizontal : .vertical let fraction = Double(next(7) + 1) / 8 let partition = next(2) == 0 let first = tree(windows.prefix(count)) let second = tree(windows.dropFirst(count)) return partition ? .partition(axis: axis, fraction: fraction, first: first, second: second) : .split(axis: axis, fraction: fraction, first: first, second: second) }}
private func expectFramePartition(_ result: LayoutResult, windows: [WindowID], area: Rect) { #expect(result.frames.count == windows.count) #expect(Set(result.frames.keys) == Set(windows)) #expect(Set(result.visibleWindows) == Set(windows)) let frames = Array(result.frames.values) for frame in frames { #expect(frame.size.width >= 0 && frame.size.height >= 0) #expect(frame.minX >= area.minX && frame.maxX <= area.maxX) #expect(frame.minY >= area.minY && frame.maxY <= area.maxY) } for left in frames.indices { for right in frames.indices where right > left { let first = frames[left] let second = frames[right] #expect( first.maxX <= second.minX || second.maxX <= first.minX || first.maxY <= second.minY || second.maxY <= first.minY) } }}
@Test(arguments: [Layout.tree, .bsp, .accordion], 1...24)func generatedTreePartitionsSurviveTransformsAndConflictingMinimums(layout: Layout, seed: Int) throws { var generator = TreeGenerator(seed: UInt64(seed)) let count = generator.next(8) + 2 let ids = (1...count).map(windowID) var space = workspace(count, layout: layout, cap: count) let original = generator.tree(ids[...]) #expect(original.rotatedClockwise().rotatedClockwise().rotatedClockwise().rotatedClockwise() == original) for axis in [Axis.horizontal, .vertical] { #expect(original.mirrored(along: axis).mirrored(along: axis) == original) } let windows = Dictionary( uniqueKeysWithValues: ids.map { id in var window = managed(Int(id.rawValue)) window.minimum = LearnedMinimumSize( size: Size(width: Double(generator.next(900)), height: Double(generator.next(700)))) return (id, window) }) let screens = [Rect(x: -900, y: 40, width: 900, height: 600), Rect(width: 120, height: 90)] for tree in [ original, original.balancedSplits(), original.rotatedClockwise(), original.mirrored(along: .horizontal), original.mirrored(along: .vertical), ] { space.tree = tree space.focus = ids[generator.next(count)] for screen in screens { space.adapt(to: screen) for gap in [0.0, 7.5, 256.0] { space.gaps = Gaps(inner: gap, outer: Insets(4)) space.reservedEdges = Insets(top: 3, left: 5, bottom: 7, right: 9) let area = screen.inset(by: space.reservedEdges).inset(by: space.gaps.outer).roundedToWholePoints() #expect(tree.isValid) expectFramePartition(LayoutEngine.arrange(space, in: screen), windows: ids, area: area) expectFramePartition( LayoutEngine.arrange(space, in: screen, windows: windows), windows: ids, area: area) } } }}
@Test(arguments: [Layout.tree, .bsp, .accordion], 1...8)func treeGeometryProperties(layout: Layout, count: Int) throws { for screen in [Rect(width: 1200, height: 800), Rect(x: -800, y: 30, width: 800, height: 1200)] { for gap in [0.0, 8.0, 35.0] { var space = workspace(count, layout: layout, gaps: Gaps(inner: gap, outer: Insets(gap))) space.adapt(to: screen) for transformation in 0..<5 { if let tree = space.tree { switch transformation { case 1: space.tree = tree.balancedSplits() case 2: space.tree = tree.rotatedClockwise() case 3: space.tree = tree.mirrored(along: .horizontal) case 4: space.tree = tree.mirrored(along: .vertical) default: break } } let tree = try #require(space.tree) #expect(tree.isValid) #expect(tree.windows.count == count) let result = LayoutEngine.arrange(space, in: screen) #expect(Set(result.frames.keys) == Set((1...count).map(windowID))) let frames = Array(result.frames.values) for frame in frames { #expect(frame.size.width > 0 && frame.size.height > 0) #expect(frame.minX >= screen.minX && frame.maxX <= screen.maxX) #expect(frame.minY >= screen.minY && frame.maxY <= screen.maxY) } for left in frames.indices { for right in frames.indices where right > left { let first = frames[left] let second = frames[right] #expect( first.maxX <= second.minX || second.maxX <= first.minX || first.maxY <= second.minY || second.maxY <= first.minY) } } } } }}