在SWT或Eclipse RCP应用程序中检测暗黑模式?

huangapple go评论64阅读模式
英文:

Detect dark mode in SWT or Eclipse RCP application?

问题

我可以从代码中确定 SWT/RCP 应用程序是否在暗色主题下运行吗?

我迄今为止尝试过的方法:

  • 方法 org.eclipse.swt.widgets.Display.isSystemDarkTheme() 报告操作系统是否喜欢暗模式。但是,我曾观察到即使该方法返回 true,应用程序也在亮模式下运行。
  • 获取类似于 SWT.COLOR_LIST_BACKGROUND 的颜色,将其转换为 HSB,然后检查其亮度。这个方法有效,但似乎有点绕。
英文:

How can I determine from code whether the SWT/RCP application it's running is being presented in a dark theme?

What I've tried so far:

  • The method org.eclipse.swt.widgets.Display.isSystemDarkTheme() reports whether the operating system prefers dark mode. However, I've observed an application running in light mode even when that method returns true.
  • Get the Color for something like SWT.COLOR_LIST_BACKGROUND, convert it to HSB, and check its brightness. Works, but seems roundabout.

答案1

得分: 1

Display.isSystemDarkTheme() 仅返回一个提示,操作系统中设置的暗色或浅色主题,可能与 Eclipse 中设置的主题不同。请参考Javadoc

注意:此操作仅为提示,不支持没有此概念的平台。

请注意,还可以存在混合的暗色和浅色主题,并且用户可以更改颜色,所以在您的情况下,可能更好的方法是使用特定元素的背景颜色(甚至更精确地,比较前景/文本颜色的亮度与背景颜色的亮度)而不是依赖提示或主题ID(例如Eclipse 本身在这里所做的)。

英文:

Display.isSystemDarkTheme() returns only a hint, whether a dark or a light theme is set in the operating system which might be differ from the theme set in Eclipse. See Javadoc:

> Note: This operation is a hint and is not supported on platforms that
> do not have this concept.

Be aware that there can also be a mixed dark and light theme and that colors can be changed by the user, so in your case it would probably be better to use the background color (or even more precise, comparing the lightness of the foreground/text color vs. the lightness of the background color) of the specific element instead of relying on a hint or on a theme ID (as Eclipse itself does here, for example).

huangapple
  • 本文由 发表于 2023年2月16日 02:29:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464042.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定