|
8 | 8 | package import Foundation
|
9 | 9 | package import OpenGraphShims
|
10 | 10 |
|
| 11 | +/// A type that represents the position and dimensions of a view in its parent's coordinate space. |
| 12 | +/// |
| 13 | +/// `ViewGeometry` encapsulates both the origin (position) and dimensions (size and alignment guides) |
| 14 | +/// of a view. It provides methods to access alignment guides and convert to other geometric types. |
11 | 15 | package struct ViewGeometry: Equatable {
|
| 16 | + /// The position of the view in its parent's coordinate space. |
12 | 17 | package var origin: ViewOrigin
|
| 18 | + |
| 19 | + /// The size and alignment information of the view. |
13 | 20 | package var dimensions: ViewDimensions
|
14 |
| - |
| 21 | + |
| 22 | + /// Creates a view geometry with the specified origin and dimensions. |
| 23 | + /// |
| 24 | + /// - Parameters: |
| 25 | + /// - origin: The position of the view. |
| 26 | + /// - dimensions: The size and alignment information of the view. |
15 | 27 | package init(origin: ViewOrigin, dimensions: ViewDimensions) {
|
16 | 28 | self.origin = origin
|
17 | 29 | self.dimensions = dimensions
|
18 | 30 | }
|
19 |
| - |
| 31 | + |
| 32 | + /// Creates a view geometry at the default origin with the specified dimensions. |
| 33 | + /// |
| 34 | + /// - Parameter dimensions: The size and alignment information of the view. |
20 | 35 | package init(dimensions: ViewDimensions) {
|
21 | 36 | self.init(origin: ViewOrigin(), dimensions: dimensions)
|
22 | 37 | }
|
23 |
| - |
| 38 | + |
| 39 | + /// Creates a view geometry with the specified CGPoint origin and dimensions. |
| 40 | + /// |
| 41 | + /// - Parameters: |
| 42 | + /// - origin: The position of the view as a CGPoint. |
| 43 | + /// - dimensions: The size and alignment information of the view. |
24 | 44 | package init(origin: CGPoint, dimensions: ViewDimensions) {
|
25 | 45 | self.init(origin: ViewOrigin(origin), dimensions: dimensions)
|
26 | 46 | }
|
27 |
| - |
| 47 | + |
| 48 | + /// Creates a view geometry using placement and dimensions information. |
| 49 | + /// |
| 50 | + /// - Parameters: |
| 51 | + /// - p: The placement that determines the origin of the view. |
| 52 | + /// - d: The size and alignment information of the view. |
28 | 53 | package init(placement p: _Placement, dimensions d: ViewDimensions) {
|
29 | 54 | self.origin = ViewOrigin(p.frameOrigin(childSize: d.size.value))
|
30 | 55 | self.dimensions = d
|
31 | 56 | }
|
32 |
| - |
| 57 | + |
| 58 | + /// Returns the position of the specified horizontal alignment guide. |
| 59 | + /// |
| 60 | + /// - Parameter guide: The horizontal alignment guide to measure. |
| 61 | + /// - Returns: The x-coordinate of the alignment guide in the parent's coordinate space. |
33 | 62 | package subscript(guide: HorizontalAlignment) -> CGFloat { dimensions[guide] }
|
| 63 | + |
| 64 | + /// Returns the position of the specified vertical alignment guide. |
| 65 | + /// |
| 66 | + /// - Parameter guide: The vertical alignment guide to measure. |
| 67 | + /// - Returns: The y-coordinate of the alignment guide in the parent's coordinate space. |
34 | 68 | package subscript(guide: VerticalAlignment) -> CGFloat { dimensions[guide] }
|
| 69 | + |
| 70 | + /// Returns the explicit position of the specified horizontal alignment guide if available. |
| 71 | + /// |
| 72 | + /// - Parameter guide: The horizontal alignment guide to measure. |
| 73 | + /// - Returns: The x-coordinate of the alignment guide, or nil if not explicitly defined. |
35 | 74 | package subscript(explicit guide: HorizontalAlignment) -> CGFloat? { dimensions[explicit: guide] }
|
| 75 | + |
| 76 | + /// Returns the explicit position of the specified vertical alignment guide if available. |
| 77 | + /// |
| 78 | + /// - Parameter guide: The vertical alignment guide to measure. |
| 79 | + /// - Returns: The y-coordinate of the alignment guide, or nil if not explicitly defined. |
36 | 80 | package subscript(explicit guide: VerticalAlignment) -> CGFloat? { dimensions[explicit: guide] }
|
37 | 81 | }
|
38 | 82 |
|
| 83 | +/// Extension to provide convenient accessors for `Attribute<ViewGeometry>`. |
39 | 84 | extension Attribute where Value == ViewGeometry {
|
| 85 | + /// Returns an attribute representing the origin of the view geometry. |
| 86 | + /// |
| 87 | + /// - Returns: An attribute containing the view's origin. |
40 | 88 | package func origin() -> Attribute<ViewOrigin> { self[keyPath: \.origin] }
|
| 89 | + |
| 90 | + /// Returns an attribute representing the size of the view geometry. |
| 91 | + /// |
| 92 | + /// - Returns: An attribute containing the view's size. |
41 | 93 | package func size() -> Attribute<ViewSize> { self[keyPath: \.dimensions.size] }
|
42 | 94 | }
|
43 | 95 |
|
44 | 96 | extension ViewGeometry {
|
| 97 | + /// The frame of the view as a CGRect. |
| 98 | + /// |
| 99 | + /// This property combines the origin and size information into a CGRect. |
45 | 100 | package var frame: CGRect {
|
46 | 101 | CGRect(origin: origin.value, size: dimensions.size.value)
|
47 | 102 | }
|
48 |
| - |
| 103 | + |
| 104 | + /// A view geometry value representing an invalid state. |
| 105 | + /// |
| 106 | + /// This value is used to indicate errors or uninitialized states. |
49 | 107 | package static let invalidValue = ViewGeometry(origin: ViewOrigin(invalid: ()), dimensions: .invalidValue)
|
50 |
| - |
| 108 | + |
| 109 | + /// Indicates whether this view geometry is in an invalid state. |
51 | 110 | package var isInvalid: Bool { origin.x.isNaN }
|
52 |
| - |
| 111 | + |
| 112 | + /// A view geometry with zero origin and zero dimensions. |
| 113 | + /// |
| 114 | + /// This property provides a convenient way to create a view geometry at the origin |
| 115 | + /// with zero size. |
53 | 116 | package static let zero = ViewGeometry(origin: CGPoint.zero, dimensions: .zero)
|
54 |
| - |
| 117 | + |
| 118 | + /// Returns the position of the specified alignment key. |
| 119 | + /// |
| 120 | + /// - Parameter key: The alignment key to measure. |
| 121 | + /// - Returns: The coordinate of the alignment guide in the parent's coordinate space. |
55 | 122 | package subscript(key: AlignmentKey) -> CGFloat { dimensions[key] }
|
56 |
| - |
| 123 | + |
| 124 | + /// Returns the explicit position of the specified alignment key if available. |
| 125 | + /// |
| 126 | + /// - Parameter key: The alignment key to measure. |
| 127 | + /// - Returns: The coordinate of the alignment guide, or nil if not explicitly defined. |
57 | 128 | package subscript(explicit key: AlignmentKey) -> CGFloat? { dimensions[explicit: key] }
|
58 |
| - |
| 129 | + |
| 130 | + /// Adjusts the origin to account for right-to-left layout direction. |
| 131 | + /// |
| 132 | + /// This method flips the x-coordinate when in right-to-left layout mode to maintain |
| 133 | + /// proper visual alignment from the right edge of the parent. |
| 134 | + /// |
| 135 | + /// - Parameters: |
| 136 | + /// - layoutDirection: The layout direction to apply. |
| 137 | + /// - parentSize: The size of the parent container. |
59 | 138 | package mutating func finalizeLayoutDirection(_ layoutDirection: LayoutDirection, parentSize: CGSize) {
|
60 | 139 | guard layoutDirection == .rightToLeft else { return }
|
61 | 140 | origin.x = parentSize.width - frame.maxX
|
|
0 commit comments