e276914157f95f1df4558df3cd376ec26fdf3517
[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 import com.intellij.openapi.wm.impl.content.ToolWindowContentUi.HIDE_ID_LABEL
16
17 private class ChangesViewToolWindowFactory : VcsToolWindowFactory() {
18   override fun init(window: ToolWindow) {
19     super.init(window)
20
21     window as ToolWindowEx
22     window.setAdditionalGearActions(ActionManager.getInstance().getAction("LocalChangesView.GearActions") as ActionGroup)
23
24     setEmptyStateBackground(window)
25     window.emptyText?.setChangesViewEmptyState(window.project)
26   }
27
28   override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
29     super.createToolWindowContent(project, toolWindow)
30
31     if (toolWindow.contentManager.isEmpty) rebuildContentUi(toolWindow) // to show id label
32   }
33
34   override fun updateState(project: Project, toolWindow: ToolWindow) {
35     super.updateState(project, toolWindow)
36     toolWindow.stripeTitle = project.vcsManager.allActiveVcss.singleOrNull()?.displayName ?: ChangesViewContentManager.TOOLWINDOW_ID
37   }
38 }
39
40 private class CommitToolWindowFactory : VcsToolWindowFactory() {
41   override fun init(window: ToolWindow) {
42     super.init(window)
43
44     window as ToolWindowEx
45     window.setAdditionalGearActions(ActionManager.getInstance().getAction("CommitView.GearActions") as ActionGroup)
46   }
47
48   override fun shouldBeAvailable(project: Project): Boolean =
49     project.vcsManager.hasAnyMappings() && project.isCommitToolWindowShown
50
51   override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
52     toolWindow.component.putClientProperty(HIDE_ID_LABEL, "true")
53     super.createToolWindowContent(project, toolWindow)
54   }
55 }
56
57 internal class SwitchToCommitDialogHint(toolWindow: ToolWindowEx, toolbar: ActionToolbar) : ChangesViewContentManagerListener {
58
59   private val actionToolbarTooltip =
60     ActionToolbarGotItTooltip("changes.view.toolwindow", message("switch.to.commit.dialog.hint.text"),
61                               toolWindow.disposable, toolbar, gearButtonOrToolbar)
62   init {
63     toolWindow.project.messageBus.connect(actionToolbarTooltip.tooltipDisposable).subscribe(ChangesViewContentManagerListener.TOPIC, this)
64   }
65
66   override fun toolWindowMappingChanged() = actionToolbarTooltip.hideHint(true)
67
68   companion object {
69     fun install(project: Project) {
70       val toolWindow = ToolWindowManager.getInstance(project).getToolWindow(COMMIT_TOOLWINDOW_ID) as? ToolWindowEx ?: return
71       val toolbar = toolWindow.decorator.headerToolbar ?: return
72
73       SwitchToCommitDialogHint(toolWindow, toolbar)
74     }
75   }
76 }