Improves helder
This commit is contained in:
@@ -19,17 +19,32 @@ public struct Tooltip: View {
|
||||
}
|
||||
|
||||
public var body: some View {
|
||||
text
|
||||
.typeRole(Typography.tooltip)
|
||||
.foregroundStyle(Palette.text2)
|
||||
.multilineTextAlignment(.leading)
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
.padding(.horizontal, 10)
|
||||
.padding(.vertical, 8)
|
||||
.frame(maxWidth: Metrics.tooltipMaxWidth, alignment: .leading)
|
||||
.fixedSize()
|
||||
.background(Palette.raised, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
|
||||
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(Palette.tooltipBorder, lineWidth: Metrics.hairline))
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,18 +69,23 @@ struct TooltipModifier: ViewModifier {
|
||||
}
|
||||
.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)
|
||||
.alignmentGuide(.bottom) { $0[.top] - 8 }
|
||||
.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 must sit above its neighbours (`zIndex`) so the tip is not drawn under them.
|
||||
/// 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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user