Presentation changes for AnAction do not persist

已回答

We have a plugin that provides actions in the toolbar and updates those actions based on selection. In the update(AnActionEvent) method, we are calling setEnabled() on both the Presentation and the template Presentation - but the next time update() is called the isEnabled property has been changed.

In our AnAction class:

@Override
public void update(AnActionEvent e) {
super.update(e);
if (...) {
Presentation view = e.getPresentation();
Presentation template = getTemplatePresentation();
view.setEnabled(false);
template.setEnabled(false);
}
}

Note that this method is called many times...but we don't want to call setEnabled() every time (our selection handler causes the next call to update() here to enter the "if" block and call setEnabled()). When we debug this method, we see the view/template isEnabled is set to true...then we change the selection and see it get set to false in the update() method...and then update() is called a few more times (for IntelliJ reasons), but the isEnabled flag has been set back to true in the Presentation (but not the template Presentation!).

0

Yes, this is how it works.

>we are calling setEnabled() on both the Presentation and the template Presentation
Why?

>we don't want to call setEnabled() every time
Why?

In practice, you shouldn't need to modify template presentation after action creation.
If you do and action was already used, old value might be cached somewhere.
Altering isEnabled/isVisible flags of it shouldn't be necessary ever.

0
Avatar
Permanently deleted user

I think I was under the impression that the Presentation provided by the event would persist from last modified, or that it would be provided as a clone of the template. It sounds like these assumptions were incorrect.

I removed the code to set properties on the template, and did some more digging. It turns out that there was unrelated code which was calling update() directly with the wrong event object...so the "not persisting" properties were, in fact, coming from a different action. I now consider the issue fixed.

Thanks!

0

Still, depending on persistent (or non-prersistent) nature of these values may lead to errors.
One should either ignore these flags or set an actual up-to-date value on all code paths on every 'update(e)' invocation.

Most of the time, one should not read these flags either. (With some exceptions. Ex: if it's implementation of 'CustomComponentAction' or if it's in the code that explicitly invoked 'update()' and owns passed Presentation.)

Persistent values led to multiple hard-to-debug errors (when one calls 'setVisible(false)' if context is bad, but doesn't call 'setVisible(true)' when it is good).
And it was changed to the one, that resets non-template Presentation flags to 'true' before 'update(e)' call (at least, for ActionToolbars and menus).
https://github.com/JetBrains/intellij-community/commit/8ca5faf856f5ae36e6b7377fa7359401e574b04c
So if something works for you in 19.2-, it might not work in 19.3+.


0

请先登录再写评论。