Wednesday, September 7, 2016

Runtime CSS styling for SWT

In the previous post it was explained how one can do global styling of SWT components in your application via CSS prior.
But that's only the surface, all these capabilities can be used for runtime styling of individual component.  NOTE: Eclipse Oxygen I20160906-0800 or newer on GTK3 required.
Styling individual component that way is even simpler than the traditional SWT API especially if you consider that to achieve some of the capabilities exposed one has to do custom drawing with all its verbosity and potential problems.
Enough words - the magic is in Widget.setData(String, Object) method and the secret key org.eclipse.swt.internal.gtk.css . All you have to do is call yourWidget.setData("org.eclipse.swt.internal.gtk.css", yourCSS);  with proper GTK 3.20+ CSS (note: this is labeled as API by GTK guys thus old versions are not accepted).
The amount of properties supported is pretty wide and ever growing. At least for my limited CSS knowledge most of the things I tried just worked.
Time for screencasts:

 I'm really enthusiastic about the future, hopefully you will be too after watching the videos and trying it yourself !