Unverified Commit b8833afc authored by Tong Mu's avatar Tong Mu Committed by GitHub

Migrate LogicalKeySet to SingleActivator (#80756)

Applies #78522 to existing code, by replacing all occurrences of LogicalKeySet to SingleActivator or ShortcutActivator.
parent 635c997a
......@@ -406,9 +406,9 @@ class _FocusDemoState extends State<FocusDemo> {
child: FocusTraversalGroup(
policy: ReadingOrderTraversalPolicy(),
child: Shortcuts(
shortcuts: <LogicalKeySet, Intent>{
LogicalKeySet(Platform.isMacOS ? LogicalKeyboardKey.meta : LogicalKeyboardKey.control, LogicalKeyboardKey.shift, LogicalKeyboardKey.keyZ): const RedoIntent(),
LogicalKeySet(Platform.isMacOS ? LogicalKeyboardKey.meta : LogicalKeyboardKey.control, LogicalKeyboardKey.keyZ): const UndoIntent(),
shortcuts: <ShortcutActivator, Intent>{
SingleActivator(LogicalKeyboardKey.keyZ, meta: Platform.isMacOS, control: !Platform.isMacOS, shift: true): const RedoIntent(),
SingleActivator(LogicalKeyboardKey.keyZ, meta: Platform.isMacOS, control: !Platform.isMacOS): const UndoIntent(),
},
child: FocusScope(
key: FocusDemo.appKey,
......
......@@ -346,9 +346,9 @@ class CupertinoApp extends StatefulWidget {
/// ```dart
/// Widget build(BuildContext context) {
/// return WidgetsApp(
/// shortcuts: <LogicalKeySet, Intent>{
/// shortcuts: <ShortcutActivator, Intent>{
/// ... WidgetsApp.defaultShortcuts,
/// LogicalKeySet(LogicalKeyboardKey.select): const ActivateIntent(),
/// const SingleActivator(LogicalKeyboardKey.select): const ActivateIntent(),
/// },
/// color: const Color(0xFFFF0000),
/// builder: (BuildContext context, Widget? child) {
......@@ -359,7 +359,7 @@ class CupertinoApp extends StatefulWidget {
/// ```
/// {@end-tool}
/// {@macro flutter.widgets.widgetsApp.shortcuts.seeAlso}
final Map<LogicalKeySet, Intent>? shortcuts;
final Map<ShortcutActivator, Intent>? shortcuts;
/// {@macro flutter.widgets.widgetsApp.actions}
/// {@tool snippet}
......
......@@ -584,9 +584,9 @@ class MaterialApp extends StatefulWidget {
/// ```dart
/// Widget build(BuildContext context) {
/// return WidgetsApp(
/// shortcuts: <LogicalKeySet, Intent>{
/// shortcuts: <ShortcutActivator, Intent>{
/// ... WidgetsApp.defaultShortcuts,
/// LogicalKeySet(LogicalKeyboardKey.select): const ActivateIntent(),
/// const SingleActivator(LogicalKeyboardKey.select): const ActivateIntent(),
/// },
/// color: const Color(0xFFFF0000),
/// builder: (BuildContext context, Widget? child) {
......@@ -597,7 +597,7 @@ class MaterialApp extends StatefulWidget {
/// ```
/// {@end-tool}
/// {@macro flutter.widgets.widgetsApp.shortcuts.seeAlso}
final Map<LogicalKeySet, Intent>? shortcuts;
final Map<ShortcutActivator, Intent>? shortcuts;
/// {@macro flutter.widgets.widgetsApp.actions}
/// {@tool snippet}
......
......@@ -495,7 +495,7 @@ class _MonthPickerState extends State<_MonthPicker> {
late PageController _pageController;
late MaterialLocalizations _localizations;
late TextDirection _textDirection;
Map<LogicalKeySet, Intent>? _shortcutMap;
Map<ShortcutActivator, Intent>? _shortcutMap;
Map<Type, Action<Intent>>? _actionMap;
late FocusNode _dayGridFocus;
DateTime? _focusedDay;
......@@ -507,11 +507,11 @@ class _MonthPickerState extends State<_MonthPicker> {
_previousMonthDate = DateUtils.addMonthsToMonthDate(_currentMonth, -1);
_nextMonthDate = DateUtils.addMonthsToMonthDate(_currentMonth, 1);
_pageController = PageController(initialPage: DateUtils.monthDelta(widget.firstDate, _currentMonth));
_shortcutMap = <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.arrowLeft): const DirectionalFocusIntent(TraversalDirection.left),
LogicalKeySet(LogicalKeyboardKey.arrowRight): const DirectionalFocusIntent(TraversalDirection.right),
LogicalKeySet(LogicalKeyboardKey.arrowDown): const DirectionalFocusIntent(TraversalDirection.down),
LogicalKeySet(LogicalKeyboardKey.arrowUp): const DirectionalFocusIntent(TraversalDirection.up),
_shortcutMap = const <ShortcutActivator, Intent>{
SingleActivator(LogicalKeyboardKey.arrowLeft): DirectionalFocusIntent(TraversalDirection.left),
SingleActivator(LogicalKeyboardKey.arrowRight): DirectionalFocusIntent(TraversalDirection.right),
SingleActivator(LogicalKeyboardKey.arrowDown): DirectionalFocusIntent(TraversalDirection.down),
SingleActivator(LogicalKeyboardKey.arrowUp): DirectionalFocusIntent(TraversalDirection.up),
};
_actionMap = <Type, Action<Intent>>{
NextFocusIntent: CallbackAction<NextFocusIntent>(onInvoke: _handleGridNextFocus),
......
......@@ -496,9 +496,9 @@ class _DatePickerDialogState extends State<DatePickerDialog> with RestorationMix
}
}
static final Map<LogicalKeySet, Intent> _formShortcutMap = <LogicalKeySet, Intent>{
static const Map<ShortcutActivator, Intent> _formShortcutMap = <ShortcutActivator, Intent>{
// Pressing enter on the field will move focus to the next field or control.
LogicalKeySet(LogicalKeyboardKey.enter): const NextFocusIntent(),
SingleActivator(LogicalKeyboardKey.enter): NextFocusIntent(),
};
@override
......@@ -1902,7 +1902,12 @@ class _CalendarKeyboardNavigator extends StatefulWidget {
class _CalendarKeyboardNavigatorState extends State<_CalendarKeyboardNavigator> {
late Map<LogicalKeySet, Intent> _shortcutMap;
final Map<ShortcutActivator, Intent> _shortcutMap = const <ShortcutActivator, Intent>{
SingleActivator(LogicalKeyboardKey.arrowLeft): DirectionalFocusIntent(TraversalDirection.left),
SingleActivator(LogicalKeyboardKey.arrowRight): DirectionalFocusIntent(TraversalDirection.right),
SingleActivator(LogicalKeyboardKey.arrowDown): DirectionalFocusIntent(TraversalDirection.down),
SingleActivator(LogicalKeyboardKey.arrowUp): DirectionalFocusIntent(TraversalDirection.up),
};
late Map<Type, Action<Intent>> _actionMap;
late FocusNode _dayGridFocus;
TraversalDirection? _dayTraversalDirection;
......@@ -1912,12 +1917,6 @@ class _CalendarKeyboardNavigatorState extends State<_CalendarKeyboardNavigator>
void initState() {
super.initState();
_shortcutMap = <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.arrowLeft): const DirectionalFocusIntent(TraversalDirection.left),
LogicalKeySet(LogicalKeyboardKey.arrowRight): const DirectionalFocusIntent(TraversalDirection.right),
LogicalKeySet(LogicalKeyboardKey.arrowDown): const DirectionalFocusIntent(TraversalDirection.down),
LogicalKeySet(LogicalKeyboardKey.arrowUp): const DirectionalFocusIntent(TraversalDirection.up),
};
_actionMap = <Type, Action<Intent>>{
NextFocusIntent: CallbackAction<NextFocusIntent>(onInvoke: _handleGridNextFocus),
PreviousFocusIntent: CallbackAction<PreviousFocusIntent>(onInvoke: _handleGridPreviousFocus),
......
......@@ -148,11 +148,11 @@ class _DropdownMenuItemButtonState<T> extends State<_DropdownMenuItemButton<T>>
);
}
static final Map<LogicalKeySet, Intent> _webShortcuts =<LogicalKeySet, Intent>{
static const Map<ShortcutActivator, Intent> _webShortcuts = <ShortcutActivator, Intent>{
// On the web, up/down don't change focus, *except* in a <select>
// element, which is what a dropdown emulates.
LogicalKeySet(LogicalKeyboardKey.arrowDown): const DirectionalFocusIntent(TraversalDirection.down),
LogicalKeySet(LogicalKeyboardKey.arrowUp): const DirectionalFocusIntent(TraversalDirection.up),
SingleActivator(LogicalKeyboardKey.arrowDown): DirectionalFocusIntent(TraversalDirection.down),
SingleActivator(LogicalKeyboardKey.arrowUp): DirectionalFocusIntent(TraversalDirection.up),
};
@override
......
......@@ -477,7 +477,12 @@ class _SliderState extends State<Slider> with TickerProviderStateMixin {
final GlobalKey _renderObjectKey = GlobalKey();
// Keyboard mapping for a focused slider.
late Map<LogicalKeySet, Intent> _shortcutMap;
final Map<ShortcutActivator, Intent> _shortcutMap = const <ShortcutActivator, Intent>{
SingleActivator(LogicalKeyboardKey.arrowUp): _AdjustSliderIntent.up(),
SingleActivator(LogicalKeyboardKey.arrowDown): _AdjustSliderIntent.down(),
SingleActivator(LogicalKeyboardKey.arrowLeft): _AdjustSliderIntent.left(),
SingleActivator(LogicalKeyboardKey.arrowRight): _AdjustSliderIntent.right(),
};
// Action mapping for a focused slider.
late Map<Type, Action<Intent>> _actionMap;
......@@ -506,12 +511,6 @@ class _SliderState extends State<Slider> with TickerProviderStateMixin {
);
enableController.value = widget.onChanged != null ? 1.0 : 0.0;
positionController.value = _unlerp(widget.value);
_shortcutMap = <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.arrowUp): const _AdjustSliderIntent.up(),
LogicalKeySet(LogicalKeyboardKey.arrowDown): const _AdjustSliderIntent.down(),
LogicalKeySet(LogicalKeyboardKey.arrowLeft): const _AdjustSliderIntent.left(),
LogicalKeySet(LogicalKeyboardKey.arrowRight): const _AdjustSliderIntent.right(),
};
_actionMap = <Type, Action<Intent>>{
_AdjustSliderIntent: CallbackAction<_AdjustSliderIntent>(
onInvoke: _actionHandler,
......
......@@ -81,9 +81,9 @@ export 'package:flutter/services.dart' show SmartQuotesType, SmartDashesType;
/// return Material(
/// child: Center(
/// child: Shortcuts(
/// shortcuts: <LogicalKeySet, Intent>{
/// shortcuts: const <ShortcutActivator, Intent>{
/// // Pressing space in the field will now move to the next field.
/// LogicalKeySet(LogicalKeyboardKey.space): const NextFocusIntent(),
/// SingleActivator(LogicalKeyboardKey.space): NextFocusIntent(),
/// },
/// child: FocusTraversalGroup(
/// child: Form(
......
......@@ -1136,8 +1136,10 @@ class _ActionsMarker extends InheritedWidget {
/// bool _focused = false;
/// bool _hovering = false;
/// bool _on = false;
/// late Map<Type, Action<Intent>> _actionMap;
/// late Map<LogicalKeySet, Intent> _shortcutMap;
/// late final Map<Type, Action<Intent>> _actionMap;
/// final Map<ShortcutActivator, Intent> _shortcutMap = const <ShortcutActivator, Intent>{
/// SingleActivator(LogicalKeyboardKey.keyX): ActivateIntent(),
/// };
///
/// @override
/// void initState() {
......@@ -1147,9 +1149,6 @@ class _ActionsMarker extends InheritedWidget {
/// onInvoke: (Intent intent) => _toggleState(),
/// ),
/// };
/// _shortcutMap = <LogicalKeySet, Intent>{
/// LogicalKeySet(LogicalKeyboardKey.keyX): const ActivateIntent(),
/// };
/// }
///
/// Color get color {
......@@ -1287,7 +1286,7 @@ class FocusableActionDetector extends StatefulWidget {
final Map<Type, Action<Intent>>? actions;
/// {@macro flutter.widgets.shortcuts.shortcuts}
final Map<LogicalKeySet, Intent>? shortcuts;
final Map<ShortcutActivator, Intent>? shortcuts;
/// A function that will be called when the focus highlight should be shown or
/// hidden.
......
This diff is collapsed.
......@@ -355,7 +355,7 @@ class LogicalKeySet extends KeySet<LogicalKeyboardKey> with Diagnosticable
/// Intent>` (the same type as the [Shortcuts.shortcuts] property) so that its
/// diagnostic output is human-readable.
class ShortcutMapProperty extends DiagnosticsProperty<Map<ShortcutActivator, Intent>> {
/// Create a diagnostics property for `Map<LogicalKeySet, Intent>` objects,
/// Create a diagnostics property for `Map<ShortcutActivator, Intent>` objects,
/// which are the same type as the [Shortcuts.shortcuts] property.
ShortcutMapProperty(
String name,
......
......@@ -260,8 +260,8 @@ void main() {
Future<void> buildTest(Intent intent) async {
return tester.pumpWidget(
Shortcuts(
shortcuts: <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.space): intent,
shortcuts: <ShortcutActivator, Intent>{
const SingleActivator(LogicalKeyboardKey.space): intent,
},
child: Directionality(
textDirection: TextDirection.ltr,
......
......@@ -82,9 +82,9 @@ void main() {
await tester.pumpWidget(Directionality(
textDirection: TextDirection.ltr,
child: Shortcuts(
shortcuts: <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.space): const ActivateIntent(),
LogicalKeySet(LogicalKeyboardKey.enter): const ButtonActivateIntent(),
shortcuts: const <ShortcutActivator, Intent>{
SingleActivator(LogicalKeyboardKey.space): ActivateIntent(),
SingleActivator(LogicalKeyboardKey.enter): ButtonActivateIntent(),
},
child: Material(
child: Center(
......
......@@ -46,9 +46,9 @@ void main() {
const Color splashColor = Color(0xff00ff00);
await tester.pumpWidget(
Shortcuts(
shortcuts: <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.enter): const ActivateIntent(),
LogicalKeySet(LogicalKeyboardKey.space): const ActivateIntent(),
shortcuts: const <ShortcutActivator, Intent>{
SingleActivator(LogicalKeyboardKey.enter): ActivateIntent(),
SingleActivator(LogicalKeyboardKey.space): ActivateIntent(),
},
child: Directionality(
textDirection: TextDirection.ltr,
......
......@@ -414,8 +414,8 @@ void main() {
child: FocusableActionDetector(
enabled: enabled,
focusNode: focusNode,
shortcuts: <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.enter): intent,
shortcuts: const <ShortcutActivator, Intent>{
SingleActivator(LogicalKeyboardKey.enter): intent,
},
actions: <Type, Action<Intent>>{
TestIntent: testAction,
......@@ -789,8 +789,8 @@ void main() {
child: FocusableActionDetector(
enabled: enabled,
focusNode: focusNode,
shortcuts: <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.enter): intent,
shortcuts: const <ShortcutActivator, Intent>{
SingleActivator(LogicalKeyboardKey.enter): intent,
},
actions: <Type, Action<Intent>>{
TestIntent: testAction,
......
......@@ -76,8 +76,8 @@ void main() {
actions: <Type, Action<Intent>>{
TestIntent: action,
},
shortcuts: <LogicalKeySet, Intent> {
LogicalKeySet(LogicalKeyboardKey.space): const TestIntent(),
shortcuts: const <ShortcutActivator, Intent> {
SingleActivator(LogicalKeyboardKey.space): TestIntent(),
},
builder: (BuildContext context, Widget? child) {
return Material(
......
......@@ -6,8 +6,12 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
class TestLeftIntent extends Intent {}
class TestRightIntent extends Intent {}
class TestLeftIntent extends Intent {
const TestLeftIntent();
}
class TestRightIntent extends Intent {
const TestRightIntent();
}
void main() {
testWidgets('DoNothingAndStopPropagationTextIntent', (WidgetTester tester) async {
......@@ -25,13 +29,13 @@ void main() {
body: Builder(
builder: (BuildContext context) {
return Shortcuts(
shortcuts: <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.arrowLeft): TestLeftIntent(),
LogicalKeySet(LogicalKeyboardKey.arrowRight): TestRightIntent(),
shortcuts: const <ShortcutActivator, Intent>{
SingleActivator(LogicalKeyboardKey.arrowLeft): TestLeftIntent(),
SingleActivator(LogicalKeyboardKey.arrowRight): TestRightIntent(),
},
child: Shortcuts(
shortcuts: <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.arrowRight): const DoNothingAndStopPropagationTextIntent(),
shortcuts: const <ShortcutActivator, Intent>{
SingleActivator(LogicalKeyboardKey.arrowRight): DoNothingAndStopPropagationTextIntent(),
},
child: Actions(
// These Actions intercept default Intents, set a flag that they
......
......@@ -7074,8 +7074,8 @@ void main() {
child: SizedBox(
width: 400,
child: Shortcuts(
shortcuts: <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.arrowLeft): const MoveSelectionRightTextIntent(),
shortcuts: const <ShortcutActivator, Intent>{
SingleActivator(LogicalKeyboardKey.arrowLeft): MoveSelectionRightTextIntent(),
},
child: EditableText(
maxLines: 10,
......
......@@ -1893,11 +1893,11 @@ void main() {
final FocusNode focusNodeLowerRight = FocusNode(debugLabel: 'lowerRight');
Widget generateTestWidgets(bool ignoreTextFields) {
final Map<LogicalKeySet, Intent> shortcuts = <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.arrowLeft): DirectionalFocusIntent(TraversalDirection.left, ignoreTextFields: ignoreTextFields),
LogicalKeySet(LogicalKeyboardKey.arrowRight): DirectionalFocusIntent(TraversalDirection.right, ignoreTextFields: ignoreTextFields),
LogicalKeySet(LogicalKeyboardKey.arrowDown): DirectionalFocusIntent(TraversalDirection.down, ignoreTextFields: ignoreTextFields),
LogicalKeySet(LogicalKeyboardKey.arrowUp): DirectionalFocusIntent(TraversalDirection.up, ignoreTextFields: ignoreTextFields),
final Map<ShortcutActivator, Intent> shortcuts = <ShortcutActivator, Intent>{
const SingleActivator(LogicalKeyboardKey.arrowLeft): DirectionalFocusIntent(TraversalDirection.left, ignoreTextFields: ignoreTextFields),
const SingleActivator(LogicalKeyboardKey.arrowRight): DirectionalFocusIntent(TraversalDirection.right, ignoreTextFields: ignoreTextFields),
const SingleActivator(LogicalKeyboardKey.arrowDown): DirectionalFocusIntent(TraversalDirection.down, ignoreTextFields: ignoreTextFields),
const SingleActivator(LogicalKeyboardKey.arrowUp): DirectionalFocusIntent(TraversalDirection.up, ignoreTextFields: ignoreTextFields),
};
return MaterialApp(
......
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