From 0e4facdc90038c057d0afe983e8a2fdbf663b4cd Mon Sep 17 00:00:00 2001 From: Phillipus Date: Wed, 23 Oct 2024 10:28:35 +0100 Subject: [PATCH] [Themes] Use proper method to store theme font preference We have to use this method for creating the font preference key for fonts so that the theme prefix is written and then it works with the high contrast theme. Normally a font is stored in org.eclipse.ui.workbench.prefs like this: com.archimatetool.editor.MODEL_TREE_FONT= and that works for all themes except the high contrast one. The high contrast theme stores it like this: org.eclipse.ui.ide.systemDefault.com.archimatetool.editor.MODEL_TREE_FONT= - Also rename method to saveCurrentThemeColor() --- .../preferences/ColoursPreferencePage.java | 2 +- .../preferences/FontsPreferencePage.java | 27 ++++++---------- .../archimatetool/editor/ui/ThemeUtils.java | 32 +++++++++++++++++-- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/ColoursPreferencePage.java b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/ColoursPreferencePage.java index f1be2b746..ffe28d4b4 100644 --- a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/ColoursPreferencePage.java +++ b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/ColoursPreferencePage.java @@ -717,7 +717,7 @@ private void saveThemeColors() { for(String colorId : themeColors) { Color color = fColorsCache.get(colorId); if(color != null && !color.getRGB().equals(ThemeUtils.getCurrentThemeColor(colorId))) { - ThemeUtils.setCurrentThemeColor(colorId, color.getRGB()); + ThemeUtils.saveCurrentThemeColor(colorId, color.getRGB()); themeColorChanged = true; } } diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/FontsPreferencePage.java b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/FontsPreferencePage.java index 205fc8cd2..4ae7a00c3 100644 --- a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/FontsPreferencePage.java +++ b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/FontsPreferencePage.java @@ -10,7 +10,6 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Objects; import org.eclipse.e4.core.services.events.IEventBroker; import org.eclipse.jface.layout.GridDataFactory; @@ -99,13 +98,7 @@ FontData getDefaultFontData() { } void performOK() { - // Our font definitions are global to all themes so just write directly to the store - if(Objects.equals(getFontData(), getSystemFontData())) { - getPreferenceStore().setToDefault(fontDefinitionId); - } - else { - getPreferenceStore().setValue(fontDefinitionId, getFontData().toString()); - } + ThemeUtils.saveCurrentThemeFont(fontDefinitionId, getFontData(), getSystemFontData()); } FontData getSystemFontData() { @@ -126,10 +119,10 @@ void performDefault() { // If on Mac we need to temporarily apply the value of the scaling checkbox in preferences // so that we can get the default scaled font size. if(fScaleFontsButton != null) { - boolean currentValue = ArchiPlugin.PREFERENCES.getBoolean(FONT_SCALING); // save this - ArchiPlugin.PREFERENCES.setValue(FONT_SCALING, fScaleFontsButton.getSelection()); // apply setting + boolean currentValue = getPreferenceStore().getBoolean(FONT_SCALING); // save this + getPreferenceStore().setValue(FONT_SCALING, fScaleFontsButton.getSelection()); // apply setting fontData = getDefaultFontData(); // get font data - ArchiPlugin.PREFERENCES.setValue(FONT_SCALING, currentValue); // apply previous value + getPreferenceStore().setValue(FONT_SCALING, currentValue); // apply previous value } else { fontData = getDefaultFontData(); @@ -140,7 +133,7 @@ void performDefault() { FontData getFontData() { // Get font data from Preferences store if(fontData == null) { - String fontDetails = ArchiPlugin.PREFERENCES.getString(fontDefinitionId); + String fontDetails = getPreferenceStore().getString(fontDefinitionId); if(StringUtils.isSet(fontDetails)) { fontData = getSafeFontData(fontDetails); } @@ -155,7 +148,7 @@ FontData getFontData() { @Override FontData getDefaultFontData() { // Get default font data from Archi Preferences store (this could be in a suppplied preference file) - String fontDetails = ArchiPlugin.PREFERENCES.getDefaultString(fontDefinitionId); + String fontDetails = getPreferenceStore().getDefaultString(fontDefinitionId); if(StringUtils.isSet(fontDetails)) { return getSafeFontData(fontDetails); } @@ -200,7 +193,7 @@ FontData getSafeFontData(String fontDetails) { private IWorkbench workbench; public FontsPreferencePage() { - setPreferenceStore(PrefUtil.getInternalPreferenceStore()); + setPreferenceStore(ArchiPlugin.PREFERENCES); // This is now shown in a label //setDescription(Messages.FontsPreferencePage_21); } @@ -224,7 +217,7 @@ public Composite createContents(Composite parent) { fScaleFontsButton = new Button(client, SWT.CHECK); fScaleFontsButton.setText(Messages.FontsPreferencePage_22); fScaleFontsButton.setToolTipText(Messages.FontsPreferencePage_23); - fScaleFontsButton.setSelection(ArchiPlugin.PREFERENCES.getBoolean(FONT_SCALING)); + fScaleFontsButton.setSelection(getPreferenceStore().getBoolean(FONT_SCALING)); fScaleFontsButton.setLayoutData(GridDataFactory.defaultsFor(fScaleFontsButton).span(2, 1).create()); // When button is selected apply default font and update table fScaleFontsButton.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> { @@ -468,7 +461,7 @@ private FontData openFontDialog(FontInfo fontInfo) { @Override public void performDefaults() { if(fScaleFontsButton != null) { - fScaleFontsButton.setSelection(ArchiPlugin.PREFERENCES.getDefaultBoolean(FONT_SCALING)); + fScaleFontsButton.setSelection(getPreferenceStore().getDefaultBoolean(FONT_SCALING)); } for(FontInfo info : fontInfos) { @@ -488,7 +481,7 @@ public void performDefaults() { @Override public boolean performOk() { if(fScaleFontsButton != null) { - ArchiPlugin.PREFERENCES.setValue(FONT_SCALING, fScaleFontsButton.getSelection()); + getPreferenceStore().setValue(FONT_SCALING, fScaleFontsButton.getSelection()); } for(FontInfo info : fontInfos) { 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 0699697a7..71c8c24b8 100644 --- a/com.archimatetool.editor/src/com/archimatetool/editor/ui/ThemeUtils.java +++ b/com.archimatetool.editor/src/com/archimatetool/editor/ui/ThemeUtils.java @@ -23,6 +23,7 @@ import org.eclipse.ui.PlatformUI; import org.eclipse.ui.internal.WorkbenchPlugin; import org.eclipse.ui.internal.themes.ColorDefinition; +import org.eclipse.ui.internal.themes.FontDefinition; import org.eclipse.ui.internal.themes.IThemeRegistry; import org.eclipse.ui.internal.themes.ThemeElementDefinition; import org.eclipse.ui.internal.themes.ThemeElementHelper; @@ -171,10 +172,11 @@ public static boolean isDarkTheme() { // =============================================== /** - * Set theme color definition value for current theme. + * Save the theme color definition value for the current theme in preferences. + * This is stored in .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.workbench.prefs * Some of this code inspired by {@link org.eclipse.ui.internal.themes.ColorsAndFontsPreferencePage} */ - public static void setCurrentThemeColor(String colorDefinitionId, RGB newValue) { + public static void saveCurrentThemeColor(String colorDefinitionId, RGB newValue) { if(!PlatformUI.isWorkbenchRunning()) { return; } @@ -190,7 +192,6 @@ public static void setCurrentThemeColor(String colorDefinitionId, RGB newValue) return; } - // Write theme color rgb to workbench preference file at .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.workbench.prefs String preferenceKey = createPreferenceKey(colorDef); if(Objects.equals(newValue, getDefaultThemeColor(colorDefinitionId))) { // If it's the default, remove it PrefUtil.getInternalPreferenceStore().setToDefault(preferenceKey); @@ -251,6 +252,31 @@ public static void setBackgroundColorIfCssThemingDisabled(Control control, Strin // Font Definitions // =============================================== + /** + * Save the theme font definition value for the current theme in preferences. + * This is stored in .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.workbench.prefs + * Some of this code inspired by {@link org.eclipse.ui.internal.themes.ColorsAndFontsPreferencePage} + */ + public static void saveCurrentThemeFont(String fontDefinitionId, FontData fontData, FontData defaultFontData) { + if(!PlatformUI.isWorkbenchRunning() || fontData == null) { + return; + } + + // Get the font definition from the registry + FontDefinition fontDef = getThemeRegistry().findFont(fontDefinitionId); + if(fontDef == null) { + return; + } + + String preferenceKey = createPreferenceKey(fontDef); + if(Objects.equals(fontData, defaultFontData)) { // If it's the default, remove it + PrefUtil.getInternalPreferenceStore().setToDefault(preferenceKey); + } + else { + PrefUtil.getInternalPreferenceStore().setValue(preferenceKey, fontData.toString()); + } + } + /** * Get the FontData in the current theme for a font definition or null */