Skip to content

Commit

Permalink
feat(gui): map back and forward mouse keys for navigation (#807)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Dec 21, 2019
1 parent 7f8d03d commit 9ab003d
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 4 deletions.
15 changes: 14 additions & 1 deletion jadx-gui/src/main/java/jadx/gui/JadxGUI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jadx.gui;

import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -10,6 +11,7 @@
import jadx.gui.settings.JadxSettingsAdapter;
import jadx.gui.ui.MainWindow;
import jadx.gui.utils.NLS;
import jadx.gui.utils.SystemInfo;
import jadx.gui.utils.logs.LogCollector;

public class JadxGUI {
Expand All @@ -28,6 +30,8 @@ public static void main(String[] args) {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
NLS.setLocale(settings.getLangLocale());
printSystemInfo();

SwingUtilities.invokeLater(new MainWindow(settings)::init);
} catch (Exception e) {
LOG.error("Error: {}", e.getMessage(), e);
Expand All @@ -47,4 +51,13 @@ private static boolean tryDefaultLookAndFeel() {
}
return false;
}

private static void printSystemInfo() {
if (LOG.isDebugEnabled()) {
LOG.debug("Starting jadx-gui. Version: '{}'. JVM: {} {}. OS: {} {}",
SystemInfo.JADX_VERSION,
SystemInfo.JAVA_VM, SystemInfo.JAVA_VER,
SystemInfo.OS_NAME, SystemInfo.OS_VERSION);
}
}
}
40 changes: 40 additions & 0 deletions jadx-gui/src/main/java/jadx/gui/ui/MainWindow.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package jadx.gui.ui;

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.event.ActionEvent;
Expand Down Expand Up @@ -97,6 +99,7 @@
import jadx.gui.utils.JumpPosition;
import jadx.gui.utils.Link;
import jadx.gui.utils.NLS;
import jadx.gui.utils.SystemInfo;
import jadx.gui.utils.UiUtils;

import static javax.swing.KeyStroke.getKeyStroke;
Expand Down Expand Up @@ -164,6 +167,7 @@ public MainWindow(JadxSettings settings) {
FontUtils.registerBundledFonts();
initUI();
initMenuAndToolbar();
registerMouseNavigationButtons();
UiUtils.setWindowIcons(this);
loadSettings();
checkForUpdate();
Expand Down Expand Up @@ -958,6 +962,42 @@ public void treeWillCollapse(TreeExpansionEvent event) {
setTitle(DEFAULT_TITLE);
}

private void registerMouseNavigationButtons() {
Toolkit toolkit = Toolkit.getDefaultToolkit();
toolkit.addAWTEventListener(event -> {
if (event instanceof MouseEvent) {
MouseEvent mouseEvent = (MouseEvent) event;
if (mouseEvent.getID() == MouseEvent.MOUSE_PRESSED) {
int rawButton = mouseEvent.getButton();
if (rawButton <= 3) {
return;
}
int button = remapMouseButton(rawButton);
switch (button) {
case 4:
tabbedPane.navBack();
break;
case 5:
tabbedPane.navForward();
break;
}
}
}
}, AWTEvent.MOUSE_EVENT_MASK);
}

private static int remapMouseButton(int rawButton) {
if (SystemInfo.IS_LINUX) {
if (rawButton == 6) {
return 4;
}
if (rawButton == 7) {
return 5;
}
}
return rawButton;
}

private static String[] getPathExpansion(TreePath path) {
List<String> pathList = new ArrayList<>();
while (path != null) {
Expand Down
8 changes: 6 additions & 2 deletions jadx-gui/src/main/java/jadx/gui/ui/TabbedPane.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package jadx.gui.ui;

import java.awt.*;
import java.awt.Component;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.swing.*;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;

import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -40,6 +41,9 @@ public class TabbedPane extends JTabbedPane {
setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

addMouseWheelListener(e -> {
if (openTabs.isEmpty()) {
return;
}
int direction = e.getWheelRotation();
int index = getSelectedIndex();
int maxIndex = getTabCount() - 1;
Expand Down
31 changes: 30 additions & 1 deletion jadx-gui/src/main/java/jadx/gui/utils/JumpManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.jetbrains.annotations.Nullable;

public class JumpManager {

private final List<JumpPosition> list = new ArrayList<>();
private int currentPos = 0;

public void addPosition(JumpPosition pos) {
if (pos.equals(getCurrent())) {
if (ignoreJump(pos)) {
return;
}
currentPos++;
Expand All @@ -25,13 +28,38 @@ public void addPosition(JumpPosition pos) {
}
}

private boolean ignoreJump(JumpPosition pos) {
JumpPosition current = getCurrent();
if (current == null) {
return false;
}
if (pos.equals(current)) {
return true;
}
if (Objects.equals(current.getNode(), pos.getNode())) {
// undefined jump line in same node // TODO: find the cause
if (pos.getLine() == 0) {
return true;
}
if (current.getLine() == 0) {
// replace current
getPrev();
return false;
}
return false;
}
return false;
}

@Nullable
private JumpPosition getCurrent() {
if (currentPos >= 0 && currentPos < list.size()) {
return list.get(currentPos);
}
return null;
}

@Nullable
public JumpPosition getPrev() {
if (currentPos == 0) {
return null;
Expand All @@ -40,6 +68,7 @@ public JumpPosition getPrev() {
return list.get(currentPos);
}

@Nullable
public JumpPosition getNext() {
int size = list.size();
if (size == 0) {
Expand Down
20 changes: 20 additions & 0 deletions jadx-gui/src/main/java/jadx/gui/utils/SystemInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package jadx.gui.utils;

import java.util.Locale;

import jadx.api.JadxDecompiler;

public class SystemInfo {
public static final String JADX_VERSION = JadxDecompiler.getVersion();

public static final String JAVA_VM = System.getProperty("java.vm.name");
public static final String JAVA_VER = System.getProperty("java.version");

public static final String OS_NAME = System.getProperty("os.name");
public static final String OS_VERSION = System.getProperty("os.version");

private static final String LOWER_OS_NAME = OS_NAME.toLowerCase(Locale.ENGLISH);
public static final boolean IS_WINDOWS = LOWER_OS_NAME.startsWith("windows");
public static final boolean IS_MAC = LOWER_OS_NAME.startsWith("mac");
public static final boolean IS_LINUX = LOWER_OS_NAME.startsWith("linux");
}

0 comments on commit 9ab003d

Please sign in to comment.