Skip to content

Commit 29eb348

Browse files
committed
Defer diff pane registration until selection dispatch completes
1 parent f641237 commit 29eb348

3 files changed

Lines changed: 112 additions & 8 deletions

File tree

‎README.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,12 @@ that guard and asks `Navigator.maybePop`; another guard may still veto it, so a
217217
handled request never implies that a route disappeared. Explicit successful
218218
operation completion keeps its separate owning-route completion path. The SDK,
219219
its `PopScope` implementation and its navigator remain unmodified.
220+
221+
Diff document selection defers pane registrar changes until the pointer event
222+
stack has unwound, then replays pending selection requests after the upstream
223+
registrars' scheduled updates. This prevents an enclosing selection clear from
224+
iterating descendants while a pane switch removes them. Generation checks retire
225+
callbacks when the source or view changes. Rapid alternating pane selection is
226+
covered before the next frame, including exact copying and closing with a
227+
pending registration; the existing wrapped, CRLF, RTL and bounded-tail cases
228+
remain covered.

‎lib/src/diff_document_selection.dart‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:async';
2+
13
import 'package:busystack_git/busystack_git.dart';
24
import 'package:flutter/material.dart';
35
import 'package:flutter/rendering.dart';
@@ -132,16 +134,20 @@ final class _Documents {
132134
activeRight = right;
133135
registrationPending = true;
134136
final generation = ++_registrationGeneration;
135-
for (final proxy in proxies.toList()) {
136-
proxy.updateRegistration();
137-
}
138-
// Registrar additions become laid-out selectable children after this frame.
139-
// Retain and replay actual selection requests through the supported pending
140-
// result, rather than making mouse correctness depend on an event delay.
137+
// A new pointer gesture also clears the enclosing SelectionArea. Removing
138+
// its descendants here can mutate a delegate still dispatching that clear.
139+
// Update registrations after the event stack has unwound, then replay real
140+
// requests after the registrars' scheduled updates in this frame.
141141
WidgetsBinding.instance.addPostFrameCallback((_) {
142142
if (disposed || generation != _registrationGeneration) return;
143-
registrationPending = false;
144-
selection.flushPending();
143+
for (final proxy in proxies.toList()) {
144+
proxy.updateRegistration();
145+
}
146+
scheduleMicrotask(() {
147+
if (disposed || generation != _registrationGeneration) return;
148+
registrationPending = false;
149+
selection.flushPending();
150+
});
145151
});
146152
WidgetsBinding.instance.scheduleFrame();
147153
}

‎test/diff_document_copy_test.dart‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,95 @@ final class _CopyClient extends FixtureClient {
3939
}
4040

4141
void main() {
42+
testWidgets('alternating pane selection clears before another frame', (
43+
tester,
44+
) async {
45+
tester.view.physicalSize = const Size(1400, 1000);
46+
tester.view.devicePixelRatio = 1;
47+
addTearDown(tester.view.resetPhysicalSize);
48+
addTearDown(tester.view.resetDevicePixelRatio);
49+
final session = GitSession(
50+
client: _CopyClient(),
51+
repository: repo,
52+
host: host(),
53+
);
54+
await session.start();
55+
session.setViewState('diffCollapse', false);
56+
await tester.pumpWidget(
57+
MaterialApp(
58+
home: Scaffold(
59+
body: GitDiffView(
60+
session: session,
61+
comparison: GitComparison(
62+
RepoPath.text('prose.md'),
63+
const ContentVersion.index(),
64+
const ContentVersion.working(),
65+
),
66+
),
67+
),
68+
),
69+
);
70+
await tester.runAsync(
71+
() => Future<void>.delayed(const Duration(milliseconds: 120)),
72+
);
73+
await tester.pumpAndSettle();
74+
final left = tester.getCenter(find.text('LEFT ONE'));
75+
final right = tester.getCenter(find.text('RIGHT ONE'));
76+
String? copied;
77+
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
78+
SystemChannels.platform,
79+
(call) async {
80+
if (call.method == 'Clipboard.setData') {
81+
copied = (call.arguments as Map)['text'] as String;
82+
}
83+
return null;
84+
},
85+
);
86+
addTearDown(
87+
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
88+
SystemChannels.platform,
89+
null,
90+
),
91+
);
92+
for (var pass = 0; pass < 12; pass++) {
93+
await tester.tapAt(
94+
pass.isEven ? left : right,
95+
kind: PointerDeviceKind.mouse,
96+
);
97+
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
98+
await tester.sendKeyEvent(LogicalKeyboardKey.keyA);
99+
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
100+
// Another mouse selection starts before the pending pane registration frame.
101+
await tester.tapAt(
102+
pass.isEven ? right : left,
103+
kind: PointerDeviceKind.mouse,
104+
);
105+
await tester.tapAt(
106+
pass.isEven ? left : right,
107+
kind: PointerDeviceKind.mouse,
108+
);
109+
expect(tester.takeException(), isNull);
110+
await tester.pump();
111+
expect(tester.takeException(), isNull);
112+
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
113+
await tester.sendKeyEvent(LogicalKeyboardKey.keyA);
114+
await tester.sendKeyEvent(LogicalKeyboardKey.keyC);
115+
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
116+
await tester.pump();
117+
expect(
118+
copied,
119+
(pass.isEven
120+
? 'LEFT ONE\nshared middle\n\nLEFT THREE\nLEFT FOUR'
121+
: 'RIGHT ONE\nshared middle\nRIGHT ONLY\nRIGHT THREE\nRIGHT FOUR')
122+
.replaceAll('shared middle', _CopyClient.contextLine),
123+
);
124+
}
125+
// Retiring the view also retires any pending registration callback.
126+
await tester.tapAt(left, kind: PointerDeviceKind.mouse);
127+
await tester.pumpWidget(const SizedBox());
128+
expect(tester.takeException(), isNull);
129+
session.dispose();
130+
});
42131
for (final right in [false, true]) {
43132
testWidgets(
44133
'copy full ${right ? 'right' : 'left'} source without building its tail',

0 commit comments

Comments
 (0)