Skip to content

Commit

Permalink
fix(gui): correct handling for tree row click (#1324)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Jan 12, 2022
1 parent 7576f9c commit 5ca7285
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions jadx-gui/src/main/java/jadx/gui/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
Expand Down Expand Up @@ -704,7 +705,7 @@ private void nodeClickAction(@Nullable Object obj) {
}

private void treeRightClickAction(MouseEvent e) {
JNode obj = getJNodeUnderMouse(e, false);
JNode obj = getJNodeUnderMouse(e);
if (obj instanceof JPackage) {
JPackagePopupMenu menu = new JPackagePopupMenu(this, (JPackage) obj);
menu.show(e.getComponent(), e.getX(), e.getY());
Expand All @@ -718,17 +719,26 @@ private void treeRightClickAction(MouseEvent e) {
}

@Nullable
private JNode getJNodeUnderMouse(MouseEvent mouseEvent, boolean trySelection) {
TreePath path = tree.getPathForLocation(mouseEvent.getX(), mouseEvent.getY());
if (path == null && trySelection) {
// maybe click on node row (mouse pressed event should select this node in tree)
path = tree.getSelectionPath();
private JNode getJNodeUnderMouse(MouseEvent mouseEvent) {
TreePath path = tree.getClosestPathForLocation(mouseEvent.getX(), mouseEvent.getY());
if (path == null) {
return null;
}
if (path != null) {
Object obj = path.getLastPathComponent();
if (obj instanceof JNode) {
return (JNode) obj;
// allow 'closest' path only at the right of the item row
Rectangle pathBounds = tree.getPathBounds(path);
if (pathBounds != null) {
int y = mouseEvent.getY();
if (y < pathBounds.y || y > (pathBounds.y + pathBounds.height)) {
return null;
}
if (mouseEvent.getX() < pathBounds.x) {
// exclude expand/collapse events
return null;
}
}
Object obj = path.getLastPathComponent();
if (obj instanceof JNode) {
return (JNode) obj;
}
return null;
}
Expand Down Expand Up @@ -1081,16 +1091,10 @@ private void initUI() {
ToolTipManager.sharedInstance().registerComponent(tree);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.addMouseListener(new MouseAdapter() {

@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
}

@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
nodeClickAction(getJNodeUnderMouse(e, true));
nodeClickAction(getJNodeUnderMouse(e));
} else if (SwingUtilities.isRightMouseButton(e)) {
treeRightClickAction(e);
}
Expand Down

0 comments on commit 5ca7285

Please sign in to comment.