修复重置字体后没有遵循 hmcl.font.override HMCL_FONT 等的问题 (#5183)

Co-authored-by: Glavo <zjx001202@gmail.com>
This commit is contained in:
辞庐
2026-01-10 21:44:01 +08:00
committed by GitHub
parent 06cce5259b
commit 45a85e006b

View File

@@ -18,6 +18,7 @@
package org.jackhuang.hmcl.setting; package org.jackhuang.hmcl.setting;
import javafx.beans.property.ObjectProperty; import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleObjectProperty; import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.text.Font; import javafx.scene.text.Font;
import org.jackhuang.hmcl.Metadata; import org.jackhuang.hmcl.Metadata;
@@ -94,31 +95,27 @@ public final class FontManager {
return null; return null;
}); });
private static final ObjectProperty<FontReference> fontProperty; private static final ObjectProperty<FontReference> font = new SimpleObjectProperty<>();
static { static {
updateFont();
LOG.info("Font: " + (font.get() != null ? font.get().getFamily() : "System"));
}
private static void updateFont() {
String fontFamily = config().getLauncherFontFamily(); String fontFamily = config().getLauncherFontFamily();
if (fontFamily == null) if (fontFamily == null)
fontFamily = System.getProperty("hmcl.font.override"); fontFamily = System.getProperty("hmcl.font.override");
if (fontFamily == null) if (fontFamily == null)
fontFamily = System.getenv("HMCL_FONT"); fontFamily = System.getenv("HMCL_FONT");
FontReference fontReference;
if (fontFamily == null) { if (fontFamily == null) {
Font defaultFont = DEFAULT_FONT.get(); Font defaultFont = DEFAULT_FONT.get();
fontReference = defaultFont != null ? new FontReference(defaultFont) : null; font.set(defaultFont != null ? new FontReference(defaultFont) : null);
} else } else {
fontReference = new FontReference(fontFamily); font.set(new FontReference(fontFamily));
}
fontProperty = new SimpleObjectProperty<>(fontReference);
LOG.info("Font: " + (fontReference != null ? fontReference.getFamily() : "System"));
fontProperty.addListener((obs, oldValue, newValue) -> {
if (newValue != null)
config().setLauncherFontFamily(newValue.getFamily());
else
config().setLauncherFontFamily(null);
});
} }
private static Font tryLoadDefaultFont(Path dir) { private static Font tryLoadDefaultFont(Path dir) {
@@ -231,20 +228,17 @@ public final class FontManager {
} }
} }
public static ObjectProperty<FontReference> fontProperty() { public static ReadOnlyObjectProperty<FontReference> fontProperty() {
return fontProperty; return font;
} }
public static FontReference getFont() { public static FontReference getFont() {
return fontProperty.get(); return font.get();
}
public static void setFont(FontReference font) {
fontProperty.set(font);
} }
public static void setFontFamily(String fontFamily) { public static void setFontFamily(String fontFamily) {
setFont(fontFamily != null ? new FontReference(fontFamily) : null); config().setLauncherFontFamily(fontFamily);
updateFont();
} }
// https://github.com/HMCL-dev/HMCL/issues/4072 // https://github.com/HMCL-dev/HMCL/issues/4072
@@ -292,3 +286,4 @@ public final class FontManager {
private FontManager() { private FontManager() {
} }
} }