From 8ebf58e8fa5560ba7ccf18ba5c22b9210f49748f Mon Sep 17 00:00:00 2001 From: Glavo Date: Wed, 9 Jul 2025 19:39:28 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20#4072:=20=E4=BF=AE=E5=A4=8D=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E4=BD=BF=E7=94=A8=E9=83=A8=E5=88=86=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E5=AD=97=E4=BD=93=E7=9A=84=E9=97=AE=E9=A2=98=20(#4074)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jackhuang/hmcl/setting/FontManager.java | 67 ++++++++++++++++--- .../jackhuang/hmcl/setting/StyleSheets.java | 11 +-- 2 files changed, 62 insertions(+), 16 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/setting/FontManager.java b/HMCL/src/main/java/org/jackhuang/hmcl/setting/FontManager.java index c812ac0f8..05b68ca4f 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/setting/FontManager.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/setting/FontManager.java @@ -26,6 +26,8 @@ import org.jackhuang.hmcl.util.Lazy; import org.jackhuang.hmcl.util.io.JarUtils; import org.jackhuang.hmcl.util.platform.OperatingSystem; import org.jackhuang.hmcl.util.platform.SystemUtils; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; @@ -35,6 +37,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Locale; +import java.util.Objects; import static org.jackhuang.hmcl.setting.ConfigHolder.config; import static org.jackhuang.hmcl.util.logging.Logger.LOG; @@ -78,7 +81,7 @@ public final class FontManager { return null; }); - private static final ObjectProperty fontProperty; + private static final ObjectProperty fontProperty; static { String fontFamily = config().getLauncherFontFamily(); @@ -87,10 +90,16 @@ public final class FontManager { if (fontFamily == null) fontFamily = System.getenv("HMCL_FONT"); - Font font = fontFamily == null ? DEFAULT_FONT.get() : Font.font(fontFamily, DEFAULT_FONT_SIZE); - fontProperty = new SimpleObjectProperty<>(font); + FontReference fontReference; + if (fontFamily == null) { + Font defaultFont = DEFAULT_FONT.get(); + fontReference = defaultFont != null ? new FontReference(defaultFont) : null; + } else + fontReference = new FontReference(fontFamily); - LOG.info("Font: " + (font != null ? font.getName() : Font.getDefault().getName())); + fontProperty = new SimpleObjectProperty<>(fontReference); + + LOG.info("Font: " + (fontReference != null ? fontReference.getFamily() : "System")); fontProperty.addListener((obs, oldValue, newValue) -> { if (newValue != null) config().setLauncherFontFamily(newValue.getFamily()); @@ -189,20 +198,62 @@ public final class FontManager { } } - public static ObjectProperty fontProperty() { + public static ObjectProperty fontProperty() { return fontProperty; } - public static Font getFont() { + public static FontReference getFont() { return fontProperty.get(); } - public static void setFont(Font font) { + public static void setFont(FontReference font) { fontProperty.set(font); } public static void setFontFamily(String fontFamily) { - setFont(fontFamily != null ? Font.font(fontFamily, DEFAULT_FONT_SIZE) : null); + setFont(fontFamily != null ? new FontReference(fontFamily) : null); + } + + // https://github.com/HMCL-dev/HMCL/issues/4072 + public static final class FontReference { + private final @NotNull String family; + private final @Nullable String style; + + public FontReference(@NotNull String family) { + this.family = Objects.requireNonNull(family); + this.style = null; + } + + public FontReference(@NotNull Font font) { + this.family = font.getFamily(); + this.style = font.getStyle(); + } + + public @NotNull String getFamily() { + return family; + } + + public @Nullable String getStyle() { + return style; + } + + @Override + public boolean equals(Object o) { + if (!(o instanceof FontReference)) + return false; + FontReference that = (FontReference) o; + return Objects.equals(family, that.family) && Objects.equals(style, that.style); + } + + @Override + public int hashCode() { + return Objects.hash(family, style); + } + + @Override + public String toString() { + return String.format("FontReference[family='%s', style='%s']", family, style); + } } private FontManager() { 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 5003d7b46..922195866 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/setting/StyleSheets.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/setting/StyleSheets.java @@ -22,7 +22,6 @@ import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.paint.Color; -import javafx.scene.text.Font; import org.jackhuang.hmcl.ui.FXUtils; import org.jackhuang.hmcl.util.io.FileUtils; @@ -80,9 +79,9 @@ public final class StyleSheets { private static String getFontStyleSheet() { final String defaultCss = "/assets/css/font.css"; - final Font font = FontManager.getFont(); + final FontManager.FontReference font = FontManager.getFont(); - if (font == null || font == Font.getDefault()) + if (font == null || "System".equals(font.getFamily())) return defaultCss; String fontFamily = font.getFamily(); @@ -115,11 +114,7 @@ public final class StyleSheets { StringBuilder builder = new StringBuilder(); builder.append(".root {"); - if (fontFamily == null) - // https://github.com/HMCL-dev/HMCL/pull/3423 - builder.append("-fx-font-family: -fx-base-font-family;"); - else - builder.append("-fx-font-family:\"").append(fontFamily).append("\";"); + builder.append("-fx-font-family:\"").append(fontFamily).append("\";"); if (weight != null) builder.append("-fx-font-weight:").append(weight).append(";");