Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gui): Make code folding context menu actions appear #2234

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions jadx-gui/src/main/java/jadx/gui/ui/codearea/AbstractCodeArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.awt.event.KeyEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Objects;

import javax.swing.AbstractAction;
Expand Down Expand Up @@ -77,6 +78,7 @@ public abstract class AbstractCodeArea extends RSyntaxTextArea {
protected JNode node;

protected volatile boolean loaded = false;
private int foldingMenuIndex;

public AbstractCodeArea(ContentPanel contentPanel, JNode node) {
this.contentPanel = contentPanel;
Expand Down Expand Up @@ -132,10 +134,35 @@ protected JPopupMenu createPopupMenu() {
menu.add(createPopupMenuItem(copyAction));
menu.add(createPopupMenuItem(getAction(SELECT_ALL_ACTION)));
}
foldingMenuIndex = menu.getComponentCount();
appendFoldingMenu(menu);
return menu;
}

@Override
public void setCodeFoldingEnabled(boolean enabled) {
boolean oldCodeFoldingEnabled = isCodeFoldingEnabled();
super.setCodeFoldingEnabled(enabled);
if (enabled && !oldCodeFoldingEnabled) {
JPopupMenu popup = getPopupMenu();
// RTextArea.appendFoldingMenu() add the menu to the end, but we want to add it
// to foldingMenuIndex, so we remove all components starting from foldingMenuIndex,
// call RTextArea.appendFoldingMenu(), and add the components back
ArrayList<Component> componentBackup = new ArrayList<>();
for (int i = popup.getComponentCount() - 1; i >= foldingMenuIndex; i--) {
componentBackup.add(0, popup.getComponent(i));
popup.remove(i);
}
appendFoldingMenu(popup);
for (Component component : componentBackup) {
popup.add(component);
}
componentBackup.clear();
} else if (!enabled && oldCodeFoldingEnabled) {
throw new JadxRuntimeException("Cannot disable code folding once enabled");
}
}

@Override
protected void appendFoldingMenu(JPopupMenu popup) {
// append code folding popup menu entry only if enabled
Expand Down