diff-preview: add action for moving diff preview to new window
[idea/community.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / changes / actions / diff / MoveDiffPreviewAction.kt
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.openapi.vcs.changes.actions.diff
3
4 import com.intellij.openapi.actionSystem.AnActionEvent
5 import com.intellij.openapi.project.DumbAwareAction
6 import com.intellij.openapi.project.Project
7 import com.intellij.openapi.vcs.VcsDataKeys.VIRTUAL_FILE_STREAM
8 import com.intellij.openapi.vcs.changes.EditorDiffPreviewFilesManager
9 import com.intellij.openapi.vcs.changes.PreviewDiffVirtualFile
10 import com.intellij.util.containers.getIfSingle
11 import com.intellij.vcsUtil.VcsUtil
12
13 internal abstract class MoveDiffPreviewAction(private val openInNewWindow: Boolean) : DumbAwareAction() {
14
15   abstract fun isEnabledAndVisible(project: Project): Boolean
16
17   override fun update(e: AnActionEvent) {
18     val project = e.project
19     val file = e.getData(VIRTUAL_FILE_STREAM).getIfSingle()
20     e.presentation.isEnabledAndVisible = project != null
21                                          && isEnabledAndVisible(project)
22                                          && file is PreviewDiffVirtualFile
23   }
24
25
26
27   override fun actionPerformed(e: AnActionEvent) {
28     val project = e.project!!
29     val diffPreviewFile = VcsUtil.getVirtualFiles(e).first() as PreviewDiffVirtualFile
30
31     EditorDiffPreviewFilesManager.getInstance(project).openFile(diffPreviewFile, true, openInNewWindow, true)
32   }
33 }
34
35 internal class MoveDiffPreviewToEditorAction : MoveDiffPreviewAction(false) {
36   override fun isEnabledAndVisible(project: Project): Boolean = EditorDiffPreviewFilesManager.getInstance(project).shouldOpenInNewWindow
37 }
38
39 internal class MoveDiffPreviewToNewWindowAction : MoveDiffPreviewAction(true) {
40   override fun isEnabledAndVisible(project: Project): Boolean = !EditorDiffPreviewFilesManager.getInstance(project).shouldOpenInNewWindow
41 }