JavaFX WebView Progress always goes from 0.0 to 1.

2019-07-09 02:12发布

问题:

I am currently developing an application with GluonHQ (JavaFXPorts) where I am using the WebView to load some Internet pages. I have noticed that when I am using the following code on Desktop

webEngine.getLoadWorker().progressProperty().addListener((a,b,progress) -> System.out.println(": " +progress.doubleValue()));

it prints out the loading from 0.0 to 1.0.

Loading Page: 0.0
Loading Page: 0.22
Loading Page: 0.38
Loading Page: 0.81
Loading Page: 1.0

Whilst on iOS it always returns 0.0 and 1.0, not any intermediate values.

Loading Page: 0.0
Loading Page: 1.0

Any way to handle this?

回答1:

With JavaFXPorts, on mobile (both Android and iOS) the WebView control is not the JavaFX built-in control but the native control. For instance, on iOS, a UIWebView control is used. Same applies to TextField and TextArea controls, by the way.

To keep the same API as in desktop, a JavaFX "WebView" control is used, but it is just a facade, a node with same properties, but without webKit. Everything is passed down to the native control.

And while there is communication in both ways between the native and the Java layers, not everything is sent, so there is no 1:1 relation between the native control properties and the JavaFX properties of the desktop implementation.

If you check the native implementation in Objective-C for iOS, you can find some methods related to the loading status, and the native callbacks:

- (void)webViewDidStartLoad:(UIWebView *)webView {
    ...
    (*env)->CallVoidMethod(env, jObject, jmidLoadStarted);
}
- (void)webViewDidFinishLoad:(UIWebView *)wv {
    ...
    (*env)->CallVoidMethod(env, jObject, jmidLoadFinished, jUrl, jInner);
}

that go to the JavaFX control:

// native callbacks
private void notifyLoadStarted() {
    engine.notifyLoadStarted();
}

private void notifyLoadFinished(String loc, String content) {
    engine.notifyLoadFinished(loc, content);
}

As you can see, there is no method related to the loading progress, so that is the reason you won't get any intermediate value.