summaryrefslogtreecommitdiff
path: root/plugins/git4idea/src/git4idea/actions/GitQuickActionsToolbarPopup.kt
blob: a9051c27e05a9bf9a174affe2049148c10e4206f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// 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.
package git4idea.actions

import com.intellij.dvcs.repo.VcsRepositoryMappingListener
import com.intellij.icons.AllIcons
import com.intellij.openapi.actionSystem.*
import com.intellij.openapi.components.Service
import com.intellij.openapi.project.Project
import com.intellij.openapi.vcs.actions.VcsQuickActionsToolbarPopup
import com.intellij.util.IconUtil
import git4idea.GitVcs
import git4idea.i18n.GitBundle
import git4idea.repo.GitRepositoryManager
import java.awt.Dimension
import javax.swing.Icon
import javax.swing.JComponent

/**
 * Git implementation of the quick popup action
 */
@Service
class GitQuickActionsToolbarService {
  var gitMappingInitialized = false
    private set

  fun initializationComplete() {
    gitMappingInitialized = true
  }
  companion object {
    fun getInstance(project: Project): GitQuickActionsToolbarService = project.getService(GitQuickActionsToolbarService::class.java)
  }
}

internal class GitQuickActionsToolbarPopup : VcsQuickActionsToolbarPopup() {

  override fun getName(project: Project): String {
    return GitVcs.getInstance(project).name
  }

  override fun createCustomComponent(presentation: Presentation, place: String): JComponent {
    return MyActionButtonWithText(this, presentation.apply { text = "" }, place)
  }

  override fun update(e: AnActionEvent) {
    val project = e.project
    val presentation = e.presentation

    if (!updateVcs(project, e)) return
    val instance = GitQuickActionsToolbarService.getInstance(project!!)
    if (!instance.gitMappingInitialized) {
      presentation.isEnabledAndVisible = false
      return
    }
    else {
      presentation.isEnabledAndVisible = true
    }

    val repo = GitRepositoryManager.getInstance(project).repositories.isNotEmpty()

    presentation.icon = if (repo) {
      AllIcons.Actions.More.toSize(ActionToolbar.DEFAULT_MINIMUM_BUTTON_SIZE)
    }
    else {
      AllIcons.Vcs.BranchNode
    }

    presentation.text = if (repo) {
      ""
    }
    else {
      GitBundle.message("action.Vcs.Toolbar.ShowMoreActions.text") + " "
    }
  }

  class MyGitRepositoryListener(val project: Project) : VcsRepositoryMappingListener {
    override fun mappingChanged() {
      GitQuickActionsToolbarService.getInstance(project).initializationComplete()
    }
  }

  private fun Icon.toSize(dimension: Dimension): Icon {
    return if (iconWidth < dimension.width) {
      IconUtil.toSize(
        this,
        dimension.width,
        dimension.height,
      )
    }
    else {
      this
    }
  }
}