Skip to content

Commit d9da7c5

Browse files
authored
[Shortcuts] Improve documentation (flutter#123499)
[Shortcuts] Improve documentation
1 parent 3dfc60c commit d9da7c5

File tree

2 files changed

+42
-32
lines changed

2 files changed

+42
-32
lines changed

packages/flutter/lib/src/widgets/actions.dart

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ BuildContext _getParent(BuildContext context) {
4343
///
4444
/// See also:
4545
///
46+
/// * [Shortcuts], a widget used to bind key combinations to [Intent]s.
47+
/// * [Actions], a widget used to map [Intent]s to [Action]s.
4648
/// * [Actions.invoke], which invokes the action associated with a specified
4749
/// [Intent] using the [Actions] widget that most tightly encloses the given
4850
/// [BuildContext].
@@ -67,13 +69,14 @@ abstract class Intent with Diagnosticable {
6769
/// To register an action listener, call [Action.addActionListener].
6870
typedef ActionListenerCallback = void Function(Action<Intent> action);
6971

70-
/// Base class for actions.
72+
/// Base class for an action or command to be performed.
73+
///
74+
/// {@youtube 560 315 https://www.youtube.com/watch?v=XawP1i314WM}
7175
///
72-
/// As the name implies, an [Action] is an action or command to be performed.
73-
/// They are typically invoked as a result of a user action, such as a keyboard
74-
/// shortcut in a [Shortcuts] widget, which is used to look up an [Intent],
75-
/// which is given to an [ActionDispatcher] to map the [Intent] to an [Action]
76-
/// and invoke it.
76+
/// [Action]s are typically invoked as a result of a user action. For example,
77+
/// the [Shortcuts] widget will map a keyboard shortcut into an [Intent], which
78+
/// is given to an [ActionDispatcher] to map the [Intent] to an [Action] and
79+
/// invoke it.
7780
///
7881
/// The [ActionDispatcher] can invoke an [Action] on the primary focus, or
7982
/// without regard for focus.
@@ -612,13 +615,13 @@ class ActionDispatcher with Diagnosticable {
612615
}
613616
}
614617

615-
/// A widget that establishes an [ActionDispatcher] and a map of [Intent] to
616-
/// [Action] to be used by its descendants when invoking an [Action].
618+
/// A widget that maps [Intent]s to [Action]s to be used by its descendants
619+
/// when invoking an [Action].
617620
///
618621
/// {@youtube 560 315 https://www.youtube.com/watch?v=XawP1i314WM}
619622
///
620-
/// Actions are typically invoked using [Actions.invoke] with the context
621-
/// containing the ambient [Actions] widget.
623+
/// Actions are typically invoked using [Shortcuts]. They can also be invoked
624+
/// using [Actions.invoke] on a context containing an ambient [Actions] widget.
622625
///
623626
/// {@tool dartpad}
624627
/// This example creates a custom [Action] subclass `ModifyAction` for modifying
@@ -641,12 +644,12 @@ class ActionDispatcher with Diagnosticable {
641644
///
642645
/// See also:
643646
///
644-
/// * [ActionDispatcher], the object that this widget uses to manage actions.
647+
/// * [Shortcuts], a widget used to bind key combinations to [Intent]s.
648+
/// * [Intent], a class that contains configuration information for running an
649+
/// [Action].
645650
/// * [Action], a class for containing and defining an invocation of a user
646651
/// action.
647-
/// * [Intent], a class that holds a unique [LocalKey] identifying an action,
648-
/// as well as configuration information for running the [Action].
649-
/// * [Shortcuts], a widget used to bind key combinations to [Intent]s.
652+
/// * [ActionDispatcher], the object that this widget uses to manage actions.
650653
class Actions extends StatefulWidget {
651654
/// Creates an [Actions] widget.
652655
///
@@ -664,7 +667,7 @@ class Actions extends StatefulWidget {
664667
///
665668
/// If this [dispatcher] is null, then [Actions.of] and [Actions.invoke] will
666669
/// look up the tree until they find an Actions widget that has a dispatcher
667-
/// set. If not such widget is found, then they will return/use a
670+
/// set. If no such widget is found, then they will return/use a
668671
/// default-constructed [ActionDispatcher].
669672
final ActionDispatcher? dispatcher;
670673

packages/flutter/lib/src/widgets/shortcuts.dart

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,13 @@ class ShortcutManager with Diagnosticable, ChangeNotifier {
869869
/// when invoking an [Action] via a keyboard key combination that maps to an
870870
/// [Intent].
871871
///
872+
/// This is similar to but more powerful than the [CallbackShortcuts] widget.
873+
/// Unlike [CallbackShortcuts], this widget separates key bindings and their
874+
/// implementations. This separation allows [Shortcuts] to have key bindings
875+
/// that adapt to the focused context. For example, the desired action for a
876+
/// deletion intent may be to delete a character in a text input, or to delete
877+
/// a file in a file menu.
878+
///
872879
/// See the article on [Using Actions and
873880
/// Shortcuts](https://docs.flutter.dev/development/ui/advanced/actions_and_shortcuts)
874881
/// for a detailed explanation.
@@ -903,8 +910,8 @@ class ShortcutManager with Diagnosticable, ChangeNotifier {
903910
///
904911
/// See also:
905912
///
906-
/// * [CallbackShortcuts], a less complicated (but less flexible) way of
907-
/// defining key bindings that just invoke callbacks.
913+
/// * [CallbackShortcuts], a simpler but less flexible widget that defines key
914+
/// bindings that invoke callbacks.
908915
/// * [Intent], a class for containing a description of a user action to be
909916
/// invoked.
910917
/// * [Action], a class for defining an invocation of a user action.
@@ -1038,32 +1045,32 @@ class _ShortcutsState extends State<Shortcuts> {
10381045
}
10391046
}
10401047

1041-
/// A widget that provides an uncomplicated mechanism for binding a key
1042-
/// combination to a specific callback.
1043-
///
1044-
/// This is similar to the functionality provided by the [Shortcuts] widget, but
1045-
/// instead of requiring a mapping to an [Intent], and an [Actions] widget
1046-
/// somewhere in the widget tree to bind the [Intent] to, it just takes a set of
1047-
/// bindings that bind the key combination directly to a [VoidCallback].
1048+
/// A widget that binds key combinations to specific callbacks.
10481049
///
1049-
/// Because it is a simpler mechanism, it doesn't provide the ability to disable
1050-
/// the callbacks, or to separate the definition of the shortcuts from the
1051-
/// definition of the code that is triggered by them (the role that actions play
1052-
/// in the [Shortcuts]/[Actions] system).
1050+
/// This is similar to but simpler than the [Shortcuts] widget as it doesn't
1051+
/// require [Intent]s and [Actions] widgets. Instead, it accepts a map
1052+
/// of [ShortcutActivator]s to [VoidCallback]s.
10531053
///
1054-
/// However, for some applications the complexity and flexibility of the
1055-
/// [Shortcuts] and [Actions] mechanism is overkill, and this widget is here for
1056-
/// those apps.
1054+
/// Unlike [Shortcuts], this widget does not separate key bindings and their
1055+
/// implementations. This separation allows [Shortcuts] to have key bindings
1056+
/// that adapt to the focused context. For example, the desired action for a
1057+
/// deletion intent may be to delete a character in a text input, or to delete
1058+
/// a file in a file menu.
10571059
///
10581060
/// [Shortcuts] and [CallbackShortcuts] can both be used in the same app. As
10591061
/// with any key handling widget, if this widget handles a key event then
10601062
/// widgets above it in the focus chain will not receive the event. This means
10611063
/// that if this widget handles a key, then an ancestor [Shortcuts] widget (or
1062-
/// any other key handling widget) will not receive that key, and similarly, if
1064+
/// any other key handling widget) will not receive that key. Similarly, if
10631065
/// a descendant of this widget handles the key, then the key event will not
10641066
/// reach this widget for handling.
10651067
///
1068+
/// See the article on [Using Actions and
1069+
/// Shortcuts](https://docs.flutter.dev/development/ui/advanced/actions_and_shortcuts)
1070+
/// for a detailed explanation.
1071+
///
10661072
/// See also:
1073+
/// * [Shortcuts], a more powerful widget for defining key bindings.
10671074
/// * [Focus], a widget that defines which widgets can receive keyboard focus.
10681075
class CallbackShortcuts extends StatelessWidget {
10691076
/// Creates a const [CallbackShortcuts] widget.

0 commit comments

Comments
 (0)