如何确保CSS文件中定义的颜色在SWT控件中生效?
SWT背景色获取与CSS应用的冲突问题
在SWT中存在一个常见的棘手问题:通过getBackground()获取的背景色可能与控件实际显示的颜色不一致。
该问题在Eclipse平台的两个官方议题中被提及。
阅读部分源码后发现,该问题解决难度较大,与SWT针对背景色的各类设计原则相关,短期内可能无法彻底解决。
目前有一个相对可靠的临时解决方案:
control.setBackground( control.getBackground() ) ;
通过这种方式,指定的颜色会成为org.eclipse.swt.widgets.Control.background字段的值,在控件渲染背景色时拥有更高优先级。
但现在出现了新问题:CSS在应用背景色时统一使用如下逻辑:
// org.eclipse.e4.ui.css.swt.helpers.CSSSWTColorHelper.setBackground(Control, Color) /** Helper function to avoid setting colors unnecessarily */ public static void setBackground(Control control, Color newColor) { if (!Objects.equals(control.getBackground(), newColor)) { control.setBackground(newColor); } }
这会触发开头提到的问题,导致CSS文件中定义的预期颜色无法在界面上正常显示。
我想到的一种临时修改方案是在程序中重写该方法:
public static void setBackground(Control control, Color newColor) { control.setBackground(newColor); }
请问是否存在更优雅的解决方法?
内容的提问来源于stack exchange,提问作者Bourbon_7
相关产品推荐
相关产品推荐

