Skip to content

Commit 4ef1d99

Browse files
committed
fix(mobile): freeze pagination windows during scrubbing
Signed-off-by: kenny lopez <[email protected]>
1 parent 6c2bd80 commit 4ef1d99

5 files changed

Lines changed: 190 additions & 21 deletions

File tree

‎mobile/ios/NativeControlTests/ThemePaginationGeometryTests.swift‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,34 @@ struct ThemePaginationGeometryTests {
3131
check(geometry.page(at: 200) == firstPage + (isRTL ? 0 : centers.count - 1), "right edge")
3232
}
3333
}
34+
for isRTL in [false, true] {
35+
for cancel in [false, true] {
36+
var selected = 10
37+
func current() -> ThemePaginationGeometry {
38+
ThemePaginationGeometry(count: 20, selected: selected, width: 92, isRTL: isRTL)
39+
}
40+
var scrub = ThemePaginationScrub()
41+
let target = current().centerX(for: 12)
42+
scrub.begin(current())
43+
for _ in 0..<8 {
44+
selected = scrub.page(at: target, current: current())
45+
check(selected == 12, "held pan cascaded in RTL=\(isRTL), cancel=\(cancel)")
46+
check(scrub.geometry?.centerX(for: 12) == target, "rendered window moved")
47+
}
48+
// The UIKit ended path consumes its last coordinate before clearing;
49+
// cancelled/failed paths clear without introducing another selection.
50+
if !cancel { selected = scrub.page(at: target, current: current()) }
51+
scrub.end()
52+
check(scrub.geometry == nil, "end/cancel retained window")
53+
let nextTarget = current().centerX(for: 14)
54+
scrub.begin(current())
55+
selected = scrub.page(at: nextTarget, current: current())
56+
check(selected == 14, "next gesture reused stale geometry")
57+
scrub.end()
58+
check(current().page(at: current().centerX(for: 15)) == 15, "tap after scrub")
59+
}
60+
}
61+
print("PASS: held scrubs, final events, cancellation and fresh gestures in LTR/RTL")
3462
print("PASS: \(checkedCenters) rendered centers, neighboring taps and clamped edges in LTR/RTL")
3563
}
3664
}

‎mobile/ios/Runner/ThemePaginationGeometry.swift‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,20 @@ struct ThemePaginationGeometry {
3333
return windowStart + (isRTL ? visibleCount - 1 - boundedSlot : boundedSlot)
3434
}
3535
}
36+
37+
/// Freezes the rendered window for one continuous pan, including its final event.
38+
struct ThemePaginationScrub {
39+
private(set) var geometry: ThemePaginationGeometry?
40+
41+
mutating func begin(_ geometry: ThemePaginationGeometry) {
42+
self.geometry = geometry
43+
}
44+
45+
func page(at x: CGFloat, current: ThemePaginationGeometry) -> Int {
46+
(geometry ?? current).page(at: x)
47+
}
48+
49+
mutating func end() {
50+
geometry = nil
51+
}
52+
}

‎mobile/ios/Runner/ThemePaginationGlassControl.swift‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ private final class ThemePaginationControl: UIControl {
3636
private var dots: [UIView] = []
3737
private var totalCount = 1
3838
private var selectedIndex = 0
39+
private var scrub = ThemePaginationScrub()
3940
private var activeColor = UIColor.label
4041
private var inactiveColor = UIColor.secondaryLabel.withAlphaComponent(0.32)
4142
var onSelectionChanged: ((Int) -> Void)?
@@ -117,6 +118,7 @@ private final class ThemePaginationControl: UIControl {
117118
if let isRTL = arguments["isRTL"] as? Bool {
118119
semanticContentAttribute = isRTL ? .forceRightToLeft : .forceLeftToRight
119120
}
121+
if count != totalCount { scrub.end() }
120122
totalCount = count
121123
if count != dots.count {
122124
rebuildDots(count: count)
@@ -145,7 +147,29 @@ private final class ThemePaginationControl: UIControl {
145147
@objc private func handleGesture(_ recognizer: UIGestureRecognizer) {
146148
guard dotsContainer.bounds.width > 0 else { return }
147149
let location = recognizer.location(in: dotsContainer)
148-
select(index: geometry.page(at: location.x))
150+
if recognizer is UIPanGestureRecognizer {
151+
switch recognizer.state {
152+
case .began:
153+
scrub.begin(geometry)
154+
case .changed:
155+
guard scrub.geometry != nil else { return }
156+
case .ended:
157+
let page = scrub.page(at: location.x, current: geometry)
158+
scrub.end()
159+
select(index: page)
160+
updateDots(animated: true)
161+
return
162+
case .cancelled, .failed:
163+
scrub.end()
164+
updateDots(animated: true)
165+
return
166+
default:
167+
return
168+
}
169+
select(index: scrub.page(at: location.x, current: geometry))
170+
} else if recognizer.state == .ended {
171+
select(index: geometry.page(at: location.x))
172+
}
149173
}
150174

151175
private func select(index: Int) {
@@ -178,7 +202,7 @@ private final class ThemePaginationControl: UIControl {
178202

179203
private func updateDots(animated: Bool) {
180204
guard !dots.isEmpty, dotsContainer.bounds.width > 0 else { return }
181-
let geometry = self.geometry
205+
let geometry = scrub.geometry ?? self.geometry
182206
let visibleCount = geometry.visibleCount
183207
let hasEarlierDots = geometry.isRTL ? geometry.hasLaterDots : geometry.hasEarlierDots
184208
let hasLaterDots = geometry.isRTL ? geometry.hasEarlierDots : geometry.hasLaterDots

‎mobile/lib/shared/widgets/page_indicator.dart‎

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:flutter/foundation.dart';
22
import 'package:flutter/material.dart';
3+
import 'package:flutter_hooks/flutter_hooks.dart';
34

45
import '../theme/theme.dart';
56
import 'ios_glass_theme_pagination.dart';
@@ -92,7 +93,14 @@ class PageIndicator extends StatelessWidget {
9293
}
9394
}
9495

95-
class _WindowedPagination extends StatelessWidget {
96+
typedef _PaginationScrubGeometry = ({
97+
int windowStart,
98+
int visibleCount,
99+
double width,
100+
bool isRtl,
101+
});
102+
103+
class _WindowedPagination extends HookWidget {
96104
const _WindowedPagination({
97105
required this.containerHeight,
98106
required this.dotKeyPrefix,
@@ -117,28 +125,37 @@ class _WindowedPagination extends StatelessWidget {
117125

118126
@override
119127
Widget build(BuildContext context) {
128+
final scrubGeometry = useState<_PaginationScrubGeometry?>(null);
120129
final reduceMotion = MediaQuery.disableAnimationsOf(context);
121130
final duration = reduceMotion || !animateChanges
122131
? Duration.zero
123132
: const Duration(milliseconds: 150);
124-
final visibleCount = count.clamp(1, _maximumVisibleDots);
125-
final maximumStart = (count - visibleCount).clamp(0, count);
126-
final centerSlot = visibleCount ~/ 2;
127-
final windowStart = (selected - centerSlot).clamp(0, maximumStart);
128-
final windowEnd = windowStart + visibleCount - 1;
129-
final hasEarlierDots = windowStart > 0;
130-
final hasLaterDots = windowEnd < count - 1;
131-
132-
final pitch = _dotSize + _spacing;
133-
final trackWidth = visibleCount * _dotSize + (visibleCount - 1) * _spacing;
134-
final isRtl = Directionality.of(context) == TextDirection.rtl;
135-
133+
final currentVisibleCount = count.clamp(1, _maximumVisibleDots);
134+
final maximumStart = (count - currentVisibleCount).clamp(0, count);
135+
final centerSlot = currentVisibleCount ~/ 2;
136136
return LayoutBuilder(
137137
builder: (context, constraints) {
138+
final geometry =
139+
scrubGeometry.value ??
140+
(
141+
windowStart: (selected - centerSlot).clamp(0, maximumStart),
142+
visibleCount: currentVisibleCount,
143+
width: constraints.maxWidth,
144+
isRtl: Directionality.of(context) == TextDirection.rtl,
145+
);
146+
final windowStart = geometry.windowStart;
147+
final visibleCount = geometry.visibleCount;
148+
final isRtl = geometry.isRtl;
149+
final windowEnd = windowStart + visibleCount - 1;
150+
final hasEarlierDots = windowStart > 0;
151+
final hasLaterDots = windowEnd < count - 1;
152+
final pitch = _dotSize + _spacing;
153+
final trackWidth =
154+
visibleCount * _dotSize + (visibleCount - 1) * _spacing;
155+
138156
void selectFromPosition(double dx) {
139157
if (count <= 1) return;
140-
final firstCenter =
141-
(constraints.maxWidth - trackWidth) / 2 + _dotSize / 2;
158+
final firstCenter = (geometry.width - trackWidth) / 2 + _dotSize / 2;
142159
final slot = ((dx - firstCenter) / pitch).round().clamp(
143160
0,
144161
visibleCount - 1,
@@ -150,11 +167,17 @@ class _WindowedPagination extends StatelessWidget {
150167
return GestureDetector(
151168
behavior: HitTestBehavior.opaque,
152169
excludeFromSemantics: true,
153-
onTapDown: (details) => selectFromPosition(details.localPosition.dx),
154-
onHorizontalDragStart: (details) =>
155-
selectFromPosition(details.localPosition.dx),
170+
// A tap must not select on pointer-down before a drag can snapshot
171+
// the window. Otherwise the first drag callback already sees a shift.
172+
onTapUp: (details) => selectFromPosition(details.localPosition.dx),
173+
onHorizontalDragStart: (details) {
174+
scrubGeometry.value = geometry;
175+
selectFromPosition(details.localPosition.dx);
176+
},
156177
onHorizontalDragUpdate: (details) =>
157178
selectFromPosition(details.localPosition.dx),
179+
onHorizontalDragEnd: (_) => scrubGeometry.value = null,
180+
onHorizontalDragCancel: () => scrubGeometry.value = null,
158181
child: SizedBox(
159182
height: 54,
160183
child: Center(
@@ -167,7 +190,8 @@ class _WindowedPagination extends StatelessWidget {
167190
),
168191
child: LayoutBuilder(
169192
builder: (context, constraints) {
170-
final trackOrigin = (constraints.maxWidth - trackWidth) / 2;
193+
final trackOrigin =
194+
(geometry.width - Grid.twelve * 2 - trackWidth) / 2;
171195
return ClipRect(
172196
child: Stack(
173197
children: [

‎mobile/test/shared/widgets/page_indicator_test.dart‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,82 @@ void main() {
5252
);
5353

5454
for (final direction in TextDirection.values) {
55+
for (final cancel in [false, true]) {
56+
testWidgets(
57+
'scrub window stays fixed across rebuilds in $direction, cancel=$cancel',
58+
(tester) async {
59+
final selected = ValueNotifier(10);
60+
addTearDown(selected.dispose);
61+
await tester.pumpWidget(
62+
WidgetHelpers.testable(
63+
child: Directionality(
64+
textDirection: direction,
65+
child: Center(
66+
child: SizedBox(
67+
width: 390,
68+
child: ValueListenableBuilder<int>(
69+
valueListenable: selected,
70+
builder: (context, page, _) => PageIndicator(
71+
semanticLabel: 'Photo',
72+
count: 20,
73+
selected: page,
74+
animateChanges: false,
75+
onSelected: (page) => selected.value = page,
76+
),
77+
),
78+
),
79+
),
80+
),
81+
),
82+
);
83+
Offset center(int page) => tester.getCenter(
84+
find.byKey(ValueKey('page-indicator-dot-$page')),
85+
);
86+
final target = center(12);
87+
final gesture = await tester.startGesture(center(10));
88+
await gesture.moveTo(target);
89+
await tester.pump();
90+
expect(selected.value, 12);
91+
for (var event = 0; event < 6; event++) {
92+
// Vertical jitter emits new pointer events at an unchanged scrub x.
93+
await gesture.moveTo(target + Offset(0, event.isEven ? 1 : 0));
94+
await tester.pump();
95+
expect(selected.value, 12, reason: 'Held pointer must not cascade');
96+
expect(
97+
center(12).dx,
98+
target.dx,
99+
reason: 'Rendered window stays under the pointer',
100+
);
101+
}
102+
if (cancel) {
103+
await gesture.cancel();
104+
} else {
105+
await gesture.up();
106+
}
107+
await tester.pump();
108+
expect(selected.value, 12);
109+
expect(
110+
center(12).dx,
111+
isNot(target.dx),
112+
reason: 'Release recenters the window',
113+
);
114+
// The next gesture must snapshot the new window after end OR cancel.
115+
final next = await tester.startGesture(center(12));
116+
await next.moveTo(center(14));
117+
await tester.pump();
118+
expect(selected.value, 14);
119+
await next.up();
120+
await tester.pump();
121+
await tester.tapAt(center(15));
122+
await tester.pump();
123+
expect(
124+
selected.value,
125+
15,
126+
reason: 'A tap uses the current window once',
127+
);
128+
},
129+
);
130+
}
55131
for (final window in [
56132
(count: 3, selected: 1, first: 0, last: 2),
57133
(count: 20, selected: 0, first: 0, last: 6),

0 commit comments

Comments
 (0)