From 05ec24bc8c61ff58e1273aeb90eee91c12e62170 Mon Sep 17 00:00:00 2001 From: Phillipus Date: Sat, 14 Sep 2024 19:06:12 +0100 Subject: [PATCH] [Themes] Auto theme for light/dark --- .../editor/ArchiWorkbenchAdvisor.java | 6 +++ .../preferences/AppearancePreferencePage.java | 43 ++++++++++++++++--- .../preferences/IPreferenceConstants.java | 2 + .../editor/preferences/Messages.java | 2 + .../preferences/PreferenceInitializer.java | 3 +- .../editor/preferences/messages.properties | 1 + .../archimatetool/editor/ui/ThemeUtils.java | 5 +++ .../help/Text/prefs_appearance.html | 2 +- 8 files changed, 57 insertions(+), 7 deletions(-) diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/ArchiWorkbenchAdvisor.java b/com.archimatetool.editor/src/com/archimatetool/editor/ArchiWorkbenchAdvisor.java index 76e715216..54fef37e5 100644 --- a/com.archimatetool.editor/src/com/archimatetool/editor/ArchiWorkbenchAdvisor.java +++ b/com.archimatetool.editor/src/com/archimatetool/editor/ArchiWorkbenchAdvisor.java @@ -20,6 +20,7 @@ import com.archimatetool.editor.perspectives.MainPerspective; import com.archimatetool.editor.preferences.IPreferenceConstants; +import com.archimatetool.editor.ui.ThemeUtils; import com.archimatetool.editor.utils.NetUtils; import com.archimatetool.editor.utils.PlatformUtils; @@ -96,6 +97,11 @@ public void initialize(IWorkbenchConfigurer configurer) { // Initialise Proxy NetUtils.initialise(); + + // Automatic theme. Just remove the "themeid" key and then the ThemeEngine will use dark/light depending on OS theme + if(ArchiPlugin.PREFERENCES.getBoolean(IPreferenceConstants.THEME_AUTO)) { + ThemeUtils.getThemePreferences().remove(ThemeUtils.THEMEID_KEY); + } } @Override diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/AppearancePreferencePage.java b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/AppearancePreferencePage.java index 6d300378d..13c42cf74 100644 --- a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/AppearancePreferencePage.java +++ b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/AppearancePreferencePage.java @@ -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; @@ -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; @@ -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); @@ -146,11 +166,19 @@ public void selectionChanged(SelectionChangedEvent event) { private void setValues() { // Themes list if(themeEngine != null) { - fThemeComboViewer.setInput(themeEngine.getThemes().toArray()); + List 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(getPreferenceStore().getBoolean(THEME_AUTO)) { + fThemeComboViewer.setSelection(new StructuredSelection(AUTOMATIC_THEME)); + } + else { + ITheme activeTheme = themeEngine.getActiveTheme(); + if(activeTheme != null) { + fThemeComboViewer.setSelection(new StructuredSelection(activeTheme)); + } } } @@ -169,8 +197,13 @@ private void setValues() { public boolean performOk() { // Theme if(themeEngine != null) { + getPreferenceStore().setValue(THEME_AUTO, false); + ITheme theme = (ITheme)((IStructuredSelection)fThemeComboViewer.getSelection()).getFirstElement(); - if(!theme.equals(lastActiveTheme)) { + if(theme == AUTOMATIC_THEME) { + getPreferenceStore().setValue(THEME_AUTO, true); + } + else if(!theme.equals(lastActiveTheme)) { themeEngine.setTheme(theme, true); lastActiveTheme = theme; } diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/IPreferenceConstants.java b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/IPreferenceConstants.java index 461a64502..006347da6 100644 --- a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/IPreferenceConstants.java +++ b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/IPreferenceConstants.java @@ -53,6 +53,8 @@ public interface IPreferenceConstants { // Mac use native item heights String MAC_ITEM_HEIGHT_PROPERTY_KEY = "org.eclipse.swt.internal.cocoa.useNativeItemHeight"; + String THEME_AUTO = "themeAuto"; + // ======================================= Connections ======================================= // Connections diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/Messages.java b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/Messages.java index 71d2f5056..412f95b19 100644 --- a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/Messages.java +++ b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/Messages.java @@ -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; diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/PreferenceInitializer.java b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/PreferenceInitializer.java index 62cbfe451..1d8eba40c 100644 --- a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/PreferenceInitializer.java +++ b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/PreferenceInitializer.java @@ -38,7 +38,8 @@ public void initializeDefaultPreferences() { // Appearance store.setDefault(SHOW_STATUS_LINE, true); - + store.setDefault(THEME_AUTO, false); + // Colours store.setDefault(DERIVE_ELEMENT_LINE_COLOR, true); diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/messages.properties b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/messages.properties index 73d854dd4..d2a6e255f 100644 --- a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/messages.properties +++ b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/messages.properties @@ -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 diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/ui/ThemeUtils.java b/com.archimatetool.editor/src/com/archimatetool/editor/ui/ThemeUtils.java index 4251990a0..d6408a874 100644 --- a/com.archimatetool.editor/src/com/archimatetool/editor/ui/ThemeUtils.java +++ b/com.archimatetool.editor/src/com/archimatetool/editor/ui/ThemeUtils.java @@ -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; /** diff --git a/com.archimatetool.help/help/Text/prefs_appearance.html b/com.archimatetool.help/help/Text/prefs_appearance.html index 8451d6fe0..329fbd532 100644 --- a/com.archimatetool.help/help/Text/prefs_appearance.html +++ b/com.archimatetool.help/help/Text/prefs_appearance.html @@ -19,7 +19,7 @@

Appearance

If selected, themes are enabled or disabled. A restart will be required for this change to take effect.

Theme
- Choose the theme to use for Archi. A restart will be required for this change to fully take effect.

+ Choose the theme to use for Archi. 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 - note that some system dark themes are not supported and so this setting might not work. A restart will be required for this change to fully take effect.

Use round tabs
Whether to use round or square tabs.