Skip to content

Commit

Permalink
Support explicit animations in SwiftUI (#52)
Browse files Browse the repository at this point in the history
* Support for explicit animations.

* wip

* wip

* wip

* Update Package.swift

---------

Co-authored-by: Stephen Celis <stephen@stephencelis.com>
  • Loading branch information
mbrandonw and stephencelis authored Mar 25, 2024
1 parent db08755 commit a2a3845
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
11 changes: 9 additions & 2 deletions Example/Example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ class CounterModel {
var isPresentingSheet = false
var text = ""
func decrementButtonTapped() {
count -= 1
withAnimation {
count -= 1
}
}
func incrementButtonTapped() {
count += 1
withAnimation {
count += 1
}
}
func presentSheetButtonTapped() {
isPresentingSheet = true
Expand All @@ -28,8 +32,10 @@ struct ContentView: View {
TextField("Text", text: $model.text)
if model.isDisplayingCount {
Text(model.count.description)
.font(.largeTitle)
} else {
Text("Not tracking count")
.font(.largeTitle)
}
Button("Decrement") { model.decrementButtonTapped() }
Button("Increment") { model.incrementButtonTapped() }
Expand All @@ -45,6 +51,7 @@ struct ContentView: View {
WithPerceptionTracking {
Form {
Text(model.count.description)
.font(.largeTitle)
Button("Decrement") { model.decrementButtonTapped() }
Button("Increment") { model.incrementButtonTapped() }
}
Expand Down
12 changes: 12 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,15 @@ let package = Package(
),
]
)

//for target in package.targets where target.type != .system {
// target.swiftSettings = target.swiftSettings ?? []
// target.swiftSettings?.append(
// .unsafeFlags([
// "-c", "release",
// "-emit-module-interface", "-enable-library-evolution",
// "-Xfrontend", "-warn-concurrency",
// "-Xfrontend", "-enable-actor-data-race-checks",
// ])
// )
//}
14 changes: 9 additions & 5 deletions Sources/Perception/WithPerceptionTracking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import SwiftUI
@available(macOS, deprecated: 14, message: "Remove WithPerceptionTracking")
@available(tvOS, deprecated: 17, message: "Remove WithPerceptionTracking")
@available(watchOS, deprecated: 10, message: "Remove WithPerceptionTracking")
@MainActor
public struct WithPerceptionTracking<Content> {
@State var id = 0
let content: () -> Content
Expand All @@ -55,10 +54,8 @@ public struct WithPerceptionTracking<Content> {
let _ = self.id
return withPerceptionTracking {
self.instrumentedBody()
} onChange: {
Task { @MainActor in
self.id += 1
}
} onChange: { [_id = UncheckedSendable(self._id)] in
_id.value.wrappedValue += 1
}
}
}
Expand Down Expand Up @@ -167,3 +164,10 @@ public enum _PerceptionLocals {
@TaskLocal public static var isInPerceptionTracking = false
@TaskLocal public static var skipPerceptionChecking = false
}

private struct UncheckedSendable<A>: @unchecked Sendable {
let value: A
init(_ value: A) {
self.value = value
}
}

0 comments on commit a2a3845

Please sign in to comment.