move inlay settings presentation to util

GitOrigin-RevId: b1ce6a0a6d17114f78fe9357189f4ac2cd7af892
This commit is contained in:
Alexandr Suhinin
2020-05-28 14:28:30 +03:00
committed by intellij-monorepo-bot
parent 2e93b3abb0
commit 4597caaf37
2 changed files with 36 additions and 32 deletions
@@ -1,11 +1,8 @@
// 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.
package com.intellij.refactoring.extractMethod.newImpl
import com.intellij.codeInsight.hints.InlayPresentationFactory
import com.intellij.codeInsight.hints.presentation.PresentationFactory
import com.intellij.codeInsight.hints.presentation.PresentationRenderer
import com.intellij.codeInsight.template.impl.TemplateManagerImpl
import com.intellij.icons.AllIcons
import com.intellij.openapi.Disposable
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.Inlay
@@ -16,7 +13,6 @@ import com.intellij.openapi.util.Pair
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement
import com.intellij.refactoring.rename.inplace.*
import com.intellij.ui.JBColor
import com.intellij.ui.layout.*
import java.util.concurrent.atomic.AtomicReference
@@ -40,35 +36,11 @@ class InplaceMethodExtractor(val project: Project, val editor: Editor) : Inplace
private val inlayReference = AtomicReference<Inlay<PresentationRenderer>>()
private fun getInlayPresentation(): SelectableInlayPresentation? {
val editor = myEditor as? EditorImpl ?: return null
val factory = PresentationFactory(editor)
val roundedCorners = InlayPresentationFactory.RoundedCorners(6, 6)
val padding = InlayPresentationFactory.Padding(4, 4, 4, 4)
val inactiveIcon = factory.container(
presentation = factory.icon(AllIcons.Actions.InlayGear),
padding = padding,
roundedCorners = roundedCorners,
background = JBColor.LIGHT_GRAY
)
val activeIcon = factory.container(
presentation = factory.icon(AllIcons.Actions.InlayGear),
padding = padding,
roundedCorners = roundedCorners,
background = JBColor.DARK_GRAY
)
val inactivePadded = factory.container(inactiveIcon, padding = InlayPresentationFactory.Padding(3, 6, 0, 0))
val activePadded = factory.container(activeIcon, padding = InlayPresentationFactory.Padding(3, 6, 0, 0))
return SelectableInlayButton(editor, inactivePadded, activePadded, activePadded, inlayReference)
}
override fun afterTemplateStart() {
super.afterTemplateStart()
val templateState = TemplateManagerImpl.getTemplateState(myEditor) ?: return
val presentation = getInlayPresentation() ?: return
val editor = templateState.editor as? EditorImpl ?: return
val presentation = TemplateInlayUtil.createSettingsPresentation(editor, inlayReference)
val offset = templateState.currentVariableRange?.endOffset ?: return
val inlay = TemplateInlayUtil.createNavigatableButtonWithPopup(templateState, offset, presentation, popupPanel) ?: return
inlayReference.set(inlay)
@@ -1,14 +1,20 @@
// 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.
package com.intellij.refactoring.rename.inplace
import com.intellij.codeInsight.hints.InlayPresentationFactory
import com.intellij.codeInsight.hints.presentation.PresentationFactory
import com.intellij.codeInsight.hints.presentation.PresentationRenderer
import com.intellij.codeInsight.template.impl.TemplateState
import com.intellij.icons.AllIcons
import com.intellij.openapi.editor.Inlay
import com.intellij.openapi.editor.impl.EditorImpl
import com.intellij.openapi.ui.popup.JBPopupFactory
import com.intellij.openapi.ui.popup.JBPopupListener
import com.intellij.openapi.ui.popup.LightweightWindowEvent
import com.intellij.openapi.util.Disposer
import com.intellij.ui.JBColor
import com.intellij.ui.popup.PopupFactoryImpl
import java.util.concurrent.atomic.AtomicReference
import javax.swing.JComponent
object TemplateInlayUtil {
@@ -35,7 +41,7 @@ object TemplateInlayUtil {
panel: JComponent): Inlay<PresentationRenderer>? {
val editor = templateState.editor
val inlay = createNavigatableButton(templateState, inEditorOffset, presentation) ?: return null
fun showPopup(){
fun showPopup() {
try {
editor.putUserData(PopupFactoryImpl.ANCHOR_POPUP_POSITION, inlay.visualPosition)
val popup = JBPopupFactory.getInstance()
@@ -48,7 +54,8 @@ object TemplateInlayUtil {
})
.createPopup()
popup.showInBestPositionFor(editor)
} finally {
}
finally {
editor.putUserData(PopupFactoryImpl.ANCHOR_POPUP_POSITION, null)
}
}
@@ -59,4 +66,29 @@ object TemplateInlayUtil {
})
return inlay
}
@JvmStatic
fun createSettingsPresentation(editor: EditorImpl, inlayToUpdate: AtomicReference<Inlay<PresentationRenderer>>): SelectableInlayPresentation {
val factory = PresentationFactory(editor)
val roundedCorners = InlayPresentationFactory.RoundedCorners(6, 6)
val padding = InlayPresentationFactory.Padding(4, 4, 4, 4)
val inactiveIcon = factory.container(
presentation = factory.icon(AllIcons.Actions.InlayGear),
padding = padding,
roundedCorners = roundedCorners,
background = JBColor.LIGHT_GRAY
)
val activeIcon = factory.container(
presentation = factory.icon(AllIcons.Actions.InlayGear),
padding = padding,
roundedCorners = roundedCorners,
background = JBColor.DARK_GRAY
)
val inactivePadded = factory.container(inactiveIcon, padding = InlayPresentationFactory.Padding(3, 6, 0, 0))
val activePadded = factory.container(activeIcon, padding = InlayPresentationFactory.Padding(3, 6, 0, 0))
return SelectableInlayButton(editor, inactivePadded, activePadded, activePadded, inlayToUpdate)
}
}