Revise Action API (#42940)
This updates the Action API in accordance with the design doc for the changes: flutter.dev/go/actions-and-shortcuts-design-revision Fixes #53276
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:collection';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
@@ -14,6 +17,43 @@ void main() {
|
||||
));
|
||||
}
|
||||
|
||||
/// A class that can hold invocation information that an [UndoableAction] can
|
||||
/// use to undo/redo itself.
|
||||
///
|
||||
/// Instances of this class are returned from [UndoableAction]s and placed on
|
||||
/// the undo stack when they are invoked.
|
||||
class Memento extends Object with DiagnosticableMixin implements Diagnosticable {
|
||||
const Memento({
|
||||
@required this.name,
|
||||
@required this.undo,
|
||||
@required this.redo,
|
||||
});
|
||||
|
||||
/// Returns true if this Memento can be used to undo.
|
||||
///
|
||||
/// Subclasses could override to provide their own conditions when a command is
|
||||
/// undoable.
|
||||
bool get canUndo => true;
|
||||
|
||||
/// Returns true if this Memento can be used to redo.
|
||||
///
|
||||
/// Subclasses could override to provide their own conditions when a command is
|
||||
/// redoable.
|
||||
bool get canRedo => true;
|
||||
|
||||
final String name;
|
||||
final VoidCallback undo;
|
||||
final ValueGetter<Memento> redo;
|
||||
|
||||
@override
|
||||
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
|
||||
super.debugFillProperties(properties);
|
||||
properties.add(StringProperty('name', name));
|
||||
properties.add(FlagProperty('undo', value: undo != null, ifTrue: 'undo'));
|
||||
properties.add(FlagProperty('redo', value: redo != null, ifTrue: 'redo'));
|
||||
}
|
||||
}
|
||||
|
||||
/// Undoable Actions
|
||||
|
||||
/// An [ActionDispatcher] subclass that manages the invocation of undoable
|
||||
@@ -29,10 +69,10 @@ class UndoableActionDispatcher extends ActionDispatcher implements Listenable {
|
||||
|
||||
// A stack of actions that have been performed. The most recent action
|
||||
// performed is at the end of the list.
|
||||
final List<UndoableAction> _completedActions = <UndoableAction>[];
|
||||
final DoubleLinkedQueue<Memento> _completedActions = DoubleLinkedQueue<Memento>();
|
||||
// A stack of actions that can be redone. The most recent action performed is
|
||||
// at the end of the list.
|
||||
final List<UndoableAction> _undoneActions = <UndoableAction>[];
|
||||
final List<Memento> _undoneActions = <Memento>[];
|
||||
|
||||
static const int _defaultMaxUndoLevels = 1000;
|
||||
|
||||
@@ -71,11 +111,11 @@ class UndoableActionDispatcher extends ActionDispatcher implements Listenable {
|
||||
}
|
||||
|
||||
@override
|
||||
bool invokeAction(Action action, Intent intent, {FocusNode focusNode}) {
|
||||
final bool result = super.invokeAction(action, intent, focusNode: focusNode);
|
||||
Object invokeAction(Action<Intent> action, Intent intent, [BuildContext context]) {
|
||||
final Object result = super.invokeAction(action, intent, context);
|
||||
print('Invoking ${action is UndoableAction ? 'undoable ' : ''}$intent as $action: $this ');
|
||||
if (action is UndoableAction) {
|
||||
_completedActions.add(action);
|
||||
_completedActions.addLast(result as Memento);
|
||||
_undoneActions.clear();
|
||||
_pruneActions();
|
||||
notifyListeners();
|
||||
@@ -86,15 +126,14 @@ class UndoableActionDispatcher extends ActionDispatcher implements Listenable {
|
||||
// Enforces undo level limit.
|
||||
void _pruneActions() {
|
||||
while (_completedActions.length > _maxUndoLevels) {
|
||||
_completedActions.removeAt(0);
|
||||
_completedActions.removeFirst();
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if there is an action on the stack that can be undone.
|
||||
bool get canUndo {
|
||||
if (_completedActions.isNotEmpty) {
|
||||
final Intent lastIntent = _completedActions.last.invocationIntent;
|
||||
return lastIntent.isEnabled(primaryFocus.context);
|
||||
return _completedActions.first.canUndo;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -102,8 +141,7 @@ class UndoableActionDispatcher extends ActionDispatcher implements Listenable {
|
||||
/// Returns true if an action that has been undone can be re-invoked.
|
||||
bool get canRedo {
|
||||
if (_undoneActions.isNotEmpty) {
|
||||
final Intent lastIntent = _undoneActions.last.invocationIntent;
|
||||
return lastIntent.isEnabled(primaryFocus?.context);
|
||||
return _undoneActions.first.canRedo;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -116,9 +154,9 @@ class UndoableActionDispatcher extends ActionDispatcher implements Listenable {
|
||||
if (!canUndo) {
|
||||
return false;
|
||||
}
|
||||
final UndoableAction action = _completedActions.removeLast();
|
||||
action.undo();
|
||||
_undoneActions.add(action);
|
||||
final Memento memento = _completedActions.removeLast();
|
||||
memento.undo();
|
||||
_undoneActions.add(memento);
|
||||
notifyListeners();
|
||||
return true;
|
||||
}
|
||||
@@ -131,9 +169,9 @@ class UndoableActionDispatcher extends ActionDispatcher implements Listenable {
|
||||
if (!canRedo) {
|
||||
return false;
|
||||
}
|
||||
final UndoableAction action = _undoneActions.removeLast();
|
||||
action.invoke(action.invocationNode, action.invocationIntent);
|
||||
_completedActions.add(action);
|
||||
final Memento memento = _undoneActions.removeLast();
|
||||
final Memento replacement = memento.redo();
|
||||
_completedActions.add(replacement);
|
||||
_pruneActions();
|
||||
notifyListeners();
|
||||
return true;
|
||||
@@ -144,71 +182,50 @@ class UndoableActionDispatcher extends ActionDispatcher implements Listenable {
|
||||
super.debugFillProperties(properties);
|
||||
properties.add(IntProperty('undoable items', _completedActions.length));
|
||||
properties.add(IntProperty('redoable items', _undoneActions.length));
|
||||
properties.add(IterableProperty<UndoableAction>('undo stack', _completedActions));
|
||||
properties.add(IterableProperty<UndoableAction>('redo stack', _undoneActions));
|
||||
properties.add(IterableProperty<Memento>('undo stack', _completedActions));
|
||||
properties.add(IterableProperty<Memento>('redo stack', _undoneActions));
|
||||
}
|
||||
}
|
||||
|
||||
class UndoIntent extends Intent {
|
||||
const UndoIntent() : super(kUndoActionKey);
|
||||
const UndoIntent();
|
||||
}
|
||||
|
||||
class UndoAction extends Action<UndoIntent> {
|
||||
@override
|
||||
bool get enabled {
|
||||
final UndoableActionDispatcher manager = Actions.of(primaryFocus?.context ?? FocusDemo.appKey.currentContext, nullOk: true) as UndoableActionDispatcher;
|
||||
return manager.canUndo;
|
||||
}
|
||||
|
||||
@override
|
||||
bool isEnabled(BuildContext context) {
|
||||
final UndoableActionDispatcher manager = Actions.of(context, nullOk: true) as UndoableActionDispatcher;
|
||||
return manager.canUndo;
|
||||
void invoke(UndoIntent intent) {
|
||||
final UndoableActionDispatcher manager = Actions.of(primaryFocus?.context ?? FocusDemo.appKey.currentContext, nullOk: true) as UndoableActionDispatcher;
|
||||
manager?.undo();
|
||||
}
|
||||
}
|
||||
|
||||
class RedoIntent extends Intent {
|
||||
const RedoIntent() : super(kRedoActionKey);
|
||||
const RedoIntent();
|
||||
}
|
||||
|
||||
class RedoAction extends Action<RedoIntent> {
|
||||
@override
|
||||
bool get enabled {
|
||||
final UndoableActionDispatcher manager = Actions.of(primaryFocus.context, nullOk: true) as UndoableActionDispatcher;
|
||||
return manager.canRedo;
|
||||
}
|
||||
|
||||
@override
|
||||
bool isEnabled(BuildContext context) {
|
||||
final UndoableActionDispatcher manager = Actions.of(context, nullOk: true) as UndoableActionDispatcher;
|
||||
return manager.canRedo;
|
||||
RedoAction invoke(RedoIntent intent) {
|
||||
final UndoableActionDispatcher manager = Actions.of(primaryFocus.context, nullOk: true) as UndoableActionDispatcher;
|
||||
manager?.redo();
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
const LocalKey kUndoActionKey = ValueKey<String>('Undo');
|
||||
const Intent kUndoIntent = UndoIntent();
|
||||
final Action kUndoAction = CallbackAction(
|
||||
kUndoActionKey,
|
||||
onInvoke: (FocusNode node, Intent tag) {
|
||||
if (node?.context == null) {
|
||||
return;
|
||||
}
|
||||
final UndoableActionDispatcher manager = Actions.of(node.context, nullOk: true) as UndoableActionDispatcher;
|
||||
manager?.undo();
|
||||
},
|
||||
);
|
||||
|
||||
const LocalKey kRedoActionKey = ValueKey<String>('Redo');
|
||||
const Intent kRedoIntent = RedoIntent();
|
||||
final Action kRedoAction = CallbackAction(
|
||||
kRedoActionKey,
|
||||
onInvoke: (FocusNode node, Intent tag) {
|
||||
if (node?.context == null) {
|
||||
return;
|
||||
}
|
||||
final UndoableActionDispatcher manager = Actions.of(node.context, nullOk: true) as UndoableActionDispatcher;
|
||||
manager?.redo();
|
||||
},
|
||||
);
|
||||
|
||||
/// An action that can be undone.
|
||||
abstract class UndoableAction extends Action {
|
||||
/// A const constructor to [UndoableAction].
|
||||
///
|
||||
/// The [intentKey] parameter must not be null.
|
||||
UndoableAction(LocalKey intentKey) : super(intentKey);
|
||||
|
||||
/// The node supplied when this command was invoked.
|
||||
FocusNode get invocationNode => _invocationNode;
|
||||
FocusNode _invocationNode;
|
||||
|
||||
@protected
|
||||
set invocationNode(FocusNode value) => _invocationNode = value;
|
||||
|
||||
abstract class UndoableAction<T extends Intent> extends Action<T> {
|
||||
/// The [Intent] this action was originally invoked with.
|
||||
Intent get invocationIntent => _invocationTag;
|
||||
Intent _invocationTag;
|
||||
@@ -216,110 +233,63 @@ abstract class UndoableAction extends Action {
|
||||
@protected
|
||||
set invocationIntent(Intent value) => _invocationTag = value;
|
||||
|
||||
/// Returns true if the data model can be returned to the state it was in
|
||||
/// previous to this action being executed.
|
||||
///
|
||||
/// Default implementation returns true.
|
||||
bool get undoable => true;
|
||||
|
||||
/// Reverts the data model to the state before this command executed.
|
||||
@mustCallSuper
|
||||
void undo();
|
||||
|
||||
@override
|
||||
@mustCallSuper
|
||||
void invoke(FocusNode node, Intent intent) {
|
||||
invocationNode = node;
|
||||
void invoke(T intent) {
|
||||
invocationIntent = intent;
|
||||
}
|
||||
}
|
||||
|
||||
class UndoableFocusActionBase<T extends Intent> extends UndoableAction<T> {
|
||||
@override
|
||||
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
|
||||
super.debugFillProperties(properties);
|
||||
properties.add(DiagnosticsProperty<FocusNode>('invocationNode', invocationNode));
|
||||
@mustCallSuper
|
||||
Memento invoke(T intent) {
|
||||
super.invoke(intent);
|
||||
final FocusNode previousFocus = primaryFocus;
|
||||
return Memento(name: previousFocus.debugLabel, undo: () {
|
||||
previousFocus.requestFocus();
|
||||
}, redo: () {
|
||||
return invoke(intent);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class UndoableFocusActionBase extends UndoableAction {
|
||||
UndoableFocusActionBase(LocalKey name) : super(name);
|
||||
|
||||
FocusNode _previousFocus;
|
||||
|
||||
class UndoableRequestFocusAction extends UndoableFocusActionBase<RequestFocusIntent> {
|
||||
@override
|
||||
void invoke(FocusNode node, Intent intent) {
|
||||
super.invoke(node, intent);
|
||||
_previousFocus = primaryFocus;
|
||||
node.requestFocus();
|
||||
}
|
||||
|
||||
@override
|
||||
void undo() {
|
||||
if (_previousFocus == null) {
|
||||
primaryFocus?.unfocus();
|
||||
return;
|
||||
}
|
||||
if (_previousFocus is FocusScopeNode) {
|
||||
// The only way a scope can be the _previousFocus is if there was no
|
||||
// focusedChild for the scope when we invoked this action, so we need to
|
||||
// return to that state.
|
||||
|
||||
// Unfocus the current node to remove it from the focused child list of
|
||||
// the scope.
|
||||
primaryFocus?.unfocus();
|
||||
// and then let the scope node be focused...
|
||||
}
|
||||
_previousFocus.requestFocus();
|
||||
_previousFocus = null;
|
||||
}
|
||||
|
||||
@override
|
||||
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
|
||||
super.debugFillProperties(properties);
|
||||
properties.add(DiagnosticsProperty<FocusNode>('previous', _previousFocus));
|
||||
}
|
||||
}
|
||||
|
||||
class UndoableRequestFocusAction extends UndoableFocusActionBase {
|
||||
UndoableRequestFocusAction() : super(RequestFocusAction.key);
|
||||
|
||||
@override
|
||||
void invoke(FocusNode node, Intent intent) {
|
||||
super.invoke(node, intent);
|
||||
node.requestFocus();
|
||||
Memento invoke(RequestFocusIntent intent) {
|
||||
final Memento memento = super.invoke(intent);
|
||||
intent.focusNode.requestFocus();
|
||||
return memento;
|
||||
}
|
||||
}
|
||||
|
||||
/// Actions for manipulating focus.
|
||||
class UndoableNextFocusAction extends UndoableFocusActionBase {
|
||||
UndoableNextFocusAction() : super(NextFocusAction.key);
|
||||
|
||||
class UndoableNextFocusAction extends UndoableFocusActionBase<NextFocusIntent> {
|
||||
@override
|
||||
void invoke(FocusNode node, Intent intent) {
|
||||
super.invoke(node, intent);
|
||||
node.nextFocus();
|
||||
Memento invoke(NextFocusIntent intent) {
|
||||
final Memento memento = super.invoke(intent);
|
||||
primaryFocus.nextFocus();
|
||||
return memento;
|
||||
}
|
||||
}
|
||||
|
||||
class UndoablePreviousFocusAction extends UndoableFocusActionBase {
|
||||
UndoablePreviousFocusAction() : super(PreviousFocusAction.key);
|
||||
|
||||
class UndoablePreviousFocusAction extends UndoableFocusActionBase<PreviousFocusIntent> {
|
||||
@override
|
||||
void invoke(FocusNode node, Intent intent) {
|
||||
super.invoke(node, intent);
|
||||
node.previousFocus();
|
||||
Memento invoke(PreviousFocusIntent intent) {
|
||||
final Memento memento = super.invoke(intent);
|
||||
primaryFocus.previousFocus();
|
||||
return memento;
|
||||
}
|
||||
}
|
||||
|
||||
class UndoableDirectionalFocusAction extends UndoableFocusActionBase {
|
||||
UndoableDirectionalFocusAction() : super(DirectionalFocusAction.key);
|
||||
|
||||
class UndoableDirectionalFocusAction extends UndoableFocusActionBase<DirectionalFocusIntent> {
|
||||
TraversalDirection direction;
|
||||
|
||||
@override
|
||||
void invoke(FocusNode node, DirectionalFocusIntent intent) {
|
||||
super.invoke(node, intent);
|
||||
final DirectionalFocusIntent args = intent;
|
||||
node.focusInDirection(args.direction);
|
||||
Memento invoke(DirectionalFocusIntent intent) {
|
||||
final Memento memento = super.invoke(intent);
|
||||
primaryFocus.focusInDirection(intent.direction);
|
||||
return memento;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -335,6 +305,7 @@ class DemoButton extends StatefulWidget {
|
||||
|
||||
class _DemoButtonState extends State<DemoButton> {
|
||||
FocusNode _focusNode;
|
||||
final GlobalKey _nameKey = GlobalKey();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -345,7 +316,7 @@ class _DemoButtonState extends State<DemoButton> {
|
||||
void _handleOnPressed() {
|
||||
print('Button ${widget.name} pressed.');
|
||||
setState(() {
|
||||
Actions.invoke(context, const Intent(RequestFocusAction.key), focusNode: _focusNode);
|
||||
Actions.invoke(_nameKey.currentContext, RequestFocusIntent(_focusNode));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -362,7 +333,7 @@ class _DemoButtonState extends State<DemoButton> {
|
||||
focusColor: Colors.red,
|
||||
hoverColor: Colors.blue,
|
||||
onPressed: () => _handleOnPressed(),
|
||||
child: Text(widget.name),
|
||||
child: Text(widget.name, key: _nameKey),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -370,6 +341,8 @@ class _DemoButtonState extends State<DemoButton> {
|
||||
class FocusDemo extends StatefulWidget {
|
||||
const FocusDemo({Key key}) : super(key: key);
|
||||
|
||||
static GlobalKey appKey = GlobalKey();
|
||||
|
||||
@override
|
||||
_FocusDemoState createState() => _FocusDemoState();
|
||||
}
|
||||
@@ -415,22 +388,23 @@ class _FocusDemoState extends State<FocusDemo> {
|
||||
final TextTheme textTheme = Theme.of(context).textTheme;
|
||||
return Actions(
|
||||
dispatcher: dispatcher,
|
||||
actions: <LocalKey, ActionFactory>{
|
||||
RequestFocusAction.key: () => UndoableRequestFocusAction(),
|
||||
NextFocusAction.key: () => UndoableNextFocusAction(),
|
||||
PreviousFocusAction.key: () => UndoablePreviousFocusAction(),
|
||||
DirectionalFocusAction.key: () => UndoableDirectionalFocusAction(),
|
||||
kUndoActionKey: () => kUndoAction,
|
||||
kRedoActionKey: () => kRedoAction,
|
||||
actions: <Type, Action<Intent>>{
|
||||
RequestFocusIntent: UndoableRequestFocusAction(),
|
||||
NextFocusIntent: UndoableNextFocusAction(),
|
||||
PreviousFocusIntent: UndoablePreviousFocusAction(),
|
||||
DirectionalFocusIntent: UndoableDirectionalFocusAction(),
|
||||
UndoIntent: UndoAction(),
|
||||
RedoIntent: RedoAction(),
|
||||
},
|
||||
child: FocusTraversalGroup(
|
||||
policy: ReadingOrderTraversalPolicy(),
|
||||
child: Shortcuts(
|
||||
shortcuts: <LogicalKeySet, Intent>{
|
||||
LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.shift, LogicalKeyboardKey.keyZ): kRedoIntent,
|
||||
LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.keyZ): kUndoIntent,
|
||||
LogicalKeySet(Platform.isMacOS ? LogicalKeyboardKey.meta : LogicalKeyboardKey.control, LogicalKeyboardKey.shift, LogicalKeyboardKey.keyZ): const RedoIntent(),
|
||||
LogicalKeySet(Platform.isMacOS ? LogicalKeyboardKey.meta : LogicalKeyboardKey.control, LogicalKeyboardKey.keyZ): const UndoIntent(),
|
||||
},
|
||||
child: FocusScope(
|
||||
key: FocusDemo.appKey,
|
||||
debugLabel: 'Scope',
|
||||
autofocus: true,
|
||||
child: DefaultTextStyle(
|
||||
@@ -477,7 +451,7 @@ class _FocusDemoState extends State<FocusDemo> {
|
||||
child: const Text('UNDO'),
|
||||
onPressed: canUndo
|
||||
? () {
|
||||
Actions.invoke(context, kUndoIntent);
|
||||
Actions.invoke(context, const UndoIntent());
|
||||
}
|
||||
: null,
|
||||
),
|
||||
@@ -488,7 +462,7 @@ class _FocusDemoState extends State<FocusDemo> {
|
||||
child: const Text('REDO'),
|
||||
onPressed: canRedo
|
||||
? () {
|
||||
Actions.invoke(context, kRedoIntent);
|
||||
Actions.invoke(context, const RedoIntent());
|
||||
}
|
||||
: null,
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user