Skip to content

Commit

Permalink
[Themes] Auto theme for light/dark
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillipus committed Sep 14, 2024
1 parent 4864d8f commit f04aa38
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
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 @@ -146,11 +166,19 @@ 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(getPreferenceStore().getBoolean(THEME_AUTO)) {
fThemeComboViewer.setSelection(new StructuredSelection(AUTOMATIC_THEME));
}
else {
ITheme activeTheme = themeEngine.getActiveTheme();
if(activeTheme != null) {
fThemeComboViewer.setSelection(new StructuredSelection(activeTheme));
}
}
}

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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);
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: 1 addition & 1 deletion com.archimatetool.help/help/Text/prefs_appearance.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h2>Appearance</h2>
If selected, themes are enabled or disabled. A restart will be required for this change to take effect.</p>

<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>
Choose the theme to use for Archi. If the theme is set to "Automatically select Light/Dark", when starting Archi the theme will be set according to the current operating system theme of light or dark (if supported). A restart will be required for this change to fully take effect.</p>

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

0 comments on commit f04aa38

Please sign in to comment.