Skip to content

Commit

Permalink
fix(gui): resolve ignored mouse actions (skylot#2360)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Dec 9, 2024
1 parent fdad829 commit 47f2e51
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
24 changes: 12 additions & 12 deletions jadx-gui/src/main/java/jadx/gui/ui/action/JadxGuiAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion jadx-gui/src/main/java/jadx/gui/ui/menu/JadxMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,7 +32,7 @@ public class ShortcutsController {

private final JadxSettings settings;
private final Map<ActionModel, Set<IShortcutAction>> boundActions = new EnumMap<>(ActionModel.class);
private final Set<ActionModel> mouseActions = EnumSet.noneOf(ActionModel.class);
private final Map<Integer, List<IShortcutAction>> mouseActions = new HashMap<>();

private ShortcutsWrapper shortcuts;

Expand Down Expand Up @@ -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);
}

/*
Expand Down Expand Up @@ -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<IShortcutAction> actions = boundActions.get(actionModel);
if (actions != null) {
for (IShortcutAction action : actions) {
if (action != null) {
mouseEvent.consume();
UiUtils.uiRun(action::performAction);
}
}
List<IShortcutAction> actions = mouseActions.get(mouseEvent.getButton());
if (actions != null) {
for (IShortcutAction action : actions) {
if (action != null) {
mouseEvent.consume();
UiUtils.uiRun(action::performAction);
}
}
}
Expand All @@ -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<IShortcutAction> actions = boundActions.get(actionModel);
if (actions != null && !actions.isEmpty()) {
mouseActions.computeIfAbsent(shortcut.getMouseButton(), i -> new ArrayList<>())
.addAll(actions);
}
}
}
}
Expand Down

0 comments on commit 47f2e51

Please sign in to comment.