Skip to content

Commit db6282e

Browse files
committed
Directly use GeometryReader in Setting
1 parent 55bb376 commit db6282e

2 files changed

Lines changed: 29 additions & 55 deletions

File tree

Sources/SettingExtensions.swift

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,3 @@ extension StringProtocol {
3535
return result
3636
}
3737
}
38-
39-
/**
40-
Read a view's size. The closure is called whenever the size itself changes.
41-
42-
From https://stackoverflow.com/a/66822461/14351818
43-
*/
44-
extension View {
45-
func readSize(size: @escaping (CGSize) -> Void) -> some View {
46-
return background(
47-
GeometryReader { geometry in
48-
Color.clear
49-
.preference(key: ContentSizeReaderPreferenceKey.self, value: geometry.size)
50-
.onPreferenceChange(ContentSizeReaderPreferenceKey.self) { newValue in
51-
DispatchQueue.main.async {
52-
size(newValue)
53-
}
54-
}
55-
}
56-
.hidden()
57-
)
58-
}
59-
}
60-
61-
struct ContentSizeReaderPreferenceKey: PreferenceKey {
62-
static var defaultValue: CGSize { return CGSize() }
63-
static func reduce(value: inout CGSize, nextValue: () -> CGSize) { value = nextValue() }
64-
}

Sources/Views/SettingStack.swift

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ public struct SettingStack: View {
4141
For handling internal state.
4242
*/
4343
@StateObject var settingViewModel = SettingViewModel()
44-
45-
/// Window size for calculating edge padding.
46-
@State var windowSize = CGSize(width: 414, height: 896)
4744

4845
/**
4946
Create a new Settings view from a `SettingPage`. The default "no results" view will be used.
@@ -84,16 +81,36 @@ public struct SettingStack: View {
8481
}
8582

8683
public var body: some View {
87-
if !embedInNavigationStack {
88-
main
89-
} else if #available(iOS 16.0, macOS 13.0, *) {
90-
NavigationStack {
91-
main
92-
}
93-
} else {
94-
NavigationView {
95-
main
84+
GeometryReader { geometry in
85+
/// Padding to line up with the navigation title.
86+
let edgePadding: Double = {
87+
/// Window size for calculating edge padding.
88+
let windowSize = geometry.size
89+
90+
/// Leading margin stays the same, whether in horizontal or vertical
91+
let narrowEdge = min(windowSize.width, windowSize.height)
92+
93+
if narrowEdge > 400 {
94+
return 20
95+
} else {
96+
return 16
97+
}
98+
}()
99+
100+
VStack {
101+
if !embedInNavigationStack {
102+
main
103+
} else if #available(iOS 16.0, macOS 13.0, *) {
104+
NavigationStack {
105+
main
106+
}
107+
} else {
108+
NavigationView {
109+
main
110+
}
111+
}
96112
}
113+
.environment(\.edgePadding, edgePadding)
97114
}
98115
}
99116

@@ -131,22 +148,6 @@ public struct SettingStack: View {
131148
let paths = settingPage.generatePaths()
132149
settingViewModel.paths = paths
133150
}
134-
.readSize { size in
135-
windowSize = size
136-
}
137-
.environment(\.edgePadding, edgePadding)
138-
}
139-
140-
/// Padding to line up with the navigation title.
141-
var edgePadding: Double {
142-
/// Leading margin stays the same, whether in horizontal or vertical
143-
let narrowEdge = min(windowSize.width, windowSize.height)
144-
145-
if narrowEdge > 400 {
146-
return 20
147-
} else {
148-
return 16
149-
}
150151
}
151152
}
152153

0 commit comments

Comments
 (0)