mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
[RDCT] GTW-6744: Support listening for custom shortcut actions
Some components, like `IrbRubyLanguageConsoleView`, don't register custom actions right away in constructor. They may postpone it, for example, until when a component is first shown. The old code in `ActionConverterUtil` didn't handle such cases, leaving a component without actions, or only with some of them. This commit supports listening for action changes and updating them during the lifetime of a component. The main flaw still left untouched in this commit is `if (contextActions.isEmpty()) return control` line. If, at the moment of a component conversion, there are no custom actions, they will not be listened to in the future as well. This is left for now because of two reasons: 1. It still fixes GTW-6744. 2. Otherwise, **every single component** would be wrapped into an action behavior wrapper, which is a very high penalty for such a small feature. It is possible to introduce some markers in the future, to avoid the creation of wrapper for every component, still supporting some of them. But this can be done when it becomes actually needed. (cherry picked from commit e48b64be70d6908efa798aaaec2ee00985069740) IJ-CR-147506 GitOrigin-RevId: 90cb2b28a5dca84012b4c775de8d69251e5ce7da
This commit is contained in:
committed by
intellij-monorepo-bot
parent
955a0d04ea
commit
f065b84c36
@@ -10,6 +10,7 @@ import com.intellij.openapi.project.PossiblyDumbAware;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Disposer;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.ui.ClientProperty;
|
||||
import com.intellij.ui.ComponentUtil;
|
||||
import com.intellij.util.SmartFMap;
|
||||
import com.intellij.util.SmartList;
|
||||
@@ -60,6 +61,8 @@ import static com.intellij.openapi.util.NlsActions.ActionText;
|
||||
public abstract class AnAction implements PossiblyDumbAware, ActionUpdateThreadAware {
|
||||
private static final Logger LOG = Logger.getInstance(AnAction.class);
|
||||
|
||||
@ApiStatus.Internal
|
||||
public static final Key<Integer> ACTIONS_MOD_COUNT = Key.create("AnAction.ACTIONS_MOD_COUNT");
|
||||
public static final Key<List<AnAction>> ACTIONS_KEY = Key.create("AnAction.shortcutSet");
|
||||
public static final AnAction[] EMPTY_ARRAY = new AnAction[0];
|
||||
|
||||
@@ -252,6 +255,7 @@ public abstract class AnAction implements PossiblyDumbAware, ActionUpdateThreadA
|
||||
}
|
||||
if (!actionList.contains(this)) {
|
||||
actionList.add(this);
|
||||
updateCustomActionsModCount(component);
|
||||
}
|
||||
|
||||
if (parentDisposable != null) {
|
||||
@@ -262,10 +266,21 @@ public abstract class AnAction implements PossiblyDumbAware, ActionUpdateThreadA
|
||||
public final void unregisterCustomShortcutSet(@NotNull JComponent component) {
|
||||
List<AnAction> actionList = ComponentUtil.getClientProperty(component, ACTIONS_KEY);
|
||||
if (actionList != null) {
|
||||
actionList.remove(this);
|
||||
if (actionList.remove(this)) {
|
||||
updateCustomActionsModCount(component);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update component's "actions mod count" on actions' update.
|
||||
* Allows subscribing on addition/removing of custom shortcut actions.
|
||||
*/
|
||||
private static void updateCustomActionsModCount(@NotNull JComponent component) {
|
||||
int oldCounter = Objects.requireNonNullElse(ClientProperty.get(component, ACTIONS_MOD_COUNT), 0);
|
||||
ClientProperty.put(component, ACTIONS_MOD_COUNT, oldCounter + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the template presentation and the set of shortcuts from {@code sourceAction}.
|
||||
* Consider using {@link com.intellij.openapi.actionSystem.ex.ActionUtil#copyFrom(AnAction, String)} instead.
|
||||
|
||||
Reference in New Issue
Block a user