Bug 539592 - Set CSS class to custom widget
Summary: Set CSS class to custom widget
Status: RESOLVED WORKSFORME
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.8   Edit
Hardware: PC All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Arian Fornaris CLA
QA Contact:
URL:
Whiteboard:
Keywords: noteworthy
Depends on:
Blocks:
 
Reported: 2018-09-27 16:54 EDT by Arian Fornaris CLA
Modified: 2019-09-17 09:41 EDT (History)
6 users (show)

See Also:


Attachments
Custom widget with not style. (282.76 KB, image/png)
2018-09-27 16:54 EDT, Arian Fornaris CLA
no flags Details
Composite instance (66.93 KB, image/png)
2018-10-01 11:58 EDT, Arian Fornaris CLA
no flags Details
Composite subclass instance (53.83 KB, image/png)
2018-10-01 11:59 EDT, Arian Fornaris CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Arian Fornaris CLA 2018-09-27 16:54:54 EDT
Created attachment 276033 [details]
Custom widget with not style.

Right now, to style custom widgets it is required to extend the themes. The proposal is to allow custom widgets to specify an already existent CSS class to be used to style him.

Something like this:


class MyCanvas extends Canvas {

    public MyCanvas(Composite parent, int style) {
    	super(parent, style);

    	setData("org.eclipse.e4.ui.css.CssClassName", "Canvas");
    }

}

So, the theme engine should apply the style defined for the class Canvas to the all the instances of MyCanvas.

In the attached image, you can see how custom Composite and Canvas are not styled.
Comment 1 Lars Vogel CLA 2018-09-27 23:57:52 EDT
Hi Arian, as discussed via the e4 mailing list, this is a good idea. Please provide a Gerrit for org.eclipse.ui.themes/dark/e4-dark_globalstyle.css adding the ".Canvas" and ".Composite" selector to the first selector.

See http://www.vogella.com/tutorials/EclipsePlatformDevelopment/article.html#exericse-eclipse-user-creation-and-gerrit-server-configuration for a description of the necessary setup.
Comment 2 Lars Vogel CLA 2018-09-28 00:00:06 EDT
Please also add a corresponding test to org.eclipse.e4.ui.tests.css.swt.CompositeTest
Comment 3 Mickael Istria CLA 2018-09-28 05:10:15 EDT
Should CSS inheritance apply here?
Comment 4 Lars Vogel CLA 2018-09-28 07:08:46 EDT
(In reply to Mickael Istria from comment #3)
> Should CSS inheritance apply here?

No.
Comment 5 Arian Fornaris CLA 2018-09-28 08:54:00 EDT
Is there a way to just build only the "org.eclipse.ui.themes" bundle. The mvn clean is running a lot of tests that in the end, one fails and the build is stopped.
Comment 6 Lars Vogel CLA 2018-09-28 09:04:52 EDT
(In reply to Arian Fornaris from comment #5)
> Is there a way to just build only the "org.eclipse.ui.themes" bundle. The
> mvn clean is running a lot of tests that in the end, one fails and the build
> is stopped.

Use -DskipTests=true

But you can also test in a runtime Eclipse as describe here: http://www.vogella.com/tutorials/EclipsePlugin/article.html#runtimeeclipse_starting
Comment 7 Arian Fornaris CLA 2018-09-28 09:10:14 EDT
Hi Lars, ok, I will try in that way.


By the way, a workaround from Mickael Istria (posted in the email list):

> I the parent is a composite, did you try parent.setBackgroundMode(SWT.INHERITS_FORCE) ?

And yes, it works with Composite and Canvas.
Comment 8 Lars Vogel CLA 2018-09-28 10:55:37 EDT
I prefer our solution to allow to set the CSS class. I think this is better compare to modifying the swt code.:-)
Comment 9 Mickael Istria CLA 2018-09-28 11:01:21 EDT
(In reply to Lars Vogel from comment #8)
> I prefer our solution to allow to set the CSS class. I think this is better
> compare to modifying the swt code.:-)

I think there is something fundamentally wrong on the usage of CSS done by SWT. CSS is by definition "cascading" and that's what makes it very practical. SWT is not (there is inheritance in styling). Hence let's not pretend CSS is the solution to the problem of inheriting colors or other style things that an adopter is very likely to prefer inherited.
Adding proper inheritance/cascading of the main style configuration in SWT will result in much cleaner, agile and customizable, and higher quality applications than trying to use CSS to fix (workaround?) every style issue caused by lack of inheritance.
Comment 10 Lars Vogel CLA 2018-09-28 11:13:38 EDT
Michael, sorry but please open new bugs for "reinventing CSS", this bug is about opting in for customer widgets into the styling of Composite and Canvas. Solution is trivial and will improve the situation.
Comment 11 Arian Fornaris CLA 2018-10-01 11:58:32 EDT
Created attachment 276096 [details]
Composite instance
Comment 12 Arian Fornaris CLA 2018-10-01 11:59:07 EDT
Created attachment 276097 [details]
Composite subclass instance
Comment 13 Arian Fornaris CLA 2018-10-01 12:00:38 EDT
Hi,

Sorry the delay, I am traveling to a conference. I did some tests and found some "solutions".

Solution 1:

I added the .Canvas and .Composite class to the first rule of the CSS file, but it does not work 100%.

You can see in the image "Composite sublass instance.png" (https://bugs.eclipse.org/bugs/attachment.cgi?id=276097), the custom composite is styled, but not with the right color.

If I do not subclass the composite, then it is styled well. See image "Composite instance.png" (https://bugs.eclipse.org/bugs/attachment.cgi?id=276097).

I think to use a .Composite or .Canvas class is not the best solution. Third party themes are not going to work if they don't include these classes in the right rules.

Solution 2:

I created this rule:

.Transparent {
	background-color: inherit;
	color: inherit;
}

If I set the Transparent class to the custom Composite and Canvas then it works. Maybe, as a good practice, all themes should include that rule, or we should create a bootstrap CSS theme with this rule, and import it in all the themes.

Solution 3:

Other possible solution is to allow the user to set the "Tag Name" of a widget. I mean, if you do something like this:

class MyWidget extends Composite {
	public MyComposite(...) {
		setData("org.eclipse.e4.ui.css.CssElementName", "Composite");
	}
}


Then, the "Composite" query selector will match the instances of MyWidget.

Solution 4:

Inline CSS. In HTML you can set the style of a DOM element in the element definition:

<div style="color: blue"></div>

We can do the same in SWT, so the user can do something like this:

class MyWidget extends Composite {
	public MyComposite(...) {
		setData("org.eclipse.e4.ui.css.CssStyle", "background-color: inherit; color: inherit;");
	}
}

Solution 5:

In the meantime, I will use this workaround from Mickael Istria, to "inherite" the background/foreground colors from the parent:


class MyWidget extends Composite {

	public MyComposite(Composite parent, int style) {
		super(parent, style);

		parent.setBackgroundMode(SWT.INHERIT_FORCE);

		addPaintListener(new PaintListener() {

			@Override
			public void paintControl(PaintEvent e) {
				MyWidget.this.setForeground(parent.getForeground());
			}
		});
	}
}



Best regards,
Arian.
Comment 14 Lars Vogel CLA 2018-10-08 11:38:32 EDT
Thanks Arian. We need Solution 1 in platform to allow Solution 3 for customer widgets. Can you upload a Gerrit for the change in the platform CSS?
Comment 15 Arian Fornaris CLA 2018-10-18 09:51:00 EDT
Hi Lars,

I was offline for several days. I will look at this soon.

Thanks
Arian
Comment 16 Arian Fornaris CLA 2018-10-18 15:12:33 EDT
Hi Lars,

I don't see how Solution 1 (setting a CSS class to a widget) is related to Solution 3 (setting a CSS "tag name" to a widget). Both are independent.

As I commented before, I think solution 1 is not the best.
Comment 17 Lars Vogel CLA 2018-10-18 15:27:02 EDT
(In reply to Arian Fornaris from comment #16)
> Hi Lars,
> As I commented before, I think solution 1 is not the best.

Solution 1 is an easy way to allow plug-in developer to opt into the platform styling and would work for all possible themes, as they will use the same styling as Composite and Canvas as defined by the theme.

Developer could then say:

myWidget.setData("org.eclipse.e4.ui.css.CssClassName", "Composite");
or
myWidget.setData("org.eclipse.e4.ui.css.CssClassName", "Canvas");

to use the same styling. I think that is the solution 3.
Comment 18 Arian Fornaris CLA 2018-10-18 15:43:51 EDT
well, using a .Canvas and .Composite class did not work for me (the widgets gets darker, but not with the right colors: https://bugs.eclipse.org/bugs/attachment.cgi?id=276097 ). I am pretty sure because I need to add more rules to the dark stylesheet. 

Yet I think a better solution is tu add new rule (Solution 2):

.Transparent {
	background-color: inherit;
	color: inherit;
}


Then the users can use the Transparent class and it just works. So the only thing we have to do is to create a common.css file that is inherited by all other CSS files.
Comment 19 Lars Vogel CLA 2018-10-23 05:32:40 EDT
(In reply to Arian Fornaris from comment #18)
> well, using a .Canvas and .Composite class did not work for me (the widgets
> gets darker, but not with the right colors:
> https://bugs.eclipse.org/bugs/attachment.cgi?id=276097 ). I am pretty sure
> because I need to add more rules to the dark stylesheet. 
> 
> Yet I think a better solution is tu add new rule (Solution 2):
> 
> .Transparent {
> 	background-color: inherit;
> 	color: inherit;
> }
> 
> 
> Then the users can use the Transparent class and it just works. So the only
> thing we have to do is to create a common.css file that is inherited by all
> other CSS files.

Please upload a Gerrit patch so that I can check.
Comment 20 Arian Fornaris CLA 2018-10-23 16:52:15 EDT
> Please upload a Gerrit patch so that I can check.

Ok I will.
Comment 21 Arian Fornaris CLA 2018-11-08 01:57:22 EST
Hi Lars,

I am trying to push the changes but I get this error:


Counting objects: 9, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (9/9), 1.13 KiB | 288.00 KiB/s, done.
Total 9 (delta 8), reused 0 (delta 0)
remote: Resolving deltas: 100% (8/8)
remote: Processing changes: refs: 1, done    
remote: (W) 5a14593: commit subject >50 characters; use shorter first paragraph
remote: (W) 5a14593: too many commit message lines longer than 72 characters; manually wrap lines
remote: ----------
remote: Reviewing commit: 5a145935
remote: Authored by: Arian Fornaris <boniatillo@gmail.com>
remote: 
remote: The author is not a committer on the project.
remote: The author has a current Eclipse Contributor Agreement (ECA) on file.
remote: error: The author has not "signed-off" on the contribution.
remote: If there are multiple commits, please ensure that each commit is signed-off.
remote: Please see http://wiki.eclipse.org/ECA
To https://git.eclipse.org/r/platform/eclipse.platform.ui
 ! [remote rejected]       HEAD -> refs/for/master (The contributor must "sign-off" on the contribution.)
error: failed to push some refs to 'https://git.eclipse.org/r/platform/eclipse.platform.ui'
Comment 22 Lars Vogel CLA 2018-11-08 02:01:08 EST
(In reply to Arian Fornaris from comment #21)
> Hi Lars,
> 
> I am trying to push the changes but I get this error:

> remote: error: The author has not "signed-off" on the contribution.
> remote: If there are multiple commits, please ensure that each commit is
> signed-off.

Sounds like you forgot to sign off the commit. If you use the Git staging view, simply amend the commit and press the "Added Sign-Off by" button in the toolbar.
Comment 23 Arian Fornaris CLA 2018-11-08 02:10:21 EST
Well, I revoked the ECA, because it shows an expiration message. Now it says it takes some time to file a new ECA.
Comment 24 Eclipse Genie CLA 2018-11-08 02:12:28 EST
New Gerrit change created: https://git.eclipse.org/r/132102
Comment 25 Arian Fornaris CLA 2018-11-08 02:14:45 EST
Hi Lars,

You can review the changes here:

https://git.eclipse.org/r/#/c/132102/

I just added the Transparent rule to the base styles.
Comment 26 Lars Vogel CLA 2018-11-08 07:08:25 EST
(In reply to Arian Fornaris from comment #25)
> Hi Lars,
> 
> You can review the changes here:
> 
> https://git.eclipse.org/r/#/c/132102/
> 
> I just added the Transparent rule to the base styles.

Please see feedback in the Gerrit.
Comment 27 Lars Vogel CLA 2018-11-19 10:34:32 EST
Arian, any plans to provide tests?
Comment 28 Arian Fornaris CLA 2018-11-19 17:24:33 EST
Sorry Lars, I cannot contribute at this moment. I sent you a private message.
Comment 29 Lars Vogel CLA 2019-02-19 03:32:37 EST
Mass change, please reset target if you still planning to fix this for 4.11.
Comment 30 Kalyan Prasad Tatavarthi CLA 2019-05-28 05:57:55 EDT
Please set the target milestone back to 4.12 if you still intend to fix this for 4.12.
Comment 31 Arian Fornaris CLA 2019-05-31 12:57:13 EDT
Hi,

I solved this by creating a "stylesheet" extension. So I add my own CSS to the current themes and include the ".Transparent" rule, and use it in my custom components. 

I think third-party plugin developers can I use this technique.

Do you think is it valid to add the .Transparent rule to the Eclipse base code?
Comment 32 Mickael Istria CLA 2019-05-31 13:02:24 EDT
(In reply to Arian Fornaris from comment #31)
> I think third-party plugin developers can I use this technique.

It would be great if you could document it in some blog post or similar.

> Do you think is it valid to add the .Transparent rule to the Eclipse base
> code?

I have no strong objection personally, but I think we usually prefer adding specific customization if and only if we use them in the workbench/IDE.
Comment 33 Arian Fornaris CLA 2019-06-01 08:01:41 EDT
(In reply to Mickael Istria from comment #32)

> 
> It would be great if you could document it in some blog post or similar.
> 

Ok, let me see where I can post it. Maybe in Medium.


> 
> I have no strong objection personally, but I think we usually prefer adding
> specific customization if and only if we use them in the workbench/IDE.

I agree. For that reason, I think this bug can be closed. The workbench/IDE does not need to use this (I guess), because it is not using it right now.
Comment 34 Dani Megert CLA 2019-09-17 09:41:53 EDT
Closing as per comment 33.