Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onAppear and onDisappear support #92

Merged
merged 4 commits into from
Jun 2, 2024
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
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"originHash" : "807d4a48456544b2827b8d4f69dc5e39b01103e9105f11c138529cc3159d18d4",
"originHash" : "7c61cf2fa8336371c9cc17d001d4e08e34b0f8b92c637a0c44f1b2cd08cd55b2",
"pins" : [
{
"identity" : "opengraph",
"kind" : "remoteSourceControl",
"location" : "https://github.com/OpenSwiftUIProject/OpenGraph",
"state" : {
"branch" : "main",
"revision" : "47a81fde4bfa4092577abd29122206c19ad0cf98"
"revision" : "48ad5323175fbfdfde2287bbf26c3e5a861ae2bb"
}
},
{
Expand Down
23 changes: 19 additions & 4 deletions Sources/OpenSwiftUI/Core/Update/Update.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,24 @@

@inline(__always)
static func dispatchActions() {
// FIXME
for action in actions {
action()
guard !actions.isEmpty else {
return

Check warning on line 75 in Sources/OpenSwiftUI/Core/Update/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/Core/Update/Update.swift#L74-L75

Added lines #L74 - L75 were not covered by tests
}

let actions = Update.actions
Update.actions = []
performOnMainThread {
// TODO: Signpost.postUpdateActions
begin()
for action in actions {
let oldDepth = depth
action()
let newDepth = depth
if newDepth != oldDepth {
fatalError("Action caused unbalanced updates.")

Check warning on line 88 in Sources/OpenSwiftUI/Core/Update/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/Core/Update/Update.swift#L77-L88

Added lines #L77 - L88 were not covered by tests
}
}
end()

Check warning on line 91 in Sources/OpenSwiftUI/Core/Update/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/Core/Update/Update.swift#L91

Added line #L91 was not covered by tests
}
}

Expand Down Expand Up @@ -107,5 +122,5 @@
// FIXME: migrate to use @_extern(c, "xx") in Swift 6
extension MovableLock {
@_silgen_name("_MovableLockSyncMain")
static func syncMain(lock: MovableLock ,body: @escaping () -> Void)
static func syncMain(lock: MovableLock, body: @escaping () -> Void)
}
6 changes: 6 additions & 0 deletions Sources/OpenSwiftUI/Core/View/View/ViewInputs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@
return newInputs
}

// MARK: - base.phase
@inline(__always)
var phase: Attribute<_GraphInputs.Phase> {
base.phase

Check warning on line 122 in Sources/OpenSwiftUI/Core/View/View/ViewInputs.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/Core/View/View/ViewInputs.swift#L121-L122

Added lines #L121 - L122 were not covered by tests
}

// MARK: - base.changedDebugProperties

@inline(__always)
Expand Down
159 changes: 159 additions & 0 deletions Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
//
// AppearanceActionModifier.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: Blocked by _makeViewList
// ID: 8817D3B1C81ADA2B53E3500D727F785A

// MARK: - AppearanceActionModifier

internal import OpenGraphShims

/// A modifier that triggers actions when its view appears and disappears.
@frozen
public struct _AppearanceActionModifier: PrimitiveViewModifier {
public var appear: (() -> Void)?
public var disappear: (() -> Void)?

@inlinable
public init(appear: (() -> Void)? = nil, disappear: (() -> Void)? = nil) {
self.appear = appear
self.disappear = disappear

Check warning on line 22 in Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift#L20-L22

Added lines #L20 - L22 were not covered by tests
}

public static func _makeView(
modifier: _GraphValue<Self>,
inputs: _ViewInputs,
body: @escaping (_Graph, _ViewInputs) -> _ViewOutputs
) -> _ViewOutputs {
let effect = AppearanceEffect(modifier: modifier.value, phase: inputs.phase)
let attribute = Attribute(effect)
attribute.flags = [.active, .removable]
return body(_Graph(), inputs)

Check warning on line 33 in Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift#L29-L33

Added lines #L29 - L33 were not covered by tests
}

public static func _makeViewList(
modifier: _GraphValue<Self>,
inputs: _ViewListInputs,
body: @escaping (_Graph, _ViewListInputs) -> _ViewListOutputs
) -> _ViewListOutputs {
fatalError("TODO")

Check warning on line 41 in Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift#L40-L41

Added lines #L40 - L41 were not covered by tests
}
}

// MARK: - AppearanceEffect

private struct AppearanceEffect {
@Attribute
var modifier: _AppearanceActionModifier
@Attribute
var phase: _GraphInputs.Phase
var lastValue: _AppearanceActionModifier?
var isVisible: Bool = false
var resetSeed: UInt32 = 0
var node: AnyOptionalAttribute = AnyOptionalAttribute()

Check warning on line 55 in Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift#L53-L55

Added lines #L53 - L55 were not covered by tests

mutating func appeared() {
guard !isVisible else { return }
defer { isVisible = true }
guard let lastValue,
let appear = lastValue.appear
else { return }
Update.enqueueAction(appear)

Check warning on line 63 in Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift#L57-L63

Added lines #L57 - L63 were not covered by tests
}

mutating func disappeared() {
guard isVisible else { return }
defer { isVisible = false }
guard let lastValue,
let disappear = lastValue.disappear
else { return }
Update.enqueueAction(disappear)

Check warning on line 72 in Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift#L66-L72

Added lines #L66 - L72 were not covered by tests
}
}

// MARK: AppearanceEffect + StatefulRule

extension AppearanceEffect: StatefulRule {
typealias Value = Void

mutating func updateValue() {
#if canImport(Darwin)
if node.attribute == nil {
node.attribute = .current

Check warning on line 84 in Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift#L81-L84

Added lines #L81 - L84 were not covered by tests
}

if phase.seed != resetSeed {
resetSeed = phase.seed
disappeared()

Check warning on line 89 in Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift#L86-L89

Added lines #L86 - L89 were not covered by tests
}
lastValue = modifier
appeared()
#else
fatalError("See #39")
#endif

Check warning on line 95 in Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift#L91-L95

Added lines #L91 - L95 were not covered by tests
}
}

#if canImport(Darwin) // See #39

// MARK: AppearanceEffect + RemovableAttribute

extension AppearanceEffect: RemovableAttribute {
static func willRemove(attribute: OGAttribute) {
let appearancePointer = UnsafeMutableRawPointer(mutating: attribute.info.body)
.assumingMemoryBound(to: AppearanceEffect.self)
guard appearancePointer.pointee.lastValue != nil else {
return

Check warning on line 108 in Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift#L104-L108

Added lines #L104 - L108 were not covered by tests
}
appearancePointer.pointee.disappeared()

Check warning on line 110 in Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift#L110

Added line #L110 was not covered by tests
}

static func didReinsert(attribute: OGAttribute) {
let appearancePointer = UnsafeMutableRawPointer(mutating: attribute.info.body)
.assumingMemoryBound(to: AppearanceEffect.self)
guard let nodeAttribute = appearancePointer.pointee.node.attribute else {
return

Check warning on line 117 in Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift#L113-L117

Added lines #L113 - L117 were not covered by tests
}
nodeAttribute.invalidateValue()
nodeAttribute.graph.graphHost().graphInvalidation(from: nil)

Check warning on line 120 in Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift#L119-L120

Added lines #L119 - L120 were not covered by tests
}
}
#endif

// MARK: - View Extension

extension View {
/// Adds an action to perform before this view appears.
///
/// The exact moment that OpenSwiftUI calls this method
/// depends on the specific view type that you apply it to, but
/// the `action` closure completes before the first
/// rendered frame appears.
///
/// - Parameter action: The action to perform. If `action` is `nil`, the
/// call has no effect.
///
/// - Returns: A view that triggers `action` before it appears.
@inlinable
public func onAppear(perform action: (() -> Void)? = nil) -> some View {
modifier(_AppearanceActionModifier(appear: action, disappear: nil))

Check warning on line 141 in Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift#L140-L141

Added lines #L140 - L141 were not covered by tests
}

/// Adds an action to perform after this view disappears.
///
/// The exact moment that OpenSwiftUI calls this method
/// depends on the specific view type that you apply it to, but
/// the `action` closure doesn't execute until the view
/// disappears from the interface.
///
/// - Parameter action: The action to perform. If `action` is `nil`, the
/// call has no effect.
///
/// - Returns: A view that triggers `action` after it disappears.
@inlinable
public func onDisappear(perform action: (() -> Void)? = nil) -> some View {
modifier(_AppearanceActionModifier(appear: nil, disappear: action))

Check warning on line 157 in Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift#L156-L157

Added lines #L156 - L157 were not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// AppearanceActionModifierTests.swift
// OpenSwiftUICompatibilityTests

import Testing

#if canImport(Darwin)
struct AppearanceActionModifierTests {
@Test
func appear() async throws {
struct ContentView: View {
var confirmation: Confirmation

var body: some View {
AnyView(EmptyView())
.onAppear {
confirmation()
}
}
}

#if os(iOS)
await confirmation { @MainActor confirmation in
let vc = UIHostingController(rootView: ContentView(confirmation: confirmation))
vc.triggerLayout()
workaroundIssue87(vc)
}
#endif
}

// TODO: Add disappear support and test case
}
#endif
Loading