Skip to content

Commit bff29fd

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. Add 'Use as default theme' checkbox to theme restart dialog
1 parent 6a29466 commit bff29fd

File tree

5 files changed

+51
-9
lines changed

5 files changed

+51
-9
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
import org.eclipse.core.runtime.IPath;
4141
import org.eclipse.core.runtime.Platform;
4242
import org.eclipse.core.runtime.RegistryFactory;
43+
import org.eclipse.core.runtime.preferences.ConfigurationScope;
4344
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
45+
import org.eclipse.core.runtime.preferences.IPreferencesService;
4446
import org.eclipse.core.runtime.preferences.InstanceScope;
4547
import org.eclipse.e4.ui.css.core.engine.CSSElementContext;
4648
import org.eclipse.e4.ui.css.core.engine.CSSEngine;
@@ -556,7 +558,8 @@ public void applyStyles(Object widget, boolean applyStylesToChildNodes) {
556558
}
557559

558560
private String getPreferenceThemeId() {
559-
return getPreferences().get(THEMEID_KEY, null);
561+
IPreferencesService prefService = Platform.getPreferencesService();
562+
return prefService.getString(THEME_PLUGIN_ID, THEMEID_KEY, null, null);
560563
}
561564

562565
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)