diff --git a/exts/ext-terminal/src/main/kotlin/cc/unitmesh/terminal/sketch/TerminalLangSketchProvider.kt b/exts/ext-terminal/src/main/kotlin/cc/unitmesh/terminal/sketch/TerminalLangSketchProvider.kt index a0a7aec90..58ad10513 100644 --- a/exts/ext-terminal/src/main/kotlin/cc/unitmesh/terminal/sketch/TerminalLangSketchProvider.kt +++ b/exts/ext-terminal/src/main/kotlin/cc/unitmesh/terminal/sketch/TerminalLangSketchProvider.kt @@ -10,13 +10,10 @@ import com.intellij.lang.Language import com.intellij.openapi.fileEditor.FileEditorManager import com.intellij.openapi.project.Project import com.intellij.openapi.project.guessProjectDir -import com.intellij.openapi.ui.DialogPanel import com.intellij.openapi.ui.popup.JBPopup import com.intellij.openapi.ui.popup.JBPopupFactory import com.intellij.openapi.ui.popup.util.MinimizeButton import com.intellij.terminal.JBTerminalWidget -import com.intellij.ui.JBColor -import com.intellij.ui.components.panels.VerticalLayout import com.intellij.util.ui.JBUI import org.jetbrains.plugins.terminal.LocalTerminalDirectRunner import java.awt.BorderLayout @@ -35,7 +32,7 @@ class TerminalLangSketchProvider : LanguageSketchProvider { var content = content return object : ExtensionLangSketch { var terminalWidget: JBTerminalWidget? = null - var layout: JPanel? = null + var panelLayout: JPanel? = null init { val projectDir = project.guessProjectDir()?.path @@ -44,63 +41,26 @@ class TerminalLangSketchProvider : LanguageSketchProvider { it.preferredSize = Dimension(it.preferredSize.width, 120) } - layout = object : JPanel(BorderLayout()) { + panelLayout = object : JPanel(BorderLayout()) { init { add(JLabel("Terminal").also { it.border = JBUI.Borders.empty(5, 0) }, BorderLayout.NORTH) add(terminalWidget!!.component, BorderLayout.CENTER) + val buttonPanel = JPanel(BorderLayout()) buttonPanel.add(JButton(AllIcons.Toolwindows.ToolWindowRun).apply { - addMouseListener(object : MouseAdapter() { - override fun mouseClicked(e: MouseEvent?) { - val tempShellName = "temp" + System.currentTimeMillis() + ".sh" - val language = Language.findLanguageByID("Shell Script")!! - val scratchFile = ScratchRootType.getInstance() - .createScratchFile(project, tempShellName, language, content) - ?: return - - try { - RunService.provider(project, scratchFile) - ?.runFile(project, scratchFile, null, isFromToolAction = true) - ?: AutoDevNotifications.notify(project, "Run Failed, no provider") - } catch (e: Exception) { - AutoDevNotifications.notify(project, "Run Failed: ${e.message}") - } - } - }) + addMouseListener(executeShellScriptOnClick(project, content)) }, BorderLayout.WEST) buttonPanel.add(JButton("Pop up Terminal").apply { - addMouseListener(object : MouseAdapter() { - override fun mouseClicked(e: MouseEvent?) { - var popup: JBPopup? = null - popup = JBPopupFactory.getInstance() - .createComponentPopupBuilder(terminalWidget!!.component, null) - .setProject(project) - .setResizable(true) - .setMovable(true) - .setTitle("Terminal") - .setCancelButton(MinimizeButton("Hide")) - .setCancelOnClickOutside(true) - .setCancelOnWindowDeactivation(false) - .setCancelCallback { - layout!!.addLayoutComponent("terminal", terminalWidget!!.component) - true - } - .setFocusable(true) - .setRequestFocus(true) - .createPopup() - - val editor = FileEditorManager.getInstance(project).selectedTextEditor!! - popup.showInBestPositionFor(editor) - } - }) + addMouseListener(executePopup(terminalWidget, project, panelLayout)) }, BorderLayout.EAST) + add(buttonPanel, BorderLayout.SOUTH) } } - layout!!.border = JBUI.Borders.compound( + panelLayout!!.border = JBUI.Borders.compound( JBUI.Borders.empty(5, 10), ) } @@ -115,11 +75,61 @@ class TerminalLangSketchProvider : LanguageSketchProvider { terminalWidget!!.terminalStarter?.sendString(content, true) } - override fun getComponent(): JComponent = layout!! + override fun getComponent(): JComponent = panelLayout!! override fun updateLanguage(language: Language?, originLanguage: String?) {} override fun dispose() {} } } + + private fun executeShellScriptOnClick( + project: Project, + content: String + ): MouseAdapter = object : MouseAdapter() { + override fun mouseClicked(e: MouseEvent?) { + val tempShellName = "temp" + System.currentTimeMillis() + ".sh" + val language = Language.findLanguageByID("Shell Script")!! + val scratchFile = ScratchRootType.getInstance() + .createScratchFile(project, tempShellName, language, content) + ?: return + + try { + RunService.provider(project, scratchFile) + ?.runFile(project, scratchFile, null, isFromToolAction = true) + ?: AutoDevNotifications.notify(project, "Run Failed, no provider") + } catch (e: Exception) { + AutoDevNotifications.notify(project, "Run Failed: ${e.message}") + } + } + } + + private fun executePopup(terminalWidget: JBTerminalWidget?, project: Project, panelLayout: JPanel?): MouseAdapter = + object : MouseAdapter() { + override fun mouseClicked(e: MouseEvent?) { + var popup: JBPopup? = null + popup = JBPopupFactory.getInstance() + .createComponentPopupBuilder(terminalWidget!!.component, null) + .setProject(project) + .setResizable(true) + .setMovable(true) + .setTitle("Terminal") + .setCancelButton(MinimizeButton("Hide")) + .setCancelOnClickOutside(true) + .setCancelOnWindowDeactivation(false) + .setCancelCallback { + panelLayout!!.add("terminal", terminalWidget!!.component) + popup!!.closeOk(null) + panelLayout.revalidate() + panelLayout.repaint() + true + } + .setFocusable(true) + .setRequestFocus(true) + .createPopup() + + val editor = FileEditorManager.getInstance(project).selectedTextEditor!! + popup.showInBestPositionFor(editor) + } + } }