refactor: Replace EmptyBorder with DPI-aware borders

- Replaced all instances of EmptyBorder with JBUI.Borders for better DPI awareness.
- Removed redundant calls to conversion methods and simplified color assignments.
- Fixed unclear button text in light themes
This commit is contained in:
liuk 2025-02-08 16:55:23 +08:00
parent f5ece3f8dc
commit f482ffd5fd
2 changed files with 27 additions and 30 deletions

View file

@ -29,7 +29,6 @@ import java.awt.geom.AffineTransform
import java.awt.geom.Path2D
import java.awt.geom.RoundRectangle2D
import javax.swing.*
import javax.swing.border.EmptyBorder
import javax.swing.event.DocumentEvent
import javax.swing.event.DocumentListener
import javax.swing.event.ListDataListener
@ -73,8 +72,8 @@ fun makePanel(
onReject: () -> Unit
): JPanel {
val topPanel =
ShadowPanel(MigLayout("wrap 1, insets 2 ${leftInset} 2 2, gap 0!")).apply {
background = JBColor(0x20888888.toInt(), 0x20888888.toInt())
ShadowPanel(MigLayout("wrap 1, insets 2 $leftInset 2 2, gap 0!")).apply {
background = JBColor(0x20888888, 0x20888888)
isOpaque = false
}
@ -374,7 +373,7 @@ class CustomPanel(
val closeIcon = IconLoader.getIcon("/icons/close.svg", javaClass)
return JLabel(closeIcon).apply {
background = Color(0, 0, 0, 0)
border = EmptyBorder(2, 6, 2, 0)
border = JBUI.Borders.empty(2, 6, 2, 0)
toolTipText = "`esc` to cancel"
isOpaque = false
addMouseListener(
@ -406,7 +405,7 @@ class CustomPanel(
background = defaultBackground
foreground = Color(128, 128, 128, 200)
font = UIUtil.getFontWithFallback("Arial", Font.PLAIN, 12)
border = EmptyBorder(8, 8, 8, 8)
border = JBUI.Borders.empty(8)
isOpaque = false
isEditable = false
cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)
@ -423,7 +422,7 @@ class CustomPanel(
val component =
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus)
if (component is JLabel) {
component.border = EmptyBorder(4, 6, 4, 6)
component.border = JBUI.Borders.empty(4, 6)
}
return component
}
@ -451,9 +450,9 @@ class CustomPanel(
val rightButton =
CustomButton("⏎ Enter") { onEnter() }
.apply {
background = JBColor(0x999998.toInt(), 0x999998.toInt())
foreground = JBColor(0xF5F5F5.toInt(), 0xF5F5F5.toInt())
border = EmptyBorder(2, 6, 2, 6)
background = JBColor(0x999998, 0x999998)
foreground = JBColor(0xF5F5F5, 0xF5F5F5)
border = JBUI.Borders.empty(2, 6)
}
val rightPanel = JPanel(MigLayout("insets 0, fillx")).apply {
@ -461,7 +460,7 @@ class CustomPanel(
add(rightButton, "align right")
}
border = EmptyBorder(0, 0, 16, 12)
border = JBUI.Borders.empty(0, 0, 16, 12)
isOpaque = false
add(dropdown, "align left")
@ -477,7 +476,7 @@ class CustomPanel(
val progressBar = JProgressBar().apply { isIndeterminate = true }
add(progressBar, BorderLayout.CENTER)
border = BorderFactory.createEmptyBorder(0, 0, 16, 12)
border = JBUI.Borders.empty(0, 0, 16, 12)
}
private val subPanelC: JPanel =
@ -486,21 +485,19 @@ class CustomPanel(
JLabel("Type to re-prompt").apply {
foreground = Color(156, 163, 175) // text-gray-400
font = UIUtil.getFontWithFallback("Arial", Font.PLAIN, 11)
border = EmptyBorder(0, 4, 0, 0)
border = JBUI.Borders.emptyLeft(4)
}
val leftButton =
CustomButton("Reject All (${getMetaKeyLabel()}${getShiftKeyLabel()}⌫)") { onReject() }
.apply {
background = JBColor(0x30FF0000.toInt(), 0x30FF0000.toInt())
foreground = JBColor(0xF5F5F5.toInt(), 0xF5F5F5.toInt())
background = JBColor(0x30FF0000, 0x30FF0000)
}
val rightButton =
CustomButton("Accept All (${getMetaKeyLabel()}${getShiftKeyLabel()}⏎)") { onAccept() }
.apply {
background = JBColor(0x3000FF00.toInt(), 0x3000FF00.toInt())
foreground = JBColor(0xF5F5F5.toInt(), 0xF5F5F5.toInt())
background = JBColor(0x3000FF00, 0x3000FF00)
}
val rightPanel =
@ -508,12 +505,12 @@ class CustomPanel(
isOpaque = false
add(leftButton, "align right")
add(rightButton, "align right")
border = EmptyBorder(0, 0, 0, 0)
border = JBUI.Borders.empty()
}
add(leftLabel, "align left")
add(rightPanel, "align right")
border = BorderFactory.createEmptyBorder(0, 0, 16, 12)
border = JBUI.Borders.empty(0, 0, 16, 12)
isOpaque = false
}
@ -625,7 +622,7 @@ class CustomButton(text: String, onClick: () -> Unit) : JLabel(text, CENTER) {
})
font = UIUtil.getFontWithFallback("Arial", Font.PLAIN, 11)
border = EmptyBorder(2, 6, 2, 6)
border = JBUI.Borders.empty(2, 6)
}
override fun paintComponent(g: Graphics) {
@ -694,7 +691,7 @@ class TransparentArrowButtonUI : BasicComboBoxUI() {
}
override fun getInsets(): Insets {
return JBUI.insets(0, 6, 0, 0)
return JBUI.insetsLeft(6)
}
override fun installUI(c: JComponent?) {

View file

@ -13,8 +13,8 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.ui.JBColor
import com.intellij.util.application
import com.intellij.util.ui.JBUI
import java.awt.*
import javax.swing.BorderFactory
import javax.swing.JButton
import javax.swing.JTextArea
import kotlin.math.min
@ -25,8 +25,8 @@ class VerticalDiffBlock(
var startLine: Int,
private val onAcceptReject: (VerticalDiffBlock, Boolean) -> Unit
) {
val deletedLines: MutableList<String> = mutableListOf();
val addedLines: MutableList<String> = mutableListOf();
val deletedLines: MutableList<String> = mutableListOf()
val addedLines: MutableList<String> = mutableListOf()
private val acceptButton: JButton
private val rejectButton: JButton
private var deletionInlay: Disposable? = null
@ -155,10 +155,10 @@ class VerticalDiffBlock(
val rejectBtn =
createButton(
"${getAltKeyLabel()}${getShiftKeyLabel()}N",
JBColor(0x99FF0000.toInt(), 0x99FF0000.toInt())
JBColor(0x77FF0000, 0x77FF0000)
).apply {
addActionListener {
handleReject();
handleReject()
onAcceptReject(this@VerticalDiffBlock, false)
}
@ -168,10 +168,10 @@ class VerticalDiffBlock(
createButton(
"${getAltKeyLabel()}${
getShiftKeyLabel()
}Y", JBColor(0x7700BB00.toInt(), 0x7700BB00.toInt())
}Y", JBColor(0x7700BB00, 0x7700BB00)
).apply {
addActionListener {
handleAccept();
handleAccept()
onAcceptReject(this@VerticalDiffBlock, true)
}
}
@ -215,9 +215,9 @@ class VerticalDiffBlock(
private fun createDeletionTextArea(text: String) = JTextArea(text).apply {
isEditable = false
background = JBColor(0x30FF0000.toInt(), 0x30FF0000.toInt())
background = JBColor(0x30FF0000, 0x30FF0000)
foreground = JBColor.GRAY
border = BorderFactory.createEmptyBorder()
border = JBUI.Borders.empty()
lineWrap = false
wrapStyleWord = false
font = editor.colorsScheme.getFont(EditorFontType.PLAIN)
@ -250,7 +250,7 @@ class VerticalDiffBlock(
font = Font("Arial", Font.BOLD, 9)
isContentAreaFilled = false
isOpaque = false
border = BorderFactory.createEmptyBorder(4, 2, 4, 2)
border = JBUI.Borders.empty(4, 2)
preferredSize = Dimension(preferredSize.width - 30, 14)
cursor = Cursor(Cursor.HAND_CURSOR)
}