|
| 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