Skip to content

Commit

Permalink
Upgraded layout guides.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvorobei committed Nov 29, 2024
1 parent 7dee82a commit e21966d
Show file tree
Hide file tree
Showing 10 changed files with 365 additions and 182 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
platforms: [
.iOS(.v15),
.watchOS(.v8),
.macOS(.v10_15)
.macOS(.v12)
],
products: [
.library(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extension View {
struct TransitionBlurReplace: ViewModifier {

func body(content: Content) -> some View {
if #available(iOS 18.0, watchOS 10.0, *) {
if #available(iOS 18.0, macOS 14.0, watchOS 10.0, *) {
content
.transition(.blurReplace.combined(with: .opacity))
} else {
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftUIExtension/Extensions/SafeArea.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extension View {
/**
Default value for `safeAreaInset` none zero — this wrapper drop all spaces.
*/
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
public func safeAreaInsetNoneSpaced<Content: View>(edge: VerticalEdge, @ViewBuilder content: () -> Content) -> some View {
self
.safeAreaInset(edge: edge, spacing: .zero) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftUIExtension/Extensions/View+Layout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ extension View {
modifier(HorizontalSystemPadding(paddingView: view))
}

public func readableMargins() -> some View {
/*public func readableMargins() -> some View {
self
.padding(.horizontal)
.frame(maxWidth: 414)
}
}*/
}

public struct HorizontalSystemPadding: ViewModifier {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import SwiftUI
/*import SwiftUI

private struct LayoutMarginsGuidesKey: EnvironmentKey {
static var defaultValue: EdgeInsets { .init() }
Expand All @@ -20,3 +20,4 @@ extension EnvironmentValues {
set { self[ReadableContentGuidesKey.self] = newValue }
}
}
*/
119 changes: 119 additions & 0 deletions Sources/SwiftUIExtension/LayoutGuides/Archived/LayoutGuidesOLD.swift
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
}
}
*/
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
*/
Loading

0 comments on commit e21966d

Please sign in to comment.