Skip to content

Commit

Permalink
[Themes] Use proper method to store theme font preference
Browse files Browse the repository at this point in the history
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=<fontdata>

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=<fontdata>
  • Loading branch information
Phillipus committed Oct 23, 2024
1 parent 8e6f271 commit b8cc4bf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.setCurrentThemeFont(fontDefinitionId, getFontData(), getSystemFontData());
}

FontData getSystemFontData() {
Expand All @@ -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();
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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 -> {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -171,7 +172,9 @@ public static boolean isDarkTheme() {
// ===============================================

/**
* Set theme color definition value for current theme.
* Set (and 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
* Setting this will notify the workbench that the theme color has changed.
* Some of this code inspired by {@link org.eclipse.ui.internal.themes.ColorsAndFontsPreferencePage}
*/
public static void setCurrentThemeColor(String colorDefinitionId, RGB newValue) {
Expand All @@ -190,7 +193,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);
Expand Down Expand Up @@ -251,6 +253,32 @@ public static void setBackgroundColorIfCssThemingDisabled(Control control, Strin
// Font Definitions
// ===============================================

/**
* Set (and save) the 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
* Setting this will notify the workbench that the theme color has changed.
* Some of this code inspired by {@link org.eclipse.ui.internal.themes.ColorsAndFontsPreferencePage}
*/
public static void setCurrentThemeFont(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
*/
Expand Down

0 comments on commit b8cc4bf

Please sign in to comment.