From c65e4d83a21280db6791c55eadd5636bbf3d592a Mon Sep 17 00:00:00 2001 From: Glavo Date: Thu, 11 Sep 2025 16:01:59 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96=20StyleSheets=20(#4453)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 现在我们总是使用 JavaFX 17+,所以不再需要为不支持 `data` URI 的 JavaFX 提供 workaround。 --- .../jackhuang/hmcl/setting/StyleSheets.java | 29 +++---------------- 1 file changed, 4 insertions(+), 25 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/setting/StyleSheets.java b/HMCL/src/main/java/org/jackhuang/hmcl/setting/StyleSheets.java index 745c8a271..d61dcd052 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/setting/StyleSheets.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/setting/StyleSheets.java @@ -22,19 +22,13 @@ import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.paint.Color; -import org.jackhuang.hmcl.ui.FXUtils; -import java.io.IOException; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.Arrays; import java.util.Base64; import java.util.Locale; import static org.jackhuang.hmcl.setting.ConfigHolder.config; -import static org.jackhuang.hmcl.util.logging.Logger.LOG; /** * @author Glavo @@ -57,23 +51,8 @@ public final class StyleSheets { config().themeProperty().addListener(o -> stylesheets.set(THEME_STYLE_SHEET_INDEX, getThemeStyleSheet())); } - private static String toStyleSheetUri(String styleSheet, String fallback) { - if (FXUtils.JAVAFX_MAJOR_VERSION >= 17) - // JavaFX 17+ support loading stylesheets from data URIs - // https://bugs.openjdk.org/browse/JDK-8267554 - return "data:text/css;charset=UTF-8;base64," + Base64.getEncoder().encodeToString(styleSheet.getBytes(StandardCharsets.UTF_8)); - else - try { - Path temp = Files.createTempFile("hmcl", ".css"); - // For JavaFX 17 or earlier, CssParser uses the default charset - // https://bugs.openjdk.org/browse/JDK-8279328 - Files.writeString(temp, styleSheet, Charset.defaultCharset()); - temp.toFile().deleteOnExit(); - return temp.toUri().toString(); - } catch (IOException | NullPointerException e) { - LOG.error("Unable to create stylesheet, fallback to " + fallback, e); - return fallback; - } + private static String toStyleSheetUri(String styleSheet) { + return "data:text/css;charset=UTF-8;base64," + Base64.getEncoder().encodeToString(styleSheet.getBytes(StandardCharsets.UTF_8)); } private static String getFontStyleSheet() { @@ -123,7 +102,7 @@ public final class StyleSheets { builder.append('}'); - return toStyleSheetUri(builder.toString(), fontFamily); + return toStyleSheetUri(builder.toString()); } private static String rgba(Color color, double opacity) { @@ -150,7 +129,7 @@ public final class StyleSheets { "-fx-base-disabled-text-fill:" + rgba(theme.getForegroundColor(), 0.7) + ";" + "-fx-base-text-fill:" + Theme.getColorDisplayName(theme.getForegroundColor()) + ";" + "-theme-thumb:" + rgba(theme.getPaint(), 0.7) + ";" + - '}', blueCss); + '}'); } public static void init(Scene scene) {