Skip to content

Commit ffb72d4

Browse files
committed
Update geometry extensions
1 parent da359c9 commit ffb72d4

File tree

3 files changed

+43
-78
lines changed

3 files changed

+43
-78
lines changed

Release Notes.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
# Release notes
22

33

4-
## 0.4.0
4+
## 0.4.1
55

6-
This release adds a new geometry `View` extension.
6+
This release changes the `View` geometry extensions and replaces `bindGeometry(to: ...)` with:
77

8-
`bindGeometry(to: ...)` lets you bind any `CGFloat` geometry value for a view. It injects a `GeometryReader` and provides it back to the extension's value resolve block.
8+
* `bindSafeAreaInsets(to: ...)`
9+
* `bindSize(to: ...)`
910

10-
You can use it like this:
11+
You can use them like this:
1112

1213
```swift
13-
@State private var height: CGFloat = 0
14+
@State private var size: CGSize = .zero
15+
@State private var size: CGSize = .zero
1416
UIColor.red.bindGeometry(to: $height) { $0.size.height }
1517
```
1618

17-
To harmonize with this new extension, the `readSafeAreaInsets` extension has been renamed to `bindSafeAreaInset(of:to:)`.
1819

19-
You can use it like this:
20+
## 0.4.0
2021

21-
```swift
22-
@State private var topInset: CGFloat = 0
23-
UIColor.red.bindSafeAreaInset(of: .top, to: $topInset)
24-
```
22+
This release adds a new geometry `View` extension:
23+
24+
* `bindGeometry(to: ...)` lets you bind any `CGFloat` geometry value for a view.
25+
26+
The extension injects a `GeometryReader` and provides values to the provided binding.
2527

2628

2729
## 0.3.0

Sources/SwiftUIKit/Extensions/View+Geometry.swift

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,45 @@ import SwiftUI
1111
public extension View {
1212

1313
/**
14-
Bind any `CGFloat` value within a `GeometryProxy` value
15-
to an external binding.
14+
Bind the view's safe area to a binding.
1615
*/
17-
func bindGeometry(
18-
to binding: Binding<CGFloat>,
19-
reader: @escaping (GeometryProxy) -> CGFloat) -> some View {
20-
self.background(GeometryBinding(reader: reader))
21-
.onPreferenceChange(GeometryPreference.self) {
22-
binding.wrappedValue = $0
23-
}
16+
func bindSafeAreaInsets(to binding: Binding<EdgeInsets>) -> some View {
17+
background(safeAreaBindingView(for: binding))
18+
}
19+
20+
/**
21+
Bind the view's size to a binding.
22+
*/
23+
func bindSize(to binding: Binding<CGSize>) -> some View {
24+
background(sizeBindingView(for: binding))
2425
}
2526
}
2627

27-
private struct GeometryBinding: View {
28+
private extension View {
2829

29-
let reader: (GeometryProxy) -> CGFloat
30+
func changeStateAsync(_ action: @escaping () -> Void) {
31+
DispatchQueue.main.async(execute: action)
32+
}
3033

31-
var body: some View {
34+
func safeAreaBindingView(for binding: Binding<EdgeInsets>) -> some View {
3235
GeometryReader { geo in
33-
Color.clear.preference(
34-
key: GeometryPreference.self,
35-
value: self.reader(geo)
36-
)
36+
self.safeAreaBindingView(for: binding, geo: geo)
3737
}
3838
}
39-
}
40-
41-
private struct GeometryPreference: PreferenceKey {
4239

43-
typealias Value = CGFloat
44-
45-
static var defaultValue: CGFloat = 0
46-
47-
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
48-
value = max(value, nextValue())
40+
func safeAreaBindingView(for binding: Binding<EdgeInsets>, geo: GeometryProxy) -> some View {
41+
changeStateAsync { binding.wrappedValue = geo.safeAreaInsets }
42+
return Color.clear
43+
}
44+
45+
func sizeBindingView(for binding: Binding<CGSize>) -> some View {
46+
GeometryReader { geo in
47+
self.sizeBindingView(for: binding, geo: geo)
48+
}
49+
}
50+
51+
func sizeBindingView(for binding: Binding<CGSize>, geo: GeometryProxy) -> some View {
52+
changeStateAsync { binding.wrappedValue = geo.size }
53+
return Color.clear
4954
}
5055
}

Sources/SwiftUIKit/Extensions/View+SafeArea.swift

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)