Skip to content

Commit 626b05a

Browse files
authored
Merge pull request #518 from devoxx/feat-508
Feat #508 : Always focus on input field when plugin window is activated
2 parents 030bfa2 + eb4036c commit 626b05a

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/main/java/com/devoxx/genie/ui/component/input/PromptInputArea.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,32 @@
77
import com.devoxx.genie.ui.topic.AppTopics;
88
import com.devoxx.genie.util.MessageBusUtil;
99
import com.intellij.openapi.application.ApplicationManager;
10+
import com.intellij.openapi.application.ModalityState;
1011
import com.intellij.openapi.project.Project;
1112

1213
import com.intellij.openapi.util.SystemInfo;
14+
import com.intellij.openapi.wm.ToolWindowManager;
15+
import com.intellij.openapi.wm.ex.ToolWindowManagerListener;
1316
import lombok.Getter;
1417
import org.jetbrains.annotations.NotNull;
1518

1619
import javax.swing.*;
1720
import java.awt.*;
1821
import java.util.ResourceBundle;
1922

23+
import static com.devoxx.genie.ui.util.WindowPluginUtil.TOOL_WINDOW_ID;
24+
2025
@Getter
21-
public class PromptInputArea extends JPanel implements ShortcutChangeListener {
26+
public class PromptInputArea extends JPanel implements ShortcutChangeListener, ToolWindowManagerListener {
2227
private final CommandAutoCompleteTextField inputField;
2328
private final SearchOptionsPanel searchOptionsPanel;
2429
private final ResourceBundle resourceBundle;
30+
private final Project project;
31+
private String lastActiveId = null;
2532

2633
public PromptInputArea(Project project, @NotNull ResourceBundle resourceBundle) {
2734
super(new BorderLayout());
28-
35+
this.project = project;
2936
this.resourceBundle = resourceBundle;
3037

3138
// Create main input area panel
@@ -59,8 +66,34 @@ public PromptInputArea(Project project, @NotNull ResourceBundle resourceBundle)
5966

6067
add(inputAreaPanel, BorderLayout.CENTER);
6168

69+
ApplicationManager.getApplication().invokeLater(inputField::requestFocusInWindow);
70+
6271
MessageBusUtil.subscribe(project.getMessageBus().connect(),
6372
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;
6497
}
6598

6699
public String getText() {

src/main/resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<LI>Feat #515 : Supports LMStudio BETA /api/v0/ endpoint, collecting window context by @stephanj</LI>
4141
<LI>Fix #512: Files appear to be added multiple times by @mydeveloperplanet</LI>
4242
<LI>Feat #514 : Show image icon in file list when images are used by @stephanj</LI>
43+
<LI>Feat #508 : Always focus input field when chat window is opened by @stephanj</LI>
4344
</UL>
4445
<h2>V0.4.16</h2>
4546
<UL>

0 commit comments

Comments
 (0)