1 // Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.vcs.commit
4 import com.intellij.openapi.project.Project
5 import com.intellij.openapi.util.Disposer
6 import com.intellij.openapi.util.registry.Registry
7 import com.intellij.openapi.util.registry.RegistryValue
8 import com.intellij.openapi.util.registry.RegistryValueListener
9 import com.intellij.openapi.vcs.VcsBundle.message
10 import com.intellij.openapi.vcs.changes.InclusionListener
11 import com.intellij.openapi.vcs.changes.ui.*
12 import com.intellij.openapi.vcs.changes.ui.ChangesViewContentManager.Companion.LOCAL_CHANGES
13 import com.intellij.ui.content.Content
14 import com.intellij.util.ui.JBUI.Borders.empty
15 import com.intellij.util.ui.JBUI.Borders.emptyRight
16 import com.intellij.util.ui.JBUI.emptyInsets
17 import com.intellij.util.ui.components.BorderLayoutPanel
19 private val isCompactCommitLegend get() = Registry.get("vcs.non.modal.commit.legend.compact")
21 private fun Project.getLocalChangesTab(): Content? =
22 ChangesViewContentManager.getInstance(this).findContents { it.tabName == LOCAL_CHANGES }.firstOrNull()
24 internal class ChangesViewCommitStatusPanel(tree: ChangesTree, private val commitWorkflowUi: CommitWorkflowUi) :
25 BorderLayoutPanel(), ChangesViewContentManagerListener, InclusionListener {
27 private val branchComponent = CurrentBranchComponent(tree.project, tree, commitWorkflowUi)
29 private val commitLegendCalculator = ChangeInfoCalculator()
30 private val commitLegend = CommitLegendPanel(commitLegendCalculator).apply {
31 component.myBorder = empty(0, 1)
32 component.ipad = emptyInsets()
35 private val project get() = branchComponent.project
40 addToRight(commitLegend.component)
41 border = emptyRight(6)
42 background = tree.background
44 commitWorkflowUi.addInclusionListener(this, commitWorkflowUi)
48 private fun setupTabUpdater() {
49 branchComponent.addChangeListener(this::updateTab, commitWorkflowUi)
50 project.messageBus.connect(commitWorkflowUi).subscribe(ChangesViewContentManagerListener.TOPIC, this)
52 Disposer.register(commitWorkflowUi) {
53 val tab = project.getLocalChangesTab() ?: return@register
55 tab.displayName = message("local.changes.tab")
56 tab.description = null
60 override fun toolWindowMappingChanged() = updateTab()
62 private fun updateTab() {
63 if (!project.isCommitToolWindow) return
64 val tab = project.getLocalChangesTab() ?: return
66 val branch = branchComponent.text
67 tab.displayName = if (branch?.isBlank() == true) message("tab.title.commit") else message("tab.title.commit.to.branch", branch)
68 tab.description = branchComponent.toolTipText
71 override fun inclusionChanged() = updateLegend()
73 private fun setupLegend() {
75 isCompactCommitLegend.addListener(object : RegistryValueListener {
76 override fun afterValueChanged(value: RegistryValue) = setLegendCompact()
80 private fun setLegendCompact() {
81 commitLegend.isCompact = isCompactCommitLegend.asBoolean()
84 private fun updateLegend() {
85 // Displayed changes and unversioned files are not actually used in legend - so we don't pass them
86 commitLegendCalculator.update(
87 includedChanges = commitWorkflowUi.getIncludedChanges(),
88 includedUnversionedFilesCount = commitWorkflowUi.getIncludedUnversionedFiles().size