Skip to content

Commit

Permalink
[Themes] Automatic theme for light/dark
Browse files Browse the repository at this point in the history
- Eclipse's ThemeEngine will try to automatically set the light or dark theme on startup if the themeid setting is not present in the org.eclipse.e4.ui.css.swt.theme.prefs file or that file doesn't exist (fresh workspace)

- We'll expose this in Preferences as a new automatic pseudo theme. This will delete the themeid setting so that the auto theming occurs on startup
  • Loading branch information
Phillipus committed Sep 15, 2024
1 parent 4864d8f commit e69890a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
package com.archimatetool.editor.preferences;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.e4.ui.css.swt.theme.ITheme;
import org.eclipse.e4.ui.css.swt.theme.IThemeEngine;
Expand All @@ -24,6 +27,7 @@
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
Expand Down Expand Up @@ -58,6 +62,22 @@ public class AppearancePreferencePage
private IThemeEngine themeEngine;
private ITheme lastActiveTheme;

/**
* Pseudo theme to set automatic light/dark on startup
*/
private static ITheme AUTOMATIC_THEME = new ITheme() {
@Override
public String getId() {
return Display.isSystemDarkTheme() ? ThemeUtils.E4_DARK_THEME_ID : ThemeUtils.E4_DEFAULT_THEME_ID;
}

@Override
public String getLabel() {
return Messages.AppearancePreferencePage_7;
}
};


public AppearancePreferencePage() {
setPreferenceStore(ArchiPlugin.PREFERENCES);
setDescription(Messages.AppearancePreferencePage_0);
Expand Down Expand Up @@ -135,7 +155,7 @@ public String getText(Object element) {
@Override
public void selectionChanged(SelectionChangedEvent event) {
ITheme theme = (ITheme)((IStructuredSelection)fThemeComboViewer.getSelection()).getFirstElement();
themeEngine.setTheme(theme, false);
themeEngine.setTheme(theme.getId(), false); // set theme id, not theme in case of automatic theme
}
});
}
Expand All @@ -146,11 +166,20 @@ public void selectionChanged(SelectionChangedEvent event) {
private void setValues() {
// Themes list
if(themeEngine != null) {
fThemeComboViewer.setInput(themeEngine.getThemes().toArray());
List<ITheme> themes = new ArrayList<>((themeEngine.getThemes()));
themes.add(0, AUTOMATIC_THEME);

ITheme activeTheme = themeEngine.getActiveTheme();
if(activeTheme != null) {
fThemeComboViewer.setSelection(new StructuredSelection(activeTheme));
fThemeComboViewer.setInput(themes.toArray());

// If themeid is not present the ThemeEngine will try to set light/dark theme based on OS theme
if(ThemeUtils.getThemePreferences().get(ThemeUtils.THEMEID_KEY, null) == null) {
fThemeComboViewer.setSelection(new StructuredSelection(AUTOMATIC_THEME));
}
else {
ITheme activeTheme = themeEngine.getActiveTheme();
if(activeTheme != null) {
fThemeComboViewer.setSelection(new StructuredSelection(activeTheme));
}
}
}

Expand All @@ -170,7 +199,19 @@ public boolean performOk() {
// Theme
if(themeEngine != null) {
ITheme theme = (ITheme)((IStructuredSelection)fThemeComboViewer.getSelection()).getFirstElement();
if(!theme.equals(lastActiveTheme)) {

if(theme == AUTOMATIC_THEME) {
// Just remove the "themeid" key and then the ThemeEngine will use dark/light depending on OS theme on startup
IEclipsePreferences themePrefs = ThemeUtils.getThemePreferences();
themePrefs.remove(ThemeUtils.THEMEID_KEY);
try {
themePrefs.flush();
}
catch(BackingStoreException ex) {
ex.printStackTrace();
}
}
else {
themeEngine.setTheme(theme, true);
lastActiveTheme = theme;
}
Expand All @@ -192,6 +233,7 @@ public boolean performOk() {
swtPrefs.flush();
}
catch(BackingStoreException ex) {
ex.printStackTrace();
}

// Mac native item heights
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class Messages extends NLS {

public static String AppearancePreferencePage_6;

public static String AppearancePreferencePage_7;

public static String ColoursPreferencePage_0;

public static String ColoursPreferencePage_1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ AppearancePreferencePage_3=Use round tabs
AppearancePreferencePage_4=Enable Theming
AppearancePreferencePage_5=Use larger (Mac native) row heights
AppearancePreferencePage_6=Requires a restart
AppearancePreferencePage_7=Automatically select Light/Dark

ColoursPreferencePage_0=Default colours for objects:
ColoursPreferencePage_1=Strategy elements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public final class ThemeUtils {
*/
public static final String THEME_ENABLED = "themeEnabled";

/**
* Used in .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.e4.ui.css.swt.theme.prefs to store current theme id
*/
public static final String THEMEID_KEY = "themeid";

private static IThemeEngine engine;

/**
Expand Down
2 changes: 2 additions & 0 deletions com.archimatetool.help/help/Text/prefs_appearance.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ <h2>Appearance</h2>

<p><strong>Theme</strong><br/>
Choose the theme to use for Archi. A restart will be required for this change to fully take effect.</p>

<p>If the theme is set to "Automatically select Light/Dark" the theme will be set according to the current operating system theme of light or dark when starting Archi. Some system dark themes are not supported and so this setting might not work. If this is the case, select another theme to suit the host system theme.</p>

<p><strong>Use round tabs</strong><br/>
Whether to use round or square tabs.</p>
Expand Down

0 comments on commit e69890a

Please sign in to comment.