diff --git a/Package.swift b/Package.swift index ef6c995..241d3d7 100644 --- a/Package.swift +++ b/Package.swift @@ -4,7 +4,7 @@ import PackageDescription let package = Package( - name: "SwiftUITooltip", + name: "SwiftUITooltipRS", platforms: [ .iOS(.v13), .macOS(.v10_15), .tvOS(.v13), .watchOS(.v6) ], diff --git a/Sources/SwiftUITooltip/TooltipConfigurations/DefaultTooltipConfig.swift b/Sources/SwiftUITooltip/TooltipConfigurations/DefaultTooltipConfig.swift index 56bc445..7a0791a 100644 --- a/Sources/SwiftUITooltip/TooltipConfigurations/DefaultTooltipConfig.swift +++ b/Sources/SwiftUITooltip/TooltipConfigurations/DefaultTooltipConfig.swift @@ -15,8 +15,8 @@ public struct DefaultTooltipConfig: TooltipConfig { public var borderRadius: CGFloat = 8 public var borderWidth: CGFloat = 2 - public var borderColor: Color = Color.primary - public var backgroundColor: Color = Color.clear + public var borderColor: Color = Color.black + public var backgroundColor: Color = Color.black public var contentPaddingLeft: CGFloat = 8 public var contentPaddingRight: CGFloat = 8 diff --git a/Sources/SwiftUITooltip/TooltipModifier.swift b/Sources/SwiftUITooltip/TooltipModifier.swift index ee78139..1b3a3d6 100644 --- a/Sources/SwiftUITooltip/TooltipModifier.swift +++ b/Sources/SwiftUITooltip/TooltipModifier.swift @@ -8,14 +8,19 @@ import SwiftUI struct TooltipModifier: ViewModifier { + // MARK: - Uninitialised properties var config: TooltipConfig var content: TooltipContent + /// Indicates whether tooltips are displayed. + @Binding var isPresented: Bool + // MARK: - Initialisers - init(config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) { + init(isPresented: Binding, config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) { + _isPresented = isPresented self.config = config self.content = content() } @@ -24,7 +29,7 @@ struct TooltipModifier: ViewModifier { @State private var contentWidth: CGFloat = 10 @State private var contentHeight: CGFloat = 10 - + @State var animationOffset: CGFloat = 0 // MARK: - Computed properties @@ -97,16 +102,16 @@ struct TooltipModifier: ViewModifier { return (g.size.height - contentHeight) / 2 } } - + // MARK: - Animation stuff - + private func dispatchAnimation() { if (config.enableAnimation) { DispatchQueue.main.asyncAfter(deadline: .now() + config.animationTime) { self.animationOffset = config.animationOffset DispatchQueue.main.asyncAfter(deadline: .now() + config.animationTime*0.1) { self.animationOffset = 0 - + self.dispatchAnimation() } } @@ -134,7 +139,6 @@ struct TooltipModifier: ViewModifier { .rotation(Angle(radians: self.arrowRotation)) .frame(width: self.config.arrowWidth+2, height: self.config.arrowHeight+1) .foregroundColor(self.config.backgroundColor) - ).frame(width: self.config.arrowWidth, height: self.config.arrowHeight) .offset(x: self.arrowOffsetX, y: self.arrowOffsetY) } @@ -163,23 +167,26 @@ struct TooltipModifier: ViewModifier { var tooltipBody: some View { GeometryReader { g in ZStack { - RoundedRectangle(cornerRadius: self.config.borderRadius) - .stroke(self.config.borderWidth == 0 ? Color.clear : self.config.borderColor) - .background(RoundedRectangle(cornerRadius: self.config.borderRadius) - .foregroundColor(self.config.backgroundColor)) - .frame(width: self.contentWidth, height: self.contentHeight) - .mask(self.arrowCutoutMask) - - ZStack { - content - .padding(self.config.contentPaddingEdgeInsets) - .fixedSize() - } - .background(self.sizeMeasurer) + if isPresented { + RoundedRectangle(cornerRadius: self.config.borderRadius) + .stroke(self.config.borderWidth == 0 ? Color.clear : self.config.borderColor) + .background(RoundedRectangle(cornerRadius: self.config.borderRadius) + .foregroundColor(self.config.backgroundColor)) + .frame(width: self.contentWidth, height: self.contentHeight) + .mask(self.arrowCutoutMask) + + ZStack { + content + .padding(self.config.contentPaddingEdgeInsets) + .fixedSize() + } + .background(self.sizeMeasurer) .overlay(self.arrowView) + } } .offset(x: self.offsetHorizontal(g), y: self.offsetVertical(g)) .animation(.easeInOut) + .opacity(isPresented ? 1 : 0) .onAppear { self.dispatchAnimation() } @@ -193,17 +200,3 @@ struct TooltipModifier: ViewModifier { .overlay(tooltipBody) } } - -struct Tooltip_Previews: PreviewProvider { - static var previews: some View { - var config = DefaultTooltipConfig(side: .top) - config.backgroundColor = Color(red: 0.8, green: 0.9, blue: 1) - - - return VStack { - Text("Say...").tooltip(config: config) { - Text("Something nice!") - } - }.previewDevice(.init(stringLiteral: "iPhone 12 mini")) - } -} diff --git a/Sources/SwiftUITooltip/TooltipViewExtension.swift b/Sources/SwiftUITooltip/TooltipViewExtension.swift index 8d2d900..0188bf1 100644 --- a/Sources/SwiftUITooltip/TooltipViewExtension.swift +++ b/Sources/SwiftUITooltip/TooltipViewExtension.swift @@ -8,27 +8,27 @@ import SwiftUI public extension View { - func tooltip(@ViewBuilder content: @escaping () -> TooltipContent) -> some View { + func tooltip(isPresented: Binding, @ViewBuilder content: @escaping () -> TooltipContent) -> some View { let config: TooltipConfig = DefaultTooltipConfig.shared - return modifier(TooltipModifier(config: config, content: content)) + return modifier(TooltipModifier(isPresented: isPresented, config: config, content: content)) } - func tooltip(config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) -> some View { - modifier(TooltipModifier(config: config, content: content)) - } + func tooltip(isPresented: Binding, config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) -> some View { + modifier(TooltipModifier(isPresented: isPresented, config: config, content: content)) + } - func tooltip(_ side: TooltipSide, @ViewBuilder content: @escaping () -> TooltipContent) -> some View { - var config = DefaultTooltipConfig.shared - config.side = side + func tooltip(isPresented: Binding, _ side: TooltipSide, @ViewBuilder content: @escaping () -> TooltipContent) -> some View { + var config = DefaultTooltipConfig.shared + config.side = side - return modifier(TooltipModifier(config: config, content: content)) - } - - func tooltip(_ side: TooltipSide, config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) -> some View { - var config = config - config.side = side + return modifier(TooltipModifier(isPresented: isPresented, config: config, content: content)) + } - return modifier(TooltipModifier(config: config, content: content)) - } + func tooltip(isPresented: Binding, _ side: TooltipSide, config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) -> some View { + var config = config + config.side = side + + return modifier(TooltipModifier(isPresented: isPresented, config: config, content: content)) + } }