Callback registered with IPopupChooserBuilder##setCancelCallback is called even when popup item was selected

Answered

Hi,

is it intended that the callback method set by IPopupChooserBuilder##setCancelCallback is called even when an item is selected? I would expect that this method is only called when the popup is left without a selection.

JBPopupFactory.getInstance() 
	.createPopupChooserBuilder(…) 
	.setSelectionMode(SINGLE_SELECTION) 
	.setItemChosenCallback { show("selected", pluginName) } 
	.setCancelCallback { show("canceled", pluginName) } 
	.createPopup() 
	.showInBestPositionFor(editor)

When selecting an item both toasts appear: first “canceled”, then “selected”.

If this is intended, what is the best way to do things only when the popup is left without a selection?

Regards Andreas

2
5 comments

See com.intellij.ui.popup.AbstractPopup#canClose .

You can just track “no selection” state in your code via (not-invoked) setItemChosenCallback().

0

The cancel-callback is called first regardless if an item is selected or not. So if I understand your answer correctly any tracking in setItemChosenCallback will be too late.

0

Hi,

I rechecked and the problem remains: The cancel-callback is called before the item-selected-callback. So I can not use Yann Cebron's idea to register some state in the selected-item-callback to change the behavior of the cancel-callback.

Andreas

0

Sorry for delay, this solution should work via `LightWeightWindowEvent.isOk` property

 

.createPopup().apply { addListener(object : JBPopupListener { override fun onClosed(event: LightweightWindowEvent) { println(event.isOk) } }) }

0

Hi Yann, Thank you! That solution works for me.

Andreas

0

Please sign in to leave a comment.