import SwiftUI /// The design's tooltip: `raised` surface, text-5 border, radius 2, Sans 12/1.5, no arrow, no shadow. /// Name the current state in amber with `Tooltip.stated`, then say what a click does. public struct Tooltip: View { var text: Text public init(_ text: Text) { self.text = text } public init(_ string: String) { text = Text(string) } /// "Lead: *state* — rest", with the state word in amber. public static func stated(_ lead: String, _ state: String, _ rest: String) -> Text { Text(lead).foregroundColor(Palette.text2) + Text(state).foregroundColor(Palette.amber) + Text(rest).foregroundColor(Palette.text2) } public var body: some View { CappedWidth(maxWidth: Metrics.tooltipMaxWidth) { text .typeRole(Typography.tooltip) .foregroundStyle(Palette.text2) .multilineTextAlignment(.leading) .padding(.horizontal, 10) .padding(.vertical, 8) } .background(Palette.raised, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall)) .overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(Palette.tooltipBorder, lineWidth: Metrics.hairline)) } } /// Sizes its content to its ideal width up to `maxWidth`, wrapping and growing taller beyond it, whatever is proposed. /// `frame(maxWidth:)` under `fixedSize()` measures a wrapped text at its one-line height, so the text overflows the box. private struct CappedWidth: Layout { var maxWidth: CGFloat func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize { guard let content = subviews.first else { return .zero } let ideal = content.sizeThatFits(.unspecified) return ideal.width <= maxWidth ? ideal : content.sizeThatFits(ProposedViewSize(width: maxWidth, height: nil)) } func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) { subviews.first?.place(at: bounds.origin, anchor: .topLeading, proposal: ProposedViewSize(bounds.size)) } } struct TooltipModifier: ViewModifier { var text: Text var edge: HorizontalAlignment @State private var isShown = false @State private var pending: Task? func body(content: Content) -> some View { content .onHover { inside in pending?.cancel() if inside { pending = Task { @MainActor in try? await Task.sleep(for: Metrics.tooltipDelay) if !Task.isCancelled { isShown = true } } } else { isShown = false } } .simultaneousGesture(TapGesture().onEnded { pending?.cancel(); isShown = false }) .overlay(alignment: Alignment(horizontal: edge, vertical: .bottom)) { // A zero-height frame hangs the tip from the host's bottom edge; an explicit `.bottom` // alignment guide is ignored here and left the tip covering the host. if isShown { Tooltip(text) .frame(height: 0, alignment: .top) .offset(y: 8) .allowsHitTesting(false) } } .zIndex(isShown ? 1 : 0) } } extension View { /// Shows a design tooltip 350ms after the pointer rests on the view, 8pt below it. /// The host lifts itself above its siblings while the tip shows; its container must still sit above /// the views below it (`zIndex`), as the title bar does over the columns. public func tip(_ text: Text, edge: HorizontalAlignment = .leading) -> some View { modifier(TooltipModifier(text: text, edge: edge)) } public func tip(_ string: String, edge: HorizontalAlignment = .leading) -> some View { tip(Text(string), edge: edge) } }