Skip to content

Commit

Permalink
Added WebView focus management in HtmlWebViewPeer
Browse files Browse the repository at this point in the history
  • Loading branch information
salmonb committed Nov 26, 2023
1 parent c983f2a commit 148f6f1
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.webfx.kit.mapper.peers.javafxgraphics.gwt.html.HtmlNodePeer;
import dev.webfx.kit.mapper.peers.javafxgraphics.gwt.util.HtmlUtil;
import elemental2.dom.CSSProperties;
import elemental2.dom.DomGlobal;
import elemental2.dom.HTMLIFrameElement;
import javafx.event.EventHandler;
import javafx.scene.web.WebErrorEvent;
Expand All @@ -29,6 +30,23 @@ public HtmlWebViewPeer(NB base, HTMLIFrameElement iFrame) {
reportError();
return null;
};
// Focus management.
// 1) Detecting when the iFrame gained focus
DomGlobal.window.addEventListener("blur", e -> { // when iFrame gained focus, the parent window lost focus
if (DomGlobal.document.activeElement == iFrame) { // and the active element should be the iFrame.
// Then, we set the WebView as the new focus owner in JavaFX
N webView = getNode();
webView.getScene().focusOwnerProperty().setValue(webView);
}
});
// 2) Detecting when the iFrame lost focus
DomGlobal.window.addEventListener("focus", e -> { // when iFrame lost focus, the parent window gained focus
// If the WebView is still the focus owner in JavaFX, we clear that focus to report the WebView lost focus
N webView = getNode();
if (webView.getScene().getFocusOwner() == webView) {
webView.getScene().focusOwnerProperty().setValue(null);
}
});
}

private void reportError() {
Expand Down

0 comments on commit 148f6f1

Please sign in to comment.