Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305import Testing
@testable import ParcelCore
private func fourTileState(_ layout: Layout) -> State { if layout == .side { return stateWithWorkspace(workspace(4, layout: layout), screen: Rect(width: 1200, height: 1200)) } return populatedTree(4, layout: layout)}
private func fourFrames(_ layout: Layout) -> [WindowID: Rect] { let frames: [Rect] switch layout { case .columns: frames = (0..<4).map { Rect(x: Double($0) * 300, y: 0, width: 300, height: 800) } case .rows: frames = (0..<4).map { Rect(x: 0, y: Double($0) * 200, width: 1200, height: 200) } case .grid: frames = [ Rect(x: 0, y: 0, width: 600, height: 400), Rect(x: 600, y: 0, width: 600, height: 400), Rect(x: 0, y: 400, width: 600, height: 400), Rect(x: 600, y: 400, width: 600, height: 400), ] case .side: frames = [ Rect(x: 0, y: 0, width: 600, height: 1200), Rect(x: 600, y: 0, width: 600, height: 400), Rect(x: 600, y: 400, width: 600, height: 400), Rect(x: 600, y: 800, width: 600, height: 400), ] case .tree, .bsp, .accordion: preconditionFailure("No four-frame fixture for \(layout)") } return Dictionary(uniqueKeysWithValues: frames.enumerated().map { (windowID($0.offset + 1), $0.element) })}
private func directionalPair(_ layout: Layout, _ direction: Direction) -> (source: Int, target: Int)? { switch (layout, direction) { case (.columns, .left), (.rows, .up), (.grid, .left), (.side, .left): (2, 1) case (.columns, .right), (.rows, .down), (.grid, .right): (1, 2) case (.grid, .up): (3, 1) case (.grid, .down): (1, 3) case (.side, .right): (1, 3) case (.side, .up): (3, 2) case (.side, .down): (2, 3) default: nil }}
@Test(arguments: [Layout.grid, .columns, .rows, .side], Direction.allCases)func swapsHaveExactFramesInCanonicalLayouts(layout: Layout, direction: Direction) throws { let pair = directionalPair(layout, direction) let state = focused(pair?.source ?? 1, in: fourTileState(layout)) let initial = fourFrames(layout) #expect(state.frames == initial) let result = command(.swap(direction), in: state) guard let pair else { #expect(result.0 == state) #expect(result.1 == [.report(.noNeighbor(direction))]) return } let source = windowID(pair.source) let target = windowID(pair.target) var expected = initial expected[source] = initial[target] expected[target] = initial[source] #expect(result.0.frames == expected) #expect( result.0.workspaces[primaryKey]?.order == (1...4).map { number in windowID(number == pair.source ? pair.target : (number == pair.target ? pair.source : number)) }) #expect(result.0.focusedWindow == source) let writes = try [source, target].sorted().map { window in WindowFrame(window: window, frame: try #require(expected[window])) } #expect(frameWrites(result.1) == writes)}
@Test func threeColumnNudgesUseTheActualMainDividerAndResetToEqualWidths() { let state = focused(3, in: populated(3, layout: .columns)) let grown = command(.nudgeRatio(0.05), in: state) #expect(grown.0.frames[windowID(1)] == Rect(width: 460, height: 800)) #expect(grown.0.frames[windowID(2)] == Rect(x: 460, width: 370, height: 800)) #expect(grown.0.frames[windowID(3)] == Rect(x: 830, width: 370, height: 800)) #expect(grown.0.focusedWindow == windowID(3)) let inverse = command(.nudgeRatio(-0.05), in: grown.0) #expect(inverse.0.frames == state.frames) #expect(command(.resetSizes, in: grown.0).0.frames == state.frames) let preset = command(.setRatio(.threeFifths), in: state).0 #expect(preset.frames[windowID(1)] == Rect(width: 720, height: 800)) let nudgedPreset = command(.nudgeRatio(0.05), in: preset).0 #expect(nudgedPreset.frames[windowID(1)] == Rect(width: 780, height: 800)) #expect(command(.resetSizes, in: nudgedPreset).0.frames == state.frames)}
@Test(arguments: Layout.allCases)func sizeNudgesGrowShrinkAndResetWithoutChangingMembership(layout: Layout) throws { let initial = focused(layout == .accordion ? 3 : 2, in: populated(3, layout: layout)) let state = command(.resetSizes, in: initial).0 let main = windowID(layout == .accordion ? 3 : 1) let before = try #require(state.frames[main]) let grown = command(.nudgeRatio(0.05), in: state) let larger = try #require(grown.0.frames[main]) let shrunk = command(.nudgeRatio(-0.05), in: state) let smaller = try #require(shrunk.0.frames[main]) #expect(larger.size.width * larger.size.height > before.size.width * before.size.height) #expect(smaller.size.width * smaller.size.height < before.size.width * before.size.height) for result in [grown, shrunk] { #expect(result.0.focusedWindow == state.focusedWindow) #expect(result.0.workspaces[primaryKey]?.layout == layout) #expect(result.0.workspaces[primaryKey]?.order == state.workspaces[primaryKey]?.order) #expect(result.0.workspaces[primaryKey]?.openingOrder == state.workspaces[primaryKey]?.openingOrder) #expect(Set(result.0.frames.keys) == Set(state.frames.keys)) #expect(!frameWrites(result.1).isEmpty) #expect(command(.resetSizes, in: result.0).0.frames == state.frames) #expect(command(.undo, in: result.0).0.frames == state.frames) } #expect(command(.nudgeRatio(-0.05), in: grown.0).0.frames == state.frames) #expect(command(.nudgeRatio(0), in: state).0 == state) #expect(command(.nudgeRatio(0), in: state).1.isEmpty)}
@Test func cappedRowsResetBothVisibleAndHiddenSizesAndRetainLaterMainSizeEdits() { let initial = focused(2, in: populated(3, layout: .rows)) let state = command(.resetSizes, in: initial).0 #expect(state.workspaces[primaryKey]?.visibleWindows.count == 2) #expect(state.frames[windowID(1)] == Rect(width: 1200, height: 400)) let grown = command(.nudgeRatio(0.05), in: state).0 let reset = command(.resetSizes, in: grown).0 #expect(reset.frames == state.frames) #expect(reset.workspaces[primaryKey]?.visibleWindows == state.workspaces[primaryKey]?.visibleWindows) #expect(reset.workspaces[primaryKey]?.order == state.workspaces[primaryKey]?.order) let expanded = command(.setWindowCap(0), in: reset).0 #expect(expanded.workspaces[primaryKey]?.visibleWindows.count == 3) #expect(expanded.frames == command(.setWindowCap(0), in: initial).0.frames) let edited = command(.nudgeRatio(0.05), in: reset).0 let expandedEdit = command(.setWindowCap(0), in: edited).0 #expect(expandedEdit.frames[windowID(1)] == edited.frames[windowID(1)]) #expect(expandedEdit.frames[windowID(1)] == Rect(width: 1200, height: 440)) #expect(expandedEdit.frames[windowID(2)] == Rect(y: 440, width: 1200, height: 180)) #expect(expandedEdit.frames[windowID(3)] == Rect(y: 620, width: 1200, height: 180))}
@Test(arguments: [Layout.grid, .tree, .bsp])func mainSizeNudgesLeaveOtherRowsUnchanged(layout: Layout) { let state = focused(4, in: populated(4, layout: layout)) let result = command(.nudgeRatio(0.1), in: state) #expect(result.0.frames[windowID(1)] == Rect(width: 720, height: 400)) #expect(result.0.frames[windowID(2)] == Rect(x: 720, width: 480, height: 400)) #expect(result.0.frames[windowID(3)] == state.frames[windowID(3)]) #expect(result.0.frames[windowID(4)] == state.frames[windowID(4)]) #expect(result.0.focusedWindow == windowID(4))}
private func movedFourFrames(_ layout: Layout, _ direction: Direction) -> [WindowID: Rect] { var frames = fourFrames(layout) switch (layout, direction) { case (.columns, .left): frames = [ windowID(1): Rect(x: 150, y: 0, width: 150, height: 800), windowID(2): Rect(x: 0, y: 0, width: 150, height: 800), windowID(3): Rect(x: 300, y: 0, width: 450, height: 800), windowID(4): Rect(x: 750, y: 0, width: 450, height: 800), ] case (.columns, .right): frames = [ windowID(1): Rect(x: 200, y: 0, width: 200, height: 800), windowID(2): Rect(x: 0, y: 0, width: 200, height: 800), windowID(3): Rect(x: 400, y: 0, width: 400, height: 800), windowID(4): Rect(x: 800, y: 0, width: 400, height: 800), ] case (.rows, .up): frames = [ windowID(1): Rect(x: 0, y: 100, width: 1200, height: 100), windowID(2): Rect(x: 0, y: 0, width: 1200, height: 100), windowID(3): Rect(x: 0, y: 200, width: 1200, height: 300), windowID(4): Rect(x: 0, y: 500, width: 1200, height: 300), ] case (.rows, .down): frames = [ windowID(1): Rect(x: 0, y: 133, width: 1200, height: 134), windowID(2): Rect(x: 0, y: 0, width: 1200, height: 133), windowID(3): Rect(x: 0, y: 267, width: 1200, height: 266), windowID(4): Rect(x: 0, y: 533, width: 1200, height: 267), ] case (.grid, .left), (.grid, .right): frames[windowID(1)] = Rect(x: 600, y: 0, width: 600, height: 400) frames[windowID(2)] = Rect(x: 0, y: 0, width: 600, height: 400) case (.grid, .up): frames[windowID(1)] = Rect(x: 0, y: 200, width: 600, height: 200) frames[windowID(3)] = Rect(x: 0, y: 0, width: 600, height: 200) frames[windowID(4)] = Rect(x: 0, y: 400, width: 1200, height: 400) case (.grid, .down): frames[windowID(1)] = Rect(x: 0, y: 600, width: 600, height: 200) frames[windowID(2)] = Rect(x: 0, y: 0, width: 1200, height: 400) frames[windowID(3)] = Rect(x: 0, y: 400, width: 600, height: 200) case (.side, .left): frames = [ windowID(1): Rect(x: 300, y: 0, width: 300, height: 1200), windowID(2): Rect(x: 0, y: 0, width: 300, height: 1200), windowID(3): Rect(x: 600, y: 0, width: 600, height: 600), windowID(4): Rect(x: 600, y: 600, width: 600, height: 600), ] case (.side, .right): frames = [ windowID(1): Rect(x: 900, y: 0, width: 300, height: 600), windowID(2): Rect(x: 0, y: 0, width: 600, height: 1200), windowID(3): Rect(x: 600, y: 0, width: 300, height: 600), windowID(4): Rect(x: 600, y: 600, width: 600, height: 600), ] case (.side, .up): frames[windowID(2)] = Rect(x: 600, y: 200, width: 600, height: 200) frames[windowID(3)] = Rect(x: 600, y: 0, width: 600, height: 200) frames[windowID(4)] = Rect(x: 600, y: 400, width: 600, height: 800) case (.side, .down): frames[windowID(2)] = Rect(x: 600, y: 300, width: 600, height: 300) frames[windowID(3)] = Rect(x: 600, y: 0, width: 600, height: 300) frames[windowID(4)] = Rect(x: 600, y: 600, width: 600, height: 600) default: break } return frames}
@Test(arguments: [Layout.grid, .columns, .rows, .side], Direction.allCases)func directionalMovesHaveExactFrames(layout: Layout, direction: Direction) throws { let pair = directionalPair(layout, direction) let state = focused(pair?.source ?? 1, in: fourTileState(layout)) let result = command(.move(direction), in: state) guard let pair else { #expect(result.0 == state) #expect(result.1 == [.report(.noNeighbor(direction))]) return } let expected = movedFourFrames(layout, direction) #expect(result.0.frames == expected) #expect(result.0.focusedWindow == windowID(pair.source)) #expect(result.0.workspaces[primaryKey]?.tree?.tileCount == 4) let writes = try expected.keys.sorted().filter { expected[$0] != state.frames[$0] }.map { WindowFrame(window: $0, frame: try #require(expected[$0])) } #expect(frameWrites(result.1) == writes)}
@Test(arguments: Direction.allCases)func moveToEveryDisplayDirectionHasExactFrames(direction: Direction) throws { let state = multipleDisplays(direction) let screen = try #require(state.displays[secondaryDisplay]?.frame) let result = command(.moveToDisplay(direction), in: state) let expected = [ windowID(1): Rect(x: screen.minX + 900, y: screen.minY, width: 300, height: 800), windowID(2): testScreen, windowID(3): Rect(x: screen.minX, y: screen.minY, width: 600, height: 800), windowID(4): Rect(x: screen.minX + 600, y: screen.minY, width: 300, height: 800), ] #expect(result.0.frames == expected) #expect(result.0.activeDisplay == secondaryDisplay) #expect(result.0.focusedWindow == windowID(1)) let writes = try [1, 2, 4].map { number in WindowFrame(window: windowID(number), frame: try #require(expected[windowID(number)])) } #expect(frameWrites(result.1) == writes)}
@Test(arguments: Direction.allCases, [2, 8])func directionalCrossDisplayMovesHaveExactFrames(direction: Direction, cap: Int) throws { let number = direction == .right ? 2 : 1 let state = focused(number, in: multipleDisplays(direction, destinationCap: cap)) let screen = try #require(state.displays[secondaryDisplay]?.frame) let target = windowID(direction == .left ? 4 : 3) var expected = state.frames expected[windowID(3 - number)] = testScreen if cap == 2 { let content = try #require(state.frames[target]) expected[windowID(number)] = content expected[target] = content } else { switch direction { case .left: expected[windowID(1)] = Rect(x: screen.minX + 600, y: 0, width: 300, height: 800) expected[windowID(4)] = Rect(x: screen.minX + 900, y: 0, width: 300, height: 800) case .right: expected[windowID(2)] = Rect(x: screen.minX + 300, y: 0, width: 300, height: 800) expected[windowID(3)] = Rect(x: screen.minX, y: 0, width: 300, height: 800) case .up: expected[windowID(1)] = Rect(x: 0, y: screen.minY, width: 600, height: 400) expected[windowID(3)] = Rect(x: 0, y: screen.minY + 400, width: 600, height: 400) case .down: expected[windowID(1)] = Rect(x: 0, y: screen.minY + 400, width: 600, height: 400) expected[windowID(3)] = Rect(x: 0, y: screen.minY, width: 600, height: 400) } } let result = command(.move(direction), in: state) #expect(result.0.frames == expected) #expect(result.0.activeDisplay == secondaryDisplay) #expect(result.0.focusedWindow == windowID(number)) #expect(result.0.workspace(containing: windowID(number)) == secondaryKey) let writes = try expected.keys.sorted().filter { expected[$0] != state.frames[$0] }.map { WindowFrame(window: $0, frame: try #require(expected[$0])) } #expect(frameWrites(result.1) == writes) if cap == 2 { #expect(!result.0.visibleWindows.contains(target)) #expect(result.1.contains(.raiseWindow(windowID(number)))) }}