11import SwiftUI
22
3- /// Style of segment content
4- public enum CustomizableSegmentedControlContentStyle {
5- /// Default style. You configure color for all states of content.
6- case `default`
7- /// Blend mode style. You configure colors, but some of them depends on background.
8- /// - parameters:
9- /// - contentBlendMode: Blend mode applies to content. Default is difference.
10- /// - firstLevelOverlayBlendMode: Blend mode applies to first level overlay. Default is hue.
11- /// - highestLevelOverlayBlendMode: Blend mode applies to highest level overlay. Default is overlay..
12- case withBlendMode(
13- contentBlendMode: BlendMode = . difference,
14- firstLevelOverlayBlendMode: BlendMode = . hue,
15- highestLevelOverlayBlendMode: BlendMode = . overlay
16- )
17- }
18-
193// MARK: - Segmented Control
204
215public struct CustomizableSegmentedControl < Option: Hashable & Identifiable , SelectionView: View , SegmentContent: View > : View {
226
237 // MARK: - Properties
248
9+ @Environment ( \. segmentedControlInsets) var segmentedControlInsets
10+ @Environment ( \. segmentedControlInterSegmentSpacing) var interSegmentSpacing
11+ @Environment ( \. segmentedControlSlidingAnimation) var slidingAnimation
12+ @Environment ( \. segmentedControlContentStyle) var contentStyle
13+
2514 @Binding private var selection : Option
2615 private let options : [ Option ]
27- private let insets : EdgeInsets
28- private let interSegmentSpacing : CGFloat
29- private let contentStyle : CustomizableSegmentedControlContentStyle
30- private let animation : Animation
3116 private let selectionView : ( ) -> SelectionView
3217 private let segmentContent : ( Option , Bool ) -> SegmentContent
3318
@@ -55,19 +40,11 @@ public struct CustomizableSegmentedControl<Option: Hashable & Identifiable, Sele
5540 public init (
5641 selection: Binding < Option > ,
5742 options: [ Option ] ,
58- insets: EdgeInsets = . init( top: 0 , leading: 0 , bottom: 0 , trailing: 0 ) ,
59- interSegmentSpacing: CGFloat = 0 ,
60- contentStyle: CustomizableSegmentedControlContentStyle = . default,
61- animation: Animation = . default,
6243 selectionView: @escaping ( ) -> SelectionView ,
6344 @ViewBuilder segmentContent: @escaping ( Option , Bool ) -> SegmentContent
6445 ) {
6546 self . _selection = selection
6647 self . options = options
67- self . insets = insets
68- self . interSegmentSpacing = interSegmentSpacing
69- self . contentStyle = contentStyle
70- self . animation = animation
7148 self . selectionView = selectionView
7249 self . segmentContent = segmentContent
7350 self . optionIsPressed = Dictionary ( uniqueKeysWithValues: options. lazy. map { ( $0. id, false ) } )
@@ -82,7 +59,7 @@ public struct CustomizableSegmentedControl<Option: Hashable & Identifiable, Sele
8259 content: segmentContent ( option, optionIsPressed [ option. id, default: false ] ) ,
8360 selectionView: selectionView ( ) ,
8461 isSelected: selection == option,
85- animation: animation ,
62+ animation: slidingAnimation ,
8663 contentBlendMode: contentStyle. contentBlendMode,
8764 firstLevelOverlayBlendMode: contentStyle. firstLevelOverlayBlendMode,
8865 highestLevelOverlayBlendMode: contentStyle. highestLevelOverlayBlendMode,
@@ -98,7 +75,7 @@ public struct CustomizableSegmentedControl<Option: Hashable & Identifiable, Sele
9875 . zIndex ( selection == option ? 0 : 1 )
9976 }
10077 }
101- . padding ( insets )
78+ . padding ( segmentedControlInsets )
10279 }
10380
10481}
@@ -107,12 +84,12 @@ public struct CustomizableSegmentedControl<Option: Hashable & Identifiable, Sele
10784
10885extension CustomizableSegmentedControl {
10986
110- fileprivate struct Segment < SelectionView : View , Content: View > : View {
87+ fileprivate struct Segment < SegmentSelectionView : View , Content: View > : View {
11188
11289 // MARK: - Properties
11390
11491 let content : Content
115- let selectionView : SelectionView
92+ let selectionView : SegmentSelectionView
11693 let isSelected : Bool
11794 let animation : Animation
11895 let contentBlendMode : BlendMode ?
@@ -181,20 +158,12 @@ extension CustomizableSegmentedControl {
181158 public init (
182159 selection: Binding < Option > ,
183160 options: [ Option ] ,
184- insets: EdgeInsets = . init( top: 0 , leading: 0 , bottom: 0 , trailing: 0 ) ,
185- interSegmentSpacing: CGFloat = 0 ,
186- contentStyle: CustomizableSegmentedControlContentStyle = . default,
187- animation: Animation = . default,
188161 selectionView: SelectionView ,
189162 @ViewBuilder segmentContent: @escaping ( Option , Bool ) -> SegmentContent
190163 ) {
191164 self . init (
192165 selection: selection,
193166 options: options,
194- insets: insets,
195- interSegmentSpacing: interSegmentSpacing,
196- contentStyle: contentStyle,
197- animation: animation,
198167 selectionView: { selectionView } ,
199168 segmentContent: segmentContent
200169 )
@@ -230,26 +199,26 @@ private extension CustomizableSegmentedControlContentStyle {
230199 switch self {
231200 case . default:
232201 return nil
233- case . withBlendMode ( let blendMode , _, _) :
234- return blendMode
202+ case . blendMode ( let mode , _, _) :
203+ return mode
235204 }
236205 }
237206
238207 var firstLevelOverlayBlendMode : BlendMode ? {
239208 switch self {
240209 case . default:
241210 return nil
242- case . withBlendMode ( _, let blendMode , _) :
243- return blendMode
211+ case . blendMode ( _, let mode , _) :
212+ return mode
244213 }
245214 }
246215
247216 var highestLevelOverlayBlendMode : BlendMode ? {
248217 switch self {
249218 case . default:
250219 return nil
251- case . withBlendMode ( _, _, let blendMode ) :
252- return blendMode
220+ case . blendMode ( _, _, let mode ) :
221+ return mode
253222 }
254223 }
255224
@@ -263,7 +232,7 @@ public extension CustomizableSegmentedControl {
263232 ///
264233 /// - Parameters:
265234 /// - completion: Takes index and total count. Returns neccessary string
266- func segmentAccessibilityValue( _ completion: @escaping ( Int , Int ) -> String ) -> some View {
235+ func segmentAccessibilityValue( _ completion: @escaping ( Int , Int ) -> String ) -> Self {
267236 var copy = self
268237 copy. segmentAccessibilityValueCompletion = completion
269238 return copy
0 commit comments