|
| 1 | +package com.snow.plugin.toolwindow |
| 2 | + |
| 3 | +import com.intellij.openapi.application.ApplicationManager |
| 4 | +import com.intellij.openapi.project.DumbAware |
| 5 | +import com.intellij.openapi.project.Project |
| 6 | +import com.intellij.openapi.wm.ToolWindow |
| 7 | +import com.intellij.openapi.wm.ToolWindowFactory |
| 8 | +import com.intellij.ui.components.JBLabel |
| 9 | +import com.intellij.ui.content.ContentFactory |
| 10 | +import com.snow.plugin.SnowWebSocketManager |
| 11 | +import org.jetbrains.plugins.terminal.ShellTerminalWidget |
| 12 | +import org.jetbrains.plugins.terminal.TerminalToolWindowManager |
| 13 | +import java.awt.BorderLayout |
| 14 | +import javax.swing.JPanel |
| 15 | + |
| 16 | +/** |
| 17 | + * Factory for Snow CLI Tool Window |
| 18 | + * Launches Snow CLI each time tool window is activated |
| 19 | + */ |
| 20 | +class SnowToolWindowFactory : ToolWindowFactory, DumbAware { |
| 21 | + override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) { |
| 22 | + // Create a simple content panel |
| 23 | + val contentPanel = JPanel(BorderLayout()) |
| 24 | + val label = JBLabel("Click to launch Snow CLI", javax.swing.SwingConstants.CENTER) |
| 25 | + contentPanel.add(label, BorderLayout.CENTER) |
| 26 | + |
| 27 | + val contentFactory = ContentFactory.getInstance() |
| 28 | + val content = contentFactory.createContent(contentPanel, "", false) |
| 29 | + toolWindow.contentManager.addContent(content) |
| 30 | + |
| 31 | + // Add listener to launch Snow CLI when tool window is shown/activated |
| 32 | + val listener = object : com.intellij.openapi.wm.ex.ToolWindowManagerListener { |
| 33 | + override fun stateChanged(toolWindowManager: com.intellij.openapi.wm.ToolWindowManager) { |
| 34 | + if (toolWindow.isVisible) { |
| 35 | + // Launch Snow CLI each time window becomes visible |
| 36 | + launchSnowCLI(project, toolWindow) |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + project.messageBus.connect().subscribe( |
| 42 | + com.intellij.openapi.wm.ex.ToolWindowManagerListener.TOPIC, |
| 43 | + listener |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + private fun launchSnowCLI(project: Project, toolWindow: ToolWindow) { |
| 48 | + // Use Terminal API to send command directly |
| 49 | + ApplicationManager.getApplication().invokeLater { |
| 50 | + try { |
| 51 | + val terminalManager = TerminalToolWindowManager.getInstance(project) |
| 52 | + |
| 53 | + // Create new terminal session with activateTool=true to show the terminal window |
| 54 | + val widget = terminalManager.createLocalShellWidget(project.basePath, "Snow CLI", true, true) |
| 55 | + |
| 56 | + if (widget is ShellTerminalWidget) { |
| 57 | + // Wait a bit for terminal to be ready, then send command |
| 58 | + ApplicationManager.getApplication().executeOnPooledThread { |
| 59 | + try { |
| 60 | + Thread.sleep(1000) |
| 61 | + |
| 62 | + // Send command directly to terminal using executeCommand |
| 63 | + ApplicationManager.getApplication().invokeLater { |
| 64 | + try { |
| 65 | + widget.executeCommand("snow") |
| 66 | + } catch (ex: Exception) { |
| 67 | + // Silently handle command execution failure |
| 68 | + } |
| 69 | + } |
| 70 | + } catch (ex: Exception) { |
| 71 | + // Silently handle background thread failure |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Hide Snow tool window and show terminal instead |
| 77 | + ApplicationManager.getApplication().invokeLater { |
| 78 | + toolWindow.hide(null) |
| 79 | + } |
| 80 | + } catch (ex: Exception) { |
| 81 | + // Silently handle terminal access failure |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Ensure WebSocket server is running |
| 86 | + val wsManager = SnowWebSocketManager.instance |
| 87 | + ApplicationManager.getApplication().executeOnPooledThread { |
| 88 | + Thread.sleep(500) |
| 89 | + wsManager.connect() |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | + |
0 commit comments