Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229import AppKitimport ParcelAnimationimport ParcelConfigimport ParcelCoreimport QuartzCoreimport Testingimport UniformTypeIdentifiers
@testable import ParcelUI
extension AppKitTestSuite { @Suite @MainActor struct TabViewPanelTests { private func card( _ id: UInt32, frame: Rect = Rect(x: -1800, y: -700, width: 200, height: 80), kind: TabViewPanelCard.Kind = .tile ) -> TabViewPanelCard { TabViewPanelCard( id: WindowID(rawValue: id), appName: "Editor", title: "Notes", pid: 0, frame: frame, position: 2, total: 5, kind: kind) }
@Test func convertsNegativeAndAbovePrimaryDisplayCoordinates() { let display = Rect(x: -1920, y: -1080, width: 1920, height: 1080) let frame = card(1).frame #expect( TabViewGeometry.panelFrame(display, primaryHeight: 900) == CGRect(x: -1920, y: 900, width: 1920, height: 1080)) #expect(TabViewGeometry.cardFrame(frame, in: display) == CGRect(x: 120, y: 380, width: 200, height: 80)) let below = Rect(x: 500, y: 900, width: 800, height: 600) #expect( TabViewGeometry.panelFrame(below, primaryHeight: 900) == CGRect(x: 500, y: -600, width: 800, height: 600)) }
@Test func labelsUseTheCompiledCatalogAndKeepFullText() throws { let url = try #require( Bundle.module.url(forResource: "TabView", withExtension: "strings", subdirectory: "en.lproj")) let strings = try PropertyListDecoder().decode([String: String].self, from: Data(contentsOf: url)) #expect(strings["%@, %@, tile %lld of %lld"] == "%1$@, %2$@, tile %3$lld of %4$lld") #expect(strings["Untitled window"] == "Untitled window") #expect(strings["Scroll to see more windows"] == "Scroll to see more windows") #expect(TabViewText.label(card(1)) == "Editor, Notes, tile 2 of 5") #expect(TabViewText.label(card(2, kind: .floating)) == "Editor, Notes, floating window 2 of 5") #expect(TabViewText.label(card(3, kind: .minimized)) == "Editor, Notes, minimized window 2 of 5") }
@Test func untitledWindowUsesLocalizedTitleForDisplayAndAccessibility() { _ = NSApplication.shared let untitled = TabViewPanelCard( id: WindowID(rawValue: 10), appName: "Editor", title: "", pid: 0, frame: Rect(x: 0, y: 0, width: 200, height: 80), position: 1, total: 2) let button = TabViewCardButton(card: untitled, selected: false, accessibility: .init()) { _ in } #expect(TabViewText.title(untitled) == "Untitled window") #expect(TabViewText.title(card(1)) == "Notes") #expect(button.accessibilityLabel() == "Editor, Untitled window, tile 1 of 2") }
@Test func canvasPreservesCardPositionsAndRoutesClicks() throws { _ = NSApplication.shared let first = card(1) let second = card(2, frame: Rect(x: -1600, y: -700, width: 200, height: 80)) let display = TabViewPanelDisplay( id: DisplayID(rawValue: "left"), frame: Rect(x: -1920, y: -1080, width: 1920, height: 1080), cards: [first, second]) var invoked: [WindowID] = [] let canvas = TabViewCanvas(display: display, selected: first.id, accessibility: .init()) { invoked.append($0) } let firstButton = try #require(canvas.buttons[first.id]) let secondButton = try #require(canvas.buttons[second.id]) #expect(canvas.isFlipped) #expect(firstButton.frame == CGRect(x: 120, y: 380, width: 200, height: 80)) #expect(secondButton.frame == CGRect(x: 320, y: 380, width: 200, height: 80)) #expect(firstButton.selected && !secondButton.selected) #expect(firstButton.accessibilityLabel() == "Editor, Notes, tile 2 of 5") #expect(secondButton.acceptsFirstMouse(for: nil)) secondButton.performClick(nil) #expect(invoked == [second.id]) canvas.updateSelection(second.id) #expect(!firstButton.selected && secondButton.selected) canvas.updateSelection(nil) #expect(!firstButton.selected && !secondButton.selected) canvas.updateAccessibility(.init(reduceTransparency: true, increaseContrast: true)) #expect(firstButton.accessibility.reduceTransparency && secondButton.accessibility.increaseContrast) canvas.disableInteraction() #expect(!firstButton.isEnabled && !secondButton.isEnabled) #expect(!firstButton.isAccessibilityElement() && !secondButton.isAccessibilityElement()) }
@Test func closeMotionUsesFadeTokenAndHonorsAccessibilityAndSettings() throws { let configuration = MotionConfiguration(speed: 2) let duration = try #require(TabViewMotion.closeDuration(configuration: configuration, reduceMotion: false)) #expect(duration == MotionToken.fade.spec(settings: configuration.uiMotionSettings).duration) #expect(TabViewMotion.closeDuration(configuration: configuration, reduceMotion: true) == nil) #expect( TabViewMotion.closeDuration(configuration: MotionConfiguration(enabled: false), reduceMotion: false) == nil) }
@Test(arguments: [ CGSize(width: 180, height: 64), CGSize(width: 200, height: 80), CGSize(width: 600, height: 300), ]) func layoutAlwaysReservesReadableIconAndBothTextLines(size: CGSize) { let layout = TabViewCardLayout(size: size) let bounds = CGRect(origin: .zero, size: size) #expect(layout.icon.width == 40 && layout.icon.height == 40) #expect(layout.app.width >= 108 && layout.title.width >= 108) #expect(layout.app.height == 17 && layout.title.height == 17) for frame in [layout.icon, layout.app, layout.title] { #expect(bounds.contains(frame)) } #expect(!layout.icon.intersects(layout.app)) #expect(!layout.icon.intersects(layout.title)) #expect(!layout.app.intersects(layout.title)) let mirrored = TabViewCardLayout(size: size, layoutDirection: .rightToLeft) #expect(mirrored.icon.minX == size.width - layout.icon.maxX) #expect(mirrored.title.minX == size.width - layout.title.maxX) #expect(mirrored.title.size == layout.title.size) #expect(bounds.contains(mirrored.icon) && bounds.contains(mirrored.title)) }
@Test func missingApplicationUsesGenericIconAndPreservesTheFullTruncatedTitleLabel() throws { _ = NSApplication.shared let title = String(repeating: "A long window title with readable details ", count: 10) let window = TabViewPanelCard( id: WindowID(rawValue: 1), appName: "Editor", title: title, pid: Int32.max, frame: Rect(width: 180, height: 64), position: 1, total: 1) #expect(NSRunningApplication(processIdentifier: window.pid) == nil) let button = TabViewCardButton(card: window, selected: false, accessibility: .init()) { _ in } #expect(button.appIcon.size.width > 0 && button.appIcon.size.height > 0) let other = TabViewCardButton(card: window, selected: false, accessibility: .init()) { _ in } #expect(button.appIcon === other.appIcon) #expect(button.accessibilityLabel() == "Editor, \(title), tile 1 of 1") #expect(button.toolTip == button.accessibilityLabel()) button.frame = CGRect(x: 0, y: 0, width: 180, height: 64) let image = try #require(button.bitmapImageRepForCachingDisplay(in: button.bounds)) button.cacheDisplay(in: button.bounds, to: image) #expect(image.pixelsWide >= 180 && image.pixelsHigh >= 64) }
private func denseDisplay(count: Int, kind: TabViewPanelCard.Kind, size: CGSize) -> TabViewPanelDisplay { let display = Rect(x: -1920, y: -1080, width: size.width, height: size.height) let cards = (0..<count).map { index in card( UInt32(index + 1), frame: Rect( x: display.minX + 16 + (kind == .minimized ? Double(index) * 2 : 0), y: display.minY + 16 + (kind == .minimized ? 0 : Double(index) * 2), width: kind == .minimized ? 2 : 200, height: kind == .minimized ? 80 : 2), kind: kind) } return TabViewPanelDisplay(id: DisplayID(rawValue: "left"), frame: display, cards: cards) }
@Test(arguments: [TabViewPanelCard.Kind.tile, .minimized], [32, 128]) func denseOverflowScrollsEveryKeyboardSelectionIntoView(kind: TabViewPanelCard.Kind, count: Int) throws { _ = NSApplication.shared let display = denseDisplay(count: count, kind: kind, size: CGSize(width: 640, height: 220)) let last = try #require(display.cards.last) var invoked: [WindowID] = [] let canvas = TabViewCanvas(display: display, selected: last.id, accessibility: .init()) { invoked.append($0) } let document = try #require(canvas.scrollView.documentView) #expect(document.isFlipped) #expect(canvas.buttons.count == count) #expect(canvas.scrollView.hasVerticalScroller && !canvas.scrollView.hasHorizontalScroller) #expect(canvas.scrollView.documentVisibleRect.contains(try #require(canvas.buttons[last.id]).frame)) #expect(canvas.scrollView.contentView.bounds.minY > 0) #expect(canvas.scrollView.accessibilityHelp() == "Scroll to see more windows") for card in display.cards { let button = try #require(canvas.buttons[card.id]) #expect(button.frame.width >= 180 && button.frame.height >= 64) let layout = TabViewCardLayout(size: button.frame.size) #expect(layout.icon.width == 40 && layout.title.width >= 108) canvas.updateSelection(card.id) #expect(button.selected) #expect(canvas.scrollView.documentVisibleRect.contains(button.frame)) #expect(canvas.scrollView.contentView.bounds.minY >= 0) #expect(canvas.scrollView.documentVisibleRect.maxY <= document.bounds.maxY) } let lastButton = try #require(canvas.buttons[last.id]) lastButton.performClick(nil) #expect(invoked == [last.id]) let visibleBeforeClearing = canvas.scrollView.documentVisibleRect canvas.updateSelection(nil) #expect(canvas.scrollView.documentVisibleRect == visibleBeforeClearing) #expect(canvas.buttons.values.allSatisfy { !$0.selected }) }
@Test func viewportSmallerThanMinimumCardsKeepsBothAxesReachable() throws { _ = NSApplication.shared let display = denseDisplay(count: 20, kind: .minimized, size: CGSize(width: 150, height: 48)) let last = try #require(display.cards.last) let canvas = TabViewCanvas(display: display, selected: last.id, accessibility: .init()) { _ in } let document = try #require(canvas.scrollView.documentView) #expect(canvas.scrollView.hasHorizontalScroller && canvas.scrollView.hasVerticalScroller) #expect(canvas.bounds.size == CGSize(width: 150, height: 48)) for card in display.cards { let button = try #require(canvas.buttons[card.id]) canvas.updateSelection(card.id) #expect(button.frame.size == CGSize(width: 180, height: 64)) #expect(canvas.scrollView.documentVisibleRect.intersects(button.frame)) #expect(document.bounds.contains(button.frame)) } canvas.scrollView.contentView.scroll(to: CGPoint(x: 0, y: 0)) canvas.scrollView.reflectScrolledClipView(canvas.scrollView.contentView) #expect(canvas.scrollView.documentVisibleRect.minX == 0) #expect(canvas.scrollView.documentVisibleRect.minY == 0) canvas.scrollView.contentView.scroll( to: CGPoint( x: document.bounds.maxX - canvas.scrollView.contentSize.width, y: document.bounds.maxY - canvas.scrollView.contentSize.height)) canvas.scrollView.reflectScrolledClipView(canvas.scrollView.contentView) #expect(canvas.scrollView.documentVisibleRect.maxX == document.bounds.maxX) #expect(canvas.scrollView.documentVisibleRect.maxY == document.bounds.maxY) }
@Test func reducedMotionRemovesExistingAnimationsWithoutAddingAny() { let layer = CALayer() layer.add(CABasicAnimation(keyPath: "opacity"), forKey: "existing") TabViewMotion.show(layer, configuration: MotionConfiguration(), reduceMotion: true) #expect(layer.animationKeys()?.isEmpty != false) TabViewMotion.show(layer, configuration: MotionConfiguration(enabled: false), reduceMotion: false) #expect(layer.animationKeys()?.isEmpty != false) } }}