@@ -28,15 +28,15 @@ final class ThemePaginationGlassControlFactory: NSObject, FlutterPlatformViewFac
2828}
2929
3030private final class ThemePaginationControl : UIControl {
31- private static let maximumVisibleDots = 7
32- private static let fullDotSize : CGFloat = 6
31+ private static let fullDotSize = ThemePaginationGeometry . dotSize
3332 private static let selectedDotSize : CGFloat = 10
34- private static let dotSpacing : CGFloat = 6
3533 private let glassView : UIVisualEffectView
3634 private let dotsContainer = UIView ( )
35+ private var glassHeightConstraint : NSLayoutConstraint ?
3736 private var dots : [ UIView ] = [ ]
3837 private var totalCount = 1
3938 private var selectedIndex = 0
39+ private var scrub = ThemePaginationScrub ( )
4040 private var activeColor = UIColor . label
4141 private var inactiveColor = UIColor . secondaryLabel . withAlphaComponent ( 0.32 )
4242 var onSelectionChanged : ( ( Int ) -> Void ) ?
@@ -53,6 +53,7 @@ private final class ThemePaginationControl: UIControl {
5353
5454 backgroundColor = . clear
5555 isOpaque = false
56+ isAccessibilityElement = true
5657 accessibilityTraits = [ . adjustable ]
5758 accessibilityLabel = " Theme "
5859
@@ -67,11 +68,13 @@ private final class ThemePaginationControl: UIControl {
6768 dotsContainer . isUserInteractionEnabled = false
6869 glassView . contentView . addSubview ( dotsContainer )
6970
71+ let glassHeightConstraint = glassView . heightAnchor . constraint ( equalToConstant : 30 )
72+ self . glassHeightConstraint = glassHeightConstraint
7073 NSLayoutConstraint . activate ( [
7174 glassView . leadingAnchor . constraint ( equalTo : leadingAnchor ) ,
7275 glassView . trailingAnchor . constraint ( equalTo : trailingAnchor ) ,
7376 glassView . centerYAnchor . constraint ( equalTo : centerYAnchor ) ,
74- glassView . heightAnchor . constraint ( equalToConstant : 30 ) ,
77+ glassHeightConstraint ,
7578 dotsContainer . leadingAnchor . constraint ( equalTo : glassView . contentView . leadingAnchor , constant : 12 ) ,
7679 dotsContainer . trailingAnchor . constraint ( equalTo : glassView . contentView . trailingAnchor , constant : - 12 ) ,
7780 dotsContainer . topAnchor . constraint ( equalTo : glassView . contentView . topAnchor ) ,
@@ -108,6 +111,15 @@ private final class ThemePaginationControl: UIControl {
108111 inactiveColor = Self . color ( fromARGB : value . uint32Value )
109112 }
110113
114+ accessibilityLabel = arguments [ " accessibilityLabel " ] as? String ?? accessibilityLabel
115+ if let height = arguments [ " containerHeight " ] as? NSNumber {
116+ glassHeightConstraint ? . constant = max ( 10 , CGFloat ( height . doubleValue ) )
117+ setNeedsLayout ( )
118+ }
119+ if let isRTL = arguments [ " isRTL " ] as? Bool {
120+ semanticContentAttribute = isRTL ? . forceRightToLeft : . forceLeftToRight
121+ }
122+ if count != totalCount { scrub . end ( ) }
111123 totalCount = count
112124 if count != dots . count {
113125 rebuildDots ( count : count )
@@ -126,11 +138,39 @@ private final class ThemePaginationControl: UIControl {
126138 select ( index : max ( selectedIndex - 1 , 0 ) )
127139 }
128140
141+ private var geometry : ThemePaginationGeometry {
142+ ThemePaginationGeometry (
143+ count : totalCount , selected : selectedIndex , width : dotsContainer . bounds . width ,
144+ isRTL : effectiveUserInterfaceLayoutDirection == . rightToLeft
145+ )
146+ }
147+
129148 @ objc private func handleGesture ( _ recognizer : UIGestureRecognizer ) {
130- guard totalCount > 0 else { return }
131- let location = recognizer . location ( in : glassView )
132- let progress = max ( 0 , min ( 0.999_999 , location . x / max ( 1 , glassView . bounds . width ) ) )
133- select ( index : Int ( progress * CGFloat ( totalCount ) ) )
149+ guard dotsContainer . bounds . width > 0 else { return }
150+ let location = recognizer . location ( in : dotsContainer )
151+ if recognizer is UIPanGestureRecognizer {
152+ switch recognizer . state {
153+ case . began :
154+ scrub . begin ( geometry )
155+ case . changed :
156+ guard scrub . geometry != nil else { return }
157+ case . ended :
158+ let page = scrub . page ( at : location . x , current : geometry )
159+ scrub . end ( )
160+ select ( index : page )
161+ updateDots ( animated : true )
162+ return
163+ case . cancelled , . failed :
164+ scrub . end ( )
165+ updateDots ( animated : true )
166+ return
167+ default :
168+ return
169+ }
170+ select ( index : scrub . page ( at : location . x , current : geometry ) )
171+ } else if recognizer . state == . ended {
172+ select ( index : geometry . page ( at : location . x ) )
173+ }
134174 }
135175
136176 private func select ( index : Int ) {
@@ -163,20 +203,13 @@ private final class ThemePaginationControl: UIControl {
163203
164204 private func updateDots ( animated : Bool ) {
165205 guard ! dots . isEmpty , dotsContainer . bounds . width > 0 else { return }
166- let visibleCount = min ( totalCount , Self . maximumVisibleDots )
167- let maximumStart = max ( 0 , totalCount - visibleCount )
168- let centerSlot = visibleCount / 2
169- let windowStart = min ( max ( 0 , selectedIndex - centerSlot ) , maximumStart )
170- let windowEnd = windowStart + visibleCount - 1
171- let hasEarlierDots = windowStart > 0
172- let hasLaterDots = windowEnd < totalCount - 1
173- let pitch = Self . fullDotSize + Self . dotSpacing
174- let trackWidth = CGFloat ( visibleCount ) * Self . fullDotSize
175- + CGFloat ( max ( 0 , visibleCount - 1 ) ) * Self . dotSpacing
176- let trackOrigin = ( dotsContainer . bounds . width - trackWidth ) / 2
206+ let geometry = scrub . geometry ?? self . geometry
207+ let visibleCount = geometry . visibleCount
208+ let hasEarlierDots = geometry . isRTL ? geometry . hasLaterDots : geometry . hasEarlierDots
209+ let hasLaterDots = geometry . isRTL ? geometry . hasEarlierDots : geometry . hasLaterDots
177210 let changes = {
178211 for ( page , dot ) in self . dots . enumerated ( ) {
179- let slot = page - windowStart
212+ let slot = geometry . slot ( for : page )
180213 let isVisible = ( 0 ..< visibleCount ) . contains ( slot )
181214 var diameter = Self . fullDotSize
182215 if page == self . selectedIndex {
@@ -191,7 +224,7 @@ private final class ThemePaginationControl: UIControl {
191224 diameter = 4
192225 }
193226 dot . center = CGPoint (
194- x : trackOrigin + Self . fullDotSize / 2 + CGFloat ( slot ) * pitch ,
227+ x : geometry . centerX ( for : page ) ,
195228 y : self . dotsContainer . bounds . midY
196229 )
197230 dot . alpha = isVisible ? 1 : 0
0 commit comments