From 36767d01e4d836aec91f989759814b2234d200ff Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Tue, 21 Apr 2020 13:18:04 -0700 Subject: [PATCH] Make Action.enabled be isEnabled(Intent intent) instead. (#55230) --- dev/manual_tests/lib/actions.dart | 4 +- packages/flutter/lib/src/widgets/actions.dart | 64 +++++++------------ .../flutter/lib/src/widgets/scrollable.dart | 2 +- .../flutter/test/widgets/actions_test.dart | 10 +-- 4 files changed, 31 insertions(+), 49 deletions(-) diff --git a/dev/manual_tests/lib/actions.dart b/dev/manual_tests/lib/actions.dart index cf638db1c9..847cf23e20 100644 --- a/dev/manual_tests/lib/actions.dart +++ b/dev/manual_tests/lib/actions.dart @@ -193,7 +193,7 @@ class UndoIntent extends Intent { class UndoAction extends Action { @override - bool get enabled { + bool isEnabled(UndoIntent intent) { final UndoableActionDispatcher manager = Actions.of(primaryFocus?.context ?? FocusDemo.appKey.currentContext, nullOk: true) as UndoableActionDispatcher; return manager.canUndo; } @@ -211,7 +211,7 @@ class RedoIntent extends Intent { class RedoAction extends Action { @override - bool get enabled { + bool isEnabled(RedoIntent intent) { final UndoableActionDispatcher manager = Actions.of(primaryFocus.context, nullOk: true) as UndoableActionDispatcher; return manager.canRedo; } diff --git a/packages/flutter/lib/src/widgets/actions.dart b/packages/flutter/lib/src/widgets/actions.dart index 096f61a937..87e0ac296e 100644 --- a/packages/flutter/lib/src/widgets/actions.dart +++ b/packages/flutter/lib/src/widgets/actions.dart @@ -82,7 +82,7 @@ abstract class Action with Diagnosticable { /// /// If the enabled state changes, overriding subclasses must call /// [notifyActionListeners] to notify any listeners of the change. - bool get enabled => true; + bool isEnabled(covariant T intent) => true; /// Called when the action is to be performed. /// @@ -379,7 +379,7 @@ class ActionDispatcher with Diagnosticable { assert(action != null); assert(intent != null); context ??= primaryFocus?.context; - if (action.enabled) { + if (action.isEnabled(intent)) { if (action is ContextAction) { return action.invoke(intent, context); } else { @@ -486,7 +486,7 @@ class Actions extends StatefulWidget { /// updated action. static VoidCallback handler(BuildContext context, T intent, {bool nullOk = false}) { final Action action = Actions.find(context, nullOk: nullOk); - if (action != null && action.enabled) { + if (action != null && action.isEnabled(intent)) { return () { Actions.of(context).invokeAction(action, intent, context); }; @@ -635,12 +635,10 @@ class Actions extends StatefulWidget { } class _ActionsState extends State { - // Keeps the last-known enabled state of each action in the action map in - // order to be able to appropriately notify dependents that the state has - // changed. - Map, bool> enabledState = , bool>{}; - // Used to tell the marker to rebuild when the enabled state of an action in - // the map changes. + // The set of actions that this Actions widget is current listening to. + Set> listenedActions = >{}; + // Used to tell the marker to rebuild its dependencies when the state of an + // action in the map changes. Object rebuildKey = Object(); @override @@ -650,42 +648,24 @@ class _ActionsState extends State { } void _handleActionChanged(Action action) { - assert(enabledState.containsKey(action)); - final bool actionEnabled = action.enabled; - if (enabledState[action] == null || enabledState[action] != actionEnabled) { - setState(() { - enabledState[action] = actionEnabled; - // Generate a new key so that the marker notifies dependents. - rebuildKey = Object(); - }); - } + // Generate a new key so that the marker notifies dependents. + setState(() { + rebuildKey = Object(); + }); } void _updateActionListeners() { - final Map, bool> newState = , bool>{}; - final Set> foundActions = >{}; - for (final Action action in widget.actions.values) { - if (enabledState.containsKey(action)) { - // Already subscribed to this action, just copy over the current enabled state. - newState[action] = enabledState[action]; - foundActions.add(action); - } else { - // New action to subscribe to. - // Don't set the new state to action.enabled, since that can cause - // problems when the enabled accessor looks up other widgets (which may - // have already been removed from the tree). - newState[action] = null; - action.addActionListener(_handleActionChanged); - } + final Set> widgetActions = widget.actions.values.toSet(); + final Set> removedActions = listenedActions.difference(widgetActions); + final Set> addedActions = widgetActions.difference(listenedActions); + + for (final Action action in removedActions) { + action.removeActionListener(_handleActionChanged); } - // Unregister from any actions in the previous enabledState map that aren't - // going to be transferred to the new one. - for (final Action action in enabledState.keys) { - if (!foundActions.contains(action)) { - action.removeActionListener(_handleActionChanged); - } + for (final Action action in addedActions) { + action.addActionListener(_handleActionChanged); } - enabledState = newState; + listenedActions = widgetActions; } @override @@ -697,10 +677,10 @@ class _ActionsState extends State { @override void dispose() { super.dispose(); - for (final Action action in enabledState.keys) { + for (final Action action in listenedActions) { action.removeActionListener(_handleActionChanged); } - enabledState = null; + listenedActions = null; } @override diff --git a/packages/flutter/lib/src/widgets/scrollable.dart b/packages/flutter/lib/src/widgets/scrollable.dart index fae6eb35b4..807806e40f 100644 --- a/packages/flutter/lib/src/widgets/scrollable.dart +++ b/packages/flutter/lib/src/widgets/scrollable.dart @@ -911,7 +911,7 @@ class ScrollAction extends Action { static const LocalKey key = ValueKey(ScrollAction); @override - bool get enabled { + bool isEnabled(ScrollIntent intent) { final FocusNode focus = primaryFocus; return focus != null && focus.context != null && Scrollable.of(focus.context) != null; } diff --git a/packages/flutter/test/widgets/actions_test.dart b/packages/flutter/test/widgets/actions_test.dart index 65e98261ca..728f6b2c64 100644 --- a/packages/flutter/test/widgets/actions_test.dart +++ b/packages/flutter/test/widgets/actions_test.dart @@ -30,6 +30,8 @@ class TestAction extends CallbackAction { super(onInvoke: onInvoke); @override + bool isEnabled(TestIntent intent) => enabled; + bool get enabled => _enabled; bool _enabled = true; set enabled(bool value) { @@ -412,17 +414,17 @@ void main() { }, ); bool enabled1 = true; - action1.addActionListener((Action action) => enabled1 = action.enabled); + action1.addActionListener((Action action) => enabled1 = action.isEnabled(const TestIntent())); action1.enabled = false; expect(enabled1, isFalse); bool enabled2 = true; - action2.addActionListener((Action action) => enabled2 = action.enabled); + action2.addActionListener((Action action) => enabled2 = action.isEnabled(const SecondTestIntent())); action2.enabled = false; expect(enabled2, isFalse); bool enabled3 = true; - action3.addActionListener((Action action) => enabled3 = action.enabled); + action3.addActionListener((Action action) => enabled3 = action.isEnabled(const ThirdTestIntent())); action3.enabled = false; expect(enabled3, isFalse); @@ -466,7 +468,7 @@ void main() { SecondTestIntent: action2, }, child: ActionListener( - listener: (Action action) => enabledChanged = action.enabled, + listener: (Action action) => enabledChanged = action.isEnabled(const ThirdTestIntent()), action: action2, child: Actions( actions: >{