vcs: Show "Commit" tool window on explicit action for projects without vcs mappings...
[idea/community.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / changes / ui / ChangesViewToolWindowFactory.kt
1 // Copyright 2000-2021 JetBrains s.r.o. and contributors. 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.openapi.vcs.changes.ui
3
4 import com.intellij.ide.actions.ToolWindowEmptyStateAction.rebuildContentUi
5 import com.intellij.ide.actions.ToolWindowEmptyStateAction.setEmptyStateBackground
6 import com.intellij.openapi.actionSystem.ActionGroup
7 import com.intellij.openapi.actionSystem.ActionManager
8 import com.intellij.openapi.actionSystem.ActionToolbar
9 import com.intellij.openapi.project.Project
10 import com.intellij.openapi.vcs.VcsBundle.message
11 import com.intellij.openapi.vcs.changes.ui.ChangesViewContentManager.Companion.COMMIT_TOOLWINDOW_ID
12 import com.intellij.openapi.wm.ToolWindow
13 import com.intellij.openapi.wm.ToolWindowManager
14 import com.intellij.openapi.wm.ex.ToolWindowEx
15
16 private class ChangesViewToolWindowFactory : VcsToolWindowFactory() {
17   override fun init(window: ToolWindow) {
18     super.init(window)
19
20     window as ToolWindowEx
21     window.setAdditionalGearActions(ActionManager.getInstance().getAction("LocalChangesView.GearActions") as ActionGroup)
22
23     setEmptyStateBackground(window)
24     window.emptyText?.setChangesViewEmptyState(window.project)
25   }
26
27   override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
28     super.createToolWindowContent(project, toolWindow)
29
30     if (toolWindow.contentManager.isEmpty) rebuildContentUi(toolWindow) // to show id label
31   }
32
33   override fun updateState(project: Project, toolWindow: ToolWindow) {
34     super.updateState(project, toolWindow)
35     toolWindow.stripeTitle = project.vcsManager.allActiveVcss.singleOrNull()?.displayName ?: ChangesViewContentManager.TOOLWINDOW_ID
36   }
37 }
38
39 private class CommitToolWindowFactory : VcsToolWindowFactory() {
40   override fun init(window: ToolWindow) {
41     super.init(window)
42
43     window as ToolWindowEx
44     window.setAdditionalGearActions(ActionManager.getInstance().getAction("CommitView.GearActions") as ActionGroup)
45
46     setEmptyStateBackground(window)
47     window.emptyText?.setCommitViewEmptyState(window.project)
48     window.hideIdLabelIfNotEmptyState()
49   }
50
51   override fun shouldBeAvailable(project: Project): Boolean =
52     project.vcsManager.hasAnyMappings() && project.isCommitToolWindowShown
53
54   override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
55     super.createToolWindowContent(project, toolWindow)
56
57     if (toolWindow.contentManager.isEmpty) rebuildContentUi(toolWindow) // to show id label
58   }
59 }
60
61 internal class SwitchToCommitDialogHint(toolWindow: ToolWindowEx, toolbar: ActionToolbar) : ChangesViewContentManagerListener {
62
63   private val actionToolbarTooltip =
64     ActionToolbarGotItTooltip("changes.view.toolwindow", message("switch.to.commit.dialog.hint.text"),
65                               toolWindow.disposable, toolbar, gearButtonOrToolbar)
66   init {
67     toolWindow.project.messageBus.connect(actionToolbarTooltip.tooltipDisposable).subscribe(ChangesViewContentManagerListener.TOPIC, this)
68   }
69
70   override fun toolWindowMappingChanged() = actionToolbarTooltip.hideHint(true)
71
72   companion object {
73     fun install(project: Project) {
74       val toolWindow = ToolWindowManager.getInstance(project).getToolWindow(COMMIT_TOOLWINDOW_ID) as? ToolWindowEx ?: return
75       val toolbar = toolWindow.decorator.headerToolbar ?: return
76
77       SwitchToCommitDialogHint(toolWindow, toolbar)
78     }
79   }
80 }