11package com.gitlab.lae.intellij.actions.tree
22
3- import com.gitlab.lae.intellij.actions.tree.ui.ActionList
4- import com.gitlab.lae.intellij.actions.tree.ui.ActionPopupEventDispatcher
53import com.gitlab.lae.intellij.actions.tree.ui.ActionPresentation
4+ import com.gitlab.lae.intellij.actions.tree.ui.ActionPresentationStep
65import com.gitlab.lae.intellij.actions.tree.util.setBestLocation
76import com.intellij.ide.DataManager
8- import com.intellij.ide.IdeEventQueue
97import com.intellij.openapi.actionSystem.ActionGroup
108import com.intellij.openapi.actionSystem.ActionManager
9+ import com.intellij.openapi.actionSystem.ActionPlaces.ACTION_PLACE_QUICK_LIST_POPUP_ACTION
1110import com.intellij.openapi.actionSystem.AnAction
1211import com.intellij.openapi.actionSystem.AnActionEvent
1312import com.intellij.openapi.actionSystem.CommonDataKeys.EDITOR
1413import com.intellij.openapi.actionSystem.DataContext
15- import com.intellij.openapi.actionSystem.IdeActions.ACTION_EDITOR_ESCAPE
1614import com.intellij.openapi.actionSystem.PlatformDataKeys.CONTEXT_COMPONENT
1715import com.intellij.openapi.actionSystem.ex.ActionUtil
1816import com.intellij.openapi.application.ApplicationManager.getApplication
19- import com.intellij.openapi.ui.popup.JBPopup
2017import com.intellij.openapi.ui.popup.JBPopupFactory
2118import com.intellij.openapi.ui.popup.JBPopupFactory.ActionSelectionAid
22- import com.intellij.openapi.ui.popup.PopupChooserBuilder
2319import com.intellij.openapi.wm.IdeFocusManager
20+ import com.intellij.ui.popup.list.ListPopupImpl
2421import java.awt.event.ActionEvent
2522import javax.swing.JComponent
2623import javax.swing.KeyStroke
@@ -31,29 +28,23 @@ internal class Popup(action: ActionNode, e: AnActionEvent) {
3128 private val actionManager = e.actionManager
3229 private val sourceComponent = e.getData(CONTEXT_COMPONENT )
3330 private val sourceEditor = e.getData(EDITOR )
34- private val list : ActionList
35- private val popup : JBPopup
31+ private val popup : ListPopupImpl
32+ private val items : List < ActionPresentation >
3633
3734 init {
38- val items =
35+ items =
3936 action.prepare(e.dataContext).map { (keys, item) ->
4037 createPresentation(item, e.actionManager, e.dataContext, keys)
4138 }
42- list = ActionList (items)
43-
44- // Register our action first before IntelliJ registers the default
45- // actions (e.g. com.intellij.ui.ScrollingUtil) so that in case of
46- // conflict our action will be executed
47- items.forEach { it.registerShortcuts(list) { item, e -> this .onActionChosen(item, e) } }
48- popup = createPopup()
49- popup.addListener(
50- ActionPopupEventDispatcher (
51- popup,
52- list,
53- IdeEventQueue .getInstance().popupManager,
54- ),
55- )
56- registerIdeAction(ACTION_EDITOR_ESCAPE , popup::cancel)
39+
40+ popup =
41+ ListPopupImpl (
42+ e.project,
43+ ActionPresentationStep (items) { value, modifiers -> onActionChosen(value, modifiers) },
44+ )
45+ items.forEach { item ->
46+ item.registerShortcuts(popup.list) { event -> onActionChosen(item, event) }
47+ }
5748 }
5849
5950 fun show (dataContext : DataContext ) {
@@ -65,41 +56,7 @@ internal class Popup(action: ActionNode, e: AnActionEvent) {
6556 actionManager : ActionManager ,
6657 dataContext : DataContext ,
6758 keysOverride : List <KeyStroke >,
68- ): ActionPresentation =
69- action.createPresentation(
70- actionManager,
71- dataContext,
72- keysOverride,
73- )
74-
75- private fun createPopup (): JBPopup =
76- PopupChooserBuilder (list)
77- .setModalContext(true )
78- .setCloseOnEnter(false )
79- .setItemChosenCallback(
80- Runnable {
81- val item = list.selectedValue
82- if (item != null ) {
83- onActionChosen(item, 0 )
84- }
85- }
86- )
87- .createPopup()
88-
89- private fun registerIdeAction (actionId : String , runnable : () -> Unit ) {
90- val action = actionManager.getAction(actionId) ? : return
91- val shortcutSet = action.shortcutSet
92- if (shortcutSet.shortcuts.isEmpty()) {
93- return
94- }
95-
96- object : AnAction () {
97- override fun actionPerformed (e : AnActionEvent ) {
98- runnable()
99- }
100- }
101- .registerCustomShortcutSet(shortcutSet, list)
102- }
59+ ): ActionPresentation = action.createPresentation(actionManager, dataContext, keysOverride)
10360
10461 private fun onActionChosen (item : ActionPresentation , e : ActionEvent ) {
10562 onActionChosen(item, e.modifiers)
@@ -110,8 +67,7 @@ internal class Popup(action: ActionNode, e: AnActionEvent) {
11067 return
11168 }
11269
113- list.setSelectedValue(item, false )
114- val invocation = { performAction(item.action, modifiers) }
70+ val invocation = { performAction(item.value, modifiers) }
11571 if (item.sticky) {
11672 invocation()
11773 getApplication().invokeLater {
@@ -138,11 +94,7 @@ internal class Popup(action: ActionNode, e: AnActionEvent) {
13894 * action should be enabled/disabled for current cursor position.
13995 */
14096 private fun updatePresentations () {
141- getDataContextAsync { context ->
142- generateSequence(0 , Int ::inc).take(list.model.size).map(list.model::getElementAt).forEach {
143- it.update(actionManager, context)
144- }
145- }
97+ getDataContextAsync { context -> items.forEach { it.update(actionManager, context) } }
14698 }
14799
148100 private fun getDataContextAsync (consumer : (DataContext ) -> Unit ) {
@@ -174,16 +126,12 @@ internal class Popup(action: ActionNode, e: AnActionEvent) {
174126 }
175127 }
176128
177- private fun performAction (
178- action : AnAction ,
179- modifiers : Int ,
180- dataContext : DataContext ,
181- ) {
129+ private fun performAction (action : AnAction , modifiers : Int , dataContext : DataContext ) {
182130 val event =
183131 AnActionEvent (
184132 null ,
185133 dataContext,
186- ActionNode . ACTION_PLACE ,
134+ ACTION_PLACE_QUICK_LIST_POPUP_ACTION ,
187135 action.templatePresentation.clone(),
188136 actionManager,
189137 modifiers,
0 commit comments