Skip to content

Commit

Permalink
Revert "Removing Shorcuts.of and Shortctus.maybeOf (#104215)"
Browse files Browse the repository at this point in the history
This reverts commit da24f10.
  • Loading branch information
CaseyHillers committed May 26, 2022
1 parent 7e4ac03 commit 3c3e5fe
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 195 deletions.
4 changes: 0 additions & 4 deletions packages/flutter/lib/src/widgets/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ typedef ActionListenerCallback = void Function(Action<Intent> action);
/// developers to change that if they add an ancestor [Actions] widget that maps
/// [SelectAllTextIntent] to a different [Action].
///
/// See the article on [Using Actions and
/// Shortcuts](https://docs.flutter.dev/development/ui/advanced/actions_and_shortcuts)
/// for a detailed explanation.
///
/// See also:
///
/// * [Shortcuts], which is a widget that contains a key map, in which it looks
Expand Down
121 changes: 68 additions & 53 deletions packages/flutter/lib/src/widgets/shortcuts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -637,15 +637,13 @@ class _ActivatorIntentPair with Diagnosticable {
}
}

/// A manager of keyboard shortcut bindings used by [Shortcuts] to handle key
/// events.
/// A manager of keyboard shortcut bindings.
///
/// A `ShortcutManager` is obtained by calling [Shortcuts.of] on the context of
/// the widget that you want to find a manager for.
///
/// The manager may be listened to (with [addListener]/[removeListener]) for
/// change notifications when the shortcuts change.
///
/// Typically, a [Shortcuts] widget supplies its own manager, but in uncommon
/// cases where overriding the usual shortcut manager behavior is desired, a
/// subclassed [ShortcutManager] may be supplied.
class ShortcutManager with Diagnosticable, ChangeNotifier {
/// Constructs a [ShortcutManager].
ShortcutManager({
Expand Down Expand Up @@ -776,10 +774,6 @@ class ShortcutManager with Diagnosticable, ChangeNotifier {
/// when invoking an [Action] via a keyboard key combination that maps to an
/// [Intent].
///
/// See the article on [Using Actions and
/// Shortcuts](https://docs.flutter.dev/development/ui/advanced/actions_and_shortcuts)
/// for a detailed explanation.
///
/// {@tool dartpad}
/// Here, we will use the [Shortcuts] and [Actions] widgets to add and subtract
/// from a counter. When the child widget has keyboard focus, and a user presses
Expand Down Expand Up @@ -817,61 +811,35 @@ class ShortcutManager with Diagnosticable, ChangeNotifier {
/// * [Action], a class for defining an invocation of a user action.
/// * [CallbackAction], a class for creating an action from a callback.
class Shortcuts extends StatefulWidget {
/// Creates a const [Shortcuts] widget that owns the map of shortcuts and
/// creates its own manager.
///
/// When using this constructor, [manager] will return null.
/// Creates a const [Shortcuts] widget.
///
/// The [child] and [shortcuts] arguments are required.
///
/// See also:
///
/// * [Shortcuts.manager], a constructor that uses a [ShortcutManager] to
/// manage the shortcuts list instead.
const Shortcuts({
super.key,
required Map<ShortcutActivator, Intent> shortcuts,
required this.child,
this.debugLabel,
}) : _shortcuts = shortcuts,
manager = null,
assert(shortcuts != null),
assert(child != null);

/// Creates a const [Shortcuts] widget that uses the [manager] to
/// manage the map of shortcuts.
///
/// If this constructor is used, [shortcuts] will return the contents of
/// [ShortcutManager.shortcuts].
///
/// The [child] and [manager] arguments are required.
const Shortcuts.manager({
super.key,
required ShortcutManager this.manager,
this.manager,
required this.shortcuts,
required this.child,
this.debugLabel,
}) : _shortcuts = const <ShortcutActivator, Intent>{},
assert(manager != null),
}) : assert(shortcuts != null),
assert(child != null);

/// The [ShortcutManager] that will manage the mapping between key
/// combinations and [Action]s.
///
/// If this widget was created with [Shortcuts.manager], then
/// [ShortcutManager.shortcuts] will be used as the source for shortcuts. If
/// the unnamed constructor is used, this manager will be null, and a
/// default-constructed `ShortcutsManager` will be used.
/// If not specified, uses a default-constructed [ShortcutManager].
///
/// This manager will be given new [shortcuts] to manage whenever the
/// [shortcuts] change materially.
final ShortcutManager? manager;

/// {@template flutter.widgets.shortcuts.shortcuts}
/// The map of shortcuts that describes the mapping between a key sequence
/// defined by a [ShortcutActivator] and the [Intent] that will be emitted
/// when that key sequence is pressed.
/// The map of shortcuts that the [ShortcutManager] will be given to manage.
///
/// For performance reasons, it is recommended that a pre-built map is passed
/// in here (e.g. a final variable from your widget class) instead of defining
/// it inline in the build function.
/// {@endtemplate}
Map<ShortcutActivator, Intent> get shortcuts {
return manager == null ? _shortcuts : manager!.shortcuts;
}
final Map<ShortcutActivator, Intent> _shortcuts;
final Map<ShortcutActivator, Intent> shortcuts;

/// The child widget for this [Shortcuts] widget.
///
Expand All @@ -887,6 +855,52 @@ class Shortcuts extends StatefulWidget {
/// unnecessarily with large default shortcut maps.
final String? debugLabel;

/// Returns the [ShortcutManager] that most tightly encloses the given
/// [BuildContext].
///
/// If no [Shortcuts] widget encloses the context given, will assert in debug
/// mode and throw an exception in release mode.
///
/// See also:
///
/// * [maybeOf], which is similar to this function, but will return null if
/// it doesn't find a [Shortcuts] ancestor.
static ShortcutManager of(BuildContext context) {
assert(context != null);
final _ShortcutsMarker? inherited = context.dependOnInheritedWidgetOfExactType<_ShortcutsMarker>();
assert(() {
if (inherited == null) {
throw FlutterError(
'Unable to find a $Shortcuts widget in the context.\n'
'$Shortcuts.of() was called with a context that does not contain a '
'$Shortcuts widget.\n'
'No $Shortcuts ancestor could be found starting from the context that was '
'passed to $Shortcuts.of().\n'
'The context used was:\n'
' $context',
);
}
return true;
}());
return inherited!.manager;
}

/// Returns the [ShortcutManager] that most tightly encloses the given
/// [BuildContext].
///
/// If no [Shortcuts] widget encloses the context given, will return null.
///
/// See also:
///
/// * [of], which is similar to this function, but returns a non-nullable
/// result, and will throw an exception if it doesn't find a [Shortcuts]
/// ancestor.
static ShortcutManager? maybeOf(BuildContext context) {
assert(context != null);
final _ShortcutsMarker? inherited = context.dependOnInheritedWidgetOfExactType<_ShortcutsMarker>();
return inherited?.manager;
}

@override
State<Shortcuts> createState() => _ShortcutsState();

Expand All @@ -913,8 +927,8 @@ class _ShortcutsState extends State<Shortcuts> {
super.initState();
if (widget.manager == null) {
_internalManager = ShortcutManager();
_internalManager!.shortcuts = widget.shortcuts;
}
manager.shortcuts = widget.shortcuts;
}

@override
Expand All @@ -928,7 +942,7 @@ class _ShortcutsState extends State<Shortcuts> {
_internalManager ??= ShortcutManager();
}
}
_internalManager?.shortcuts = widget.shortcuts;
manager.shortcuts = widget.shortcuts;
}

KeyEventResult _handleOnKey(FocusNode node, RawKeyEvent event) {
Expand Down Expand Up @@ -1318,7 +1332,8 @@ class _ShortcutRegistrarState extends State<ShortcutRegistrar> {

@override
Widget build(BuildContext context) {
return Shortcuts.manager(
return Shortcuts(
shortcuts: registry.shortcuts,
manager: manager,
child: _ShortcutRegistrarMarker(
registry: registry,
Expand Down

0 comments on commit 3c3e5fe

Please sign in to comment.