@@ -12,15 +12,14 @@ import com.intellij.util.ui.JBUI
1212import com.intellij.util.ui.components.BorderLayoutPanel
1313import java.awt.Color
1414import java.awt.Dimension
15- import java.awt.Graphics
1615import java.awt.Insets
1716import java.awt.event.MouseAdapter
1817import java.awt.event.MouseEvent
1918import javax.swing.Box
2019import javax.swing.JComponent
2120
2221class EditorPadding (private val editor : Editor , pad : Int ) :
23- Box .Filler (Dimension (pad, 0 ), Dimension (pad, 0 ), Dimension (pad, 32767 )) {
22+ Box .Filler (Dimension (pad, pad ), Dimension (pad, pad ), Dimension (pad, pad )) {
2423 init {
2524 setOpaque(true )
2625 editor.caretModel.addCaretListener(object : CaretListener {
@@ -30,76 +29,74 @@ class EditorPadding(private val editor: Editor, pad: Int) :
3029 })
3130 }
3231
33- override fun getBackground (): Color {
34- return editor.contentComponent.getBackground()
35- }
36-
37- override fun paintComponent (g : Graphics ) {
38- super .paintComponent(g)
39- }
32+ override fun getBackground (): Color = editor.contentComponent.getBackground()
4033}
4134
4235
4336class EditorFragment (private val editor : EditorEx , message : CompletableMessage ) {
4437 private val editorLineThreshold = 6
4538 private val expandCollapseTextLabel: JBLabel = JBLabel (message.getRole().roleName(), 0 ).apply {
46- setOpaque( true )
39+ isOpaque = true
4740 isVisible = false
4841 }
4942
50- private val content: BorderLayoutPanel
43+ private val content: BorderLayoutPanel = createContentPanel()
5144 private var collapsed = false
5245
53- init {
54- content = object : BorderLayoutPanel () {
46+ private fun createContentPanel (): BorderLayoutPanel {
47+ return object : BorderLayoutPanel () {
5548 override fun getPreferredSize (): Dimension {
5649 val preferredSize = super .getPreferredSize()
57- val lineCount = editor.document.lineCount
58- val shouldCollapse = lineCount > editorLineThreshold
59- if (shouldCollapse && getCollapsed()) {
50+ if (editor.document.lineCount > editorLineThreshold && collapsed) {
6051 val lineHeight = editor.lineHeight
61- val insets = editor.scrollPane.getInsets()
62- val height = lineHeight * editorLineThreshold + insets.height
63-
64- var editorMaxHeight = height + expandCollapseTextLabel.preferredSize.height + getInsets().height
65- val header = editor.headerComponent
66- if (header != null ) {
67- editorMaxHeight + = header.getPreferredSize().height
68- }
69-
52+ val insets = editor.scrollPane.insets
53+ val editorMaxHeight = calculateMaxHeight(lineHeight, insets)
7054 return Dimension (preferredSize.width, editorMaxHeight)
7155 }
72-
7356 return preferredSize
7457 }
75- }
7658
77- content.setBorder(
78- JBUI .Borders .compound(
59+ private fun calculateMaxHeight (lineHeight : Int , insets : Insets ): Int {
60+ val height = lineHeight * editorLineThreshold + insets.height
61+ val headerHeight = editor.headerComponent?.preferredSize?.height ? : 0
62+ val labelHeight = expandCollapseTextLabel.preferredSize.height
63+ return height + headerHeight + labelHeight + insets().height
64+ }
65+ }.apply {
66+ border = JBUI .Borders .compound(
7967 JBUI .Borders .empty(10 , 0 ),
8068 JBUI .Borders .customLine(JBColor (0xD4E1570 , 0x474071 ))
8169 )
82- )
83- content.setOpaque(false )
84- content.addToLeft((EditorPadding (editor, 5 )))
85- content.addToCenter((editor.component))
86- content.addToRight((EditorPadding (editor, 5 )))
87- content.addToBottom((expandCollapseTextLabel))
70+ isOpaque = false
71+
72+ addToLeft(EditorPadding (editor, 5 ))
73+ addToCenter(editor.component)
74+ addToRight(EditorPadding (editor, 5 ))
75+ addToBottom(expandCollapseTextLabel)
76+ }
77+ }
8878
79+ init {
8980 expandCollapseTextLabel.addMouseListener(object : MouseAdapter () {
9081 override fun mouseClicked (e : MouseEvent ? ) {
91- setCollapsed( ! getCollapsed() )
82+ toggleCollapsedState( )
9283 }
9384 })
9485 }
9586
9687 fun getContent (): JComponent = content
9788
98- fun getCollapsed (): Boolean = collapsed
89+ fun isCollapsed (): Boolean = collapsed
9990
10091 fun setCollapsed (value : Boolean ) {
101- collapsed = value
102- updateExpandCollapseLabel()
92+ if (collapsed != value) {
93+ collapsed = value
94+ updateExpandCollapseLabel()
95+ }
96+ }
97+
98+ private fun toggleCollapsedState () {
99+ setCollapsed(! collapsed)
103100 }
104101
105102 fun updateExpandCollapseLabel () {
0 commit comments