An open source design system that's fully customizable and agent ready
TypeScript
fix(native): the Swift tooltip bubble hangs above or below its view instead of over it (#8449)
Cindy Zhang045224610b8b
diff --git a/packages/native/apple/Sources/Astryx/Tooltip/TooltipPresenter.swift b/packages/native/apple/Sources/Astryx/Tooltip/TooltipPresenter.swiftindex 2440aa55f7..654e12f552 100644--- a/packages/native/apple/Sources/Astryx/Tooltip/TooltipPresenter.swift+++ b/packages/native/apple/Sources/Astryx/Tooltip/TooltipPresenter.swift@@ -181,6 +181,14 @@ struct TooltipPresenter: ViewModifier {isFocused: holdsByFocus(isFocused: isFocused, focusIsKeyboard: focusIsKeyboard), focus: focus)}+ /// Whether keyboard focus still holds the bubble after the pointer enters+ /// or leaves the view or the bubble: a pointer leaving lets the bubble hide+ /// even though focus opened it, as core's `mouseleave` schedules the hide+ /// whatever brought it up, until focus leaves and returns.+ nonisolated static func keyboardFocusHolds(afterHover inside: Bool, holds: Bool) -> Bool {+ inside && holds+ }+/// Whether focus came from the keyboard, as core's `:focus-visible`: not/// while a pointer rests on the view, and not within a press of it.nonisolated static func isKeyboardFocus(isHovering: Bool, sincePress: TimeInterval) -> Bool {@@ -195,6 +203,7 @@ struct TooltipPresenter: ViewModifier {.simultaneousGesture(TapGesture().onEnded { pressed() }).onHover { inside inisHovering = inside+ focusIsKeyboard = Self.keyboardFocusHolds(afterHover: inside, holds: focusIsKeyboard)update()}.onChange(of: isFocused) {@@ -208,23 +217,20 @@ struct TooltipPresenter: ViewModifier {}.overlay(alignment: above ? .top : .bottom) {if isShown || isTapShown, let text {- let gap = astryx.tokens.space.s1- let isAbove = aboveTooltipBubble(text: text).onGeometryChange(for: CGRect.self) { $0.frame(in: .global) } action: { bubbleFrame = $0 }.background(TooltipWindowReader { window = $0 }).onHover { inside inisHoveringBubble = inside+ focusIsKeyboard = Self.keyboardFocusHolds(afterHover: inside, holds: focusIsKeyboard)update()}- .offset(x: sideways).background(TooltipEscape(hide: hideAll)).background(TooltipOutsideTap(isActive: isTapShown, viewFrame: viewFrame, bubbleFrame: bubbleFrame) {isTapShown = false})- // `space.s1` between the bubble and the view, above or below it.- .alignmentGuide(isAbove ? .top : .bottom) { d in isAbove ? d[.bottom] + gap : d[.top] - gap }+ .modifier(TooltipPlacement(isAbove: above, gap: astryx.tokens.space.s1, slide: sideways))}}.zIndex(isShown || isTapShown ? 1 : 0)@@ -340,6 +346,22 @@ private struct TooltipWindowProbe: View {}#endif+/// Places the bubble `gap` above the view it decorates, or below it, slid+/// sideways by `slide`. The bubble hangs from a zero-height frame on the+/// view's top or bottom edge, so it needs no measured height and sits in+/// place on its first frame. Used inside an overlay aligned to that edge.+struct TooltipPlacement: ViewModifier {+ let isAbove: Bool+ let gap: CGFloat+ let slide: CGFloat++ func body(content: Content) -> some View {+ content+ .frame(height: 0, alignment: isAbove ? .bottom : .top)+ .offset(x: slide, y: isAbove ? -gap : gap)+ }+}+/// Escape hides a shown bubble wherever focus is, as core's top-most layer/// takes the press: a hidden button bound to Escape lives while the bubble/// shows.diff --git a/packages/native/apple/Tests/AstryxTests/TooltipPresenterTests.swift b/packages/native/apple/Tests/AstryxTests/TooltipPresenterTests.swiftindex 5fb0994349..7c8072291b 100644--- a/packages/native/apple/Tests/AstryxTests/TooltipPresenterTests.swift+++ b/packages/native/apple/Tests/AstryxTests/TooltipPresenterTests.swift@@ -168,3 +168,53 @@ import Testing#expect(P.showsBubble(hasText: true, isHovering: false, isHoveringBubble: false, isFocused: true, focusIsKeyboard: true, focus: .tabStop))}+@Test func aPointerLeavingLetsAKeyboardFocusedBubbleHide() {+ typealias P = TooltipPresenter+ #expect(!P.keyboardFocusHolds(afterHover: false, holds: true))+ #expect(P.keyboardFocusHolds(afterHover: true, holds: true))+ #expect(!P.keyboardFocusHolds(afterHover: true, holds: false))+}++/// Rows the bubble's dark fill covers, and rows the view shows, in a render+/// of a 36-point view with a placed bubble.+@MainActor+private func placedRows(isAbove: Bool, text: String = "Ada Lovelace") -> (bubble: ClosedRange<Int>?, view: ClosedRange<Int>) {+ let theme = Theme.default.light+ let probe = ZStack(alignment: .topLeading) {+ Color.white+ Color.blue.frame(width: 36, height: 36)+ .overlay(alignment: isAbove ? .top : .bottom) {+ TooltipBubble(text: text).modifier(TooltipPlacement(isAbove: isAbove, gap: 4, slide: 0))+ }+ .offset(x: 150, y: 100)+ }+ .frame(width: 400, height: 240)+ let pixels = Pixels(probe.astryxTheme())+ let ink = rgba(theme.color.textPrimary)+ func rows(_ match: ([Int]) -> Bool) -> [Int] {+ (0..<pixels.height).filter { y in (0..<pixels.width).contains { x in match(pixels.at(x, y)) } }+ }+ let dark = rows { close($0, ink, within: 12) }+ let blue = rows { $0[2] > 200 && $0[0] < 60 && $0[1] < 140 }+ return (dark.isEmpty ? nil : dark.first!...dark.last!, blue.first!...blue.last!)+}++// INV1: the bubble sits `gap` above the view, or below it, and never on it.+@MainActor+@Test func theBubbleSitsAboveOrBelowTheViewNotOnIt() throws {+ let above = placedRows(isAbove: true)+ let bubble = try #require(above.bubble)+ #expect(above.view == 100...135)+ #expect(bubble.upperBound == 95)+ #expect(bubble.count >= 20)+ let below = placedRows(isAbove: false)+ let under = try #require(below.bubble)+ #expect(below.view == 100...135)+ #expect(under.lowerBound == 140)+ // Long words wrap upward from the same edge.+ let long = placedRows(isAbove: true, text: String(repeating: "A long tooltip ", count: 6))+ let wrapped = try #require(long.bubble)+ #expect(wrapped.upperBound == 95)+ #expect(wrapped.count > bubble.count)+}+diff --git a/packages/native/specs/swift/library/tooltip.spec.md b/packages/native/specs/swift/library/tooltip.spec.mdindex 600802fe5a..ad2fd2816c 100644--- a/packages/native/specs/swift/library/tooltip.spec.md+++ b/packages/native/specs/swift/library/tooltip.spec.md@@ -69,10 +69,13 @@ shows with the system's own look and delay, never on keyboard focus or a tap.300-point width. The bubble draws in the view's own layer, so a scroll viewor another clipping container around the view cuts it off, where core's toplayer is never clipped.- On its first show, before the window's safe area and the bubble's own size are known, the bubble opens above the view, or below it when even the gap does not fit under the `space.s4` floor, with no slide, and moves into place on the next frame.+ The bubble hangs from the view's top or bottom edge, so it sits `space.s1` clear of the view from its first frame. On that first show, before the window's safe area and the bubble's own size are known, it opens above the view, or below it when even the gap does not fit under the `space.s4` floor, with no slide, and flips or slides into place on the next frame.- **INV2 — Pointer.** A pointer resting on the view shows the bubble aftercore's 200 ms. It stays while the pointer is on the view or on the bubble,hides 100 ms after the pointer leaves both, and a press hides it.+ A pointer that leaves the view or the bubble hides it after the bridge even+ when keyboard focus opened it, as core's `mouseleave` does, until focus+ leaves and returns.- **INV3 — Keyboard focus.** Where focus reaches the tooltip, focus thekeyboard brings shows the bubble at once, as core's `focusin` does, and ithides 100 ms after focus leaves. Whether the keyboard brought focus is@@ -122,8 +125,8 @@ tooltip tests prove it stays current.| Invariant | Evidence | Failure signal || ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |-| INV1 | `TooltipPresenterTests.swift`: above unless no room; slides inside the window; long words wrap within 300 points | a bubble below with room above, or one line past 300 points |-| INV2 | `TooltipPresenterTests.swift`: delay and bridge; the pointer on the view or the bubble keeps it up | a bubble that hides while hovered or shows at once on a pointer |+| INV1 | `TooltipPresenterTests.swift`: above unless no room; slides inside the window; long words wrap within 300 points; a render puts the bubble 4 points above or below a view, never on it | a bubble below with room above, or one line past 300 points |+| INV2 | `TooltipPresenterTests.swift`: delay and bridge; the pointer on the view or the bubble keeps it up; the rule that a pointer leaving lets a keyboard-opened bubble hide, as logic only, since the hover path needs an app-hosted test | a bubble that hides while hovered or shows at once on a pointer || INV3 | `TooltipPresenterTests.swift`: focus shows at once for `control` and `tabStop`, never for `none`; only keyboard focus opens or holds it | a delayed focus bubble, one on a view focus does not reach, or one a click's focus opens or holds || INV5 | `TooltipPresenterTests.swift`: `auto` resolves as core's; only a direct touch on a tap trigger opens it; a tap elsewhere closes it, a tap on the view toggles it, a scroll or pinch on the view closes it, and a gesture on the bubble does not | a bubble from a pointer click, under VoiceOver, or on a control under `auto`; one a gesture elsewhere or a scroll on the view leaves open || INV6 | `TooltipPresenterTests.swift`: blank words and a disabled view show nothing | a tooltip on a disabled view |