Skip to content

Commit

Permalink
Fix Preferences not retrieving default font if set in pluginCustomiza…
Browse files Browse the repository at this point in the history
…tion

- When a user font is set also store a preference key prefixed with ".default" so that if the user exports Archi's preferences they can get both keys to add to their custom preferences file

- This gives us a workaround for a default font's value not retrieved if user has a pluginCustomization setting for a default font

- Before this, pressing the "Default" button in Preferences showed the system font instead of the one in the pluginCustomization
  • Loading branch information
Phillipus committed Dec 22, 2024
1 parent cd2b72b commit 4c9561a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.CellLabelProvider;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
Expand Down Expand Up @@ -94,15 +93,27 @@ FontData getFontData() {
}

FontData getDefaultFontData() {
return getSystemFontData();
// Get default font that might be set in a pluginCustomization settings file
FontData fontData = ThemeUtils.getDefaultThemeFontData(fontDefinitionId);
return fontData != null ? fontData : getSystemFontData();
}

void performOK() {
ThemeUtils.setCurrentThemeFont(fontDefinitionId, getFontData(), getSystemFontData());
}

FontData getSystemFontData() {
return JFaceResources.getDefaultFont().getFontData()[0];
return getShell().getDisplay().getSystemFont().getFontData()[0];
}

FontData getSafeFontData(String fontDetails) {
try {
return new FontData(fontDetails);
}
catch(Exception ex) {
}

return getSystemFontData();
}
}

Expand Down Expand Up @@ -165,16 +176,6 @@ void performOK() {
FontData getSystemFontData() {
return FontFactory.getDefaultViewOSFontData();
}

FontData getSafeFontData(String fontDetails) {
try {
return new FontData(fontDetails);
}
catch(Exception ex) {
}

return getSystemFontData();
}
}

// Table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.ui.css.swt.internal.theme.ThemeEngine;
import org.eclipse.e4.ui.css.swt.theme.IThemeEngine;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.jface.resource.StringConverter;
import org.eclipse.jface.util.IPropertyChangeListener;
Expand Down Expand Up @@ -269,11 +270,17 @@ public static void setCurrentThemeFont(String fontDefinitionId, FontData fontDat
}

String preferenceKey = createPreferenceKey(fontDef);

// Also write a ".default" preference key so that if the user exports Archi's preferences
// They can get both keys to add to their custom preferences file

if(Objects.equals(fontData, defaultFontData)) { // If it's the default, remove it
PrefUtil.getInternalPreferenceStore().setToDefault(preferenceKey);
PrefUtil.getInternalPreferenceStore().setToDefault("default." + preferenceKey);
}
else {
PrefUtil.getInternalPreferenceStore().setValue(preferenceKey, fontData.toString());
PrefUtil.getInternalPreferenceStore().setValue("default." + preferenceKey, fontData.toString());
}
}

Expand All @@ -298,6 +305,34 @@ public static Font getCurrentThemeFont(String fontDefinitionId) {
private static FontRegistry getCurrentThemeFontRegistry() {
return getThemeManager() != null ? getThemeManager().getCurrentTheme().getFontRegistry() : null;
}

/**
* Get the default FontData value for a font definition or null
*/
public static FontData getDefaultThemeFontData(String fontDefinitionId) {
FontDefinition fontDef = getThemeRegistry().findFont(fontDefinitionId);
if(fontDef == null) {
return null;
}

String preferenceKey = createPreferenceKey(fontDef);

// Check if there is a default setting in custom preferences or plugin_customization.ini
// We use our own key for this since Eclipse will have set the default value to that set in the theme's plugin.xml file
String value = PrefUtil.getInternalPreferenceStore().getDefaultString("default." + preferenceKey);
if(StringUtils.isSet(value)) {
try {
return new FontData(value);
}
catch(Exception ex) {
ex.printStackTrace();
}
}

// Else check the default value set in the theme plugin.xml file, if any.
// If not set this will return the system font
return PreferenceConverter.getDefaultFontData(PrefUtil.getInternalPreferenceStore(), preferenceKey);
}

/**
* If theming is disabled set the font on the control from the fontDefinitionId
Expand Down

0 comments on commit 4c9561a

Please sign in to comment.