Unverified Commit 7ec3f6bd authored by Todd Volkert's avatar Todd Volkert Committed by GitHub

Make FocusManager configurable in BuildOwner (#71872)

parent 7324ade3
...@@ -71,6 +71,22 @@ class PointerRouter { ...@@ -71,6 +71,22 @@ class PointerRouter {
_globalRoutes.remove(route); _globalRoutes.remove(route);
} }
/// The number of global routes that have been registered.
///
/// This is valid in debug builds only. In release builds, this will throw an
/// [UnsupportedError].
int get debugGlobalRouteCount {
int? count;
assert(() {
count = _globalRoutes.length;
return true;
}());
if (count != null) {
return count!;
}
throw UnsupportedError('debugGlobalRouteCount is not supported in release builds');
}
void _dispatch(PointerEvent event, PointerRoute route, Matrix4? transform) { void _dispatch(PointerEvent event, PointerRoute route, Matrix4? transform) {
try { try {
event = event.transformed(transform); event = event.transformed(transform);
......
...@@ -2554,7 +2554,8 @@ abstract class BuildContext { ...@@ -2554,7 +2554,8 @@ abstract class BuildContext {
/// widget tree. /// widget tree.
class BuildOwner { class BuildOwner {
/// Creates an object that manages widgets. /// Creates an object that manages widgets.
BuildOwner({ this.onBuildScheduled }); BuildOwner({ this.onBuildScheduled, FocusManager? focusManager }) :
focusManager = focusManager ?? FocusManager();
/// Called on each build pass when the first buildable element is marked /// Called on each build pass when the first buildable element is marked
/// dirty. /// dirty.
...@@ -2585,7 +2586,7 @@ class BuildOwner { ...@@ -2585,7 +2586,7 @@ class BuildOwner {
/// the [FocusScopeNode] for a given [BuildContext]. /// the [FocusScopeNode] for a given [BuildContext].
/// ///
/// See [FocusManager] for more details. /// See [FocusManager] for more details.
FocusManager focusManager = FocusManager(); FocusManager focusManager;
/// Adds an element to the dirty elements list so that it will be rebuilt /// Adds an element to the dirty elements list so that it will be rebuilt
/// when [WidgetsBinding.drawFrame] calls [buildScope]. /// when [WidgetsBinding.drawFrame] calls [buildScope].
......
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
...@@ -1483,6 +1485,27 @@ void main() { ...@@ -1483,6 +1485,27 @@ void main() {
) )
); );
}); });
testWidgets('Can create BuildOwner that does not interfere with pointer router or raw key event handler', (WidgetTester tester) async {
final int pointerRouterCount = GestureBinding.instance!.pointerRouter.debugGlobalRouteCount;
final RawKeyEventHandler? rawKeyEventHandler = RawKeyboard.instance.keyEventHandler;
expect(rawKeyEventHandler, isNotNull);
BuildOwner(focusManager: _FakeFocusManager());
expect(GestureBinding.instance!.pointerRouter.debugGlobalRouteCount, pointerRouterCount);
expect(RawKeyboard.instance.keyEventHandler, same(rawKeyEventHandler));
});
}
class _FakeFocusManager implements FocusManager {
@override
dynamic noSuchMethod(Invocation invocation) {
return super.noSuchMethod(invocation);
}
@override
String toString({ DiagnosticLevel minLevel = DiagnosticLevel.info }) {
return '_FakeFocusManager';
}
} }
class _WidgetWithNoVisitChildren extends StatelessWidget { class _WidgetWithNoVisitChildren extends StatelessWidget {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment