Tool bar icon and popup menu icon behaves in two different ways in 2019.3.x versions
Hey there...!!
Iam part of a custom plugin development team. I noticed a different behavior of the pop-menu icons only on latest intellij versions (i.e. 2019.3.x versions)
The issue is the toolbar icon is working as expected, which means it enables and disable the icon as the action update() method gets triggered, but the problem would be with the tool-menu icon (navigate Tools -> CustomPluginItem -> Popup-action-1) which is related to the same action which wont behave the same.
The popup menu action item is always enabled even though the update() method is triggered and disable presentation is set as follows anActionEvent.getPresentation().setEnabled(false)
But the happy path is that at the time of disable action, though the icon/action-item is always enabled when we click on that it wont trigger the action, i.e. actionPerformed() method
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This is the plugin.xml configurations to register action as follows
<actions>
<group id="MyPlugin.ToolBarMenu" icon="/images/icons/my-icon.png">
<separator/>
<add-to-group group-id="MainToolBarSettings"/>
<separator/>
</group>
<group id="MyPlugin.ToolsMenu" text="MyPlugin" popup="true" icon="/images/icons/my-icon.png">
<add-to-group group-id="ToolsMenu"/>
<add-to-group group-id="ProjectViewPopupMenu" relative-to-action="ProjectViewPopupMenuRunGroup" anchor="after"/>
<action id="MyPlugin.Action1" class="com.foo.bar.Action1" text="Perform Action1" icon="/images/icons/my-icon.png" description="Perform Action1">
<keyboard-shortcut keymap="$default" first-keystroke="control shift G"/>
<add-to-group group-id="MyPlugin.ToolBarMenu"/>
</action>
</group>
</actions>
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The update() method impl as follows
@Override
public void update(AnActionEvent anActionEvent) {
boolean enable = false;
Project currentProject = anActionEvent.getProject();
if (IdeFocusManager.getInstance(currentProject).getFocusOwner() instanceof EditorComponentImpl) {
// basically what this code snippet does is if a user click on a file which is opened in the Editor window
// the icon will be enable if it is a valid file, if not disable
if(clickedFile is valid) {
enable = true;
} else {
enable = false;
}
} else {
// if a file is selected on the project structure window
if(clickedFile is valid) {
enable = true;
} else {
enable = false;
}
}
anActionEvent.getPresentation().setEnabled(enable);
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The above mentioned update action is enable and disable as expected in the toolbar icon, but the same action which is in the popup-menu is always enabled
In the above code snippet the weird behavior is caused by the following line which i have added in the if condition:
IdeFocusManager.getInstance(currentProject).getFocusOwner()
But when i comment out that line all icons are working as expected, Can any one point out why, getting the focus owner getFocusOwner() is affecting the presentation anActionEvent.getPresentation().setEnabled(enabled)
Please sign in to leave a comment.
I have tried to reproduce your problem with a bit simplified case since I have no information about your file validation process:
So far, everything works as expected:
- if Editor is focused and I open Tools > My Plugin, the action is enabled
- if I focus anything else than Editor, above action is disabled
- if RMB click the Project Tree's file, My Plugin action is disabled as well, because Editor is not focused.
Did I miss something?
Hey Jakub,
Thanks for the response
Here we are enabling the plugin action based on the file validity (i.e. if the clicked file is a supported file type for our plugin then enable icon else disable icon)
User have the luxury to select file from the left side pane (i.e. project file structure) or select from the opened files (i.e. via the editor window)
The icon located on the tool bar is enabled/disabled as expected, (Working as expected)
But the user also have the option to do the same action by right-clicking the file and via the popup menu
or by navigating to Tools > Myplugin > Popup menu. This is the place where the action-item / icon is always enabled even when clicked on an invalid file.
-------------------------------------------------------------------------------------------------------------------------------------
but instead of anActionEvent.getPresentation().setEnabled(false)
if i apply anActionEvent.getPresentation().setVisible(false)
the action-item is hidden from all the places (i.e. from [ToolBar, RMB on file popup-menu, Tool popup-menu])
I am confused because if setVisible() applies to all the places why does setEnable() is not applied
This behavior is only on 2019.3.x versions
Does the same issue happen on 2020.1?
I check with 2020.1 version. Yes, the issue still exist
The correct way to check whether a file is selected in the editor is to use `anActionEvent.getData(CommonDataKeys.EDITOR) != null` instead of using `IdeFocusManager` as you do. Could you please check that your action is updated as expected if you use the correct way for the check?
I also have this same issue when registering an action the toolbar icon behaves as expected but the popup menu icon doesn't function as per the action enable/disable event.when using getFocusOwner() I see this wired behavior on 2019.3 and upper versions
Please don't use `getFocusOwner` for action updates. The correct way of updating actions works in all IDE versions.