diff options
| author | Stephan Wahlbrink | 2021-08-02 18:51:12 +0000 |
|---|---|---|
| committer | Stephan Wahlbrink | 2021-08-03 14:05:57 +0000 |
| commit | 216df95ab81f0b7b7f0cde6d5917134a9fda9e50 (patch) | |
| tree | 3d7a938e737bfc2961d72a75d5f0f54abe6f4687 | |
| parent | 1c8d45b70a72acae8fc0bb1ee5b82b77419a56d5 (diff) | |
| download | org.eclipse.statet-commons-216df95ab81f0b7b7f0cde6d5917134a9fda9e50.tar.gz org.eclipse.statet-commons-216df95ab81f0b7b7f0cde6d5917134a9fda9e50.tar.xz org.eclipse.statet-commons-216df95ab81f0b7b7f0cde6d5917134a9fda9e50.zip | |
Bug 575176: [UI-Workbench] Add StylingUtils
Change-Id: I492167a312bbf4ab3f1176fa9bf19609c6efa526
| -rw-r--r-- | ecommons/org.eclipse.statet.ecommons.uimisc/META-INF/MANIFEST.MF | 2 | ||||
| -rw-r--r-- | ecommons/org.eclipse.statet.ecommons.uimisc/src/org/eclipse/statet/ecommons/ui/workbench/StylingUtils.java | 122 |
2 files changed, 124 insertions, 0 deletions
diff --git a/ecommons/org.eclipse.statet.ecommons.uimisc/META-INF/MANIFEST.MF b/ecommons/org.eclipse.statet.ecommons.uimisc/META-INF/MANIFEST.MF index 6c803526..fb076ff4 100644 --- a/ecommons/org.eclipse.statet.ecommons.uimisc/META-INF/MANIFEST.MF +++ b/ecommons/org.eclipse.statet.ecommons.uimisc/META-INF/MANIFEST.MF @@ -13,6 +13,7 @@ Require-Bundle: org.eclipse.statet.ecommons.runtime.core;bundle-version="[4.5.0, org.eclipse.core.runtime;bundle-version="3.19.0", org.eclipse.e4.core.contexts;bundle-version="1.7.0";resolution:=optional, org.eclipse.ui, + org.eclipse.e4.ui.services, org.eclipse.jface.text;resolution:=optional, org.eclipse.core.databinding, org.eclipse.core.databinding.property, @@ -38,6 +39,7 @@ Import-Package: com.ibm.icu.lang;version="67.1.0", org.eclipse.statet.ecommons.variables.core, org.eclipse.statet.jcommons.collections;version="4.5.0", org.eclipse.statet.jcommons.lang;version="4.5.0", + org.eclipse.e4.ui.css.swt.theme;resolution:=optional, org.eclipse.ui.dialogs;resolution:=optional, org.eclipse.ui.model;resolution:=optional, org.eclipse.ui.views.contentoutline;resolution:=optional, diff --git a/ecommons/org.eclipse.statet.ecommons.uimisc/src/org/eclipse/statet/ecommons/ui/workbench/StylingUtils.java b/ecommons/org.eclipse.statet.ecommons.uimisc/src/org/eclipse/statet/ecommons/ui/workbench/StylingUtils.java new file mode 100644 index 00000000..bc04d723 --- /dev/null +++ b/ecommons/org.eclipse.statet.ecommons.uimisc/src/org/eclipse/statet/ecommons/ui/workbench/StylingUtils.java @@ -0,0 +1,122 @@ +/*=============================================================================# + # Copyright (c) 2021 Stephan Wahlbrink and others. + # + # This program and the accompanying materials are made available under the + # terms of the Eclipse Public License 2.0 which is available at + # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 + # which is available at https://www.apache.org/licenses/LICENSE-2.0. + # + # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 + # + # Contributors: + # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation + #=============================================================================*/ + +package org.eclipse.statet.ecommons.ui.workbench; + +import static org.eclipse.statet.jcommons.lang.ObjectUtils.nonNullAssert; + +import org.eclipse.e4.ui.services.IStylingEngine; +import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.PlatformUI; + +import org.eclipse.statet.jcommons.lang.NonNullByDefault; + + +@NonNullByDefault +public final class StylingUtils { + + + public static enum ThemeType { + + DARK, + LIGHT, + OTHER; + + } + + + /** + * Key value for setting and getting the CSS ID of a widget. + * + * @see org.eclipse.swt.widgets.Widget#getData(String) + * @see org.eclipse.swt.widgets.Widget#setData(String, Object) + */ + /* org.eclipse.e4.ui.css.swt.CSSSWTConstants.CSS_ID_KEY */ + public static final String WIDGET_CSS_ID_KEY= "org.eclipse.e4.ui.css.id"; //$NON-NLS-1$ + + + private static final String SYSTEM_THEME_ID= "org.eclipse.e4.ui.css.theme.e4_system"; //$NON-NLS-1$ + private static final String DEFAULT_THEME_ID= "org.eclipse.e4.ui.css.theme.e4_default"; //$NON-NLS-1$ + private static final String DARK_THEME_ID= "org.eclipse.e4.ui.css.theme.e4_dark"; //$NON-NLS-1$ + + private static final String NONE_THEME_ID= "none"; //$NON-NLS-1$ + + + public static boolean isStylingSupported() { + return (PlatformUI.getWorkbench().getService(IStylingEngine.class) != null); + } + + public static IStylingEngine getStylingEngine() { + return nonNullAssert(PlatformUI.getWorkbench().getService(IStylingEngine.class)); + } + + + @SuppressWarnings("restriction") + private static String getCurrentCssThemeId() { + try { + final var engine= PlatformUI.getWorkbench().getService( + org.eclipse.e4.ui.css.swt.theme.IThemeEngine.class ); + if (engine != null) { + final var theme= engine.getActiveTheme(); + if (theme != null) { + return theme.getId(); + } + } + } + catch (final Exception e) {} + + return NONE_THEME_ID; + } + + public static ThemeType getCurrentThemeType(final Display display) { + final var themeId= getCurrentCssThemeId(); + switch (themeId) { + case SYSTEM_THEME_ID: + case NONE_THEME_ID: + return (Display.isSystemDarkTheme()) ? ThemeType.DARK : ThemeType.LIGHT; + case DARK_THEME_ID: + return ThemeType.DARK; + case DEFAULT_THEME_ID: + return ThemeType.LIGHT; + default: + if (themeId.contains("dark")) { //$NON-NLS-1$ + return ThemeType.DARK; + } + if (themeId.contains("light")) { //$NON-NLS-1$ + return ThemeType.LIGHT; + } + return ThemeType.OTHER; + } + } + + public static boolean isCurrentThemeMatchingSystem(final Display display) { + final var themeId= getCurrentCssThemeId(); + switch (themeId) { + case SYSTEM_THEME_ID: + case NONE_THEME_ID: + return true; + case DEFAULT_THEME_ID: + return !Display.isSystemDarkTheme(); + default: + if (themeId.contains("dark")) { //$NON-NLS-1$ + return Display.isSystemDarkTheme(); + } + return false; + } + } + + + private StylingUtils() {} + +} |
