How to ensure that the colors defined in the CSS file are used?

68 Views Asked by At

There is a common thorny problem in SWT: the background color obtained by getBackground() may not be the same as the color it actually displays.

As stated in these two issues 1 2 .

After reading some source code, I think the solution to this problem is a bit tricky, and it has to do with the various design principles of SWT for background colors. So this may not be solved for a while.

But it seems that one of the more foolproof solutions is:

control.setBackground( control.getBackground() ) ;

In this way, the given color becomes the value of the field org.eclipse.swt.widgets.Control.background and has a higher priority when the control is rendered using the background color.

So here's the question, CSS uniformly does the following when applying background colors:

// 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);
    }
}

This would run into the problem described at the beginning, resulting in the desired color in the CSS file not actually appearing in the interface.

One way I can think of is to hack this code in my program:

public static void setBackground(Control control, Color newColor) {
    control.setBackground(newColor);
}

Is there a more elegant way to do it ?

0

There are 0 best solutions below