|
1 |
| -import Foundation |
2 | 1 | import SwiftUI
|
3 | 2 |
|
4 | 3 | /// [DE] A configuration class working for `DefinedView` protocol system.
|
| 4 | +/// |
| 5 | +/// Fields of this struct should be able to access from outside (for development purpose), |
| 6 | +/// but should NOT be able to modify from outside! |
| 7 | +/// So we make them all `private(set)` for modifier and `public` for accessor. |
5 | 8 | public struct DefinedViewConfiguration {
|
6 | 9 | /// A boolean representing if the `width` and `height` are activated for defining the internal frame size.
|
7 | 10 | ///
|
8 |
| - /// - Important: This variable should not be changed! |
9 |
| - var inactive: Bool |
| 11 | + /// - Important: This variable should NOT be changed! |
| 12 | + private(set) public var inactive: Bool |
10 | 13 |
|
| 14 | + /// The frame width of the view. |
11 | 15 | ///
|
12 |
| - var width: CGFloat |
| 16 | + /// When it is `.infinity`, we will automatically push left and right boundaries to the limit. |
| 17 | + /// You can manually set the width by using `.infinity`, |
| 18 | + /// or you can use `.full` in setting the frame to avoid buggy. |
| 19 | + private(set) public var width: CGFloat |
13 | 20 |
|
| 21 | + /// The frame height of the view. |
14 | 22 | ///
|
15 |
| - var height: CGFloat |
| 23 | + /// When it is `.infinity`, we will automatically push top and bottom boundaries to the limit. |
| 24 | + /// You can manually set the width by using `.infinity`, |
| 25 | + /// or you can use `.full` in setting the frame to avoid buggy. |
| 26 | + private(set) public var height: CGFloat |
| 27 | + |
| 28 | + /// Width constrain flag. Is this view constrained by the **width**? |
| 29 | + /// |
| 30 | + /// True for constrain. False for not. |
| 31 | + /// |
| 32 | + /// In other words, if it is not constrained, then we will not render the frame by the width value. |
| 33 | + private(set) public var isWidthConstrained: Bool |
16 | 34 |
|
| 35 | + /// Height constrain flag. Is this view constrained by the **height**? |
17 | 36 | ///
|
18 |
| - var isWidthConstrained: Bool |
| 37 | + /// True for constrain. False for not. |
| 38 | + /// |
| 39 | + /// In other words, if it is not constrained, then we will not render the frame by the height value. |
| 40 | + private(set) public var isHeightConstrained: Bool |
19 | 41 |
|
| 42 | + /// Width setup flag. Is this view constrained by a post-setting **width**? |
| 43 | + /// |
| 44 | + /// True for manually setting. False for default setting. |
20 | 45 | ///
|
21 |
| - var isHeightConstrained: Bool |
| 46 | + /// In other words, check if we need to apply another frame set on this view. |
| 47 | + /// If it has already been set once, we do NOT need to, then try a wrap. |
| 48 | + private(set) public var isWidthSet: Bool |
22 | 49 |
|
| 50 | + /// Height setup flag. Is this view constrained by a post-setting **height**? |
23 | 51 | ///
|
24 |
| - public init() { |
25 |
| - self.inactive = false |
| 52 | + /// True for manually setting. False for default setting. |
| 53 | + /// |
| 54 | + /// In other words, check if we need to apply another frame set on this view. |
| 55 | + /// If it has already been set once, we do NOT need to, then try a wrap. |
| 56 | + private(set) public var isHeightSet: Bool |
| 57 | + |
| 58 | + /// The frame alignment of the view. |
| 59 | + /// |
| 60 | + /// It treats as the same as the default `alignment`, |
| 61 | + /// but we are actually able to manually setup the usage of this value in a view component. |
| 62 | + /// |
| 63 | + /// - Note: The developer can manually use this value instead of using it in the default way. |
| 64 | + private(set) public var alignment: Alignment |
| 65 | + |
| 66 | + /// Alignment setup flag. Is this view constrained by a post-setting **alignment**? |
| 67 | + /// |
| 68 | + /// True for manually setting. False for default setting. |
| 69 | + private(set) public var isAlignmentSet: Bool |
| 70 | + |
| 71 | + /// [DE Inactive] Create a ViewConfiguration that does NOT apply onto the view. |
| 72 | + /// |
| 73 | + /// - Note: In this case, the root view should NOT use these values anymore! |
| 74 | + public init(inactive: Bool) { |
| 75 | + self.inactive = inactive |
26 | 76 | self.width = 0
|
27 | 77 | self.isWidthConstrained = false
|
| 78 | + self.isWidthSet = false |
28 | 79 | self.height = 0
|
29 | 80 | self.isHeightConstrained = false
|
| 81 | + self.isHeightSet = false |
| 82 | + self.alignment = .center |
| 83 | + self.isAlignmentSet = false |
30 | 84 | }
|
31 | 85 |
|
| 86 | + /// [DE] Create a ViewConfiguration with by default no frame constrained. |
32 | 87 | ///
|
33 |
| - public init(inactive: Bool) { |
34 |
| - self.inactive = inactive |
| 88 | + /// - Parameters: |
| 89 | + /// - alignment: The default alignment of the frame. (Optional, default as `.center`) |
| 90 | + /// - isAlignmentSet: Will we treat it as a manual set? (So the alignment will not be overrided later) |
| 91 | + public init( |
| 92 | + alignment: Alignment = .center, |
| 93 | + isAlignmentSet: Bool = false |
| 94 | + ) { |
| 95 | + self.inactive = false |
35 | 96 | self.width = 0
|
36 | 97 | self.isWidthConstrained = false
|
| 98 | + self.isWidthSet = false |
| 99 | + self.height = 0 |
| 100 | + self.isHeightConstrained = false |
| 101 | + self.isHeightSet = false |
| 102 | + self.alignment = alignment |
| 103 | + self.isAlignmentSet = isAlignmentSet |
| 104 | + } |
| 105 | + |
| 106 | + /// [DE] Create a ViewConfiguration with by default width frame constrained. |
| 107 | + /// |
| 108 | + /// - Parameters: |
| 109 | + /// - width: The width of the view frame. |
| 110 | + /// - isWidthSet: Will we treat this width as a manual set? (So the width will not be overrided later) |
| 111 | + /// - alignment: The default alignment of the frame. |
| 112 | + /// - isAlignmentSet: Will we treat this alignment as a manual set? (So the alignment will not be overrided later) |
| 113 | + public init( |
| 114 | + width: CGFloat, |
| 115 | + isWidthSet: Bool = false, |
| 116 | + alignment: Alignment = .center, |
| 117 | + isAlignmentSet: Bool = false |
| 118 | + ) { |
| 119 | + self.inactive = false |
| 120 | + self.width = width |
| 121 | + self.isWidthConstrained = true |
| 122 | + self.isWidthSet = isWidthSet |
37 | 123 | self.height = 0
|
38 | 124 | self.isHeightConstrained = false
|
| 125 | + self.isHeightSet = false |
| 126 | + self.alignment = alignment |
| 127 | + self.isAlignmentSet = isAlignmentSet |
| 128 | + } |
| 129 | + |
| 130 | + /// [DE] Create a ViewConfiguration with by default height frame constrained. |
| 131 | + /// |
| 132 | + /// - Parameters: |
| 133 | + /// - height: The height of the view frame. |
| 134 | + /// - isHeightSet: Will we treat this height as a manual set? (So the height will not be overrided later) |
| 135 | + /// - alignment: The default alignment of the frame. |
| 136 | + /// - isAlignmentSet: Will we treat this alignment as a manual set? (So the alignment will not be overrided later) |
| 137 | + public init( |
| 138 | + height: CGFloat, |
| 139 | + isHeightSet: Bool = false, |
| 140 | + alignment: Alignment = .center, |
| 141 | + isAlignmentSet: Bool = false |
| 142 | + ) { |
| 143 | + self.inactive = false |
| 144 | + self.width = 0 |
| 145 | + self.isWidthConstrained = false |
| 146 | + self.isWidthSet = false |
| 147 | + self.height = height |
| 148 | + self.isHeightConstrained = true |
| 149 | + self.isHeightSet = isHeightSet |
| 150 | + self.alignment = alignment |
| 151 | + self.isAlignmentSet = isAlignmentSet |
39 | 152 | }
|
40 | 153 |
|
| 154 | + /// [DE] Create a ViewConfiguration with by default full frame constrained. |
41 | 155 | ///
|
42 |
| - public init(width: CGFloat, height: CGFloat) { |
| 156 | + /// - Parameters: |
| 157 | + /// - width: The width of the view frame. |
| 158 | + /// - isWidthSet: Will we treat this width as a manual set? (So the width will not be overrided later) |
| 159 | + /// - height: The height of the view frame. |
| 160 | + /// - isHeightSet: Will we treat this height as a manual set? (So the height will not be overrided later) |
| 161 | + /// - alignment: The default alignment of the frame. |
| 162 | + /// - isAlignmentSet: Will we treat this alignment as a manual set? (So the alignment will not be overrided later) |
| 163 | + public init( |
| 164 | + width: CGFloat, |
| 165 | + isWidthSet: Bool = false, |
| 166 | + height: CGFloat, |
| 167 | + isHeightSet: Bool = false, |
| 168 | + alignment: Alignment = .center, |
| 169 | + isAlignmentSet: Bool = false |
| 170 | + ) { |
43 | 171 | self.inactive = false
|
44 | 172 | self.width = width
|
45 | 173 | self.isWidthConstrained = true
|
| 174 | + self.isWidthSet = isWidthSet |
46 | 175 | self.height = height
|
47 | 176 | self.isHeightConstrained = true
|
| 177 | + self.isHeightSet = isHeightSet |
| 178 | + self.alignment = alignment |
| 179 | + self.isAlignmentSet = isAlignmentSet |
48 | 180 | }
|
49 | 181 |
|
50 | 182 | /// [Internal]
|
51 |
| - init(oldConfiguration: DefinedViewConfiguration, |
52 |
| - width: CGFloat? = nil, |
53 |
| - height: CGFloat? = nil) { |
| 183 | + internal init( |
| 184 | + oldConfiguration: DefinedViewConfiguration, |
| 185 | + width: CGFloat? = nil, |
| 186 | + isWidthSet: Bool? = nil, |
| 187 | + height: CGFloat? = nil, |
| 188 | + isHeightSet: Bool? = nil, |
| 189 | + alignment: Alignment? = nil, |
| 190 | + isAlignmentSet: Bool? = nil |
| 191 | + ) { |
54 | 192 | self.inactive = oldConfiguration.inactive // this value may NOT be changed during copying.
|
55 | 193 | self.width = width ?? oldConfiguration.width
|
56 | 194 | self.isWidthConstrained = oldConfiguration.isWidthConstrained || width != nil
|
| 195 | + self.isWidthSet = isWidthSet ?? oldConfiguration.isWidthSet |
57 | 196 | self.height = height ?? oldConfiguration.height
|
58 | 197 | self.isHeightConstrained = oldConfiguration.isHeightConstrained || height != nil
|
| 198 | + self.isHeightSet = isHeightSet ?? oldConfiguration.isHeightSet |
| 199 | + self.alignment = alignment ?? oldConfiguration.alignment |
| 200 | + self.isAlignmentSet = isAlignmentSet ?? (oldConfiguration.isAlignmentSet || alignment != nil) |
59 | 201 | }
|
60 | 202 | }
|
0 commit comments