From 47f2e516e54be4e91eee1fc56dd260ee634d0cfd Mon Sep 17 00:00:00 2001 From: Skylot <118523+skylot@users.noreply.github.com> Date: Mon, 9 Dec 2024 17:44:32 +0000 Subject: [PATCH] fix(gui): resolve ignored mouse actions (#2360) --- .../jadx/gui/ui/action/JadxGuiAction.java | 24 +++++++------- .../main/java/jadx/gui/ui/menu/JadxMenu.java | 2 +- .../utils/shortcut/ShortcutsController.java | 32 +++++++++---------- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/jadx-gui/src/main/java/jadx/gui/ui/action/JadxGuiAction.java b/jadx-gui/src/main/java/jadx/gui/ui/action/JadxGuiAction.java index f66b3c28f38..bc4af24ffde 100644 --- a/jadx-gui/src/main/java/jadx/gui/ui/action/JadxGuiAction.java +++ b/jadx-gui/src/main/java/jadx/gui/ui/action/JadxGuiAction.java @@ -9,12 +9,13 @@ import org.jetbrains.annotations.Nullable; +import jadx.gui.ui.menu.JadxMenu; import jadx.gui.utils.UiUtils; import jadx.gui.utils.shortcut.Shortcut; import jadx.gui.utils.ui.ActionHandler; public class JadxGuiAction extends ActionHandler implements IShortcutAction { - private static final String COMMAND = "JadxGuiAction.Command.%s"; + private static final String COMMAND_PREFIX = "JadxGuiAction.Command."; private final ActionModel actionModel; private final String id; @@ -102,21 +103,20 @@ public void actionPerformed(ActionEvent e) { @Override public void performAction() { - if (shortcutComponent != null && !shortcutComponent.isShowing()) { - return; - } - - String shortcutType = "null"; - if (shortcut != null) { - shortcutType = shortcut.getTypeString(); + if (shortcutComponent != null) { + if (shortcutComponent == JadxMenu.JADX_MENU_COMPONENT) { + // always enabled + } else if (!shortcutComponent.isShowing()) { + return; + } } - actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, - String.format(COMMAND, shortcutType))); + String shortcutType = shortcut != null ? shortcut.getTypeString() : "null"; + actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, COMMAND_PREFIX + shortcutType)); } public static boolean isSource(ActionEvent event) { - return event.getActionCommand() != null - && event.getActionCommand().startsWith(String.format(COMMAND, "")); + String command = event.getActionCommand(); + return command != null && command.startsWith(COMMAND_PREFIX); } @Override diff --git a/jadx-gui/src/main/java/jadx/gui/ui/menu/JadxMenu.java b/jadx-gui/src/main/java/jadx/gui/ui/menu/JadxMenu.java index d5ec1a36c0c..580acbdeba0 100644 --- a/jadx-gui/src/main/java/jadx/gui/ui/menu/JadxMenu.java +++ b/jadx-gui/src/main/java/jadx/gui/ui/menu/JadxMenu.java @@ -12,7 +12,7 @@ public class JadxMenu extends JMenu { // fake component to fill action shortcut component property - private static final JComponent JADX_MENU_COMPONENT = new JComponent() { + public static final JComponent JADX_MENU_COMPONENT = new JComponent() { @Override public String toString() { return "JADX_MENU_COMPONENT"; diff --git a/jadx-gui/src/main/java/jadx/gui/utils/shortcut/ShortcutsController.java b/jadx-gui/src/main/java/jadx/gui/utils/shortcut/ShortcutsController.java index e98a2a633db..65c508811f9 100644 --- a/jadx-gui/src/main/java/jadx/gui/utils/shortcut/ShortcutsController.java +++ b/jadx-gui/src/main/java/jadx/gui/utils/shortcut/ShortcutsController.java @@ -3,10 +3,11 @@ import java.awt.AWTEvent; import java.awt.Toolkit; import java.awt.event.MouseEvent; +import java.util.ArrayList; import java.util.EnumMap; -import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; @@ -31,7 +32,7 @@ public class ShortcutsController { private final JadxSettings settings; private final Map> boundActions = new EnumMap<>(ActionModel.class); - private final Set mouseActions = EnumSet.noneOf(ActionModel.class); + private final Map> mouseActions = new HashMap<>(); private ShortcutsWrapper shortcuts; @@ -73,8 +74,7 @@ public void bind(IShortcutAction action) { LOG.warn("No shortcut component in action: {}", action, new JadxRuntimeException()); return; } - boundActions.computeIfAbsent(action.getActionModel(), k -> new HashSet<>()); - boundActions.get(action.getActionModel()).add(action); + boundActions.computeIfAbsent(action.getActionModel(), k -> new HashSet<>()).add(action); } /* @@ -106,18 +106,12 @@ public void registerMouseEventListener(MainWindow mw) { if (mouseEvent.getID() != MouseEvent.MOUSE_PRESSED) { return; } - int mouseButton = mouseEvent.getButton(); - for (ActionModel actionModel : mouseActions) { - Shortcut shortcut = shortcuts.get(actionModel); - if (shortcut != null && shortcut.getMouseButton() == mouseButton) { - Set actions = boundActions.get(actionModel); - if (actions != null) { - for (IShortcutAction action : actions) { - if (action != null) { - mouseEvent.consume(); - UiUtils.uiRun(action::performAction); - } - } + List actions = mouseActions.get(mouseEvent.getButton()); + if (actions != null) { + for (IShortcutAction action : actions) { + if (action != null) { + mouseEvent.consume(); + UiUtils.uiRun(action::performAction); } } } @@ -129,7 +123,11 @@ private void indexMouseActions() { for (ActionModel actionModel : ActionModel.values()) { Shortcut shortcut = shortcuts.get(actionModel); if (shortcut != null && shortcut.isMouse()) { - mouseActions.add(actionModel); + Set actions = boundActions.get(actionModel); + if (actions != null && !actions.isEmpty()) { + mouseActions.computeIfAbsent(shortcut.getMouseButton(), i -> new ArrayList<>()) + .addAll(actions); + } } } }