Friday, September 9, 2016

Re: jsinterop with browser-specific api


On Friday, September 9, 2016 at 8:32:08 PM UTC+2, Zufar Fakhurtdinov wrote:
Hi all. I'm trying to understand how can I effective work with jsinterop and browser-specific api.
For example Fullscreen api is mostly work in latest browsers, but methods are prefixed.

Before jsinterop I was write something like this:

public static native void leaveFullscreen() /*-{
if ($doc.cancelFullscreen) {
$doc.cancelFullscreen();
} else if ($doc.mozCancelFullScreen) {
$doc.mozCancelFullScreen();
} else if ($doc.webkitCancelFullScreen) {
$doc.webkitCancelFullScreen();
}
}-*/; 

What should I do now? Add 
mozCancelFullScreen, webkitCancelFullScreen methods to my jsinteropped Document interface? And then add static jsni method
boolean exists(Object a)
/*-{return a;}-*/; and add java method with "if (exists(..))"  chain. 
It looks very verbose and 
inefficiently.

Have a look at https://github.com/gwtproject/gwt/issues/9327

I'd do it like:

@JsOverlay
public void exitFullScreen() {
  if (getExitFullScreen() != null) {
    nativeExitFullScreen();
  } else if (getMozCancelFullScreen() != null) {
    mozCancelFullScreen();
  } else if (getWebkitExitFullscreen() != null) {
    webkitExitFullscreen();
  } else if (getMsExitFullscreen() != null) {
    msExitFullscreen();
  } else {
    throw new UnsupportedOperationException();
  }
}

@JsMethod(name = "exitFullScreen") private native void nativeExitFullScreen();
@JsProperty(name = "exitFullScreen") private native Object getExitFullScreen();
@JsMethod private native void webkitExitFullscreen();
@JsProperty(name = "webkitExitFullscreen") private native Object getWebkitExitFullscreen();
@JsMethod private native void mozCancelFullScreen();
@JsProperty(name = "mozCancelFullScreen") private native Object getMozCancelFullScreen();
@JsMethod private native void msExitFullscreen();
@JsProperty(name = "msExitFullscreen") private native Object getMsExitFullscreen();

It's a bit verbose in the method declarations (but the idea of JsInterop is that this could be somehow generated; https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API has the info in wiki format, there probably is a machine-readable format somewhere, from Closure externs or DefinitelyTyped; an equivalence table like in the MDN wiki could even allow generating that @JsOverlay method),
but the if-cascade is otherwise the same as the original JSNI method.

--
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