Skip to content

Commit

Permalink
fix(gui): restore open tabs on project load (regression fix)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Mar 25, 2022
1 parent 3d45191 commit 1c2b2c0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 41 deletions.
22 changes: 12 additions & 10 deletions jadx-gui/src/main/java/jadx/gui/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -403,15 +403,22 @@ void open(List<Path> paths) {
}

void open(List<Path> paths, Runnable onFinish) {
closeAll();
if (paths.size() == 1) {
Path singleFile = paths.get(0);
String fileExtension = CommonFileUtils.getFileExtension(singleFile.getFileName().toString());
if (fileExtension != null && fileExtension.equalsIgnoreCase(JadxProject.PROJECT_EXTENSION)) {
openProject(singleFile, onFinish);
List<Path> projectFiles = openProject(singleFile);
if (!Utils.isEmpty(projectFiles)) {
openFiles(projectFiles, onFinish);
}
return;
}
}
closeAll();
openFiles(paths, onFinish);
}

private void openFiles(List<Path> paths, Runnable onFinish) {
project.setFilePath(paths);
if (paths.isEmpty()) {
return;
Expand Down Expand Up @@ -491,9 +498,9 @@ private boolean ensureProjectIsSaved() {
return true;
}

private void openProject(Path path, Runnable onFinish) {
private List<Path> openProject(Path path) {
if (!ensureProjectIsSaved()) {
return;
return Collections.emptyList();
}
JadxProject jadxProject = JadxProject.from(path);
if (jadxProject == null) {
Expand All @@ -506,12 +513,7 @@ private void openProject(Path path, Runnable onFinish) {
}
updateProject(jadxProject);
settings.addRecentProject(path);
List<Path> filePaths = jadxProject.getFilePaths();
if (filePaths == null) {
closeAll();
} else {
open(filePaths, onFinish);
}
return jadxProject.getFilePaths();
}

public void updateProject(JadxProject jadxProject) {
Expand Down
70 changes: 39 additions & 31 deletions jadx-gui/src/main/java/jadx/gui/ui/TabbedPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import jadx.gui.ui.panel.ImagePanel;
import jadx.gui.utils.JumpManager;
import jadx.gui.utils.JumpPosition;
import jadx.gui.utils.NLS;

public class TabbedPane extends JTabbedPane {
private static final long serialVersionUID = -8833600618794570904L;
Expand Down Expand Up @@ -165,42 +166,49 @@ private void showCode(final JumpPosition jumpPos) {
JNode jumpNode = jumpPos.getNode();
Objects.requireNonNull(jumpNode, "Null node in JumpPosition");

final AbstractCodeContentPanel contentPanel = (AbstractCodeContentPanel) getContentPanel(jumpNode);
if (contentPanel == null) {
return;
}
SwingUtilities.invokeLater(() -> {
selectTab(contentPanel);
AbstractCodeArea codeArea = contentPanel.getCodeArea();
int pos = jumpPos.getPos();
if (pos > 0) {
codeArea.scrollToPos(pos);
} else {
int line = jumpPos.getLine();
if (line < 0) {
try {
line = 1 + codeArea.getLineOfOffset(-line);
} catch (BadLocationException e) {
LOG.error("Can't get line for: {}", jumpPos, e);
line = jumpNode.getLine();
mainWindow.getBackgroundExecutor().execute(
NLS.str("progress.load"),
jumpNode::getContent, // run heavy loading in background
status -> {
// show the code in UI thread
AbstractCodeContentPanel contentPanel = (AbstractCodeContentPanel) getContentPanel(jumpNode);
if (contentPanel != null) {
scrollToPos(contentPanel, jumpPos);
selectTab(contentPanel);
}
}
int lineNum = Math.max(0, line - 1);
});
}

private void scrollToPos(AbstractCodeContentPanel contentPanel, JumpPosition jumpPos) {
AbstractCodeArea codeArea = contentPanel.getCodeArea();
int pos = jumpPos.getPos();
if (pos > 0) {
codeArea.scrollToPos(pos);
} else {
int line = jumpPos.getLine();
if (line < 0) {
try {
int offs = codeArea.getLineStartOffset(lineNum);
while (StringUtils.isWhite(codeArea.getText(offs, 1).charAt(0))) {
offs += 1;
}
offs += pos;
jumpPos.setPos(offs);
codeArea.scrollToPos(offs);
line = 1 + codeArea.getLineOfOffset(-line);
} catch (BadLocationException e) {
LOG.error("Failed to jump to position: {}", pos, e);
codeArea.scrollToLine(line);
LOG.error("Can't get line for: {}", jumpPos, e);
line = jumpPos.getNode().getLine();
}
}
codeArea.requestFocus();
});
int lineNum = Math.max(0, line - 1);
try {
int offs = codeArea.getLineStartOffset(lineNum);
while (StringUtils.isWhite(codeArea.getText(offs, 1).charAt(0))) {
offs += 1;
}
offs += pos;
jumpPos.setPos(offs);
codeArea.scrollToPos(offs);
} catch (BadLocationException e) {
LOG.error("Failed to jump to position: {}", pos, e);
codeArea.scrollToLine(line);
}
}
codeArea.requestFocus();
}

public boolean showNode(JNode node) {
Expand Down

0 comments on commit 1c2b2c0

Please sign in to comment.