-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7dee82a
commit e21966d
Showing
10 changed files
with
365 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
Sources/SwiftUIExtension/LayoutGuides/Archived/LayoutGuidesOLD.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/*import SwiftUI | ||
|
||
extension View { | ||
|
||
public func fitToReadableContentWidth(alignment: Alignment = .center) -> some View { | ||
self.modifier(FitLayoutGuidesWidth(alignment: alignment, kind: .readableContent)) | ||
} | ||
|
||
public func fitToLayoutMarginsWidth(alignment: Alignment = .center) -> some View { | ||
self.modifier(FitLayoutGuidesWidth(alignment: alignment, kind: .layoutMargins)) | ||
} | ||
|
||
public func measureLayoutGuides() -> some View { | ||
self.modifier(LayoutGuidesModifier()) | ||
} | ||
} | ||
|
||
public struct WithLayoutMargins<Content>: View where Content: View { | ||
|
||
let content: (EdgeInsets) -> Content | ||
|
||
public init(@ViewBuilder content: @escaping (EdgeInsets) -> Content) { | ||
self.content = content | ||
} | ||
|
||
public init(@ViewBuilder content: @escaping () -> Content) { | ||
self.content = { _ in content() } | ||
} | ||
|
||
public var body: some View { | ||
InsetContent(content: content) | ||
.measureLayoutGuides() | ||
} | ||
|
||
private struct InsetContent: View { | ||
|
||
let content: (EdgeInsets) -> Content | ||
|
||
@Environment(\.layoutMarginsInsets) var layoutMarginsInsets | ||
|
||
var body: some View { | ||
content(layoutMarginsInsets) | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Private | ||
|
||
internal struct FitLayoutGuidesWidth: ViewModifier { | ||
|
||
enum Kind { | ||
case layoutMargins | ||
case readableContent | ||
} | ||
|
||
let alignment: Alignment | ||
let kind: Kind | ||
|
||
func body(content: Content) -> some View { | ||
switch kind { | ||
case .layoutMargins: | ||
content.modifier(InsetLayoutMargins(alignment: alignment)) | ||
.measureLayoutGuides() | ||
case .readableContent: | ||
content.modifier(InsetReadableContent(alignment: alignment)) | ||
.measureLayoutGuides() | ||
} | ||
} | ||
|
||
private struct InsetReadableContent: ViewModifier { | ||
|
||
let alignment: Alignment | ||
@Environment(\.readableContentInsets) var readableContentInsets | ||
|
||
func body(content: Content) -> some View { | ||
content | ||
.frame(maxWidth: .infinity, alignment: alignment) | ||
.padding(.leading, readableContentInsets.leading) | ||
.padding(.trailing, readableContentInsets.trailing) | ||
} | ||
} | ||
|
||
private struct InsetLayoutMargins: ViewModifier { | ||
|
||
let alignment: Alignment | ||
@Environment(\.layoutMarginsInsets) var layoutMarginsInsets | ||
|
||
func body(content: Content) -> some View { | ||
content | ||
.frame(maxWidth: .infinity, alignment: alignment) | ||
.padding(.leading, layoutMarginsInsets.leading) | ||
.padding(.trailing, layoutMarginsInsets.trailing) | ||
} | ||
} | ||
} | ||
|
||
internal struct LayoutGuidesModifier: ViewModifier { | ||
|
||
@State var layoutMarginsInsets: EdgeInsets = .init() | ||
@State var readableContentInsets: EdgeInsets = .init() | ||
|
||
func body(content: Content) -> some View { | ||
content | ||
#if os(iOS) || os(tvOS) | ||
.environment(\.layoutMarginsInsets, layoutMarginsInsets) | ||
.environment(\.readableContentInsets, readableContentInsets) | ||
.background( | ||
LayoutGuidesObserverView( | ||
onLayoutMarginsGuideChange: { | ||
layoutMarginsInsets = $0 | ||
}, | ||
onReadableContentGuideChange: { | ||
readableContentInsets = $0 | ||
}) | ||
) | ||
#endif | ||
} | ||
} | ||
*/ |
88 changes: 88 additions & 0 deletions
88
Sources/SwiftUIExtension/LayoutGuides/Archived/LayoutGuidesObserverViewOLD.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/*#if os(iOS) || os(tvOS) | ||
import UIKit | ||
import SwiftUI | ||
|
||
struct LayoutGuidesObserverView: UIViewRepresentable { | ||
|
||
let onLayoutMarginsGuideChange: (EdgeInsets) -> Void | ||
let onReadableContentGuideChange: (EdgeInsets) -> Void | ||
|
||
func makeUIView(context: Context) -> LayoutGuidesView { | ||
let uiView = LayoutGuidesView() | ||
uiView.onLayoutMarginsGuideChange = onLayoutMarginsGuideChange | ||
uiView.onReadableContentGuideChange = onReadableContentGuideChange | ||
return uiView | ||
} | ||
|
||
func updateUIView(_ uiView: LayoutGuidesView, context: Context) { | ||
uiView.onLayoutMarginsGuideChange = onLayoutMarginsGuideChange | ||
uiView.onReadableContentGuideChange = onReadableContentGuideChange | ||
} | ||
|
||
final class LayoutGuidesView: UIView { | ||
var onLayoutMarginsGuideChange: (EdgeInsets) -> Void = { _ in } | ||
var onReadableContentGuideChange: (EdgeInsets) -> Void = { _ in } | ||
|
||
override func layoutMarginsDidChange() { | ||
super.layoutMarginsDidChange() | ||
updateLayoutMargins() | ||
updateReadableContent() | ||
} | ||
|
||
override func layoutSubviews() { | ||
super.layoutSubviews() | ||
updateReadableContent() | ||
} | ||
|
||
override var frame: CGRect { | ||
didSet { | ||
self.updateReadableContent() | ||
} | ||
} | ||
|
||
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { | ||
super.traitCollectionDidChange(previousTraitCollection) | ||
if traitCollection.layoutDirection != previousTraitCollection?.layoutDirection { | ||
updateReadableContent() | ||
} | ||
} | ||
|
||
var previousLayoutMargins: EdgeInsets? = nil | ||
func updateLayoutMargins() { | ||
let edgeInsets = EdgeInsets( | ||
top: directionalLayoutMargins.top, | ||
leading: directionalLayoutMargins.leading, | ||
bottom: directionalLayoutMargins.bottom, | ||
trailing: directionalLayoutMargins.trailing | ||
) | ||
guard previousLayoutMargins != edgeInsets else { return } | ||
onLayoutMarginsGuideChange(edgeInsets) | ||
previousLayoutMargins = edgeInsets | ||
} | ||
|
||
var previousReadableContentGuide: EdgeInsets? = nil | ||
func updateReadableContent() { | ||
let isRightToLeft = traitCollection.layoutDirection == .rightToLeft | ||
let layoutFrame = readableContentGuide.layoutFrame | ||
|
||
let readableContentInsets = | ||
UIEdgeInsets( | ||
top: layoutFrame.minY - bounds.minY, | ||
left: layoutFrame.minX - bounds.minX, | ||
bottom: -(layoutFrame.maxY - bounds.maxY), | ||
right: -(layoutFrame.maxX - bounds.maxX) | ||
) | ||
let edgeInsets = EdgeInsets( | ||
top: readableContentInsets.top, | ||
leading: isRightToLeft ? readableContentInsets.right : readableContentInsets.left, | ||
bottom: readableContentInsets.bottom, | ||
trailing: isRightToLeft ? readableContentInsets.left : readableContentInsets.right | ||
) | ||
guard previousReadableContentGuide != edgeInsets else { return } | ||
onReadableContentGuideChange(edgeInsets) | ||
previousReadableContentGuide = edgeInsets | ||
} | ||
} | ||
} | ||
#endif | ||
*/ |
Oops, something went wrong.