Skip to content

Commit

Permalink
refactor(EditorFragment): simplify padding and collapse logic
Browse files Browse the repository at this point in the history
- Remove unnecessary imports and methods.
- Simplify padding initialization in EditorPadding.
- Refactor collapse logic in EditorFragment.
- Extract common logic into helper functions.
  • Loading branch information
phodal committed Dec 8, 2024
1 parent 34794a5 commit de1586b
Showing 1 changed file with 36 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ import com.intellij.util.ui.JBUI
import com.intellij.util.ui.components.BorderLayoutPanel
import java.awt.Color
import java.awt.Dimension
import java.awt.Graphics
import java.awt.Insets
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import javax.swing.Box
import javax.swing.JComponent

class EditorPadding(private val editor: Editor, pad: Int) :
Box.Filler(Dimension(pad, 0), Dimension(pad, 0), Dimension(pad, 32767)) {
Box.Filler(Dimension(pad, pad), Dimension(pad, pad), Dimension(pad, pad)) {
init {
setOpaque(true)
editor.caretModel.addCaretListener(object : CaretListener {
Expand All @@ -30,76 +29,74 @@ class EditorPadding(private val editor: Editor, pad: Int) :
})
}

override fun getBackground(): Color {
return editor.contentComponent.getBackground()
}

override fun paintComponent(g: Graphics) {
super.paintComponent(g)
}
override fun getBackground(): Color = editor.contentComponent.getBackground()
}


class EditorFragment(private val editor: EditorEx, message: CompletableMessage) {
private val editorLineThreshold = 6
private val expandCollapseTextLabel: JBLabel = JBLabel(message.getRole().roleName(), 0).apply {
setOpaque(true)
isOpaque = true
isVisible = false
}

private val content: BorderLayoutPanel
private val content: BorderLayoutPanel = createContentPanel()
private var collapsed = false

init {
content = object : BorderLayoutPanel() {
private fun createContentPanel(): BorderLayoutPanel {
return object : BorderLayoutPanel() {
override fun getPreferredSize(): Dimension {
val preferredSize = super.getPreferredSize()
val lineCount = editor.document.lineCount
val shouldCollapse = lineCount > editorLineThreshold
if (shouldCollapse && getCollapsed()) {
if (editor.document.lineCount > editorLineThreshold && collapsed) {
val lineHeight = editor.lineHeight
val insets = editor.scrollPane.getInsets()
val height = lineHeight * editorLineThreshold + insets.height

var editorMaxHeight = height + expandCollapseTextLabel.preferredSize.height + getInsets().height
val header = editor.headerComponent
if (header != null) {
editorMaxHeight += header.getPreferredSize().height
}

val insets = editor.scrollPane.insets
val editorMaxHeight = calculateMaxHeight(lineHeight, insets)
return Dimension(preferredSize.width, editorMaxHeight)
}

return preferredSize
}
}

content.setBorder(
JBUI.Borders.compound(
private fun calculateMaxHeight(lineHeight: Int, insets: Insets): Int {
val height = lineHeight * editorLineThreshold + insets.height
val headerHeight = editor.headerComponent?.preferredSize?.height ?: 0
val labelHeight = expandCollapseTextLabel.preferredSize.height
return height + headerHeight + labelHeight + insets().height
}
}.apply {
border = JBUI.Borders.compound(
JBUI.Borders.empty(10, 0),
JBUI.Borders.customLine(JBColor(0xD4E1570, 0x474071))
)
)
content.setOpaque(false)
content.addToLeft((EditorPadding(editor, 5)))
content.addToCenter((editor.component))
content.addToRight((EditorPadding(editor, 5)))
content.addToBottom((expandCollapseTextLabel))
isOpaque = false

addToLeft(EditorPadding(editor, 5))
addToCenter(editor.component)
addToRight(EditorPadding(editor, 5))
addToBottom(expandCollapseTextLabel)
}
}

init {
expandCollapseTextLabel.addMouseListener(object : MouseAdapter() {
override fun mouseClicked(e: MouseEvent?) {
setCollapsed(!getCollapsed())
toggleCollapsedState()
}
})
}

fun getContent(): JComponent = content

fun getCollapsed(): Boolean = collapsed
fun isCollapsed(): Boolean = collapsed

fun setCollapsed(value: Boolean) {
collapsed = value
updateExpandCollapseLabel()
if (collapsed != value) {
collapsed = value
updateExpandCollapseLabel()
}
}

private fun toggleCollapsedState() {
setCollapsed(!collapsed)
}

fun updateExpandCollapseLabel() {
Expand Down

0 comments on commit de1586b

Please sign in to comment.