|
| 1 | +#if os(macOS) |
| 2 | +import AppKit |
| 3 | +import SwiftUI |
| 4 | +import XCTest |
| 5 | +@testable import AnnotKit |
| 6 | + |
| 7 | +/// "Closing the menu shortens it to the left, then it jumps to the right." |
| 8 | +/// |
| 9 | +/// The toolbar panel does not change size in step with the pill it carries. It |
| 10 | +/// GROWS the instant the mode flips and SHRINKS a third of a second later, once the |
| 11 | +/// pill's 0.15s width animation has settled (``OverlayController``'s asymmetry: a |
| 12 | +/// panel that shrinks early clips the control mid-animation). So on every open and |
| 13 | +/// close there is a window in which the panel is wider than its content — and |
| 14 | +/// ``OverlayPlacement/toolbarFrame(hostFrame:visibleFrame:panelSize:)`` places the |
| 15 | +/// panel by computing backwards from the pill's corner, which is only where the |
| 16 | +/// pill actually IS if the content is pinned to that corner. |
| 17 | +/// |
| 18 | +/// It was not: a `fixedSize` root in an oversized `NSHostingView` is CENTERED. |
| 19 | +/// Measured before the fix, idle content (72pt) in the still-open panel (258pt): |
| 20 | +/// the pill sat 93pt from each edge instead of 14pt from the trailing one — so it |
| 21 | +/// collapsed toward the middle as it narrowed, then snapped 79pt right when the |
| 22 | +/// panel finally shrank under it. |
| 23 | +/// |
| 24 | +/// These tests rasterise the real view and look for the pill's opaque body, because |
| 25 | +/// the bug is entirely about where the control is DRAWN — every value the placement |
| 26 | +/// math produced was already correct. That is also why |
| 27 | +/// `PanelFrameEnforcementTests.testTheToolbarDoesNotMoveWhenTheMenuOpensOrCloses` |
| 28 | +/// passed throughout: it derives the pill's corner FROM the panel frame, which is |
| 29 | +/// the very assumption that only held once the panel had settled. |
| 30 | +@MainActor |
| 31 | +final class ToolbarPillAnchorTests: XCTestCase { |
| 32 | + /// The panel size the controller holds during a close: measured for ANNOTATE |
| 33 | + /// mode, while the pill has already animated back down to the lone pencil. |
| 34 | + private let oversized = CGSize(width: 258, height: 104) |
| 35 | + |
| 36 | + private func makeSession() -> AnnotationSession { |
| 37 | + AnnotationSession(source: MacElementSource(), sink: NotesFileSink(path: "/dev/null")) |
| 38 | + } |
| 39 | + |
| 40 | + /// The mid-close state: idle content, panel still at its annotate-mode width. |
| 41 | + func testThePillKeepsItsCornerWhileThePanelIsWiderThanTheContent() throws { |
| 42 | + let pill = try drawnPillFrame(panelSize: oversized, session: makeSession()) |
| 43 | + XCTAssertEqual(oversized.width - pill.maxX, PillStyle.panelChrome.trailing, accuracy: 1, |
| 44 | + "the pill's trailing edge must stay one chrome margin from the panel's, " |
| 45 | + + "or the panel's late shrink drags the control sideways") |
| 46 | + XCTAssertEqual(pill.minY, PillStyle.panelChrome.bottom, accuracy: 1, |
| 47 | + "same for the bottom edge — the seed panel is taller than the pill too") |
| 48 | + } |
| 49 | + |
| 50 | + /// The settled state, and the one the placement math is written against. The |
| 51 | + /// two must agree pixel for pixel: that they did NOT is the jump. |
| 52 | + func testTheSettledPanelDrawsThePillInTheSamePlaceAsTheOversizedOne() throws { |
| 53 | + let session = makeSession() |
| 54 | + let snugSize = measuredSize(session: session) |
| 55 | + let snug = try drawnPillFrame(panelSize: snugSize, session: session) |
| 56 | + let wide = try drawnPillFrame(panelSize: oversized, session: session) |
| 57 | + |
| 58 | + XCTAssertEqual(snugSize.width - snug.maxX, oversized.width - wide.maxX, accuracy: 1) |
| 59 | + XCTAssertEqual(snug.minY, wide.minY, accuracy: 1) |
| 60 | + XCTAssertEqual(snug.size.width, wide.size.width, accuracy: 1) |
| 61 | + } |
| 62 | + |
| 63 | + /// The corner pin is a `maxWidth`/`maxHeight` frame, and the SAME view is what |
| 64 | + /// ``OverlayController/measuredToolbarSize()`` sizes the panel from. A flexible |
| 65 | + /// frame that reported its max instead of its content's ideal would inflate the |
| 66 | + /// panel to fill the screen — every pixel of which is a pixel of the host app |
| 67 | + /// that can no longer be clicked. |
| 68 | + func testPinningTheCornerDoesNotInflateTheMeasuredSize() { |
| 69 | + let size = measuredSize(session: makeSession()) |
| 70 | + XCTAssertLessThan(size.width, 200, "idle is one 44pt pill plus 14pt of chrome a side") |
| 71 | + XCTAssertLessThan(size.height, 120) |
| 72 | + XCTAssertGreaterThan(size.width, 40) |
| 73 | + XCTAssertGreaterThan(size.height, 40) |
| 74 | + } |
| 75 | + |
| 76 | + // MARK: - Rasterising |
| 77 | + |
| 78 | + private func toolbarView(session: AnnotationSession) -> ToolbarOverlayView { |
| 79 | + ToolbarOverlayView(session: session, onToggle: {}, onCopy: {}, onExport: {}) |
| 80 | + } |
| 81 | + |
| 82 | + /// What the controller sizes the panel from: a fresh, uninstalled hosting view's |
| 83 | + /// `fittingSize`, with the default sizing options that make it report one. |
| 84 | + private func measuredSize(session: AnnotationSession) -> CGSize { |
| 85 | + NSHostingView(rootView: toolbarView(session: session)).fittingSize |
| 86 | + } |
| 87 | + |
| 88 | + /// The pill's drawn bounds inside a panel of `panelSize`, in AppKit's y-UP |
| 89 | + /// panel-local coordinates. |
| 90 | + /// |
| 91 | + /// Found by alpha, not by colour: the pill's capsule is opaque, and the only |
| 92 | + /// other thing the view draws is its shadow — black at 0.4, radius 12 — so a |
| 93 | + /// high alpha threshold picks out the body and ignores the halo around it. |
| 94 | + private func drawnPillFrame(panelSize: CGSize, session: AnnotationSession) throws -> CGRect { |
| 95 | + let view = NSHostingView(rootView: toolbarView(session: session)) |
| 96 | + // Match the panel's own hosting view: the controller owns the frame, so the |
| 97 | + // view must NOT resize itself to its content — being stretched past its |
| 98 | + // content is the whole state under test. |
| 99 | + view.sizingOptions = [] |
| 100 | + view.frame = CGRect(origin: .zero, size: panelSize) |
| 101 | + view.layoutSubtreeIfNeeded() |
| 102 | + |
| 103 | + let rep = try XCTUnwrap(view.bitmapImageRepForCachingDisplay(in: view.bounds), |
| 104 | + "no bitmap backing for the hosting view") |
| 105 | + view.cacheDisplay(in: view.bounds, to: rep) |
| 106 | + |
| 107 | + let scale = CGFloat(rep.pixelsWide) / panelSize.width |
| 108 | + var minX = Int.max, maxX = Int.min, minY = Int.max, maxY = Int.min |
| 109 | + for y in 0..<rep.pixelsHigh { |
| 110 | + for x in 0..<rep.pixelsWide { |
| 111 | + guard let color = rep.colorAt(x: x, y: y), color.alphaComponent > 0.9 else { continue } |
| 112 | + minX = min(minX, x); maxX = max(maxX, x) |
| 113 | + minY = min(minY, y); maxY = max(maxY, y) |
| 114 | + } |
| 115 | + } |
| 116 | + try XCTSkipIf(minX == Int.max, "the offscreen rasterisation drew nothing on this machine") |
| 117 | + |
| 118 | + // The bitmap is y-DOWN; the panel geometry this is compared against is y-UP. |
| 119 | + let top = CGFloat(minY) / scale |
| 120 | + let bottom = CGFloat(maxY + 1) / scale |
| 121 | + return CGRect(x: CGFloat(minX) / scale, |
| 122 | + y: panelSize.height - bottom, |
| 123 | + width: CGFloat(maxX + 1 - minX) / scale, |
| 124 | + height: bottom - top) |
| 125 | + } |
| 126 | +} |
| 127 | +#endif |
0 commit comments