Skip to content

Commit a7e91a0

Browse files
committed
Allow user to set default theme
During theme switch the user can now set the selected theme as default. This setting is saved as configuration scope and loaded for new workspaces unless the user has a theme set for this specific workspace. Adds 'Use as default theme' checkbox to the theme restart dialog.
1 parent efb185f commit a7e91a0

5 files changed

Lines changed: 59 additions & 10 deletions

File tree

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.eclipse.core.runtime.Platform;
4242
import org.eclipse.core.runtime.RegistryFactory;
4343
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
44+
import org.eclipse.core.runtime.preferences.IPreferencesService;
4445
import org.eclipse.core.runtime.preferences.InstanceScope;
4546
import org.eclipse.e4.ui.css.core.engine.CSSElementContext;
4647
import org.eclipse.e4.ui.css.core.engine.CSSEngine;
@@ -86,6 +87,8 @@ public class ThemeEngine implements IThemeEngine {
8687

8788
public static final String E4_DARK_THEME_ID = "org.eclipse.e4.ui.css.theme.e4_dark";
8889

90+
public static final String E4_DEFAULT_THEME_ID = "org.eclipse.e4.ui.css.theme.e4_default";
91+
8992
public static final String DISABLE_OS_DARK_THEME_INHERIT = "org.eclipse.e4.ui.css.theme.disableOSDarkThemeInherit";
9093

9194
public ThemeEngine(Display display) {
@@ -424,9 +427,15 @@ public void setTheme(String themeId, boolean restore) {
424427
for (Theme t : themes) {
425428
if (t.getId().equals(themeId)) {
426429
setTheme(t, restore);
427-
break;
430+
return;
428431
}
429432
}
433+
// Theme not found (e.g. it was uninstalled); fall back to default
434+
Platform.getLog(ThemeEngine.class)
435+
.warn("Theme '" + themeId + "' not found; falling back to default theme."); //$NON-NLS-1$ //$NON-NLS-2$
436+
if (!E4_DEFAULT_THEME_ID.equals(themeId)) {
437+
setTheme(E4_DEFAULT_THEME_ID, restore);
438+
}
430439
}
431440

432441
@Override
@@ -556,7 +565,8 @@ public void applyStyles(Object widget, boolean applyStylesToChildNodes) {
556565
}
557566

558567
private String getPreferenceThemeId() {
559-
return getPreferences().get(THEMEID_KEY, null);
568+
IPreferencesService prefService = Platform.getPreferencesService();
569+
return prefService.getString(THEME_PLUGIN_ID, THEMEID_KEY, null, null);
560570
}
561571

562572
private IEclipsePreferences getPreferences() {

bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/E4Application.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import org.eclipse.core.runtime.IProduct;
3939
import org.eclipse.core.runtime.Platform;
4040
import org.eclipse.core.runtime.RegistryFactory;
41+
import org.eclipse.core.runtime.preferences.ConfigurationScope;
42+
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
4143
import org.eclipse.e4.core.contexts.ContextFunction;
4244
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
4345
import org.eclipse.e4.core.contexts.EclipseContextFactory;
@@ -315,7 +317,10 @@ private void setCSSContextVariables(IApplicationContext applicationContext, IEcl
315317
: getArgValue(E4Application.THEME_ID, applicationContext, false);
316318

317319
if (!themeId.isPresent() && !cssURI.isPresent()) {
318-
context.set(E4Application.THEME_ID, DEFAULT_THEME_ID);
320+
IEclipsePreferences configurationScopeNode = ConfigurationScope.INSTANCE
321+
.getNode("org.eclipse.e4.ui.css.swt.theme");
322+
String defaultThemeId = configurationScopeNode.get("themeid", DEFAULT_THEME_ID);
323+
context.set(E4Application.THEME_ID, defaultThemeId);
319324
} else {
320325
context.set(E4Application.THEME_ID, themeId.orElseGet(() -> null));
321326
}

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/WorkbenchMessages.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class WorkbenchMessages extends NLS {
4949
public static String RescaleAtRuntimeSettingChangeWarningText;
5050

5151
public static String ThemeChangeWarningText;
52-
52+
public static String ThemeChange_useAsDefault;
5353
public static String ThemeChangeWarningTitle;
5454

5555
public static String BundleSigningTray_Cant_Find_Service;

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/dialogs/ViewsPreferencePage.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.eclipse.ui.internal.dialogs;
2121

2222
import static org.eclipse.jface.viewers.LabelProvider.createTextProvider;
23+
import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter;
2324
import static org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants.ATT_COLOR_AND_FONT_ID;
2425
import static org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants.ATT_OS_VERSION;
2526
import static org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants.ATT_THEME_ASSOCIATION;
@@ -61,7 +62,6 @@
6162
import org.eclipse.jface.viewers.ISelection;
6263
import org.eclipse.jface.viewers.LabelProvider;
6364
import org.eclipse.jface.viewers.StructuredSelection;
64-
import org.eclipse.jface.window.Window;
6565
import org.eclipse.osgi.util.NLS;
6666
import org.eclipse.swt.SWT;
6767
import org.eclipse.swt.events.SelectionEvent;
@@ -419,16 +419,49 @@ public boolean performOk() {
419419
}
420420

421421
if (showRestartDialog) {
422-
showRestartDialog(restartDialogTitle, restartDialogMessage);
422+
String themeId = null;
423+
if (isThemingPossible()) {
424+
ITheme theme = getSelectedTheme();
425+
if (theme != null) {
426+
themeId = theme.getId();
427+
}
428+
}
429+
showRestartDialog(restartDialogTitle, restartDialogMessage, themeId);
423430
}
424431

425432
return super.performOk();
426433
}
427434

428-
private void showRestartDialog(String title, String warningText) {
429-
if (new MessageDialog(null, title, null, warningText, MessageDialog.NONE, 2,
430-
WorkbenchMessages.Workbench_RestartButton, WorkbenchMessages.Workbench_DontRestartButton)
431-
.open() == Window.OK) {
435+
private void showRestartDialog(String title, String warningText, String themeId) {
436+
boolean[] useAsDefault = { true };
437+
MessageDialog dialog = new MessageDialog(null, title, null, warningText, MessageDialog.NONE, 2,
438+
WorkbenchMessages.Workbench_RestartButton, WorkbenchMessages.Workbench_DontRestartButton) {
439+
@Override
440+
protected Control createCustomArea(Composite parent) {
441+
if (themeId == null) {
442+
return null;
443+
}
444+
Button checkbox = new Button(parent, SWT.CHECK);
445+
checkbox.setText(WorkbenchMessages.ThemeChange_useAsDefault);
446+
checkbox.setSelection(useAsDefault[0]);
447+
checkbox.addSelectionListener(widgetSelectedAdapter(e -> useAsDefault[0] = checkbox.getSelection()));
448+
return checkbox;
449+
}
450+
};
451+
int result = dialog.open();
452+
if (result == 0 || result == 1) { // 0: Restart, 1: Don't Restart
453+
if (themeId != null && useAsDefault[0]) {
454+
IEclipsePreferences configurationScopeNode = ConfigurationScope.INSTANCE
455+
.getNode(E4_THEME_EXTENSION_POINT);
456+
configurationScopeNode.put("themeid", themeId); //$NON-NLS-1$
457+
try {
458+
configurationScopeNode.flush();
459+
} catch (BackingStoreException e) {
460+
WorkbenchPlugin.log("Failed to set default theme in configuration scope", e); //$NON-NLS-1$
461+
}
462+
}
463+
}
464+
if (result == 0) {
432465
Display.getDefault().asyncExec(() -> PlatformUI.getWorkbench().restart());
433466
}
434467
}

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ PreferencePage_noDescription = (No description available)
498498
PreferencePageParameterValues_pageLabelSeparator = \ >\
499499
ThemingEnabled = E&nable theming
500500
ThemeChangeWarningText = Restart to fully apply theme changes
501+
ThemeChange_useAsDefault = &Use as default theme (only applies to workspaces without a configured theme)
501502
ThemeChangeWarningTitle = Theme Changed
502503
RescaleAtRuntimeSettingChangeWarningTitle = DPI Setting Changed
503504
RescaleAtRuntimeSettingChangeWarningText = Restart for the DPI setting changes to take effect

0 commit comments

Comments
 (0)