Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,72 @@
// Audited for 6.5.4
// Status: Complete

// MARK: - ViewTraitCollection + canTransition
// MARK: - View + transition

@available(OpenSwiftUI_v1_0, *)
extension View {

/// Associates a transition with the view.
///
/// When this view appears or disappears, the transition will be applied to
/// it, allowing for animating it in and out.
///
/// The following code will conditionally show MyView, and when it appears
/// or disappears, will use a slide transition to show it.
///
/// if isActive {
/// MyView()
/// .transition(.slide)
/// }
/// Button("Toggle") {
/// withAnimation {
/// isActive.toggle()
/// }
/// }
@inlinable
@_disfavoredOverload
nonisolated public func transition(_ t: AnyTransition) -> some View {
return _trait(TransitionTraitKey.self, t)
}

/// Associates a transition with the view.
///
/// When this view appears or disappears, the transition will be applied to
/// it, allowing for animating it in and out.
///
/// The following code will conditionally show MyView, and when it appears
/// or disappears, will use a custom RotatingFadeTransition transition to
/// show it.
///
/// if isActive {
/// MyView()
/// .transition(RotatingFadeTransition())
/// }
/// Button("Toggle") {
/// withAnimation {
/// isActive.toggle()
/// }
/// }
@available(OpenSwiftUI_v5_0, *)
@_alwaysEmitIntoClient
nonisolated public func transition<T>(_ transition: T) -> some View where T: Transition {
self.transition(AnyTransition(transition))
}
}

// MARK: - TransitionTraitKey

@available(OpenSwiftUI_v1_0, *)
@usableFromInline
struct TransitionTraitKey: _ViewTraitKey {
@inlinable
static var defaultValue: AnyTransition { .opacity }
}

@available(*, unavailable)
extension TransitionTraitKey: Sendable {}

// MARK: - CanTransitionTraitKey

@available(OpenSwiftUI_v1_0, *)
@usableFromInline
Expand All @@ -17,6 +82,8 @@ struct CanTransitionTraitKey: _ViewTraitKey {
@available(*, unavailable)
extension CanTransitionTraitKey: Sendable {}

// MARK: - ViewTraitCollection + canTransition

extension ViewTraitCollection {
package var canTransition: Bool {
get { self[CanTransitionTraitKey.self] }
Expand All @@ -26,10 +93,6 @@ extension ViewTraitCollection {

// MARK: - ViewTraitCollection + transition

struct TransitionTraitKey: _ViewTraitKey {
static var defaultValue: AnyTransition { .opacity }
}

extension ViewTraitCollection {
package var transition: AnyTransition {
self[TransitionTraitKey.self]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,49 @@ struct AppearanceActionModifierCompatibilityTests {
}
await #expect(Helper.result == "AFATDTDT")
}

@Test("idTest with identity transition")
func idTestWithIdentityTransition() async throws {
enum Helper {
@MainActor
static var result = ""
}

struct ContentView: View {
@State private var toggle = false
var continuation: UnsafeContinuation<Void, Never>

var body: some View {
Color.red
.onAppear {
if Helper.result.isEmpty {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
toggle.toggle()
}
} else {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
continuation.resume()
}
}
Helper.result += "A"
Helper.result += toggle ? "T" : "F"
}
.onDisappear {
Helper.result += "D"
Helper.result += toggle ? "T" : "F"
}
.transition(.identity)
.id(toggle)
}
}

try await triggerLayoutWithWindow { continuation in
PlatformHostingController(
rootView: ContentView(
continuation: continuation
)
)
}
await #expect(Helper.result == "AFDTATDT")
}
}