feat: make the file tag labels colorful depending on its state in VCS

This commit is contained in:
Carl-Robert Linnupuu 2025-11-13 11:08:15 +00:00
parent 550fcce797
commit 8e567302dc

View file

@ -5,6 +5,7 @@ import com.intellij.icons.AllIcons.Actions.Close
import com.intellij.openapi.components.service
import com.intellij.openapi.editor.colors.EditorColorsManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.vcs.FileStatusManager
import com.intellij.ui.components.JBLabel
import com.intellij.util.IconUtil
import com.intellij.util.ui.JBUI
@ -20,7 +21,12 @@ abstract class TagPanel(
private val shouldPreventDeselection: Boolean = true,
protected val project: Project,
) : JToggleButton() {
private val label = TagLabel(tagDetails.name, tagDetails.icon, tagDetails.selected)
private val label = TagLabel(
tagDetails.name,
tagDetails.icon,
tagDetails.selected,
getForegroundColor(tagDetails)
)
private val closeButton = CloseButton {
isVisible = isSelected && tagDetails.isRemovable
onClose()
@ -35,9 +41,27 @@ abstract class TagPanel(
abstract fun onClose()
private fun getForegroundColor(tagDetails: TagDetails): Color? {
return getVirtualFile(tagDetails)?.let { file ->
FileStatusManager.getInstance(project).getStatus(file).color
}
}
private fun getVirtualFile(tagDetails: TagDetails) = when (tagDetails) {
is EditorTagDetails -> tagDetails.virtualFile
is FileTagDetails -> tagDetails.virtualFile
is SelectionTagDetails -> tagDetails.virtualFile
is EditorSelectionTagDetails -> tagDetails.virtualFile
else -> null
}
fun update(text: String, icon: Icon? = null) {
val foregroundColor = getVirtualFile(tagDetails)?.let { file ->
FileStatusManager.getInstance(project).getStatus(file).color
}
closeButton.isVisible = tagDetails.isRemovable
label.update(text, icon, isSelected)
label.update(text, icon, isSelected, foregroundColor)
tagDetails.getTooltipText()?.let { tooltip ->
val relativeTooltipText = toProjectRelative(tooltip)
this.toolTipText = relativeTooltipText
@ -137,26 +161,33 @@ abstract class TagPanel(
private class TagLabel(
name: String,
icon: Icon? = null,
selected: Boolean = true
selected: Boolean = true,
foregroundColor: Color? = null
) : JBLabel(name) {
init {
update(name, icon, selected)
update(name, icon, selected, foregroundColor)
}
fun update(selected: Boolean) {
update(text, icon, selected)
}
fun update(name: String, icon: Icon? = null, selected: Boolean = true) {
fun update(
name: String,
icon: Icon? = null,
selected: Boolean = true,
foregroundColor: Color? = null
) {
text = name
cursor = Cursor(Cursor.HAND_CURSOR)
font = JBUI.Fonts.miniFont()
foreground = if (selected) {
service<EditorColorsManager>().globalScheme.defaultForeground
} else {
JBUI.CurrentTheme.Label.disabledForeground(false)
}
foreground = foregroundColor
?: if (selected) {
service<EditorColorsManager>().globalScheme.defaultForeground
} else {
JBUI.CurrentTheme.Label.disabledForeground(false)
}
icon?.let {
this.icon = IconUtil.scale(it, null, 0.65f)
}
@ -209,4 +240,4 @@ class SelectionTagPanel(
override fun onClose() {
(tagDetails as? EditorSelectionTagDetails)?.selectionModel?.removeSelection()
}
}
}