Skip to content

Commit

Permalink
feat(editor): replace variable panel with table and add refresh action
Browse files Browse the repository at this point in the history
…#157

- Replace the variable panel's custom layout with a JBTable for better scalability and usability.
- Add a refresh action to manually update the preview output.
- Remove automatic document re-parsing to improve performance and control.
  • Loading branch information
phodal committed Jan 4, 2025
1 parent e967bf8 commit 1dca503
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,20 @@ class ShireFileEditorWithPreview(
private fun createActionGroup(project: Project, virtualFile: VirtualFile, editor: Editor): ActionGroup {
return DefaultActionGroup(
showPreviewAction(project, virtualFile, editor),
createRefreshAction(project),
Separator(),
createHelpAction(project)
)
}

private fun createRefreshAction(project: Project): AnAction {
return object : AnAction("Refresh Preview", "Refresh Preview", AllIcons.Actions.Refresh) {
override fun actionPerformed(e: AnActionEvent) {
preview.updateOutput()
}
}
}

private fun createHelpAction(project: Project): AnAction {
val idleIcon = AllIcons.Actions.Help
return object : AnAction("Help", "Help", idleIcon) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,11 @@ open class ShirePreviewEditor(
}

this.mainPanel.add(corePanel, BorderLayout.CENTER)
mainEditor.value?.document?.addDocumentListener(ReparseContentDocumentListener())
// 删除自动更新监听
// mainEditor.value?.document?.addDocumentListener(ReparseContentDocumentListener())
updateOutput()
}

private inner class ReparseContentDocumentListener : DocumentListener {
override fun documentChanged(event: DocumentEvent) {
updateOutput()
}
}

fun updateOutput() {
ApplicationManager.getApplication().invokeLater {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,103 +11,61 @@ import com.intellij.ui.components.JBLabel
import com.intellij.ui.components.JBPanel
import com.intellij.ui.components.JBScrollPane
import com.intellij.util.ui.JBUI
import com.intellij.ui.table.JBTable
import java.awt.BorderLayout
import java.awt.FlowLayout
import java.awt.GridBagConstraints
import java.awt.GridBagLayout
import java.awt.datatransfer.StringSelection
import javax.swing.Box
import javax.swing.JPanel
import javax.swing.ScrollPaneConstants
import javax.swing.table.DefaultTableModel

class ShireVariablePanel : JPanel(BorderLayout()) {
private val contentPanel = JBPanel<JBPanel<*>>(GridBagLayout())
private val contentPanel = JBPanel<JBPanel<*>>(BorderLayout())
private val tableModel = DefaultTableModel(arrayOf("Key", "Value", ""), 0)

init {
val table = JBTable(tableModel).apply {
tableHeader.reorderingAllowed = true
tableHeader.resizingAllowed = true
setShowGrid(true)
gridColor = JBColor.PanelBackground
intercellSpacing = JBUI.size(0, 0)
}

val scrollPane = JBScrollPane(
contentPanel,
table,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
)
).apply {
minimumSize = JBUI.size(0, 200)
preferredSize = JBUI.size(0, 200)
}
add(scrollPane, BorderLayout.CENTER)
setupPanel()
}

private fun setupPanel() {
contentPanel.background = JBColor(0xF5F5F5, 0x2B2D30)
contentPanel.border = JBUI.Borders.empty(4)

// 添加标题
val titleLabel = JBLabel("Variables").apply {
font = JBUI.Fonts.label(14f).asBold()
border = JBUI.Borders.empty(4, 8)
}

contentPanel.add(titleLabel, GridBagConstraints().apply {
fill = GridBagConstraints.HORIZONTAL
anchor = GridBagConstraints.NORTHWEST
weightx = 1.0
gridx = 0
insets = JBUI.insets(2)
})

contentPanel.add(titleLabel, BorderLayout.NORTH)
}

fun updateVariables(variables: Map<String, Any>) {
contentPanel.removeAll()
setupPanel()
tableModel.rowCount = 0 // 清空表格内容

var gridy = 1
variables.forEach { (key, value) ->
addVariableRow(key, value, gridy++)
val valueStr = value.toString()
tableModel.addRow(arrayOf(key, valueStr, createCopyButton(valueStr)))
}

// 填充剩余空间
contentPanel.add(Box.createVerticalGlue(), GridBagConstraints().apply {
fill = GridBagConstraints.HORIZONTAL
anchor = GridBagConstraints.NORTHWEST
weightx = 1.0
weighty = 1.0
gridx = 0
gridy = gridy
})

revalidate()
repaint()
}

private fun addVariableRow(key: String, value: Any, gridy: Int) {
val varPanel = JBPanel<JBPanel<*>>(FlowLayout(FlowLayout.LEFT, 0, 0))
varPanel.isOpaque = false
varPanel.border = JBUI.Borders.compound(
JBUI.Borders.customLine(JBColor(0xE6E6E6, 0x3C3F41), 0, 0, 1, 0),
JBUI.Borders.empty(6, 8)
)

varPanel.add(createLabel(key, true))
varPanel.add(JBLabel(": ").apply {
foreground = JBColor(0x666666, 0x999999)
})

val valueStr = value.toString()
varPanel.add(createLabel(valueStr, false))
varPanel.add(Box.createHorizontalStrut(4))
varPanel.add(createCopyButton(valueStr))

contentPanel.add(varPanel, GridBagConstraints().apply {
fill = GridBagConstraints.HORIZONTAL
anchor = GridBagConstraints.NORTHWEST
weightx = 1.0
gridx = 0
this.gridy = gridy
insets = JBUI.insets(2)
})
}

private fun createLabel(text: String, isKey: Boolean) = JBLabel(text).apply {
font = JBUI.Fonts.label(11f)
foreground = if (isKey) JBColor(0x666666, 0x999999) else JBColor(0x000000, 0xCCCCCC)
}

private fun createCopyButton(text: String) = ActionButton(
object : AnAction(AllIcons.Actions.Copy) {
override fun actionPerformed(e: AnActionEvent) {
Expand Down

0 comments on commit 1dca503

Please sign in to comment.