Skip to content

Commit 61cca8f

Browse files
tobias-melcherBeckerWdf
authored andcommitted
Reset in colors preference page now respects theme-specific defaults
1 parent b019337 commit 61cca8f

File tree

4 files changed

+99
-6
lines changed

4 files changed

+99
-6
lines changed

bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/EclipsePreferencesHelper.java

+46
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
import java.util.ArrayList;
1717
import java.util.Collections;
1818
import java.util.List;
19+
import org.eclipse.core.runtime.preferences.DefaultScope;
1920
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
2021
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
2122
import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
23+
import org.osgi.service.prefs.BackingStoreException;
2224
import org.osgi.service.prefs.Preferences;
2325

2426
public class EclipsePreferencesHelper {
@@ -34,6 +36,8 @@ public class EclipsePreferencesHelper {
3436

3537
private static String currentThemeId;
3638

39+
private static final String PROPS_DEFAULT_VALUE_BEFORE_OVERIDDEN_FROM_CSS = "defaultValueBeforeOverriddenFromCSS";
40+
3741
public static void appendOverriddenPropertyName(
3842
IEclipsePreferences preferences, String name) {
3943
String value = preferences.get(PROPS_OVERRIDDEN_BY_CSS_PROP, SEPARATOR);
@@ -134,4 +138,46 @@ protected void removeOverriddenByCssProperty(PreferenceChangeEvent event) {
134138
}
135139
}
136140
}
141+
142+
public static void overrideDefault(IEclipsePreferences preferences, String name, String value) {
143+
String prefName = preferences.name();
144+
if (prefName == null) {
145+
return;
146+
}
147+
IEclipsePreferences defaultPrefs = DefaultScope.INSTANCE.getNode(prefName);
148+
if (defaultPrefs == null) {
149+
return;
150+
}
151+
String existing = defaultPrefs.get(name, null);
152+
if (existing != null && value != null && existing.equals(value) == false) {
153+
defaultPrefs.put(name, value);
154+
preferences.put(name + SEPARATOR + PROPS_DEFAULT_VALUE_BEFORE_OVERIDDEN_FROM_CSS, existing);
155+
}
156+
}
157+
158+
public static void resetOverriddenDefaults(IEclipsePreferences preferences) {
159+
try {
160+
String[] keys = preferences.keys();
161+
if (keys == null) {
162+
return;
163+
}
164+
for (String key : keys) {
165+
if (key != null && key.endsWith(SEPARATOR + PROPS_DEFAULT_VALUE_BEFORE_OVERIDDEN_FROM_CSS)) {
166+
String overriddenDefault = preferences.get(key, null);
167+
String originKey = key.substring(0,
168+
key.lastIndexOf(SEPARATOR + PROPS_DEFAULT_VALUE_BEFORE_OVERIDDEN_FROM_CSS));
169+
IEclipsePreferences defaultPrefs = DefaultScope.INSTANCE.getNode(preferences.name());
170+
if (defaultPrefs != null) {
171+
String currentDefault = defaultPrefs.get(originKey, null);
172+
if (overriddenDefault != null && currentDefault != null
173+
&& !currentDefault.equals(overriddenDefault)) {
174+
defaultPrefs.put(originKey, overriddenDefault);
175+
}
176+
}
177+
preferences.remove(key);
178+
}
179+
}
180+
} catch (BackingStoreException e) { // silently ignored
181+
}
182+
}
137183
}

bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/preference/EclipsePreferencesHandler.java

+1
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@ protected void overrideProperty(IEclipsePreferences preferences, String name, St
6161
preferences.put(name, value);
6262
EclipsePreferencesHelper.appendOverriddenPropertyName(preferences, name);
6363
}
64+
EclipsePreferencesHelper.overrideDefault(preferences, name, value);
6465
}
6566
}

bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/PartRenderingEngine.java

+1
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,7 @@ protected void resetOverriddenPreferences() {
14401440
}
14411441

14421442
protected void resetOverriddenPreferences(IEclipsePreferences preferences) {
1443+
EclipsePreferencesHelper.resetOverriddenDefaults(preferences);
14431444
for (String name : getOverriddenPropertyNames(preferences)) {
14441445
preferences.remove(name);
14451446
}

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/themes/ColorsAndFontsPreferencePage.java

+51-6
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
import org.eclipse.core.runtime.IStatus;
3636
import org.eclipse.e4.core.services.events.IEventBroker;
3737
import org.eclipse.e4.ui.css.swt.theme.IThemeEngine;
38+
import org.eclipse.jface.preference.IPreferenceStore;
3839
import org.eclipse.jface.preference.PreferenceConverter;
3940
import org.eclipse.jface.preference.PreferencePage;
41+
import org.eclipse.jface.resource.DataFormatException;
4042
import org.eclipse.jface.resource.JFaceResources;
4143
import org.eclipse.jface.resource.StringConverter;
4244
import org.eclipse.jface.util.IPropertyChangeListener;
@@ -87,6 +89,7 @@
8789
import org.eclipse.ui.internal.WorkbenchPlugin;
8890
import org.eclipse.ui.internal.misc.StatusUtil;
8991
import org.eclipse.ui.internal.util.PrefUtil;
92+
import org.eclipse.ui.themes.ColorUtil;
9093
import org.eclipse.ui.themes.ITheme;
9194
import org.eclipse.ui.themes.IThemeManager;
9295
import org.eclipse.ui.themes.IThemePreview;
@@ -331,9 +334,20 @@ private class PresentationLabelProvider extends LabelProvider implements IFontPr
331334

332335
private int usableImageSize = -1;
333336

337+
private static final int REFRESH_INTERVAL_IN_MS = 300;
338+
339+
private final Runnable updateControlsAndRefreshTreeRunnable = () -> {
340+
if (fontChangeButton == null || fontChangeButton.isDisposed()) {
341+
return;
342+
}
343+
updateControls();
344+
tree.getViewer().refresh();
345+
};
346+
334347
private IPropertyChangeListener listener = event -> {
335348
if (event.getNewValue() != null) {
336349
fireLabelProviderChanged(new LabelProviderChangedEvent(PresentationLabelProvider.this));
350+
Display.getDefault().timerExec(REFRESH_INTERVAL_IN_MS, updateControlsAndRefreshTreeRunnable);
337351
} else {
338352
// Some theme definition element has been modified and we
339353
// need to refresh the viewer
@@ -1304,6 +1318,30 @@ private void updateThemeInfo(IThemeManager manager) {
13041318
labelProvider.hookListeners(); // rehook the listeners
13051319
}
13061320

1321+
private RGB getColorTakingPreferenceDefaultValueIntoAccount(ColorDefinition definition) {
1322+
final RGB valueFromExtension = definition.getValue();
1323+
IPreferenceStore store = getPreferenceStore();
1324+
if (store == null) {
1325+
return valueFromExtension;
1326+
}
1327+
String id = definition.getId();
1328+
if (id == null || id.isBlank()) {
1329+
return valueFromExtension;
1330+
}
1331+
String storeDefault = store.getDefaultString(id);
1332+
if (storeDefault == null) {
1333+
return valueFromExtension;
1334+
}
1335+
try {
1336+
RGB defaultRGB = ColorUtil.getColorValue(storeDefault);
1337+
if (defaultRGB != null && !defaultRGB.equals(valueFromExtension)) {
1338+
return defaultRGB;
1339+
}
1340+
} catch (DataFormatException e) { // silently ignored
1341+
}
1342+
return valueFromExtension;
1343+
}
1344+
13071345
/**
13081346
* Answers whether the definition is currently set to the default value.
13091347
*
@@ -1314,23 +1352,29 @@ private void updateThemeInfo(IThemeManager manager) {
13141352
*/
13151353
private boolean isDefault(ColorDefinition definition) {
13161354
String id = definition.getId();
1317-
13181355
if (colorPreferencesToSet.containsKey(definition)) {
13191356
if (definition.getValue() != null) { // value-based color
1320-
if (colorPreferencesToSet.get(definition).equals(definition.getValue()))
1357+
if (colorPreferencesToSet.get(definition)
1358+
.equals(getColorTakingPreferenceDefaultValueIntoAccount(definition)))
13211359
return true;
13221360
} else if (colorPreferencesToSet.get(definition).equals(getColorAncestorValue(definition)))
13231361
return true;
13241362
} else if (colorValuesToSet.containsKey(id)) {
13251363
if (definition.getValue() != null) { // value-based color
1326-
if (colorValuesToSet.get(id).equals(definition.getValue()))
1364+
if (colorValuesToSet.get(id).equals(getColorTakingPreferenceDefaultValueIntoAccount(definition)))
13271365
return true;
13281366
} else {
13291367
if (colorValuesToSet.get(id).equals(getColorAncestorValue(definition)))
13301368
return true;
13311369
}
13321370
} else if (definition.getValue() != null) { // value-based color
1333-
if (getPreferenceStore().isDefault(createPreferenceKey(definition)))
1371+
IPreferenceStore store = getPreferenceStore();
1372+
String defaultString = store.getDefaultString(id);
1373+
String string = store.getString(id);
1374+
if (defaultString != null && string != null && defaultString.equals(string)) {
1375+
return true;
1376+
}
1377+
if (store.isDefault(createPreferenceKey(definition)))
13341378
return true;
13351379
} else {
13361380
// a descendant is default if it's the same value as its ancestor
@@ -1516,8 +1560,9 @@ private void refreshCategory() {
15161560
private boolean resetColor(ColorDefinition definition, boolean force) {
15171561
if (force || !isDefault(definition)) {
15181562
RGB newRGB;
1519-
if (definition.getValue() != null)
1520-
newRGB = definition.getValue();
1563+
if (definition.getValue() != null) {
1564+
newRGB = getColorTakingPreferenceDefaultValueIntoAccount(definition);
1565+
}
15211566
else
15221567
newRGB = getColorAncestorValue(definition);
15231568

0 commit comments

Comments
 (0)