Home » Eclipse Projects » Eclipse 4 » Specify theme-specific preferences via CSS(How to update UI preferences when the user switches to dark theme)
Specify theme-specific preferences via CSS [message #1818562] |
Thu, 19 December 2019 11:44  |
Eclipse User |
|
|
|
I am trying to automatically change the colors of my Xtext editor when the user switches to dark mode [1] but did not succeed so far. According to this newsletter [2] it is possible to ask the CSS Engine to automatically updates preferences according to Eclipse IDE's theme but I can't make it work, even on simple use cases.
Here is what I tried:
- Contribute to the org.eclipse.e4.ui.css.swt.theme extension point
<extension
point="org.eclipse.e4.ui.css.swt.theme">
<stylesheet
uri="css/dark-theme.css">
<themeid
refid="org.eclipse.e4.ui.css.theme.e4_dark">
</themeid>
</stylesheet>
</extension>
- Link the following CSS file
IEclipsePreferences#my-custom-preference-node {
preferences:
'myPref=someValue'
}
- Open an Eclipse IDE runtime
- Call the following code:
System.out.println(InstanceScope.INSTANCE.getNode("my.custom.preference.node").get("myPref", "nothing"));
- Switch to dark theme
- Call the code again
Sadly, "nothing" is shown both time, indicating that the preference specified in the CSS file is not used. I tracked down some relevant bugs on Bugzilla [3, 4, 5] but still can't figure out why my experiment doesn't work. So, before I fill a new bug, may some of you know if I am facing a bug, if I made a mistake or if I am expecting a behavior that is actually not handled by the extension point?
Note: I tested on both Eclipse Photon and 2019-12.
[1] https://www.eclipse.org/forums/index.php/t/1101735/
[2] https://www.eclipse.org/community/eclipse_newsletter/2018/june/darktheme.php#guide-for-plug-in-developers-to-contribute-to-the-default-dark-theme
[3] https://bugs.eclipse.org/bugs/show_bug.cgi?id=433475
[4] https://bugs.eclipse.org/bugs/show_bug.cgi?id=436183
[5] https://bugs.eclipse.org/bugs/show_bug.cgi?id=544714
|
|
| | |
Re: Specify theme-specific preferences via CSS [message #1818608 is a reply to message #1818600] |
Fri, 20 December 2019 08:23   |
Eclipse User |
|
|
|
It should work, we have multiple implementations out there, which style different preferences. For example, JDT and EGit uses this also. /org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/preference/EclipsePreferencesHandler.java handles this. Maybe you put a breakpoint in it and can debug it. It may be that we still have a bug but its works for others.
Here is for example the EGit css file.
/* ############################## EGIT preferences ############################## */
/* See bug 466075 about the pseudo-selector ":org-eclipse-egit-ui" */
IEclipsePreferences#org-eclipse-ui-workbench:org-eclipse-egit-ui {
preferences:
'org.eclipse.egit.ui.DiffAddBackgroundColor=11,121,90'
'org.eclipse.egit.ui.DiffAddForegroundColor=216,254,245'
'org.eclipse.egit.ui.DiffHeadlineBackgroundColor=71,71,71'
'org.eclipse.egit.ui.DiffHeadlineForegroundColor=242,242,242'
'org.eclipse.egit.ui.DiffHunkBackgroundColor=53,97,113'
'org.eclipse.egit.ui.DiffHunkForegroundColor=233,242,254'
'org.eclipse.egit.ui.DiffRemoveBackgroundColor=117,2,36'
'org.eclipse.egit.ui.DiffRemoveForegroundColor=255,232,237'
'org.eclipse.egit.ui.IgnoredResourceBackgroundColor=42,42,42'
'org.eclipse.egit.ui.IgnoredResourceForegroundColor=120,120,120'
'org.eclipse.egit.ui.UncommittedChangeBackgroundColor=42,42,42'
'org.eclipse.egit.ui.UncommittedChangeForegroundColor=114,157,186'
}
#org-eclipse-egit-ui-StagingView StyledText,
Form Section LayoutComposite StyledText
{
background-color: #313538;
color: #dddddd;
}
PushResultTable-SpellcheckableMessageAreaExtension StyledText {
background-color: #313538;
color: #dddddd;
}
|
|
|
Re: Specify theme-specific preferences via CSS [message #1821997 is a reply to message #1818608] |
Tue, 25 February 2020 09:15  |
Eclipse User |
|
|
|
OK, so I've finally figured it out. Eclipse was actually ignoring my Dark-specific preferences because of "naming" issues. I detail everything below, please feel free to provide further info or correct my mistakes.
On Eclipse Photon:
Cause: The CSS Engine only overrides the preferences nodes which are named after a bundle's symbolic name.
Bug location: PartRenderingEngine.java#L1489.
Details: this is an issue because Xtext's preferences nodes are named after the language, leading to something like com.my.Dsl. Since the corresponding bundle is usually named com.my.dsl (without the uppercase) the CSS Engine ignores the com.my.Dsl preferences node.
Solutions: create a bundle named after your Xtext language (obviously not the best solution, but likely the simpler)
On Eclipse 2019-12:
Cause: The CSS Engine expects the contributions to org.eclipse.e4.ui.css.swt.theme to have a "namespace identifier" and discards the extensions that don't have one.
Bug location: PartRenderingEngine.java#L1482.
Details: AFAIK extensions don't have a namespace identifier by default, so we must add one that matches the preferences node.
Solution: the namespace identifier of an extension corresponds to its id attribute; its value should be the name of the preferences node suffixed by ".theme". Actually it can be anything, just a dot followed by something else, the reason is that Eclipse removes the last segment when computing the namespace identifier (I guess the last segment is used as some kind of ID but I didn't have the courage to look further).
In conclusion, to provide a dark theme for an Xtext editor:
Given a language com.my.Dsl and the will to support both recent and older Eclipse releases:
- Create a plug-in com.my.dsl.theme and contribute to the org.eclipse.e4.ui.css.swt.theme extension point as shown a few posts above
- Set the id of the extension to com.my.Dsl.theme
<plugin>
<extension
id="com.my.Dsl.theme"
point="org.eclipse.e4.ui.css.swt.theme">
<stylesheet
uri="css/dark-theme.css">
<themeid
refid="org.eclipse.e4.ui.css.theme.e4_dark">
</themeid>
</stylesheet>
</extension>
</plugin>
- Create a plug-in com.my.Dsl or change the symbolic name of one of your plug-ins to com.my.Dsl
|
|
|
Goto Forum:
Current Time: Sun Jul 20 15:08:39 EDT 2025
Powered by FUDForum. Page generated in 0.04783 seconds
|