Skip to content

Commit 4e127ef

Browse files
committed
Respect user light/dark preference when restoring nonexistent theme
When restoring a theme from preferences, which does not exist anymore. we previously fell back to inherting from the operating system. However, when when the user actively set the Eclipse theme to not match the light/dark mode of the OS, we would then fall back again to inheritance and override the user's choice. To improve this, we look at the preference the user had actively chosen before and see whether that was a dark theme or not and decide on that which theme to fall back to. Note: To check whether the theme is light or dark, we use the same heuristic also used in other places, themeId.contains("dark"): - org.eclipse.e4.ui.swt.internal.gtk.DarkThemeProcessor - org.eclipse.e4.ui.swt.internal.win32.DarkThemeProcessor - org.eclipse.e4.ui.swt.internal.cocoa.CocoaDarkThemeProcessor Future improvements to consider, out of scope for this change: - Move the heuristic themeId.contains("dark") to central place and to re-use - Even better, add an explicit "isDark" flag / preference somewhere - On top of that, use a mode=dark/light/followSystem instead, see also #2440.
1 parent a879598 commit 4e127ef

File tree

1 file changed

+23
-11
lines changed
  • bundles/org.eclipse.e4.ui.css.swt.theme/src/org/eclipse/e4/ui/css/swt/internal/theme

1 file changed

+23
-11
lines changed

bundles/org.eclipse.e4.ui.css.swt.theme/src/org/eclipse/e4/ui/css/swt/internal/theme/ThemeEngine.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -588,27 +588,39 @@ public void restore(String alternateTheme) {
588588
prefThemeId = "org.eclipse.e4.ui.css.theme.e4_classic"; //$NON-NLS-1$
589589
}
590590

591-
boolean flag = true;
591+
// use theme from preferences if it exists
592592
if (prefThemeId != null) {
593593
for (ITheme t : getThemes()) {
594594
if (prefThemeId.equals(t.getId())) {
595595
setTheme(t, false);
596-
flag = false;
597-
break;
596+
return;
598597
}
599598
}
600599
}
601600

602-
/*
603-
* Any Platform: if the system has Dark appearance set and Eclipse is using the
604-
* default settings, then start Eclipse in Dark theme. Check that there is a
605-
* dark theme present. Can be disabled using a system property.
606-
*/
607601
boolean hasDarkTheme = getThemes().stream().anyMatch(t -> t.getId().startsWith(E4_DARK_THEME_ID));
608-
boolean disableOSDarkThemeInherit = "true".equalsIgnoreCase(System.getProperty(DISABLE_OS_DARK_THEME_INHERIT));
609-
boolean overrideWithDarkTheme = Display.isSystemDarkTheme() && hasDarkTheme && !disableOSDarkThemeInherit;
602+
boolean overrideWithDarkTheme = false;
603+
if (hasDarkTheme) {
604+
if (prefThemeId != null) {
605+
/*
606+
* The user had previously selected a theme which is not available anymore. In
607+
* this case want to fall back to respect whether that previous choice was dark
608+
* or not. https://github.com/eclipse-platform/eclipse.platform.ui/issues/2776
609+
*/
610+
overrideWithDarkTheme = prefThemeId.contains("dark");
611+
} else {
612+
/*
613+
* No previous theme selection in preferences. In this case check if the system
614+
* has dark appearance set and let Eclipse inherit that. Can be disabled using a
615+
* system property.
616+
*/
617+
overrideWithDarkTheme = Display.isSystemDarkTheme()
618+
&& !"true".equalsIgnoreCase(System.getProperty(DISABLE_OS_DARK_THEME_INHERIT));
619+
}
620+
}
621+
610622
String themeToRestore = overrideWithDarkTheme ? E4_DARK_THEME_ID : alternateTheme;
611-
if (themeToRestore != null && flag) {
623+
if (themeToRestore != null) {
612624
setTheme(themeToRestore, false);
613625
}
614626
}

0 commit comments

Comments
 (0)