-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
DesignSystem/Sources/DesignSystem/CommonUI/TooltipView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import SwiftUI | ||
|
||
public struct TooltipView<Content: View>: View { | ||
let alignment: Edge | ||
@Binding var isVisible: Bool | ||
let arrowOffset: CGFloat = 4 | ||
var xPadding: CGFloat = 0 | ||
var yPadding: CGFloat = 0 | ||
|
||
let content: () -> Content | ||
|
||
public init(alignment: Edge, isVisible: Binding<Bool>, xPadding: CGFloat, yPadding: CGFloat, content: @escaping () -> Content) { | ||
self.alignment = alignment | ||
self._isVisible = isVisible | ||
self.xPadding = xPadding | ||
self.yPadding = yPadding | ||
self.content = content | ||
} | ||
|
||
private var oppositeAlignment: Alignment { | ||
let result: Alignment | ||
switch alignment { | ||
case .top: result = .bottom | ||
case .bottom: result = .top | ||
case .leading: result = .trailing | ||
case .trailing: result = .leading | ||
} | ||
return result | ||
} | ||
|
||
private var theHint: some View { | ||
content() | ||
.padding(9.2) | ||
.background(.white) | ||
.foregroundColor(.white) | ||
.cornerRadius(8) | ||
.background(alignment: oppositeAlignment) { | ||
Rectangle() | ||
.fill(.white) | ||
.frame(width: 22, height: 22) | ||
.rotationEffect(.degrees(45)) | ||
.offset(x: alignment == .leading ? arrowOffset : 0) | ||
.offset(x: alignment == .trailing ? -arrowOffset : 0) | ||
.offset(y: alignment == .top ? arrowOffset : 0) | ||
.offset(y: alignment == .bottom ? -arrowOffset : 0) | ||
} | ||
.padding(9.2) | ||
.fixedSize() | ||
} | ||
|
||
public var body: some View { | ||
if isVisible { | ||
GeometryReader { proxy1 in | ||
theHint | ||
.hidden() | ||
.overlay { | ||
GeometryReader { proxy2 in | ||
theHint | ||
.drawingGroup() | ||
.offset( | ||
x: -(proxy2.size.width / 2 - xPadding) + (proxy1.size.width / 2), | ||
y: -(proxy2.size.height / 2 - yPadding) + (proxy1.size.height / 2) | ||
) | ||
.offset(x: alignment == .leading ? (-proxy2.size.width / 2) - (proxy1.size.width / 2) : 0) | ||
.offset(x: alignment == .trailing ? (proxy2.size.width / 2) + (proxy1.size.width / 2) : 0) | ||
.offset(y: alignment == .top ? (-proxy2.size.height / 2) - (proxy1.size.height / 2) : 0) | ||
.offset(y: alignment == .bottom ? (proxy2.size.height / 2) + (proxy1.size.height / 2) : 0) | ||
} | ||
} | ||
} | ||
.onTapGesture { | ||
withAnimation { | ||
isVisible.toggle() | ||
} | ||
} | ||
} | ||
} | ||
} |