Skip to content

Commit e21966d

Browse files
committed
Upgraded layout guides.
1 parent 7dee82a commit e21966d

10 files changed

+365
-182
lines changed

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let package = Package(
77
platforms: [
88
.iOS(.v15),
99
.watchOS(.v8),
10-
.macOS(.v10_15)
10+
.macOS(.v12)
1111
],
1212
products: [
1313
.library(

Sources/SwiftUIExtension/Compability/TransitionBlurReplace.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extension View {
1010
struct TransitionBlurReplace: ViewModifier {
1111

1212
func body(content: Content) -> some View {
13-
if #available(iOS 18.0, watchOS 10.0, *) {
13+
if #available(iOS 18.0, macOS 14.0, watchOS 10.0, *) {
1414
content
1515
.transition(.blurReplace.combined(with: .opacity))
1616
} else {

Sources/SwiftUIExtension/Extensions/SafeArea.swift

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extension View {
55
/**
66
Default value for `safeAreaInset` none zero — this wrapper drop all spaces.
77
*/
8+
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
89
public func safeAreaInsetNoneSpaced<Content: View>(edge: VerticalEdge, @ViewBuilder content: () -> Content) -> some View {
910
self
1011
.safeAreaInset(edge: edge, spacing: .zero) {

Sources/SwiftUIExtension/Extensions/View+Layout.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ extension View {
66
modifier(HorizontalSystemPadding(paddingView: view))
77
}
88

9-
public func readableMargins() -> some View {
9+
/*public func readableMargins() -> some View {
1010
self
1111
.padding(.horizontal)
1212
.frame(maxWidth: 414)
13-
}
13+
}*/
1414
}
1515

1616
public struct HorizontalSystemPadding: ViewModifier {

Sources/SwiftUIExtension/LayoutGuides/LayoutGuidesEnvironment.swift renamed to Sources/SwiftUIExtension/LayoutGuides/Archived/LayoutGuidesEnvironmentOLD.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import SwiftUI
1+
/*import SwiftUI
22

33
private struct LayoutMarginsGuidesKey: EnvironmentKey {
44
static var defaultValue: EdgeInsets { .init() }
@@ -20,3 +20,4 @@ extension EnvironmentValues {
2020
set { self[ReadableContentGuidesKey.self] = newValue }
2121
}
2222
}
23+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*import SwiftUI
2+
3+
extension View {
4+
5+
public func fitToReadableContentWidth(alignment: Alignment = .center) -> some View {
6+
self.modifier(FitLayoutGuidesWidth(alignment: alignment, kind: .readableContent))
7+
}
8+
9+
public func fitToLayoutMarginsWidth(alignment: Alignment = .center) -> some View {
10+
self.modifier(FitLayoutGuidesWidth(alignment: alignment, kind: .layoutMargins))
11+
}
12+
13+
public func measureLayoutGuides() -> some View {
14+
self.modifier(LayoutGuidesModifier())
15+
}
16+
}
17+
18+
public struct WithLayoutMargins<Content>: View where Content: View {
19+
20+
let content: (EdgeInsets) -> Content
21+
22+
public init(@ViewBuilder content: @escaping (EdgeInsets) -> Content) {
23+
self.content = content
24+
}
25+
26+
public init(@ViewBuilder content: @escaping () -> Content) {
27+
self.content = { _ in content() }
28+
}
29+
30+
public var body: some View {
31+
InsetContent(content: content)
32+
.measureLayoutGuides()
33+
}
34+
35+
private struct InsetContent: View {
36+
37+
let content: (EdgeInsets) -> Content
38+
39+
@Environment(\.layoutMarginsInsets) var layoutMarginsInsets
40+
41+
var body: some View {
42+
content(layoutMarginsInsets)
43+
}
44+
}
45+
}
46+
47+
// MARK: - Private
48+
49+
internal struct FitLayoutGuidesWidth: ViewModifier {
50+
51+
enum Kind {
52+
case layoutMargins
53+
case readableContent
54+
}
55+
56+
let alignment: Alignment
57+
let kind: Kind
58+
59+
func body(content: Content) -> some View {
60+
switch kind {
61+
case .layoutMargins:
62+
content.modifier(InsetLayoutMargins(alignment: alignment))
63+
.measureLayoutGuides()
64+
case .readableContent:
65+
content.modifier(InsetReadableContent(alignment: alignment))
66+
.measureLayoutGuides()
67+
}
68+
}
69+
70+
private struct InsetReadableContent: ViewModifier {
71+
72+
let alignment: Alignment
73+
@Environment(\.readableContentInsets) var readableContentInsets
74+
75+
func body(content: Content) -> some View {
76+
content
77+
.frame(maxWidth: .infinity, alignment: alignment)
78+
.padding(.leading, readableContentInsets.leading)
79+
.padding(.trailing, readableContentInsets.trailing)
80+
}
81+
}
82+
83+
private struct InsetLayoutMargins: ViewModifier {
84+
85+
let alignment: Alignment
86+
@Environment(\.layoutMarginsInsets) var layoutMarginsInsets
87+
88+
func body(content: Content) -> some View {
89+
content
90+
.frame(maxWidth: .infinity, alignment: alignment)
91+
.padding(.leading, layoutMarginsInsets.leading)
92+
.padding(.trailing, layoutMarginsInsets.trailing)
93+
}
94+
}
95+
}
96+
97+
internal struct LayoutGuidesModifier: ViewModifier {
98+
99+
@State var layoutMarginsInsets: EdgeInsets = .init()
100+
@State var readableContentInsets: EdgeInsets = .init()
101+
102+
func body(content: Content) -> some View {
103+
content
104+
#if os(iOS) || os(tvOS)
105+
.environment(\.layoutMarginsInsets, layoutMarginsInsets)
106+
.environment(\.readableContentInsets, readableContentInsets)
107+
.background(
108+
LayoutGuidesObserverView(
109+
onLayoutMarginsGuideChange: {
110+
layoutMarginsInsets = $0
111+
},
112+
onReadableContentGuideChange: {
113+
readableContentInsets = $0
114+
})
115+
)
116+
#endif
117+
}
118+
}
119+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*#if os(iOS) || os(tvOS)
2+
import UIKit
3+
import SwiftUI
4+
5+
struct LayoutGuidesObserverView: UIViewRepresentable {
6+
7+
let onLayoutMarginsGuideChange: (EdgeInsets) -> Void
8+
let onReadableContentGuideChange: (EdgeInsets) -> Void
9+
10+
func makeUIView(context: Context) -> LayoutGuidesView {
11+
let uiView = LayoutGuidesView()
12+
uiView.onLayoutMarginsGuideChange = onLayoutMarginsGuideChange
13+
uiView.onReadableContentGuideChange = onReadableContentGuideChange
14+
return uiView
15+
}
16+
17+
func updateUIView(_ uiView: LayoutGuidesView, context: Context) {
18+
uiView.onLayoutMarginsGuideChange = onLayoutMarginsGuideChange
19+
uiView.onReadableContentGuideChange = onReadableContentGuideChange
20+
}
21+
22+
final class LayoutGuidesView: UIView {
23+
var onLayoutMarginsGuideChange: (EdgeInsets) -> Void = { _ in }
24+
var onReadableContentGuideChange: (EdgeInsets) -> Void = { _ in }
25+
26+
override func layoutMarginsDidChange() {
27+
super.layoutMarginsDidChange()
28+
updateLayoutMargins()
29+
updateReadableContent()
30+
}
31+
32+
override func layoutSubviews() {
33+
super.layoutSubviews()
34+
updateReadableContent()
35+
}
36+
37+
override var frame: CGRect {
38+
didSet {
39+
self.updateReadableContent()
40+
}
41+
}
42+
43+
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
44+
super.traitCollectionDidChange(previousTraitCollection)
45+
if traitCollection.layoutDirection != previousTraitCollection?.layoutDirection {
46+
updateReadableContent()
47+
}
48+
}
49+
50+
var previousLayoutMargins: EdgeInsets? = nil
51+
func updateLayoutMargins() {
52+
let edgeInsets = EdgeInsets(
53+
top: directionalLayoutMargins.top,
54+
leading: directionalLayoutMargins.leading,
55+
bottom: directionalLayoutMargins.bottom,
56+
trailing: directionalLayoutMargins.trailing
57+
)
58+
guard previousLayoutMargins != edgeInsets else { return }
59+
onLayoutMarginsGuideChange(edgeInsets)
60+
previousLayoutMargins = edgeInsets
61+
}
62+
63+
var previousReadableContentGuide: EdgeInsets? = nil
64+
func updateReadableContent() {
65+
let isRightToLeft = traitCollection.layoutDirection == .rightToLeft
66+
let layoutFrame = readableContentGuide.layoutFrame
67+
68+
let readableContentInsets =
69+
UIEdgeInsets(
70+
top: layoutFrame.minY - bounds.minY,
71+
left: layoutFrame.minX - bounds.minX,
72+
bottom: -(layoutFrame.maxY - bounds.maxY),
73+
right: -(layoutFrame.maxX - bounds.maxX)
74+
)
75+
let edgeInsets = EdgeInsets(
76+
top: readableContentInsets.top,
77+
leading: isRightToLeft ? readableContentInsets.right : readableContentInsets.left,
78+
bottom: readableContentInsets.bottom,
79+
trailing: isRightToLeft ? readableContentInsets.left : readableContentInsets.right
80+
)
81+
guard previousReadableContentGuide != edgeInsets else { return }
82+
onReadableContentGuideChange(edgeInsets)
83+
previousReadableContentGuide = edgeInsets
84+
}
85+
}
86+
}
87+
#endif
88+
*/

0 commit comments

Comments
 (0)