Skip to content

Commit

Permalink
feat(gui): improve search bar behavior when using key bindings (PR #2074
Browse files Browse the repository at this point in the history
)

Before, the search bar would toggle, which was quite annoying. Now, it replicates IntelliJ's search bar behaviour.

1.1. If the user has selected text, use that as the search text
1.2. Otherwise, use the previous search text (or empty if none)
2. Select all text in the search bar and give it focus
  • Loading branch information
iscle authored Jan 5, 2024
1 parent 38e64fa commit faeae08
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion jadx-gui/src/main/java/jadx/gui/ui/codearea/CodePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public CodePanel(AbstractCodeArea codeArea) {

@Override
public void actionPerformed(ActionEvent e) {
searchBar.toggle();
searchBar.showAndFocus();
}
});
JMenuItem searchItem = new JMenuItem();
Expand Down
20 changes: 19 additions & 1 deletion jadx-gui/src/main/java/jadx/gui/ui/codearea/SearchBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ public void keyReleased(KeyEvent e) {
setVisible(false);
}

/*
* Replicates IntelliJ's search bar behavior
* 1.1. If the user has selected text, use that as the search text
* 1.2. Otherwise, use the previous search text (or empty if none)
* 2. Select all text in the search bar and give it focus
*/
public void showAndFocus() {
setVisible(true);

String selectedText = rTextArea.getSelectedText();
if (!StringUtils.isEmpty(selectedText)) {
searchField.setText(selectedText);
}

searchField.selectAll();
searchField.requestFocus();
}

public void toggle() {
boolean visible = !isVisible();
setVisible(visible);
Expand All @@ -149,8 +167,8 @@ public void toggle() {
if (!StringUtils.isEmpty(preferText)) {
searchField.setText(preferText);
}
searchField.requestFocus();
searchField.selectAll();
searchField.requestFocus();
} else {
rTextArea.requestFocus();
}
Expand Down

0 comments on commit faeae08

Please sign in to comment.