From 168c4ac708f5be1039d7ea0667dd5ec7f7dc7d78 Mon Sep 17 00:00:00 2001 From: Glavo Date: Mon, 29 Sep 2025 16:51:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=80=9A=E8=BF=87=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=E6=8E=A7=E5=88=B6=20UI=20=E7=BC=A9?= =?UTF-8?q?=E6=94=BE=E6=AF=94=E4=BE=8B=20(#4521)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/jackhuang/hmcl/EntryPoint.java | 81 +++++++++++++++---- 1 file changed, 66 insertions(+), 15 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/EntryPoint.java b/HMCL/src/main/java/org/jackhuang/hmcl/EntryPoint.java index 396c880f7..dc8ba26d8 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/EntryPoint.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/EntryPoint.java @@ -46,21 +46,7 @@ public final class EntryPoint { createHMCLDirectories(); LOG.start(Metadata.HMCL_CURRENT_DIRECTORY.resolve("logs")); - if ("true".equalsIgnoreCase(System.getenv("HMCL_FORCE_GPU"))) - System.getProperties().putIfAbsent("prism.forceGPU", "true"); - - String animationFrameRate = System.getenv("HMCL_ANIMATION_FRAME_RATE"); - if (animationFrameRate != null) { - try { - if (Integer.parseInt(animationFrameRate) <= 0) - throw new NumberFormatException(animationFrameRate); - - System.getProperties().putIfAbsent("javafx.animation.pulse", animationFrameRate); - } catch (NumberFormatException e) { - LOG.warning("Invalid animation frame rate: " + animationFrameRate); - } - } - + setupJavaFXVMOptions(); checkDirectoryPath(); if (OperatingSystem.CURRENT_OS == OperatingSystem.MACOS) @@ -80,6 +66,71 @@ public final class EntryPoint { System.exit(exitCode); } + private static void setupJavaFXVMOptions() { + if ("true".equalsIgnoreCase(System.getenv("HMCL_FORCE_GPU"))) { + LOG.info("HMCL_FORCE_GPU: true"); + System.getProperties().putIfAbsent("prism.forceGPU", "true"); + } + + String animationFrameRate = System.getenv("HMCL_ANIMATION_FRAME_RATE"); + if (animationFrameRate != null) { + LOG.info("HMCL_ANIMATION_FRAME_RATE: " + animationFrameRate); + + try { + if (Integer.parseInt(animationFrameRate) <= 0) + throw new NumberFormatException(animationFrameRate); + + System.getProperties().putIfAbsent("javafx.animation.pulse", animationFrameRate); + } catch (NumberFormatException e) { + LOG.warning("Invalid animation frame rate: " + animationFrameRate); + } + } + + String uiScale = System.getProperty("hmcl.uiScale", System.getenv("HMCL_UI_SCALE")); + if (uiScale != null) { + uiScale = uiScale.trim(); + + LOG.info("HMCL_UI_SCALE: " + uiScale); + + try { + float scaleValue; + if (uiScale.endsWith("%")) { + scaleValue = Integer.parseInt(uiScale.substring(0, uiScale.length() - 1)) / 100.0f; + } else if (uiScale.endsWith("dpi") || uiScale.endsWith("DPI")) { + scaleValue = Integer.parseInt(uiScale.substring(0, uiScale.length() - 3)) / 96.0f; + } else { + scaleValue = Float.parseFloat(uiScale); + } + + float lowerBound; + float upperBound; + + if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) { + // JavaFX behavior may be abnormal when the DPI scaling factor is too high + lowerBound = 0.25f; + upperBound = 4f; + } else { + lowerBound = 0.01f; + upperBound = 10f; + } + + if (scaleValue >= lowerBound && scaleValue <= upperBound) { + if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) { + System.getProperties().putIfAbsent("glass.win.uiScale", uiScale); + } else if (OperatingSystem.CURRENT_OS == OperatingSystem.MACOS) { + LOG.warning("macOS does not support setting UI scale, so it will be ignored"); + } else { + System.getProperties().putIfAbsent("glass.gtk.uiScale", uiScale); + } + } else { + LOG.warning("UI scale out of range: " + uiScale); + } + } catch (Throwable e) { + LOG.warning("Invalid UI scale: " + uiScale); + } + } + } + private static void createHMCLDirectories() { if (!Files.isDirectory(Metadata.HMCL_CURRENT_DIRECTORY)) { try {