|
7 | 7 | import com.devoxx.genie.ui.topic.AppTopics; |
8 | 8 | import com.devoxx.genie.util.MessageBusUtil; |
9 | 9 | import com.intellij.openapi.application.ApplicationManager; |
| 10 | +import com.intellij.openapi.application.ModalityState; |
10 | 11 | import com.intellij.openapi.project.Project; |
11 | 12 |
|
12 | 13 | import com.intellij.openapi.util.SystemInfo; |
| 14 | +import com.intellij.openapi.wm.ToolWindowManager; |
| 15 | +import com.intellij.openapi.wm.ex.ToolWindowManagerListener; |
13 | 16 | import lombok.Getter; |
14 | 17 | import org.jetbrains.annotations.NotNull; |
15 | 18 |
|
16 | 19 | import javax.swing.*; |
17 | 20 | import java.awt.*; |
18 | 21 | import java.util.ResourceBundle; |
19 | 22 |
|
| 23 | +import static com.devoxx.genie.ui.util.WindowPluginUtil.TOOL_WINDOW_ID; |
| 24 | + |
20 | 25 | @Getter |
21 | | -public class PromptInputArea extends JPanel implements ShortcutChangeListener { |
| 26 | +public class PromptInputArea extends JPanel implements ShortcutChangeListener, ToolWindowManagerListener { |
22 | 27 | private final CommandAutoCompleteTextField inputField; |
23 | 28 | private final SearchOptionsPanel searchOptionsPanel; |
24 | 29 | private final ResourceBundle resourceBundle; |
| 30 | + private final Project project; |
| 31 | + private String lastActiveId = null; |
25 | 32 |
|
26 | 33 | public PromptInputArea(Project project, @NotNull ResourceBundle resourceBundle) { |
27 | 34 | super(new BorderLayout()); |
28 | | - |
| 35 | + this.project = project; |
29 | 36 | this.resourceBundle = resourceBundle; |
30 | 37 |
|
31 | 38 | // Create main input area panel |
@@ -59,8 +66,34 @@ public PromptInputArea(Project project, @NotNull ResourceBundle resourceBundle) |
59 | 66 |
|
60 | 67 | add(inputAreaPanel, BorderLayout.CENTER); |
61 | 68 |
|
| 69 | + ApplicationManager.getApplication().invokeLater(inputField::requestFocusInWindow); |
| 70 | + |
62 | 71 | MessageBusUtil.subscribe(project.getMessageBus().connect(), |
63 | 72 | AppTopics.SHORTCUT_CHANGED_TOPIC, this); |
| 73 | + |
| 74 | + // Request focus when tool window is activated or switched from another plugin window |
| 75 | + project.getMessageBus().connect().subscribe(ToolWindowManagerListener.TOPIC, this); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void stateChanged(@NotNull ToolWindowManager toolWindowManager) { |
| 80 | + String currentActiveId = null; |
| 81 | + var activeToolWindowId = toolWindowManager.getActiveToolWindowId(); |
| 82 | + |
| 83 | + if (activeToolWindowId != null) { |
| 84 | + currentActiveId = activeToolWindowId; |
| 85 | + } |
| 86 | + |
| 87 | + // Only focus when our window becomes active from another window |
| 88 | + if (TOOL_WINDOW_ID.equals(currentActiveId) && !TOOL_WINDOW_ID.equals(lastActiveId)) { |
| 89 | + ApplicationManager.getApplication().invokeLater(() -> { |
| 90 | + if (inputField != null && inputField.isDisplayable()) { |
| 91 | + inputField.requestFocusInWindow(); |
| 92 | + } |
| 93 | + }, ModalityState.nonModal()); |
| 94 | + } |
| 95 | + |
| 96 | + lastActiveId = currentActiveId; |
64 | 97 | } |
65 | 98 |
|
66 | 99 | public String getText() { |
|
0 commit comments