Skip to content

Commit

Permalink
refactor(terminal): extract mouse listeners and rename layout variable
Browse files Browse the repository at this point in the history
…#257

- Extracted `executeShellScriptOnClick` and `executePopup` as separate functions for better readability.
- Renamed `layout` variable to `panelLayout` for clarity.
- Simplified popup cancellation logic and improved component revalidation.
  • Loading branch information
phodal committed Jan 16, 2025
1 parent 50432df commit 0f67562
Showing 1 changed file with 58 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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),
)
}
Expand All @@ -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)
}
}
}

0 comments on commit 0f67562

Please sign in to comment.