From 7309763d421a5591c25ff3db462d2ad9bab4cfc4 Mon Sep 17 00:00:00 2001 From: crschnick Date: Tue, 7 May 2024 05:00:20 +0000 Subject: [PATCH] Make webview fail resilient --- .../xpipe/app/fxcomps/impl/PrettySvgComp.java | 19 ++++++++------ .../io/xpipe/app/fxcomps/impl/SvgView.java | 25 +++++++++++++++---- gradle/gradle_scripts/javafx.gradle | 6 +---- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/fxcomps/impl/PrettySvgComp.java b/app/src/main/java/io/xpipe/app/fxcomps/impl/PrettySvgComp.java index 86bbcf9c0..3fb8cf80b 100644 --- a/app/src/main/java/io/xpipe/app/fxcomps/impl/PrettySvgComp.java +++ b/app/src/main/java/io/xpipe/app/fxcomps/impl/PrettySvgComp.java @@ -75,14 +75,17 @@ public class PrettySvgComp extends SimpleComp { ar); var stack = new StackPane(); - var node = storeIcon.createWebview(); - node.prefWidthProperty().bind(widthProperty); - node.maxWidthProperty().bind(widthProperty); - node.minWidthProperty().bind(widthProperty); - node.prefHeightProperty().bind(heightProperty); - node.maxHeightProperty().bind(heightProperty); - node.minHeightProperty().bind(heightProperty); - stack.getChildren().add(node); + var wv = storeIcon.createWebview(); + if (wv.isPresent()) { + var node = wv.get(); + node.prefWidthProperty().bind(widthProperty); + node.maxWidthProperty().bind(widthProperty); + node.minWidthProperty().bind(widthProperty); + node.prefHeightProperty().bind(heightProperty); + node.maxHeightProperty().bind(heightProperty); + node.minHeightProperty().bind(heightProperty); + stack.getChildren().add(node); + } Consumer update = val -> { var fixed = val != null diff --git a/app/src/main/java/io/xpipe/app/fxcomps/impl/SvgView.java b/app/src/main/java/io/xpipe/app/fxcomps/impl/SvgView.java index d8f1bec2c..d7fc93ad5 100644 --- a/app/src/main/java/io/xpipe/app/fxcomps/impl/SvgView.java +++ b/app/src/main/java/io/xpipe/app/fxcomps/impl/SvgView.java @@ -3,7 +3,7 @@ package io.xpipe.app.fxcomps.impl; import io.xpipe.app.core.AppProperties; import io.xpipe.app.fxcomps.CompStructure; import io.xpipe.app.fxcomps.util.PlatformThread; - +import io.xpipe.app.issue.ErrorEvent; import javafx.beans.binding.Bindings; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.value.ObservableValue; @@ -13,12 +13,12 @@ import javafx.scene.Node; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.scene.web.WebView; - import lombok.Builder; import lombok.Getter; import lombok.SneakyThrows; import lombok.Value; +import java.util.Optional; import java.util.Set; @Getter @@ -28,6 +28,8 @@ public class SvgView { private final ObservableValue height; private final ObservableValue svgContent; + private static boolean canCreateWebview = true; + private SvgView(ObservableValue width, ObservableValue height, ObservableValue svgContent) { this.width = PlatformThread.sync(width); this.height = PlatformThread.sync(height); @@ -55,7 +57,20 @@ public class SvgView { } private WebView createWebView() { - var wv = new WebView(); + if (!canCreateWebview) { + return null; + } + + WebView wv; + try { + // This can happen if we are using a custom JavaFX build without webkit + wv = new WebView(); + } catch (Throwable t) { + ErrorEvent.fromThrowable(t).omit().expected().handle(); + canCreateWebview = false; + return null; + } + wv.getEngine() .setUserDataDirectory( AppProperties.get().getDataDir().resolve("webview").toFile()); @@ -109,10 +124,10 @@ public class SvgView { return wv; } - public WebView createWebview() { + public Optional createWebview() { var wv = createWebView(); wv.getStyleClass().add("svg-comp"); - return wv; + return Optional.ofNullable(wv); } @Value diff --git a/gradle/gradle_scripts/javafx.gradle b/gradle/gradle_scripts/javafx.gradle index b25a9f935..8720c92d0 100644 --- a/gradle/gradle_scripts/javafx.gradle +++ b/gradle/gradle_scripts/javafx.gradle @@ -25,11 +25,7 @@ if (customJavaFxPath != null) { } } dependencies { - javafx name: "org.openjfx:javafx-base" - javafx name: "org.openjfx:javafx-controls" - javafx name: "org.openjfx:javafx-graphics" - javafx name: "org.openjfx:javafx-media" - javafx name: "org.openjfx:javafx-web" + javafx fileTree(dir: customJavaFxPath, include: '*.jar') } } else { // Always use maven version for now