Skip to content

Commit

Permalink
Directly use GeometryReader in Setting
Browse files Browse the repository at this point in the history
  • Loading branch information
aheze committed May 13, 2023
1 parent 55bb376 commit db6282e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 55 deletions.
27 changes: 0 additions & 27 deletions Sources/SettingExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,3 @@ extension StringProtocol {
return result
}
}

/**
Read a view's size. The closure is called whenever the size itself changes.
From https://stackoverflow.com/a/66822461/14351818
*/
extension View {
func readSize(size: @escaping (CGSize) -> Void) -> some View {
return background(
GeometryReader { geometry in
Color.clear
.preference(key: ContentSizeReaderPreferenceKey.self, value: geometry.size)
.onPreferenceChange(ContentSizeReaderPreferenceKey.self) { newValue in
DispatchQueue.main.async {
size(newValue)
}
}
}
.hidden()
)
}
}

struct ContentSizeReaderPreferenceKey: PreferenceKey {
static var defaultValue: CGSize { return CGSize() }
static func reduce(value: inout CGSize, nextValue: () -> CGSize) { value = nextValue() }
}
57 changes: 29 additions & 28 deletions Sources/Views/SettingStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ public struct SettingStack: View {
For handling internal state.
*/
@StateObject var settingViewModel = SettingViewModel()

/// Window size for calculating edge padding.
@State var windowSize = CGSize(width: 414, height: 896)

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

public var body: some View {
if !embedInNavigationStack {
main
} else if #available(iOS 16.0, macOS 13.0, *) {
NavigationStack {
main
}
} else {
NavigationView {
main
GeometryReader { geometry in
/// Padding to line up with the navigation title.
let edgePadding: Double = {
/// Window size for calculating edge padding.
let windowSize = geometry.size

/// Leading margin stays the same, whether in horizontal or vertical
let narrowEdge = min(windowSize.width, windowSize.height)

if narrowEdge > 400 {
return 20
} else {
return 16
}
}()

VStack {
if !embedInNavigationStack {
main
} else if #available(iOS 16.0, macOS 13.0, *) {
NavigationStack {
main
}
} else {
NavigationView {
main
}
}
}
.environment(\.edgePadding, edgePadding)
}
}

Expand Down Expand Up @@ -131,22 +148,6 @@ public struct SettingStack: View {
let paths = settingPage.generatePaths()
settingViewModel.paths = paths
}
.readSize { size in
windowSize = size
}
.environment(\.edgePadding, edgePadding)
}

/// Padding to line up with the navigation title.
var edgePadding: Double {
/// Leading margin stays the same, whether in horizontal or vertical
let narrowEdge = min(windowSize.width, windowSize.height)

if narrowEdge > 400 {
return 20
} else {
return 16
}
}
}

Expand Down

0 comments on commit db6282e

Please sign in to comment.