Skip to content

在 Linux 平台使用 dbus-send 检测深色模式设置#5317

Merged
Glavo merged 6 commits intoHMCL-dev:mainfrom
Glavo:dbus
Feb 1, 2026
Merged

在 Linux 平台使用 dbus-send 检测深色模式设置#5317
Glavo merged 6 commits intoHMCL-dev:mainfrom
Glavo:dbus

Conversation

@Glavo
Copy link
Member

@Glavo Glavo commented Jan 24, 2026

No description provided.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

为 Linux 平台增加通过 dbus-send(xdg-desktop-portal Settings API)读取系统 color-scheme,从而在“自动”主题亮度模式下更准确跟随系统深色/浅色设置。

Changes:

  • Themes.getDefaultBrightness() 中新增 Linux 分支,通过 dbus-send 调用 org.freedesktop.portal.Settings.Read 获取 org.freedesktop.appearance color-scheme
  • 解析返回值并映射到 Brightness.DARK/LIGHT,失败时记录 warning 日志
  • 增加相关 imports(Path, FileUtils, LOG

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +133 to +137
String[] result = SystemUtils.run(
FileUtils.getAbsolutePath(dbusSend),
"--session",
"--print-reply=literal",
"--reply-timeout=1000",
Copy link

Copilot AI Jan 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This runs an external process synchronously inside getDefaultBrightness(), which is used by a JavaFX ObjectBinding (computeValue() -> getBrightness()), so it will typically execute on the FX thread. SystemUtils.run(...) can block up to 15s (see HMCLCore/.../SystemUtils.java:98-103), which risks noticeable UI freezes on Linux if dbus-send hangs. Consider enforcing a much shorter overall wait (e.g., Process.waitFor with a small timeout and then destroy the process) or moving the D-Bus query off the FX thread and caching the result before bindings are evaluated.

Copilot uses AI. Check for mistakes.
"org.freedesktop.portal.Settings.Read",
"string:org.freedesktop.appearance",
"string:color-scheme"
).trim().split(" ");
Copy link

Copilot AI Jan 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trim().split(" ") only splits on single space characters and will behave poorly with other whitespace (tabs/newlines) and with the multi-space indentation dbus-send commonly prints. To make theme detection more robust, split on \\s+ (or parse the trailing integer via a regex) before interpreting the last token as the color-scheme value.

Suggested change
).trim().split(" ");
).trim().split("\\s+");

Copilot uses AI. Check for mistakes.
Glavo added 3 commits January 30, 2026 20:44
# Conflicts:
#	HMCL/src/main/java/org/jackhuang/hmcl/theme/Themes.java
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

"org.freedesktop.portal.Settings.Read",
"string:org.freedesktop.appearance",
"string:color-scheme"
), Duration.ofSeconds(2)).trim().split(" ");
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using .trim().split(" ") only splits on single space characters. If dbus-send --print-reply=literal returns output with tabs/newlines or multiple whitespace between tokens, this parsing can fail to extract the numeric value.

Consider splitting on \\s+ (or extracting the trailing integer via regex) before mapping 1/2 to dark/light.

Suggested change
), Duration.ofSeconds(2)).trim().split(" ");
), Duration.ofSeconds(2)).trim().split("\\s+");

Copilot uses AI. Check for mistakes.
@Glavo Glavo merged commit 17be25c into HMCL-dev:main Feb 1, 2026
2 checks passed
@Glavo Glavo deleted the dbus branch February 1, 2026 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants