Update Highlight mode initial value calculation. (#52990)

This commit is contained in:
Greg Spencer 2020-04-03 20:26:01 -07:00 committed by GitHub
parent 14f3a36aba
commit a2e6c30b44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 9 deletions

View File

@ -3,7 +3,6 @@
// found in the LICENSE file.
import 'dart:async';
import 'dart:io';
import 'dart:ui';
import 'package:flutter/foundation.dart';
@ -1368,7 +1367,34 @@ class FocusManager with DiagnosticableTreeMixin, ChangeNotifier {
/// the [WidgetsBinding] instance.
static FocusManager get instance => WidgetsBinding.instance.focusManager;
bool _lastInteractionWasTouch = true;
bool get _lastInteractionWasTouch {
// Assume that if we're on one of these mobile platforms, or if there's no
// mouse connected, that the initial interaction will be touch-based, and
// that it's traditional mouse and keyboard on all others.
//
// This only affects the initial value: the ongoing value is updated to a
// known correct value as soon as any pointer events are received.
if (_lastInteractionWasTouchValue == null) {
switch (defaultTargetPlatform) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.iOS:
_lastInteractionWasTouchValue = !WidgetsBinding.instance.mouseTracker.mouseIsConnected;
break;
case TargetPlatform.linux:
case TargetPlatform.macOS:
case TargetPlatform.windows:
_lastInteractionWasTouchValue = false;
break;
}
}
return _lastInteractionWasTouchValue;
}
bool _lastInteractionWasTouchValue;
set _lastInteractionWasTouch(bool value) {
_lastInteractionWasTouchValue = value;
}
/// Sets the strategy by which [highlightMode] is determined.
///
@ -1418,13 +1444,6 @@ class FocusManager with DiagnosticableTreeMixin, ChangeNotifier {
// Update function to be called whenever the state relating to highlightMode
// changes.
void _updateHighlightMode() {
// Assume that if we're on one of these mobile platforms, or if there's no
// mouse connected, that the initial interaction will be touch-based, and
// that it's traditional mouse and keyboard on all others.
//
// This only affects the initial value: the ongoing value is updated as soon
// as any input events are received.
_lastInteractionWasTouch ??= Platform.isAndroid || Platform.isIOS || !WidgetsBinding.instance.mouseTracker.mouseIsConnected;
FocusHighlightMode newMode;
switch (highlightStrategy) {
case FocusHighlightStrategy.automatic:

View File

@ -828,6 +828,21 @@ void main() {
// receive it.
expect(receivedAnEvent, isEmpty);
});
testWidgets('Initial highlight mode guesses correctly.', (WidgetTester tester) async {
FocusManager.instance.highlightStrategy = FocusHighlightStrategy.automatic;
switch (defaultTargetPlatform) {
case TargetPlatform.fuchsia:
case TargetPlatform.android:
case TargetPlatform.iOS:
expect(FocusManager.instance.highlightMode, equals(FocusHighlightMode.touch));
break;
case TargetPlatform.linux:
case TargetPlatform.macOS:
case TargetPlatform.windows:
expect(FocusManager.instance.highlightMode, equals(FocusHighlightMode.traditional));
break;
}
}, variant: TargetPlatformVariant.all());
testWidgets('Events change focus highlight mode.', (WidgetTester tester) async {
await setupWidget(tester);
int callCount = 0;