@@ -203,21 +203,36 @@ public struct DimensionSelector: View {
203
203
private func getHStack( ) -> some View {
204
204
HStack ( alignment: . center, spacing: self . model. interItemSpacing) {
205
205
ForEach ( self . model. titles. indices, id: \. self) { index in
206
- Segment ( title: self . model. titles [ index] , isSelected: self . model. selectedIndex == index, isEnable: self . model. isEnable, cornerRadius: 10.0 , backgroundColor: . preferredColor( . primaryFill) , segmentAttributes: self . model. segmentAttributes, insets: self . titleInsets)
206
+ Text ( self . model. titles [ index] )
207
+ . padding ( self . titleInsets)
208
+ . font ( segmentAttributes ( for: index) ? . font)
209
+ . foregroundColor ( segmentAttributes ( for: index) ? . textColor)
210
+ . background ( SegmentPreferenceSetter ( ) )
211
+ . modifier ( SegmentFrame ( segmentWidthMode: self . model. segmentWidthMode, width: self . _segmentWidth) )
212
+ . background (
213
+ RoundedRectangle ( cornerRadius: 10 , style: . continuous)
214
+ . inset ( by: segmentAttributes ( for: index) !. borderWidth! / 2.0 )
215
+ . stroke ( segmentAttributes ( for: index) !. borderColor!, lineWidth: segmentAttributes ( for: index) !. borderWidth!)
216
+ )
217
+ . background ( RoundedRectangle ( cornerRadius: 10 , style: . continuous) . fill ( segmentAttributes ( for: index) !. backgroundColor!) )
207
218
. onTapGesture {
208
219
if self . model. isEnable {
209
220
self . selectionDidChange ( index: index)
210
221
}
211
222
}
212
- . background ( SegmentPreferenceSetter ( ) )
213
- . modifier ( SegmentFrame ( segmentWidthMode: self . model. segmentWidthMode, width: self . _segmentWidth) )
214
223
}
215
224
}
216
225
. padding ( self . contentInset)
217
226
. lineLimit ( 1 )
218
227
. background ( HStackPreferenceSetter ( ) )
219
228
}
220
229
230
+ func segmentAttributes( for index: Int ) -> SegmentAttributes ? {
231
+ let isSelected = self . model. selectedIndex == index
232
+
233
+ return self . model. isEnable ? ( isSelected ? self . model. segmentAttributes [ . selected] : self . model. segmentAttributes [ . normal] ) : self . model. segmentAttributes [ . disabled]
234
+ }
235
+
221
236
private var _segmentWidth : CGFloat ? {
222
237
let width : CGFloat ?
223
238
@@ -249,39 +264,6 @@ public struct DimensionSelector: View {
249
264
}
250
265
251
266
extension DimensionSelector {
252
- struct Segment : View {
253
- let title : String
254
-
255
- let isSelected : Bool
256
-
257
- let isEnable : Bool
258
-
259
- let cornerRadius : CGFloat
260
-
261
- let backgroundColor : Color
262
-
263
- let segmentAttributes : [ ControlState : SegmentAttributes ]
264
-
265
- let insets : EdgeInsets
266
-
267
- var body : some View {
268
- Text ( self . title)
269
- . padding ( insets)
270
- . font ( getSegmentAttributes ( ) ? . font)
271
- . foregroundColor ( getSegmentAttributes ( ) ? . textColor)
272
- . background (
273
- RoundedRectangle ( cornerRadius: cornerRadius, style: . continuous)
274
- . inset ( by: getSegmentAttributes ( ) !. borderWidth! / 2.0 )
275
- . stroke ( getSegmentAttributes ( ) !. borderColor!, lineWidth: getSegmentAttributes ( ) !. borderWidth!)
276
- )
277
- . background ( RoundedRectangle ( cornerRadius: cornerRadius, style: . continuous) . fill ( getSegmentAttributes ( ) !. backgroundColor!) )
278
- }
279
-
280
- func getSegmentAttributes( ) -> SegmentAttributes ? {
281
- self . isEnable ? ( self . isSelected ? self . segmentAttributes [ . selected] : self . segmentAttributes [ . normal] ) : self . segmentAttributes [ . disabled]
282
- }
283
- }
284
-
285
267
class Model : ObservableObject {
286
268
@Published var titles : [ String ] = [ ]
287
269
@Published var selectedIndex : Int ?
0 commit comments