@@ -3,19 +3,33 @@ package cc.unitmesh.devti.sketch.ui
3
3
import cc.unitmesh.devti.sketch.ui.code.CodeHighlightSketch
4
4
import cc.unitmesh.devti.sketch.ui.plan.MarkdownPlanParser
5
5
import cc.unitmesh.devti.sketch.ui.plan.PlanItem
6
+ import com.intellij.icons.AllIcons
6
7
import com.intellij.lang.Language
8
+ import com.intellij.openapi.actionSystem.ActionManager
9
+ import com.intellij.openapi.actionSystem.AnAction
10
+ import com.intellij.openapi.actionSystem.AnActionEvent
11
+ import com.intellij.openapi.actionSystem.DefaultActionGroup
12
+ import com.intellij.openapi.fileEditor.FileEditorManager
7
13
import com.intellij.openapi.project.Project
14
+ import com.intellij.openapi.ui.popup.JBPopup
15
+ import com.intellij.openapi.ui.popup.JBPopupFactory
16
+ import com.intellij.openapi.ui.popup.util.MinimizeButton
8
17
import com.intellij.ui.components.JBCheckBox
9
18
import com.intellij.ui.components.JBPanel
10
19
import com.intellij.ui.components.JBScrollPane
20
+ import com.intellij.ui.components.panels.Wrapper
11
21
import com.intellij.util.ui.JBEmptyBorder
12
22
import com.intellij.util.ui.JBUI
23
+ import com.intellij.util.ui.UIUtil
13
24
import java.awt.BorderLayout
14
25
import java.awt.FlowLayout
26
+ import java.awt.event.MouseAdapter
27
+ import java.awt.event.MouseEvent
15
28
import javax.swing.Box
16
29
import javax.swing.BoxLayout
17
30
import javax.swing.JComponent
18
31
import javax.swing.JLabel
32
+ import javax.swing.JPanel
19
33
20
34
class ThoughtPlanSketchProvider : LanguageSketchProvider {
21
35
override fun isSupported (lang : String ): Boolean = lang == " plan"
@@ -43,26 +57,90 @@ class PlanSketch(
43
57
layout = BoxLayout (this , BoxLayout .Y_AXIS )
44
58
border = JBEmptyBorder (JBUI .insets(8 ))
45
59
}
60
+
61
+ private val actionGroup = DefaultActionGroup (createConsoleActions())
62
+ private val toolbar = ActionManager .getInstance().createActionToolbar(" PlanSketch" , actionGroup, true ).apply {
63
+ targetComponent = panel
64
+ }
65
+
66
+ private val titleLabel = JLabel (" Thought Plan" ).apply {
67
+ border = JBUI .Borders .empty(0 , 10 )
68
+ }
69
+
70
+ private val toolbarPanel = JPanel (BorderLayout ()).apply {
71
+ add(titleLabel, BorderLayout .WEST )
72
+ add(toolbar.component, BorderLayout .EAST )
73
+ }
74
+
75
+ private val toolbarWrapper = Wrapper (JBUI .Panels .simplePanel(toolbarPanel)).also {
76
+ it.border = JBUI .Borders .customLine(UIUtil .getBoundsColor(), 1 , 1 , 1 , 1 )
77
+ }
46
78
47
79
init {
48
80
createPlanUI()
49
81
50
82
val scrollPane = JBScrollPane (contentPanel)
51
83
panel.add(scrollPane, BorderLayout .CENTER )
84
+ panel.add(toolbarWrapper, BorderLayout .NORTH )
85
+
52
86
add(panel, BorderLayout .CENTER )
53
87
}
88
+
89
+ private fun createConsoleActions (): List <AnAction > {
90
+ val popupAction = object : AnAction (" Popup" , " Show in popup window" , AllIcons .Ide .External_link_arrow ) {
91
+ override fun displayTextInToolbar (): Boolean = true
92
+
93
+ override fun actionPerformed (e : AnActionEvent ) {
94
+ executePopup().mouseClicked(null )
95
+ }
96
+ }
97
+
98
+ return listOf (popupAction)
99
+ }
100
+
101
+ private fun executePopup (): MouseAdapter = object : MouseAdapter () {
102
+ override fun mouseClicked (e : MouseEvent ? ) {
103
+ var popup: JBPopup ? = null
104
+ popup = JBPopupFactory .getInstance()
105
+ .createComponentPopupBuilder(panel, null )
106
+ .setProject(project)
107
+ .setResizable(true )
108
+ .setMovable(true )
109
+ .setTitle(" Thought Plan" )
110
+ .setCancelButton(MinimizeButton (" Hide" ))
111
+ .setCancelCallback {
112
+ popup?.cancel()
113
+ // Return the panel to its original location
114
+ remove(panel)
115
+ add(panel, BorderLayout .CENTER )
116
+ revalidate()
117
+ repaint()
118
+ true
119
+ }
120
+ .setFocusable(true )
121
+ .setRequestFocus(true )
122
+ .createPopup()
123
+
124
+ val editor = FileEditorManager .getInstance(project).selectedTextEditor
125
+ if (editor != null ) {
126
+ popup.showInBestPositionFor(editor)
127
+ } else {
128
+ popup.showInFocusCenter()
129
+ }
130
+ }
131
+ }
54
132
55
133
private fun createPlanUI () {
56
134
planItems.forEachIndexed { index, planItem ->
57
135
val titlePanel = JBPanel <JBPanel <* >>(FlowLayout (FlowLayout .LEFT )).apply {
58
136
border = JBUI .Borders .empty()
59
137
}
60
138
61
- val titleText = if (planItem.completed)
62
- " <html><b>${index + 1 } . ${planItem.title} ✓</b></html>"
63
- else
139
+ val titleText = if (planItem.completed)
140
+ " <html><b>${index + 1 } . ${planItem.title} ✓</b></html>"
141
+ else
64
142
" <html><b>${index + 1 } . ${planItem.title} </b></html>"
65
-
143
+
66
144
val sectionLabel = JLabel (titleText)
67
145
sectionLabel.border = JBUI .Borders .empty()
68
146
titlePanel.add(sectionLabel)
0 commit comments