Skip to content

Commit

Permalink
Patch
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Jul 20, 2024
1 parent 3ca1e70 commit 9f7f30a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 40 deletions.
71 changes: 71 additions & 0 deletions Sources/SwiftUISupportSizing/View+MeasureSize.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import SwiftUI

private struct SizingPreferenceKey: PreferenceKey {

typealias Value = CGSize

static var defaultValue: Value = .zero

static func reduce(value: inout Value, nextValue: () -> Value) {
let next = nextValue()
value = next
}

}

extension View {

/**
Measures the receiver view size using GeometryReader.

!! Should be deprecated in favor of using directly ``readingGeometry(transform:target:)``
*/
public func measureSize(_ size: Binding<CGSize>) -> some View {
readingGeometry(transform: \.size, target: size)
}

}

private enum GeometryReaderPreferenceKey<Projected: Equatable>: PreferenceKey {

static var defaultValue: Projected? {
return nil
}

static func reduce(value: inout Value, nextValue: () -> Projected?) {
let next = nextValue()
value = next
}

}

extension View {

public consuming func readingGeometry<Projected: Equatable>(
transform: @escaping (GeometryProxy) -> Projected,
target: Binding<Projected>
) -> some View {
readingGeometry(transform: transform, onChange: {
target.wrappedValue = $0
})
}

public consuming func readingGeometry<Projected: Equatable>(
transform: @escaping (GeometryProxy) -> Projected,
onChange: @escaping (Projected) -> Void
) -> some View {
background(
Color.clear.background(
GeometryReader(content: { proxy in
Color.clear
.preference(key: GeometryReaderPreferenceKey<Projected>.self, value: transform(proxy))
})
)
.onPreferenceChange(GeometryReaderPreferenceKey<Projected>.self) { projected in
guard let projected else { return }
onChange(projected)
}
)
}

}
40 changes: 0 additions & 40 deletions Sources/SwiftUISupportSizing/View+Sizing.swift

This file was deleted.

0 comments on commit 9f7f30a

Please sign in to comment.