Factor out RawView, make View listen to engine generated view focus events (#143259)

## Description

This factors out a separate `RawView` that doesn't add a `MediaQuery` or a `FocusScope`. This PR also adds a new method `WidgetsBindingObserver.didChangeViewFocus` which allows the observer to know when the `FlutterView` that has focus has changed.

It also makes the `View` widget a stateful widget that contains a `FocusScope` and ` FocusTraversalGroup` so that it can respond to changes in the focus of the view.

I've also added a new function to `FocusScopeNode` that will allow the scope node itself to be focused, without looking for descendants that could take the focus. This lets the focus be "parked" at the `FocusManager.instance.rootScope` so that nothing else appears to have focus.

## Tests
 - Added tests for the new functionality.
This commit is contained in:
Greg Spencer
2024-05-20 10:17:55 -07:00
committed by GitHub
parent 72f06d2bf3
commit 333c076e53
17 changed files with 588 additions and 175 deletions

View File

@@ -154,6 +154,7 @@ class TestPlatformDispatcher implements PlatformDispatcher {
}) : _platformDispatcher = platformDispatcher {
_updateViewsAndDisplays();
_platformDispatcher.onMetricsChanged = _handleMetricsChanged;
_platformDispatcher.onViewFocusChange = _handleViewFocusChanged;
}
/// The [PlatformDispatcher] that is wrapped by this [TestPlatformDispatcher].
@@ -176,12 +177,23 @@ class TestPlatformDispatcher implements PlatformDispatcher {
set onMetricsChanged(VoidCallback? callback) {
_onMetricsChanged = callback;
}
void _handleMetricsChanged() {
_updateViewsAndDisplays();
_onMetricsChanged?.call();
}
@override
ViewFocusChangeCallback? get onViewFocusChange => _platformDispatcher.onViewFocusChange;
ViewFocusChangeCallback? _onViewFocusChange;
@override
set onViewFocusChange(ViewFocusChangeCallback? callback) {
_onViewFocusChange = callback;
}
void _handleViewFocusChanged(ViewFocusEvent event) {
_updateViewsAndDisplays();
_onViewFocusChange?.call(event);
}
@override
Locale get locale => _localeTestValue ?? _platformDispatcher.locale;
Locale? _localeTestValue;

View File

@@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:ui' show AccessibilityFeatures, Brightness, Display, FlutterView, Locale, PlatformDispatcher, VoidCallback;
import 'dart:ui' show AccessibilityFeatures, Brightness, Display, FlutterView,
Locale, PlatformDispatcher, ViewFocusChangeCallback, VoidCallback;
import 'package:flutter/widgets.dart' show WidgetsBinding, WidgetsBindingObserver;
import 'package:flutter_test/flutter_test.dart';
@@ -303,4 +304,7 @@ class _FakePlatformDispatcher extends Fake implements PlatformDispatcher {
@override
VoidCallback? onMetricsChanged;
@override
ViewFocusChangeCallback? onViewFocusChange;
}