Thanks Jens. Now switched it to use $doc. And, yes, I just register once, and fire a custom event on the event bus. Although, I just did it using my own event bus like this:
I suspect your way is better, although, I don't really understand what sinking events onto the DOM is doing.
And the handler:
Cheers.
On Monday, August 7, 2017 at 7:21:33 PM UTC+10, Jens wrote:
-- final EventBus EVENT_BUS = GWT.create(SimpleEventBus.class);
listenForVisibilityChange(new Command() {
@Override
public void execute() {
boolean visible = "visible".equals(getVisibilityState());
EVENT_BUS.fireEvent(new WindowVisibilityChangedEvent(visible));
}
});
I suspect your way is better, although, I don't really understand what sinking events onto the DOM is doing.
elemental2 looks cool! Will check it out.
My custom event if anyone wants to reuse it:
public class WindowVisibilityChangedEvent extends GwtEvent<WindowVisibilityChangedEventHandler> {
public static Type<WindowVisibilityChangedEventHandler> TYPE = new Type<WindowVisibilityChangedEventHandler>();
private boolean visibile;
public WindowVisibilityChangedEvent(boolean visible) {
this.visibile = visible;
}
@Override
public Type<WindowVisibilityChangedEventHandler> getAssociatedType() {
return TYPE;
}
@Override
protected void dispatch(WindowVisibilityChangedEventHandler handler) {
handler.visibilityChanged(this);
}
public boolean isVisibile() {
return visibile;
}
}
And the handler:
public interface WindowVisibilityChangedEventHandler extends EventHandler {
void visibilityChanged(WindowVisibilityChangedEvent windowVisibilityChangedEvent);
}
Cheers.
On Monday, August 7, 2017 at 7:21:33 PM UTC+10, Jens wrote:
Generally ok, given it is a global listener and you probably just install it once on app initialization. However you should use $doc instead of document to make sure you listen on the global document. Depending on your browser support you might want a utility method to check if visibility change events are actually supported by the browser.
static native boolean isSupported() /*-{
return $doc.hidden !== 'undefined' && $doc.visibilityState !== 'undefined';
}-*/;In our app we created a PageVisibilityChangedEvent which extends GwtEvent and then fire it on the app wide EventBus so everyone can easily listen for it. To register the DOM handler we used
Element doc = Document.get().cast();
DOM.sinkBitlessEvent(doc, VISIBILITY_CHANGE_EVENT);
DOM.setEventListener(doc, new EventListener() {
@Override
public void onBrowserEvent(final Event event) {
if (VISIBILITY_CHANGE_EVENT.equals (event.getType())) {
firePageVisibilityChangedEvent ();
}
}
});You could of course also use elemental2 (requires newest GWT) or JsInterop to reduce the amount of JSNI if that is important for you.-- J.
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment