From 8a20184961fed7abafc67707707bdf0ebd04ab5d Mon Sep 17 00:00:00 2001 From: Dmitry Zhuravlev Date: Thu, 29 Apr 2021 20:59:38 +0300 Subject: [PATCH] diff-preview: extract Esc handler setup to common editor customizer This will unify Esc handler setup for all components which customized by such editor customizer. GitOrigin-RevId: 2dd86a2389902fe9c1c84e64b4bb1562c9039abc --- .../DiffFileEditorEscapeHandlerCustomizer.kt | 36 +++++++++++++++++++ .../diff/editor/DiffRequestProcessorEditor.kt | 22 ------------ .../com/intellij/diff/util/FileEditorBase.kt | 6 +++- .../resources/META-INF/VcsExtensions.xml | 1 + .../github/pullrequest/GHPRDiffFileEditor.kt | 16 ++------- 5 files changed, 44 insertions(+), 37 deletions(-) create mode 100644 platform/diff-impl/src/com/intellij/diff/editor/DiffFileEditorEscapeHandlerCustomizer.kt diff --git a/platform/diff-impl/src/com/intellij/diff/editor/DiffFileEditorEscapeHandlerCustomizer.kt b/platform/diff-impl/src/com/intellij/diff/editor/DiffFileEditorEscapeHandlerCustomizer.kt new file mode 100644 index 000000000000..d203222e4d67 --- /dev/null +++ b/platform/diff-impl/src/com/intellij/diff/editor/DiffFileEditorEscapeHandlerCustomizer.kt @@ -0,0 +1,36 @@ +// Copyright 2000-2021 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. +package com.intellij.diff.editor + +import com.intellij.diff.impl.DiffRequestProcessor +import com.intellij.diff.util.DiffUserDataKeysEx +import com.intellij.diff.util.DiffUtil +import com.intellij.diff.util.FileEditorBase +import com.intellij.openapi.Disposable +import com.intellij.openapi.actionSystem.CommonShortcuts +import com.intellij.openapi.fileEditor.FileEditor +import com.intellij.openapi.util.Disposer +import com.intellij.openapi.vfs.VirtualFile +import java.awt.event.KeyEvent +import javax.swing.JComponent +import javax.swing.KeyStroke + +class DiffFileEditorEscapeHandlerCustomizer : DiffRequestProcessorEditorCustomizer { + + override fun customize(file: VirtualFile, editor: FileEditor, processor: DiffRequestProcessor) { + if (editor !is FileEditorBase) return + + if (!DiffUtil.isUserDataFlagSet(DiffUserDataKeysEx.DIFF_IN_EDITOR_WITH_EXPLICIT_DISPOSABLE, processor.context)) { + Disposer.register(editor, Disposable { + Disposer.dispose(processor) + }) + } + Disposer.register(processor, Disposable { + editor.firePropertyChange(FileEditor.PROP_VALID, true, false) + }) + + processor.component.registerKeyboardAction({ Disposer.dispose(editor) }, + KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW) + + file.getUserData(DiffVirtualFile.ESCAPE_HANDLER)?.registerCustomShortcutSet(CommonShortcuts.ESCAPE, editor.component, editor) + } +} diff --git a/platform/diff-impl/src/com/intellij/diff/editor/DiffRequestProcessorEditor.kt b/platform/diff-impl/src/com/intellij/diff/editor/DiffRequestProcessorEditor.kt index 6bd9c1bd78f0..4bacea62fbbf 100644 --- a/platform/diff-impl/src/com/intellij/diff/editor/DiffRequestProcessorEditor.kt +++ b/platform/diff-impl/src/com/intellij/diff/editor/DiffRequestProcessorEditor.kt @@ -2,25 +2,16 @@ package com.intellij.diff.editor import com.intellij.diff.impl.DiffRequestProcessor -import com.intellij.diff.util.DiffUserDataKeysEx -import com.intellij.diff.util.DiffUtil import com.intellij.diff.util.FileEditorBase -import com.intellij.openapi.Disposable -import com.intellij.openapi.actionSystem.CommonShortcuts import com.intellij.openapi.diagnostic.logger import com.intellij.openapi.diff.DiffBundle -import com.intellij.openapi.fileEditor.FileEditor import com.intellij.openapi.fileEditor.impl.EditorWindow -import com.intellij.openapi.util.Disposer import com.intellij.openapi.vfs.VirtualFile import java.awt.BorderLayout import java.awt.event.ContainerAdapter import java.awt.event.ContainerEvent -import java.awt.event.KeyEvent import javax.swing.JComponent -import javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW import javax.swing.JPanel -import javax.swing.KeyStroke open class DiffRequestProcessorEditor( private val file: DiffVirtualFile, @@ -36,19 +27,6 @@ open class DiffRequestProcessorEditor( init { putUserData(EditorWindow.HIDE_TABS, true) - if (!DiffUtil.isUserDataFlagSet(DiffUserDataKeysEx.DIFF_IN_EDITOR_WITH_EXPLICIT_DISPOSABLE, processor.context)) { - Disposer.register(this, Disposable { - Disposer.dispose(processor) - }) - } - Disposer.register(processor, Disposable { - propertyChangeSupport.firePropertyChange(FileEditor.PROP_VALID, true, false) - }) - - processor.component.registerKeyboardAction({ Disposer.dispose(this) }, - KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), WHEN_IN_FOCUSED_WINDOW) - - file.getUserData(DiffVirtualFile.ESCAPE_HANDLER)?.registerCustomShortcutSet(CommonShortcuts.ESCAPE, component, this) } override fun getComponent(): JComponent = panel diff --git a/platform/diff-impl/src/com/intellij/diff/util/FileEditorBase.kt b/platform/diff-impl/src/com/intellij/diff/util/FileEditorBase.kt index 42366920531b..e7fc434eab3c 100644 --- a/platform/diff-impl/src/com/intellij/diff/util/FileEditorBase.kt +++ b/platform/diff-impl/src/com/intellij/diff/util/FileEditorBase.kt @@ -1,4 +1,4 @@ -// Copyright 2000-2019 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. +// Copyright 2000-2021 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. package com.intellij.diff.util import com.intellij.codeHighlighting.BackgroundEditorHighlighter @@ -27,6 +27,10 @@ abstract class FileEditorBase : UserDataHolderBase(), FileEditor { override fun selectNotify() {} override fun deselectNotify() {} + fun firePropertyChange(propName: String, oldValue: Boolean, newValue: Boolean) { + propertyChangeSupport.firePropertyChange(propName, oldValue, newValue) + } + override fun addPropertyChangeListener(listener: PropertyChangeListener) { propertyChangeSupport.addPropertyChangeListener(listener) } diff --git a/platform/vcs-impl/resources/META-INF/VcsExtensions.xml b/platform/vcs-impl/resources/META-INF/VcsExtensions.xml index 6f27b03460d7..79962352a407 100644 --- a/platform/vcs-impl/resources/META-INF/VcsExtensions.xml +++ b/platform/vcs-impl/resources/META-INF/VcsExtensions.xml @@ -300,6 +300,7 @@ + diff --git a/plugins/github/src/org/jetbrains/plugins/github/pullrequest/GHPRDiffFileEditor.kt b/plugins/github/src/org/jetbrains/plugins/github/pullrequest/GHPRDiffFileEditor.kt index dc3c4b4701ce..afccc1109de6 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/pullrequest/GHPRDiffFileEditor.kt +++ b/plugins/github/src/org/jetbrains/plugins/github/pullrequest/GHPRDiffFileEditor.kt @@ -1,18 +1,13 @@ -// 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. +// Copyright 2000-2021 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. package org.jetbrains.plugins.github.pullrequest +import com.intellij.collaboration.ui.codereview.diff.MutableDiffRequestChainProcessor import com.intellij.diff.util.FileEditorBase -import com.intellij.openapi.fileEditor.FileEditor.PROP_VALID import com.intellij.openapi.project.Project import com.intellij.openapi.util.Disposer -import com.intellij.openapi.vfs.VirtualFile -import com.intellij.collaboration.ui.codereview.diff.MutableDiffRequestChainProcessor import com.intellij.util.ui.update.MergingUpdateQueue import com.intellij.util.ui.update.Update import org.jetbrains.plugins.github.i18n.GithubBundle -import java.awt.event.KeyEvent -import javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW -import javax.swing.KeyStroke internal class GHPRDiffFileEditor(project: Project, private val diffRequestModel: GHPRDiffRequestModel, @@ -29,13 +24,6 @@ internal class GHPRDiffFileEditor(project: Project, override fun isValid() = !Disposer.isDisposed(diffProcessor) init { - Disposer.register(this, diffProcessor) - - diffProcessor.component.registerKeyboardAction({ - propertyChangeSupport.firePropertyChange(PROP_VALID, true, false) - }, - KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), WHEN_IN_FOCUSED_WINDOW) - diffRequestModel.addAndInvokeRequestChainListener(diffChainUpdateQueue) { val chain = diffRequestModel.requestChain diffChainUpdateQueue.run(Update.create(diffRequestModel) { -- 2.32.0