Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162import Testing
@testable import ParcelCore
@Test(arguments: [ (SnapAction.leftHalf, Rect(x: 10, y: 30, width: 490, height: 755)), (.rightHalf, Rect(x: 504, y: 30, width: 490, height: 755)), (.topHalf, Rect(x: 10, y: 30, width: 984, height: 376)), (.bottomHalf, Rect(x: 10, y: 410, width: 984, height: 375)), (.topLeft, Rect(x: 10, y: 30, width: 490, height: 376)), (.topRight, Rect(x: 504, y: 30, width: 490, height: 376)), (.bottomLeft, Rect(x: 10, y: 410, width: 490, height: 375)), (.bottomRight, Rect(x: 504, y: 410, width: 490, height: 375)), (.leftThird, Rect(x: 10, y: 30, width: 326, height: 755)), (.centerThird, Rect(x: 340, y: 30, width: 324, height: 755)), (.rightThird, Rect(x: 668, y: 30, width: 326, height: 755)), (.leftTwoThirds, Rect(x: 10, y: 30, width: 654, height: 755)), (.rightTwoThirds, Rect(x: 340, y: 30, width: 654, height: 755)), (.maximize, Rect(x: 10, y: 30, width: 984, height: 755)), (.almostMaximize, Rect(x: 59, y: 68, width: 886, height: 679)), (.center, Rect(x: 302, y: 308, width: 400, height: 200)),])func snapRegionsOnOddVisibleFrame(action: SnapAction, expected: Rect) throws { let actual = try SnapGeometry.frame( for: action, current: Rect(x: 33, y: 100, width: 400, height: 200), visibleFrame: Rect(x: 0, y: 20, width: 1004, height: 775), gaps: Gaps(inner: 4, outer: Insets(10))) #expect(actual == expected)}
@Test(arguments: [SnapAction.leftHalf, .rightHalf, .topHalf, .bottomHalf])func snapHalfCycle(action: SnapAction) throws { let screen = Rect(width: 1200, height: 900) let sizes = try (0...3).map { cycle in try SnapGeometry.frame(for: action, current: screen, visibleFrame: screen, cycle: cycle).size } if action == .leftHalf || action == .rightHalf { #expect(sizes.map(\.width) == [600, 800, 400, 600]) } else { #expect(sizes.map(\.height) == [450, 600, 300, 450]) }}
@Test(arguments: [ ("menu bar", Rect(x: 0, y: 24, width: 1440, height: 876), Rect(x: 17, y: 37, width: 1398, height: 842)), ("notch", Rect(x: 0, y: 38, width: 1440, height: 862), Rect(x: 17, y: 51, width: 1398, height: 828)), ("left Dock", Rect(x: 90, y: 24, width: 1350, height: 876), Rect(x: 107, y: 37, width: 1308, height: 842)), ("right Dock", Rect(x: 0, y: 24, width: 1350, height: 876), Rect(x: 17, y: 37, width: 1308, height: 842)), ("bottom Dock", Rect(x: 0, y: 24, width: 1440, height: 786), Rect(x: 17, y: 37, width: 1398, height: 752)), ( "negative origin", Rect(x: -1200, y: -700, width: 1200, height: 676), Rect(x: -1183, y: -687, width: 1158, height: 642) ),])func snapRespectsVisibleAndReservedEdges(obstruction: String, visible: Rect, expected: Rect) throws { let result = try SnapGeometry.frame( for: .maximize, current: Rect(width: 200, height: 100), visibleFrame: visible, gaps: Gaps(inner: 4, outer: Insets(5)), reservedEdges: Insets(top: 8, left: 12, bottom: 16, right: 20)) #expect(result == expected, "\(obstruction)") let half = try SnapGeometry.frame( for: .leftHalf, current: Rect(width: 200, height: 100), visibleFrame: visible, gaps: Gaps(inner: 4, outer: Insets(5)), reservedEdges: Insets(top: 8, left: 12, bottom: 16, right: 20)) #expect( half == Rect( x: expected.minX, y: expected.minY, width: expected.size.width / 2 - 2, height: expected.size.height), "\(obstruction)")}
@Test func snapDisplayMovePreservesRelativePositionAndPointSize() throws { let source = Rect(x: -1600, y: 24, width: 1600, height: 876) let destination = Rect(x: 0, y: 38, width: 1000, height: 662) let frame = Rect(x: -1000, y: 462, width: 400, height: 438) #expect( try SnapGeometry.move(frame, from: source, to: destination) == Rect(x: 300, y: 262, width: 400, height: 438)) #expect(try SnapGeometry.move(Rect(width: 2000, height: 1000), from: source, to: destination) == destination)}
@Test(arguments: [ (1.0, Rect(x: 1080, y: 669, width: 400, height: 300)), (2.0, Rect(x: 440, y: 269, width: 400, height: 300)),])func snapDisplayMoveUsesDifferentPointExtentsAtDifferentScales(destinationScale: Double, expected: Rect) throws { let sourceScale = 2.0 let source = Rect(x: -3840 / sourceScale, y: 24, width: 3840 / sourceScale, height: 2160 / sourceScale - 24) // Core gets points: the same pixel panel at a different scale has a different usable extent. let destination = Rect(x: 0, y: 38, width: 2560 / destinationScale, height: 1600 / destinationScale - 38) let frame = Rect(x: -1160, y: 402, width: 400, height: 300) let result = try SnapGeometry.move(frame, from: source, to: destination) #expect(result == expected) #expect(result.size == frame.size) #expect(result.minX != frame.minX)}
@Test(arguments: [ (Rect(x: -1600, y: -900, width: 1200, height: 800), Rect(x: -1500, y: -800, width: 300, height: 200)), (Rect(x: -1000, y: -700, width: 250, height: 180), Rect(x: -1000, y: -700, width: 250, height: 180)), (Rect(x: 90, y: 38, width: 1350, height: 762), Rect(x: 190, y: 138, width: 300, height: 200)),])func snapRestoreTranslatesRecordedDisplayAndClamps(visible: Rect, expected: Rect) throws { #expect( try SnapGeometry.restore( Rect(x: 100, y: 100, width: 300, height: 200), recordedDisplayFrame: Rect(width: 1200, height: 800), visibleFrame: visible) == expected)}
@Test func snapRestoreClampsToCurrentGapsAndReservedEdges() throws { #expect( try SnapGeometry.restore( Rect(x: 900, y: 650, width: 300, height: 150), recordedDisplayFrame: Rect(width: 1200, height: 800), visibleFrame: Rect(x: -1200, y: 38, width: 1000, height: 700), gaps: Gaps(outer: Insets(10)), reservedEdges: Insets(top: 40, left: 20, bottom: 60, right: 80)) == Rect(x: -590, y: 518, width: 300, height: 150))}
@Test func snapRestoreWithoutRecordedDisplayClampsLegacyAbsoluteFrame() throws { #expect( try SnapGeometry.restore( Rect(x: 100, y: 100, width: 300, height: 200), recordedDisplayFrame: nil, visibleFrame: Rect(x: -1000, y: -700, width: 900, height: 600)) == Rect(x: -400, y: -300, width: 300, height: 200))}
@Test func snapRestoreRejectsInvalidGeometry() { let frame = Rect(width: 300, height: 200) let visible = Rect(width: 1200, height: 800) #expect(throws: SnapGeometryError.invalidGeometry) { try SnapGeometry.restore(frame, recordedDisplayFrame: Rect(x: .infinity), visibleFrame: visible) } #expect(throws: SnapGeometryError.invalidGeometry) { try SnapGeometry.restore(frame, recordedDisplayFrame: visible, visibleFrame: Rect(width: .nan)) } #expect(throws: SnapGeometryError.invalidGeometry) { try SnapGeometry.restore( frame, recordedDisplayFrame: visible, visibleFrame: visible, gaps: Gaps(outer: Insets(.nan))) } #expect(throws: SnapGeometryError.invalidGeometry) { try SnapGeometry.restore( frame, recordedDisplayFrame: visible, visibleFrame: visible, reservedEdges: Insets(.infinity)) }}
@Test func snapResizeAndInvalidInput() throws { let area = Rect(width: 1000, height: 800) let current = Rect(x: 300, y: 300, width: 400, height: 200) #expect( try SnapGeometry.frame(for: .grow, current: current, visibleFrame: area) == Rect(x: 250, y: 260, width: 500, height: 280)) #expect( try SnapGeometry.frame(for: .shrink, current: current, visibleFrame: area) == Rect(x: 350, y: 340, width: 300, height: 120)) #expect(throws: SnapGeometryError.invalidGeometry) { try SnapGeometry.frame(for: .center, current: Rect(width: .infinity), visibleFrame: area) } #expect(throws: SnapGeometryError.actionNeedsContext) { try SnapGeometry.frame(for: .restore, current: current, visibleFrame: area) } #expect(throws: SnapGeometryError.invalidGeometry) { try SnapGeometry.move(Rect(x: .greatestFiniteMagnitude, width: .greatestFiniteMagnitude), from: area, to: area) }}