Skip to content

Commit 8a5381a

Browse files
committed
Style workspace selection dialog with default theme setting
1 parent 1375cd4 commit 8a5381a

File tree

4 files changed

+101
-8
lines changed

4 files changed

+101
-8
lines changed

bundles/org.eclipse.ui.ide.application/src/org/eclipse/ui/internal/ide/application/IDEApplication.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@
4141
import org.eclipse.core.runtime.Platform;
4242
import org.eclipse.core.runtime.Status;
4343
import org.eclipse.core.runtime.jobs.Job;
44+
import org.eclipse.core.runtime.IProduct;
4445
import org.eclipse.core.runtime.preferences.ConfigurationScope;
46+
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
47+
import org.eclipse.core.runtime.preferences.UserScope;
4548
import org.eclipse.equinox.app.IApplication;
4649
import org.eclipse.equinox.app.IApplicationContext;
4750
import org.eclipse.jface.dialogs.IDialogConstants;
@@ -52,6 +55,7 @@
5255
import org.eclipse.osgi.service.datalocation.Location;
5356
import org.eclipse.osgi.util.NLS;
5457
import org.eclipse.swt.SWT;
58+
import org.eclipse.swt.graphics.Color;
5559
import org.eclipse.swt.layout.FillLayout;
5660
import org.eclipse.swt.widgets.Composite;
5761
import org.eclipse.swt.widgets.Control;
@@ -94,6 +98,8 @@ public class IDEApplication implements IApplication, IExecutableExtension {
9498

9599
private static final String USER_NAME = "user.name"; //$NON-NLS-1$
96100

101+
private boolean isDark;
102+
97103
// Use the branding plug-in of the platform feature since this is most likely
98104
// to change on an update of the IDE.
99105
private static final String WORKSPACE_CHECK_REFERENCE_BUNDLE_NAME = "org.eclipse.platform"; //$NON-NLS-1$
@@ -145,6 +151,9 @@ public Object start(IApplicationContext appContext) throws Exception {
145151
Job.getJobManager().suspend();
146152

147153
Display display = createDisplay();
154+
155+
initializeDefaultTheme(display);
156+
148157
// processor must be created before we start event loop
149158
DelayedEventsProcessor processor = new DelayedEventsProcessor(display);
150159

@@ -587,6 +596,14 @@ protected Shell getParentShell() {
587596
return null;
588597
}
589598

599+
@Override
600+
protected Control createContents(Composite parent) {
601+
Control contents = super.createContents(parent);
602+
if (isDark) {
603+
applyDarkStyles(getShell());
604+
}
605+
return contents;
606+
}
590607
}.prompt(force);
591608
}
592609

@@ -852,6 +869,61 @@ protected static Version toMajorMinorVersion(Version version) {
852869
return new Version(version.getMajor(), version.getMinor(), 0);
853870
}
854871

872+
protected void initializeDefaultTheme(Display display) {
873+
IEclipsePreferences themeNode = UserScope.INSTANCE.getNode("org.eclipse.e4.ui.css.swt.theme"); //$NON-NLS-1$
874+
String productOrAppId = getProductOrApplicationId();
875+
String defaultThemeId = null;
876+
if (productOrAppId != null) {
877+
defaultThemeId = themeNode.node(productOrAppId).get("themeid", null); //$NON-NLS-1$
878+
}
879+
if (defaultThemeId == null) {
880+
defaultThemeId = themeNode.get("themeid", null); //$NON-NLS-1$
881+
}
882+
isDark = defaultThemeId != null && defaultThemeId.contains("dark"); //$NON-NLS-1$
883+
if (isDark) {
884+
display.addListener(SWT.Show, event -> {
885+
if (event.widget instanceof Shell shell) {
886+
applyDarkStyles(shell);
887+
}
888+
});
889+
}
890+
}
891+
892+
/**
893+
* Returns the product ID if a product is configured, otherwise falls back to
894+
* the application ID from the system property. Returns {@code null} if neither
895+
* is available.
896+
*/
897+
private static String getProductOrApplicationId() {
898+
IProduct product = Platform.getProduct();
899+
if (product != null) {
900+
return product.getId();
901+
}
902+
return System.getProperty("eclipse.application"); //$NON-NLS-1$
903+
}
904+
905+
private void applyDarkStyles(Shell shell) {
906+
Color bg = new Color(shell.getDisplay(), 72, 72, 76); // #48484c
907+
Color fg = new Color(shell.getDisplay(), 238, 238, 238); // #eeeeee
908+
shell.setBackground(bg);
909+
shell.setForeground(fg);
910+
applyStylesRecursive(shell, bg, fg);
911+
shell.addDisposeListener(e -> {
912+
bg.dispose();
913+
fg.dispose();
914+
});
915+
}
916+
917+
private void applyStylesRecursive(Control control, Color bg, Color fg) {
918+
control.setBackground(bg);
919+
control.setForeground(fg);
920+
if (control instanceof Composite composite) {
921+
for (Control child : composite.getChildren()) {
922+
applyStylesRecursive(child, bg, fg);
923+
}
924+
}
925+
}
926+
855927
@Override
856928
public void stop() {
857929
final IWorkbench workbench = PlatformUI.getWorkbench();

bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/ChooseWorkspaceDialog.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.eclipse.jface.dialogs.IDialogConstants;
4040
import org.eclipse.jface.dialogs.IDialogSettings;
4141
import org.eclipse.jface.dialogs.TitleAreaDialog;
42+
import org.eclipse.jface.resource.JFaceColors;
4243
import org.eclipse.jface.util.Geometry;
4344
import org.eclipse.jface.window.Window;
4445
import org.eclipse.osgi.util.NLS;
@@ -319,6 +320,7 @@ private void createRecentWorkspacesComposite(final Composite composite) {
319320
recentWorkspacesForm.getBody().setLayout(new GridLayout());
320321
ExpandableComposite recentWorkspacesExpandable = toolkit.createExpandableComposite(recentWorkspacesForm.getBody(),
321322
ExpandableComposite.TWISTIE);
323+
recentWorkspacesExpandable.setTitleBarForeground(composite.getForeground());
322324
recentWorkspacesForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
323325
recentWorkspacesExpandable.setBackground(composite.getBackground());
324326
recentWorkspacesExpandable.setText(IDEWorkbenchMessages.ChooseWorkspaceDialog_recentWorkspaces);
@@ -352,6 +354,7 @@ public void expansionStateChanged(ExpansionEvent e) {
352354
final String recentWorkspace = uniqueWorkspaceEntry.getValue();
353355

354356
Link link = new Link(panel, SWT.WRAP);
357+
link.setForeground(JFaceColors.getHyperlink(composite.getDisplay()));
355358
link.setLayoutData(new RowData(SWT.DEFAULT, SWT.DEFAULT));
356359
link.setText("<a>" + uniqueWorkspaceEntry.getKey() + "</a>"); //$NON-NLS-1$ //$NON-NLS-2$
357360
link.setToolTipText(recentWorkspace);

bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/ChooseWorkspaceWithSettingsDialog.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ private void createSettingsControls(Composite workArea) {
128128

129129
copySettingsExpandable.setText(IDEWorkbenchMessages.ChooseWorkspaceWithSettingsDialog_SettingsGroupName);
130130
copySettingsExpandable.setBackground(workArea.getBackground());
131+
copySettingsExpandable.setTitleBarForeground(workArea.getForeground());
131132
copySettingsExpandable.setLayout(new GridLayout());
132133
copySettingsExpandable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
133134
copySettingsExpandable.addExpansionListener(new IExpansionListener() {

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.eclipse.core.runtime.IExtension;
3636
import org.eclipse.core.runtime.IExtensionPoint;
3737
import org.eclipse.core.runtime.IExtensionRegistry;
38+
import org.eclipse.core.runtime.IProduct;
3839
import org.eclipse.core.runtime.Platform;
3940
import org.eclipse.core.runtime.Platform.OS;
4041
import org.eclipse.core.runtime.RegistryFactory;
@@ -339,8 +340,11 @@ private ITheme getSelectedTheme() {
339340
}
340341

341342
private void openManageDefaultThemeDialog() {
342-
IEclipsePreferences configNode = ConfigurationScope.INSTANCE.getNode(E4_THEME_EXTENSION_POINT);
343-
String currentDefaultId = configNode.get("themeid", null); //$NON-NLS-1$
343+
String productOrAppId = getProductOrApplicationId();
344+
IEclipsePreferences baseNode = UserScope.INSTANCE.getNode(E4_THEME_EXTENSION_POINT);
345+
IEclipsePreferences scopedNode = productOrAppId != null ? (IEclipsePreferences) baseNode.node(productOrAppId)
346+
: baseNode;
347+
String currentDefaultId = scopedNode.get("themeid", null); //$NON-NLS-1$
344348

345349
String currentDefaultLabel = null;
346350
if (currentDefaultId != null) {
@@ -377,23 +381,36 @@ private void openManageDefaultThemeDialog() {
377381
int result = dialog.open();
378382
if (result == 0 && selectedTheme != null) {
379383
// Set as default
380-
configNode.put("themeid", selectedTheme.getId()); //$NON-NLS-1$
384+
scopedNode.put("themeid", selectedTheme.getId()); //$NON-NLS-1$
381385
try {
382-
configNode.flush();
386+
scopedNode.flush();
383387
} catch (BackingStoreException e) {
384-
WorkbenchPlugin.log("Failed to set default theme in configuration scope", e); //$NON-NLS-1$
388+
WorkbenchPlugin.log("Failed to set default theme in user scope", e); //$NON-NLS-1$
385389
}
386390
} else if (currentDefaultId != null && result == 1) {
387391
// Remove default
388-
configNode.remove("themeid"); //$NON-NLS-1$
392+
scopedNode.remove("themeid"); //$NON-NLS-1$
389393
try {
390-
configNode.flush();
394+
scopedNode.flush();
391395
} catch (BackingStoreException e) {
392-
WorkbenchPlugin.log("Failed to remove default theme from configuration scope", e); //$NON-NLS-1$
396+
WorkbenchPlugin.log("Failed to remove default theme from user scope", e); //$NON-NLS-1$
393397
}
394398
}
395399
}
396400

401+
/**
402+
* Returns the product ID if a product is configured, otherwise falls back to
403+
* the application ID from the system property. Returns {@code null} if neither
404+
* is available.
405+
*/
406+
private static String getProductOrApplicationId() {
407+
IProduct product = Platform.getProduct();
408+
if (product != null) {
409+
return product.getId();
410+
}
411+
return System.getProperty("eclipse.application"); //$NON-NLS-1$
412+
}
413+
397414
@Override
398415
public void init(IWorkbench workbench) {
399416
MApplication application = workbench.getService(MApplication.class);

0 commit comments

Comments
 (0)