From 9f7f30aae04c9ed4eea61d0d57f55f04f02c611d Mon Sep 17 00:00:00 2001 From: Muukii Date: Sat, 20 Jul 2024 15:32:26 +0700 Subject: [PATCH] Patch --- .../View+MeasureSize.swift | 71 +++++++++++++++++++ .../SwiftUISupportSizing/View+Sizing.swift | 40 ----------- 2 files changed, 71 insertions(+), 40 deletions(-) create mode 100644 Sources/SwiftUISupportSizing/View+MeasureSize.swift delete mode 100644 Sources/SwiftUISupportSizing/View+Sizing.swift diff --git a/Sources/SwiftUISupportSizing/View+MeasureSize.swift b/Sources/SwiftUISupportSizing/View+MeasureSize.swift new file mode 100644 index 0000000000..4a18289cfe --- /dev/null +++ b/Sources/SwiftUISupportSizing/View+MeasureSize.swift @@ -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) -> some View { + readingGeometry(transform: \.size, target: size) + } + +} + +private enum GeometryReaderPreferenceKey: 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( + transform: @escaping (GeometryProxy) -> Projected, + target: Binding + ) -> some View { + readingGeometry(transform: transform, onChange: { + target.wrappedValue = $0 + }) + } + + public consuming func readingGeometry( + transform: @escaping (GeometryProxy) -> Projected, + onChange: @escaping (Projected) -> Void + ) -> some View { + background( + Color.clear.background( + GeometryReader(content: { proxy in + Color.clear + .preference(key: GeometryReaderPreferenceKey.self, value: transform(proxy)) + }) + ) + .onPreferenceChange(GeometryReaderPreferenceKey.self) { projected in + guard let projected else { return } + onChange(projected) + } + ) + } + +} diff --git a/Sources/SwiftUISupportSizing/View+Sizing.swift b/Sources/SwiftUISupportSizing/View+Sizing.swift deleted file mode 100644 index c950a24666..0000000000 --- a/Sources/SwiftUISupportSizing/View+Sizing.swift +++ /dev/null @@ -1,40 +0,0 @@ -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. - */ - public func measureSize(_ size: Binding) -> some View { - background(Color.clear._measureSize(size)) - } - - private func _measureSize(_ size: Binding) -> some View { - - self.background( - GeometryReader(content: { proxy in - Color.clear - .preference(key: SizingPreferenceKey.self, value: proxy.size) - }) - ) - .onPreferenceChange(SizingPreferenceKey.self) { _size in - size.wrappedValue = _size - } - - } - -} -