Skip to content

Commit

Permalink
[Themes] Use theme method to store theme font
Browse files Browse the repository at this point in the history
- We have to use this method of creating the font preference key for fonts
  • Loading branch information
Phillipus committed Oct 22, 2024
1 parent 74cd30c commit 127ca53
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
6 changes: 6 additions & 0 deletions com.archimatetool.editor.themes/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@
refid="org.eclipse.e4.ui.css.theme.e4_classic">
</themeid>
</stylesheet>
<stylesheet
uri="themes/eclipse/css/archi-high-contrast.css">
<themeid
refid="org.eclipse.e4.ui.css.theme.high-contrast">
</themeid>
</stylesheet>
<stylesheet
uri="themes/eclipse/css/dark/archi-dark-styling.css">
<themeid
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*******************************************************************************
* Styling for Archi that over-rides some Eclipse stylings
*******************************************************************************/

@import url("platform:/plugin/com.archimatetool.editor.themes/themes/archi/css/common/archi-globalstyle.css");

/* Theme colors */

ColorDefinition#com-archimatetool-editor-VIEW_BACKGROUND {
color: '#COLOR_WIDGET_BACKGROUND';
}

ColorDefinition#com-archimatetool-editor-VISUALISER_BACKGROUND {
color: '#COLOR_WIDGET_BACKGROUND';
}

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());
}

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 @@ -13,6 +13,7 @@
import org.eclipse.e4.ui.css.swt.internal.theme.ThemeEngine;
import org.eclipse.e4.ui.css.swt.theme.IThemeEngine;
import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.resource.StringConverter;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.swt.graphics.Color;
Expand All @@ -23,6 +24,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 @@ -251,6 +253,32 @@ public static void setBackgroundColorIfCssThemingDisabled(Control control, Strin
// Font Definitions
// ===============================================


/**
* Set theme font definition value for current theme.
* Some of this code inspired by {@link org.eclipse.ui.internal.themes.ColorsAndFontsPreferencePage}
*/
public static void setCurrentThemeFont(String fontDefinitionId, FontData fontData) {
if(!PlatformUI.isWorkbenchRunning() || fontData == null) {
return;
}

// Get the font definition from the registry
FontDefinition fontDef = getThemeRegistry().findFont(fontDefinitionId);
if(fontDef == null) {
return;
}

// Write FontData to workbench preference file at .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.workbench.prefs
String preferenceKey = createPreferenceKey(fontDef);
if(Objects.equals(fontData, JFaceResources.getDefaultFont().getFontData()[0])) { // If it's the system 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 Expand Up @@ -284,7 +312,7 @@ public static void setFontIfCssThemingDisabled(Control control, String fontDefin
}
});
}

// ===============================================
// Utils
// ===============================================
Expand Down

0 comments on commit 127ca53

Please sign in to comment.