Skip to content

Commit

Permalink
Merge pull request #8 from p-x9/fix/macos-error
Browse files Browse the repository at this point in the history
fix to prevent errors even on multi-platform project
  • Loading branch information
p-x9 authored Apr 15, 2023
2 parents 213eca9 + d587bba commit e3a63a8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
10 changes: 10 additions & 0 deletions Sources/TouchTracker/Extension/View+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@

import SwiftUI

extension View {
/// show a mark on the touched point
@available(macOS, unavailable)
public func touchTrack() -> TouchTrackingView<Self> {
TouchTrackingView {
self
}
}
}

// ref: https://github.com/YusukeHosonuma/SwiftUI-Common/blob/main/Sources/SwiftUICommon/Extension/View%2B.swift
extension View {
@ViewBuilder
Expand Down
20 changes: 9 additions & 11 deletions Sources/TouchTracker/TouchTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import SwiftUI

#if canImport(UIKit)
import UIKit
#endif

@available(macOS, unavailable)
public struct TouchTrackingView<Content: View>: View {
let content: Content

Expand All @@ -27,7 +29,9 @@ public struct TouchTrackingView<Content: View>: View {

public init(_ content: Content) {
self.content = content
#if canImport(UIKit)
UIWindow.hook()
#endif
}

public init(_ content: () -> Content) {
Expand Down Expand Up @@ -58,6 +62,7 @@ public struct TouchTrackingView<Content: View>: View {

public var body: some View {
content
#if canImport(UIKit)
.hidden()
.background(
ZStack {
Expand All @@ -68,9 +73,11 @@ public struct TouchTrackingView<Content: View>: View {
.zIndex(.infinity)
}
)
#endif
}
}

@available(macOS, unavailable)
extension TouchTrackingView {
/// radius of mark on touched point
public func touchPointRadius(_ radius: CGFloat) -> Self {
Expand Down Expand Up @@ -135,16 +142,6 @@ extension TouchTrackingView {
}
}

public extension View {
/// show a mark on the touched point
@available(macOS, unavailable)
func touchTrack() -> TouchTrackingView<Self> {
TouchTrackingView {
self
}
}
}

struct TouchTrackingView_Preview: PreviewProvider {
static var previews: some View {
VStack {
Expand All @@ -154,14 +151,15 @@ struct TouchTrackingView_Preview: PreviewProvider {
}
}
.frame(width: 100, height: 100)
#if canImport(UIKit)
.touchTrack()
.touchPointRadius(8)
.touchPointOffset(x: 0, y: -10)
.touchPointColor(.orange)
.touchPointBorder(true, color: .blue, width: 1)
.touchPointShadow(true, color: .purple, radius: 3)
.showLocationLabel(true)
#endif
}
}

#endif

0 comments on commit e3a63a8

Please sign in to comment.