Skip to content

Commit 0a417c3

Browse files
authored
A bunch of cleanups and a missing ShortcutRegistar in WidgetsApp (#104560)
A bunch of random cleanup things I found while doing MenuBar development. Changes an if test to an assert in binding.dart, since the if should always be true. Adds the default ShortcutRegistrar that should have been in the ShortcutRegistry PR. Moves a debug message in the FocusManager to print the result after the focus change instead of before. Reorders the test parameters in theme_data_test.dart to match the order of the theme data fields everywhere else.
1 parent 19d69a9 commit 0a417c3

File tree

7 files changed

+295
-182
lines changed

7 files changed

+295
-182
lines changed

packages/flutter/lib/src/gestures/binding.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,9 @@ mixin GestureBinding on BindingBase implements HitTestable, HitTestDispatcher, H
399399
@pragma('vm:notify-debugger-on-exception')
400400
void dispatchEvent(PointerEvent event, HitTestResult? hitTestResult) {
401401
assert(!locked);
402-
// No hit test information implies that this is a [PointerHoverEvent],
403-
// [PointerAddedEvent], or [PointerRemovedEvent]. These events are specially
404-
// routed here; other events will be routed through the `handleEvent` below.
402+
// No hit test information implies that this is a [PointerAddedEvent] or
403+
// [PointerRemovedEvent]. These events are specially routed here; other
404+
// events will be routed through the `handleEvent` below.
405405
if (hitTestResult == null) {
406406
assert(event is PointerAddedEvent || event is PointerRemovedEvent);
407407
try {

packages/flutter/lib/src/widgets/app.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1730,7 +1730,9 @@ class _WidgetsAppState extends State<WidgetsApp> with WidgetsBindingObserver {
17301730
actions: widget.actions ?? WidgetsApp.defaultActions,
17311731
child: FocusTraversalGroup(
17321732
policy: ReadingOrderTraversalPolicy(),
1733-
child: child,
1733+
child: ShortcutRegistrar(
1734+
child: child,
1735+
),
17341736
),
17351737
),
17361738
),

packages/flutter/lib/src/widgets/focus_manager.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1798,10 +1798,10 @@ class FocusManager with DiagnosticableTreeMixin, ChangeNotifier {
17981798
_dirtyNodes.add(_primaryFocus!);
17991799
}
18001800
}
1801-
assert(_focusDebug('Notifying ${_dirtyNodes.length} dirty nodes:', _dirtyNodes.toList().map<String>((FocusNode node) => node.toString())));
18021801
for (final FocusNode node in _dirtyNodes) {
18031802
node._notify();
18041803
}
1804+
assert(_focusDebug('Notified ${_dirtyNodes.length} dirty nodes:', _dirtyNodes.toList().map<String>((FocusNode node) => node.toString())));
18051805
_dirtyNodes.clear();
18061806
if (previousFocus != _primaryFocus) {
18071807
notifyListeners();

packages/flutter/test/material/debug_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ void main() {
186186
' Localizations\n'
187187
' MediaQuery\n'
188188
' _MediaQueryFromWindow\n'
189+
' _ShortcutRegistrarMarker\n'
190+
' _ShortcutsMarker\n'
191+
' Semantics\n'
192+
' _FocusMarker\n'
193+
' Focus\n'
194+
' Shortcuts\n'
195+
' ShortcutRegistrar\n'
189196
' _FocusMarker\n'
190197
' Focus\n'
191198
' _FocusTraversalGroupMarker\n'

packages/flutter/test/material/theme_data_test.dart

Lines changed: 235 additions & 176 deletions
Large diffs are not rendered by default.

packages/flutter/test/widgets/focus_manager_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1697,7 +1697,7 @@ void main() {
16971697
final String messagesStr = messages.toString();
16981698
expect(messagesStr.split('\n').length, equals(58));
16991699
expect(messagesStr, contains(RegExp(r' └─Child 1: FocusScopeNode#[a-f0-9]{5}\(parent1 \[PRIMARY FOCUS\]\)')));
1700-
expect(messagesStr, contains('FOCUS: Notifying 2 dirty nodes'));
1700+
expect(messagesStr, contains('FOCUS: Notified 2 dirty nodes'));
17011701
expect(messagesStr, contains(RegExp(r'FOCUS: Scheduling update, current focus is null, next focus will be FocusScopeNode#.*parent1')));
17021702
});
17031703
}

packages/flutter/test/widgets/shortcuts_test.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,51 @@ void main() {
13541354
expect(invokedB, equals(1));
13551355
});
13561356

1357+
testWidgets('MaterialApp has a ShortcutRegistrar listening', (WidgetTester tester) async {
1358+
int invokedA = 0;
1359+
int invokedB = 0;
1360+
await tester.pumpWidget(
1361+
MaterialApp(
1362+
home: TestCallbackRegistration(
1363+
shortcuts: <ShortcutActivator, Intent>{
1364+
const SingleActivator(LogicalKeyboardKey.keyA): VoidCallbackIntent(() {
1365+
invokedA += 1;
1366+
}),
1367+
const SingleActivator(LogicalKeyboardKey.keyB): VoidCallbackIntent(() {
1368+
invokedB += 1;
1369+
}),
1370+
},
1371+
child: Actions(
1372+
actions: <Type, Action<Intent>>{
1373+
VoidCallbackIntent: VoidCallbackAction(),
1374+
},
1375+
child: const Focus(
1376+
autofocus: true,
1377+
child: Placeholder(),
1378+
),
1379+
),
1380+
),
1381+
),
1382+
);
1383+
await tester.pump();
1384+
1385+
await tester.sendKeyDownEvent(LogicalKeyboardKey.keyA);
1386+
await tester.pump();
1387+
expect(invokedA, equals(1));
1388+
expect(invokedB, equals(0));
1389+
await tester.sendKeyUpEvent(LogicalKeyboardKey.keyA);
1390+
expect(invokedA, equals(1));
1391+
expect(invokedB, equals(0));
1392+
invokedA = 0;
1393+
invokedB = 0;
1394+
await tester.sendKeyDownEvent(LogicalKeyboardKey.keyB);
1395+
expect(invokedA, equals(0));
1396+
expect(invokedB, equals(1));
1397+
await tester.sendKeyUpEvent(LogicalKeyboardKey.keyB);
1398+
expect(invokedA, equals(0));
1399+
expect(invokedB, equals(1));
1400+
});
1401+
13571402
testWidgets("doesn't override text field shortcuts", (WidgetTester tester) async {
13581403
final TextEditingController controller = TextEditingController();
13591404
await tester.pumpWidget(

0 commit comments

Comments
 (0)