Unverified Commit 909e529e authored by Greg Spencer's avatar Greg Spencer Committed by GitHub

Convert some usage of `RawKeyEvent`, et al to `KeyEvent` (#139329)

## Description

This converts some uses of `RawKeyEvent` to `KeyEvent` in preparation for deprecating `RawKeyEvent`.

## Related Issues
 - https://github.com/flutter/flutter/issues/136419
parent 2d60241d
......@@ -1079,8 +1079,9 @@ class _MaterialAppState extends State<MaterialApp> {
Widget result = _buildWidgetApp(context);
result = Focus(
canRequestFocus: false,
onKey: (FocusNode node, RawKeyEvent event) {
if (event is! RawKeyDownEvent || event.logicalKey != LogicalKeyboardKey.escape) {
onKeyEvent: (FocusNode node, KeyEvent event) {
if ((event is! KeyDownEvent && event is! KeyRepeatEvent) ||
event.logicalKey != LogicalKeyboardKey.escape) {
return KeyEventResult.ignored;
}
return Tooltip.dismissAllToolTips() ? KeyEventResult.handled : KeyEventResult.ignored;
......
......@@ -31,143 +31,6 @@ import '../widgets/live_text_utils.dart';
import '../widgets/semantics_tester.dart';
import 'feedback_tester.dart';
typedef FormatEditUpdateCallback = void Function(TextEditingValue, TextEditingValue);
// On web, key events in text fields are handled by the browser.
const bool areKeyEventsHandledByPlatform = isBrowser;
class CupertinoLocalizationsDelegate extends LocalizationsDelegate<CupertinoLocalizations> {
@override
bool isSupported(Locale locale) => true;
@override
Future<CupertinoLocalizations> load(Locale locale) =>
DefaultCupertinoLocalizations.load(locale);
@override
bool shouldReload(CupertinoLocalizationsDelegate old) => false;
}
class MaterialLocalizationsDelegate extends LocalizationsDelegate<MaterialLocalizations> {
@override
bool isSupported(Locale locale) => true;
@override
Future<MaterialLocalizations> load(Locale locale) => DefaultMaterialLocalizations.load(locale);
@override
bool shouldReload(MaterialLocalizationsDelegate old) => false;
}
class WidgetsLocalizationsDelegate extends LocalizationsDelegate<WidgetsLocalizations> {
@override
bool isSupported(Locale locale) => true;
@override
Future<WidgetsLocalizations> load(Locale locale) => DefaultWidgetsLocalizations.load(locale);
@override
bool shouldReload(WidgetsLocalizationsDelegate old) => false;
}
Widget overlay({ required Widget child }) {
final OverlayEntry entry = OverlayEntry(
builder: (BuildContext context) {
return Center(
child: Material(
child: child,
),
);
},
);
addTearDown(() => entry..remove()..dispose());
return overlayWithEntry(entry);
}
Widget overlayWithEntry(OverlayEntry entry) {
return Localizations(
locale: const Locale('en', 'US'),
delegates: <LocalizationsDelegate<dynamic>>[
WidgetsLocalizationsDelegate(),
MaterialLocalizationsDelegate(),
CupertinoLocalizationsDelegate(),
],
child: DefaultTextEditingShortcuts(
child: Directionality(
textDirection: TextDirection.ltr,
child: MediaQuery(
data: const MediaQueryData(size: Size(800.0, 600.0)),
child: Overlay(
initialEntries: <OverlayEntry>[
entry,
],
),
),
),
),
);
}
Widget boilerplate({ required Widget child, ThemeData? theme }) {
return MaterialApp(
theme: theme,
home: Localizations(
locale: const Locale('en', 'US'),
delegates: <LocalizationsDelegate<dynamic>>[
WidgetsLocalizationsDelegate(),
MaterialLocalizationsDelegate(),
],
child: Directionality(
textDirection: TextDirection.ltr,
child: MediaQuery(
data: const MediaQueryData(size: Size(800.0, 600.0)),
child: Center(
child: Material(
child: child,
),
),
),
),
),
);
}
Future<void> skipPastScrollingAnimation(WidgetTester tester) async {
await tester.pump();
await tester.pump(const Duration(milliseconds: 200));
}
double getOpacity(WidgetTester tester, Finder finder) {
return tester.widget<FadeTransition>(
find.ancestor(
of: finder,
matching: find.byType(FadeTransition),
),
).opacity.value;
}
class TestFormatter extends TextInputFormatter {
TestFormatter(this.onFormatEditUpdate);
FormatEditUpdateCallback onFormatEditUpdate;
@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
onFormatEditUpdate(oldValue, newValue);
return newValue;
}
}
FocusNode _focusNode() {
final FocusNode result = FocusNode();
addTearDown(result.dispose);
return result;
}
TextEditingController _textEditingController({String text = ''}) {
final TextEditingController result = TextEditingController(text: text);
addTearDown(result.dispose);
return result;
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final MockClipboard mockClipboard = MockClipboard();
......@@ -7127,7 +6990,7 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: Material(
child: RawKeyboardListener(
child: KeyboardListener(
focusNode: focusNode,
child: TextField(
controller: controller,
......@@ -7320,7 +7183,7 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: Material(
child: RawKeyboardListener(
child: KeyboardListener(
focusNode: focusNode,
child: textField,
),
......@@ -7394,7 +7257,7 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: Material(
child: RawKeyboardListener(
child: KeyboardListener(
focusNode: focusNode,
child: textField,
),
......@@ -7449,7 +7312,7 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: Material(
child: RawKeyboardListener(
child: KeyboardListener(
focusNode: focusNode,
child: textField,
),
......@@ -7517,7 +7380,7 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: Material(
child: RawKeyboardListener(
child: KeyboardListener(
focusNode: focusNode,
child: textField,
),
......@@ -7568,7 +7431,7 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: Material(
child: RawKeyboardListener(
child: KeyboardListener(
focusNode: focusNode,
child: textField,
),
......@@ -7613,7 +7476,7 @@ void main() {
testWidgetsWithLeakTracking('Changing positions of text fields', (WidgetTester tester) async {
final FocusNode focusNode = _focusNode();
final List<RawKeyEvent> events = <RawKeyEvent>[];
final List<KeyEvent> events = <KeyEvent>[];
final TextEditingController c1 = _textEditingController();
final TextEditingController c2 = _textEditingController();
......@@ -7624,9 +7487,9 @@ void main() {
MaterialApp(
home:
Material(
child: RawKeyboardListener(
child: KeyboardListener(
focusNode: focusNode,
onKey: events.add,
onKeyEvent: events.add,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
......@@ -7669,9 +7532,9 @@ void main() {
MaterialApp(
home:
Material(
child: RawKeyboardListener(
child: KeyboardListener(
focusNode: focusNode,
onKey: events.add,
onKeyEvent: events.add,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
......@@ -7707,7 +7570,7 @@ void main() {
testWidgetsWithLeakTracking('Changing focus test', (WidgetTester tester) async {
final FocusNode focusNode = _focusNode();
final List<RawKeyEvent> events = <RawKeyEvent>[];
final List<KeyEvent> events = <KeyEvent>[];
final TextEditingController c1 = _textEditingController();
final TextEditingController c2 = _textEditingController();
......@@ -7718,9 +7581,9 @@ void main() {
MaterialApp(
home:
Material(
child: RawKeyboardListener(
child: KeyboardListener(
focusNode: focusNode,
onKey: events.add,
onKeyEvent: events.add,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
......@@ -17238,3 +17101,141 @@ class _ObscureTextTestWidgetState extends State<_ObscureTextTestWidget> {
);
}
}
typedef FormatEditUpdateCallback = void Function(TextEditingValue, TextEditingValue);
// On web, key events in text fields are handled by the browser.
const bool areKeyEventsHandledByPlatform = isBrowser;
class CupertinoLocalizationsDelegate extends LocalizationsDelegate<CupertinoLocalizations> {
@override
bool isSupported(Locale locale) => true;
@override
Future<CupertinoLocalizations> load(Locale locale) =>
DefaultCupertinoLocalizations.load(locale);
@override
bool shouldReload(CupertinoLocalizationsDelegate old) => false;
}
class MaterialLocalizationsDelegate extends LocalizationsDelegate<MaterialLocalizations> {
@override
bool isSupported(Locale locale) => true;
@override
Future<MaterialLocalizations> load(Locale locale) => DefaultMaterialLocalizations.load(locale);
@override
bool shouldReload(MaterialLocalizationsDelegate old) => false;
}
class WidgetsLocalizationsDelegate extends LocalizationsDelegate<WidgetsLocalizations> {
@override
bool isSupported(Locale locale) => true;
@override
Future<WidgetsLocalizations> load(Locale locale) => DefaultWidgetsLocalizations.load(locale);
@override
bool shouldReload(WidgetsLocalizationsDelegate old) => false;
}
Widget overlay({ required Widget child }) {
final OverlayEntry entry = OverlayEntry(
builder: (BuildContext context) {
return Center(
child: Material(
child: child,
),
);
},
);
addTearDown(() => entry..remove()..dispose());
return overlayWithEntry(entry);
}
Widget overlayWithEntry(OverlayEntry entry) {
return Localizations(
locale: const Locale('en', 'US'),
delegates: <LocalizationsDelegate<dynamic>>[
WidgetsLocalizationsDelegate(),
MaterialLocalizationsDelegate(),
CupertinoLocalizationsDelegate(),
],
child: DefaultTextEditingShortcuts(
child: Directionality(
textDirection: TextDirection.ltr,
child: MediaQuery(
data: const MediaQueryData(size: Size(800.0, 600.0)),
child: Overlay(
initialEntries: <OverlayEntry>[
entry,
],
),
),
),
),
);
}
Widget boilerplate({ required Widget child, ThemeData? theme }) {
return MaterialApp(
theme: theme,
home: Localizations(
locale: const Locale('en', 'US'),
delegates: <LocalizationsDelegate<dynamic>>[
WidgetsLocalizationsDelegate(),
MaterialLocalizationsDelegate(),
],
child: Directionality(
textDirection: TextDirection.ltr,
child: MediaQuery(
data: const MediaQueryData(size: Size(800.0, 600.0)),
child: Center(
child: Material(
child: child,
),
),
),
),
),
);
}
Future<void> skipPastScrollingAnimation(WidgetTester tester) async {
await tester.pump();
await tester.pump(const Duration(milliseconds: 200));
}
double getOpacity(WidgetTester tester, Finder finder) {
return tester.widget<FadeTransition>(
find.ancestor(
of: finder,
matching: find.byType(FadeTransition),
),
).opacity.value;
}
class TestFormatter extends TextInputFormatter {
TestFormatter(this.onFormatEditUpdate);
FormatEditUpdateCallback onFormatEditUpdate;
@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
onFormatEditUpdate(oldValue, newValue);
return newValue;
}
}
FocusNode _focusNode() {
final FocusNode result = FocusNode();
addTearDown(result.dispose);
return result;
}
TextEditingController _textEditingController({String text = ''}) {
final TextEditingController result = TextEditingController(text: text);
addTearDown(result.dispose);
return result;
}
......@@ -2809,8 +2809,9 @@ void main() {
await tester.pumpWidget(MaterialApp(home: Container()));
RawKeyboard.instance.addListener((RawKeyEvent event) {
HardwareKeyboard.instance.addHandler((KeyEvent event) {
events.add(event);
return true;
});
await tester.idle();
......@@ -2829,7 +2830,7 @@ void main() {
});
testWidgetsWithLeakTracking('Focus traversal does not break when no focusable is available on a WidgetsApp', (WidgetTester tester) async {
final List<RawKeyEvent> events = <RawKeyEvent>[];
final List<KeyEvent> events = <KeyEvent>[];
await tester.pumpWidget(
WidgetsApp(
......@@ -2843,8 +2844,9 @@ void main() {
),
);
RawKeyboard.instance.addListener((RawKeyEvent event) {
HardwareKeyboard.instance.addHandler((KeyEvent event) {
events.add(event);
return true;
});
await tester.idle();
......
......@@ -1895,7 +1895,7 @@ void main() {
testWidgetsWithLeakTracking('Changing positions of selectable text', (WidgetTester tester) async {
final FocusNode focusNode = FocusNode();
addTearDown(focusNode.dispose);
final List<RawKeyEvent> events = <RawKeyEvent>[];
final List<KeyEvent> events = <KeyEvent>[];
final Key key1 = UniqueKey();
final Key key2 = UniqueKey();
......@@ -1904,9 +1904,9 @@ void main() {
MaterialApp(
home:
Material(
child: RawKeyboardListener(
child: KeyboardListener(
focusNode: focusNode,
onKey: events.add,
onKeyEvent: events.add,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
......@@ -1947,9 +1947,9 @@ void main() {
MaterialApp(
home:
Material(
child: RawKeyboardListener(
child: KeyboardListener(
focusNode: focusNode,
onKey: events.add,
onKeyEvent: events.add,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
......@@ -1986,7 +1986,7 @@ void main() {
testWidgetsWithLeakTracking('Changing focus test', (WidgetTester tester) async {
final FocusNode focusNode = FocusNode();
addTearDown(focusNode.dispose);
final List<RawKeyEvent> events = <RawKeyEvent>[];
final List<KeyEvent> events = <KeyEvent>[];
final Key key1 = UniqueKey();
final Key key2 = UniqueKey();
......@@ -1995,9 +1995,9 @@ void main() {
MaterialApp(
home:
Material(
child: RawKeyboardListener(
child: KeyboardListener(
focusNode: focusNode,
onKey: events.add,
onKeyEvent: events.add,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
......
......@@ -175,7 +175,7 @@ void main() {
expect(invoked, 0);
invoked = 0;
expect(RawKeyboard.instance.keysPressed, isEmpty);
expect(HardwareKeyboard.instance.logicalKeysPressed, isEmpty);
});
test('LogicalKeySet.hashCode is stable', () {
......@@ -352,7 +352,7 @@ void main() {
expect(invoked, 1);
invoked = 0;
expect(RawKeyboard.instance.keysPressed, isEmpty);
expect(HardwareKeyboard.instance.logicalKeysPressed, isEmpty);
}, variant: KeySimulatorTransitModeVariant.all());
testWidgetsWithLeakTracking('handles repeated events', (WidgetTester tester) async {
......@@ -378,7 +378,7 @@ void main() {
expect(invoked, 2);
invoked = 0;
expect(RawKeyboard.instance.keysPressed, isEmpty);
expect(HardwareKeyboard.instance.logicalKeysPressed, isEmpty);
}, variant: KeySimulatorTransitModeVariant.all());
testWidgetsWithLeakTracking('rejects repeated events if requested', (WidgetTester tester) async {
......@@ -405,7 +405,7 @@ void main() {
expect(invoked, 1);
invoked = 0;
expect(RawKeyboard.instance.keysPressed, isEmpty);
expect(HardwareKeyboard.instance.logicalKeysPressed, isEmpty);
}, variant: KeySimulatorTransitModeVariant.all());
testWidgetsWithLeakTracking('handles Shift-Ctrl-C', (WidgetTester tester) async {
......@@ -455,7 +455,7 @@ void main() {
expect(invoked, 0);
invoked = 0;
expect(RawKeyboard.instance.keysPressed, isEmpty);
expect(HardwareKeyboard.instance.logicalKeysPressed, isEmpty);
});
testWidgetsWithLeakTracking('isActivatedBy works as expected', (WidgetTester tester) async {
......
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