RIDER-77734 Compound run configs are duplicated if it's started out of the toolbar
[idea/community.git] / platform / execution-impl / src / com / intellij / execution / runToolbar / RunToolbarProcessAction.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.execution.runToolbar
3
4 import com.intellij.execution.ExecutionTargetManager
5 import com.intellij.execution.Executor
6 import com.intellij.execution.ExecutorRegistryImpl
7 import com.intellij.execution.RunnerAndConfigurationSettings
8 import com.intellij.execution.compound.SettingsAndEffectiveTarget
9 import com.intellij.openapi.actionSystem.AnActionEvent
10 import com.intellij.openapi.project.DumbAware
11 import com.intellij.openapi.project.Project
12 import javax.swing.Icon
13
14 open class RunToolbarProcessAction(override val process: RunToolbarProcess, val executor: Executor) : ExecutorRegistryImpl.ExecutorAction(executor), ExecutorRunToolbarAction, DumbAware {
15
16   override fun displayTextInToolbar(): Boolean {
17     return true
18   }
19
20   override fun getInformativeIcon(project: Project, selectedConfiguration: RunnerAndConfigurationSettings): Icon {
21     return executor.icon
22   }
23
24   override fun actionPerformed(e: AnActionEvent) {
25     e.project?.let { project ->
26       if (canRun(e)) {
27         getSelectedConfiguration(e)?.let {
28            ExecutorRegistryImpl.RunnerHelper.run(project, it.configuration, it, e.dataContext, executor)
29         }
30       }
31     }
32   }
33
34   override fun checkMainSlotVisibility(state: RunToolbarMainSlotState): Boolean {
35     return state == RunToolbarMainSlotState.CONFIGURATION
36   }
37
38   override fun update(e: AnActionEvent) {
39     e.presentation.text = executor.actionName
40     e.presentation.isVisible = !e.isActiveProcess()
41
42     if (!RunToolbarProcess.isExperimentalUpdatingEnabled) {
43       e.mainState()?.let {
44         e.presentation.isVisible = e.presentation.isVisible && checkMainSlotVisibility(it)
45       }
46     }
47
48     if(e.presentation.isVisible) {
49       e.presentation.isEnabled = canRun(e)
50     }
51   }
52
53   override fun getSelectedConfiguration(e: AnActionEvent): RunnerAndConfigurationSettings? {
54     return e.configuration()
55   }
56
57   protected fun canRun(e: AnActionEvent): Boolean {
58     return e.project?.let { project->
59       val activeTarget = ExecutionTargetManager.getActiveTarget(project)
60       return getSelectedConfiguration(e)?.let {
61
62         val target = SettingsAndEffectiveTarget(it.configuration, activeTarget)
63
64         val canRun = ExecutorRegistryImpl.RunnerHelper.canRun(project, listOf(target), executor)
65         canRun
66
67       } ?: false
68     } ?: false
69   }
70 }
71
72 class RunToolbarGroupProcessAction(process: RunToolbarProcess, executor: Executor) : RunToolbarProcessAction(process, executor) {
73   override fun update(e: AnActionEvent) {
74     super.update(e)
75     e.presentation.isEnabledAndVisible = e.presentation.isEnabled
76   }
77 }