From faeae086d120f9625023a0d94dae63c90225fce3 Mon Sep 17 00:00:00 2001 From: Iscle Date: Fri, 5 Jan 2024 19:31:34 +0100 Subject: [PATCH] feat(gui): improve search bar behavior when using key bindings (PR #2074) 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 --- .../java/jadx/gui/ui/codearea/CodePanel.java | 2 +- .../java/jadx/gui/ui/codearea/SearchBar.java | 20 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/jadx-gui/src/main/java/jadx/gui/ui/codearea/CodePanel.java b/jadx-gui/src/main/java/jadx/gui/ui/codearea/CodePanel.java index 393bffa91e0..dc558cb5fa6 100644 --- a/jadx-gui/src/main/java/jadx/gui/ui/codearea/CodePanel.java +++ b/jadx-gui/src/main/java/jadx/gui/ui/codearea/CodePanel.java @@ -64,7 +64,7 @@ public CodePanel(AbstractCodeArea codeArea) { @Override public void actionPerformed(ActionEvent e) { - searchBar.toggle(); + searchBar.showAndFocus(); } }); JMenuItem searchItem = new JMenuItem(); diff --git a/jadx-gui/src/main/java/jadx/gui/ui/codearea/SearchBar.java b/jadx-gui/src/main/java/jadx/gui/ui/codearea/SearchBar.java index d00652ea340..8489f2457e5 100644 --- a/jadx-gui/src/main/java/jadx/gui/ui/codearea/SearchBar.java +++ b/jadx-gui/src/main/java/jadx/gui/ui/codearea/SearchBar.java @@ -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); @@ -149,8 +167,8 @@ public void toggle() { if (!StringUtils.isEmpty(preferText)) { searchField.setText(preferText); } - searchField.requestFocus(); searchField.selectAll(); + searchField.requestFocus(); } else { rTextArea.requestFocus(); }