You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Introduce getListeners(eventType) method on com.vaadin.flow.component.ComponentEventBus #9766
With Vaadin 8.x it was possible to use com.vaadin.server.AbstractClientConnector#getListeners
It is not possible to access the listeners with Vaadin Flow without reflection.
Please re-introduce a getListeners(eventType) method on com.vaadin.flow.component.ComponentEventBus
in Vaadin Flow 14.x
In our case, we need it to record user interaction with a component.
For example a click via click listener.
For this, every component will add an "internal" click listener.
On layouts (like VerticalLayout) the interaction is only recorded if another click listener (!= internal) is registered.
So basically the internal click listener checks for "getListeners(ClickEvent.class) > 1" on click event.
And only if more than one click listener is added, the click is recorded.
What we already have tested:
Temporary remove our internal click listener and then call "hasListener".
But the problem is that this will change the listener order.
Our internal click listener must be always fired as the first.
Another possible solution would be to override all add(Click)Listener methods and track it by our self.
But that feels a bit poor
With Vaadin Flow the only way I see - without reflection - is to overwrite addKeyPressListener.
This feels weired since the returned Registration must be also wrapped.
The code would look like this (untested code, without 'modifiers' support):
@retomerz it's tagged with Good First Issue, so if you wanna contribute it, got ahead - it could probably be included in the upcoming release in June/July
I've added support for getListeners, but as for your second issue - getting KeyCode from listener - even if now we have access to listeners the key property is not accessible. I could add just a getter for it but I don't see wider usecase for it.
As a workaround you can do sth like:
public class MyTextField extends TextField {
class MyKeyEventListener<E extends KeyboardEvent> extends KeyEventListener<E> {
private Key key;
public MyKeyEventListener(ComponentEventListener<E> listener, Key key, KeyModifier... modifiers) {
super(listener, key, modifiers);
this.key = key;
}
public Key getKey() {
return key;
}
}
public MyTextField() {
addKeyPressListener(new MyKeyEventListener<>(ev -> {}, Key.KEY_Y));
}
public boolean hasShortcutListener(Key key) {
return getListeners(KeyboardEvent.class).stream()
.filter(MyKeyEventListener.class::isInstance)
.map(MyKeyEventListener.class::cast)
.map(MyKeyEventListener::getKey)
.anyMatch(key::equals);
}
}
Activity
pleku commentedon Oct 7, 2021
It should be quite straight forward to introduce the a similar API to https://github.com/vaadin/framework/blob/master/server/src/main/java/com/vaadin/server/AbstractClientConnector.java#L1174
into Flow's
ComponentEventBus. It is enough to include a unit test to https://github.com/vaadin/flow/blob/master/flow-server/src/test/java/com/vaadin/flow/component/ComponentEventBusTest.javaretomerz commentedon Apr 7, 2022
Any news on this?
We also need to be able to check if a KeyEventListener exists for a specific key.
This was also possible in Vaadin 8, like this:
With Vaadin Flow the only way I see - without reflection - is to overwrite addKeyPressListener.
This feels weired since the returned Registration must be also wrapped.
The code would look like this (untested code, without 'modifiers' support):
Thanks
knoobie commentedon Apr 7, 2022
@retomerz it's tagged with
Good First Issue, so if you wanna contribute it, got ahead - it could probably be included in the upcoming release in June/Julyfeat: introduced ComponentEventBus.getListeners(eventType)
feat: introduced ComponentEventBus.getListeners(eventType)
MarcinVaadin commentedon Jul 22, 2022
I've added support for getListeners, but as for your second issue - getting KeyCode from listener - even if now we have access to listeners the key property is not accessible. I could add just a getter for it but I don't see wider usecase for it.
As a workaround you can do sth like:
feat: Introduce getListeners(eventType) method on com.vaadin.flow.com…
vaadin-bot commentedon Aug 18, 2022
This ticket/PR has been released with Vaadin 23.2.0.beta1 and is also targeting the upcoming stable 23.2.0 version.