Skip to content

Commit b647719

Browse files
Reset in colors preference page now respects theme-specific defaults
1 parent c5928bb commit b647719

File tree

4 files changed

+100
-6
lines changed

4 files changed

+100
-6
lines changed

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

Lines changed: 43 additions & 0 deletions
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,43 @@ protected void removeOverriddenByCssProperty(PreferenceChangeEvent event) {
134138
}
135139
}
136140
}
141+
142+
/**
143+
* @since 0.15
144+
*/
145+
public static void overrideDefault(IEclipsePreferences preferences, String name, String value) {
146+
IEclipsePreferences defaultPrefs = DefaultScope.INSTANCE.getNode(preferences.name());
147+
if (defaultPrefs != null) {
148+
String existing = defaultPrefs.get(name, null);
149+
if (existing != null && value != null && existing.equals(value) == false) {
150+
defaultPrefs.put(name, value);
151+
preferences.put(name + SEPARATOR + PROPS_DEFAULT_VALUE_BEFORE_OVERIDDEN_FROM_CSS, existing);
152+
}
153+
}
154+
}
155+
156+
/**
157+
* @since 0.15
158+
*/
159+
public static void resetOverriddenDefaults(IEclipsePreferences preferences) {
160+
try {
161+
for (String key : preferences.keys()) {
162+
if (key!=null && key.endsWith(SEPARATOR + PROPS_DEFAULT_VALUE_BEFORE_OVERIDDEN_FROM_CSS)) {
163+
String overriddenDefault = preferences.get(key, null);
164+
String originKey = key.substring(0,
165+
key.lastIndexOf(SEPARATOR + PROPS_DEFAULT_VALUE_BEFORE_OVERIDDEN_FROM_CSS));
166+
IEclipsePreferences defaultPrefs = DefaultScope.INSTANCE.getNode(preferences.name());
167+
if (defaultPrefs != null) {
168+
String currentDefault = defaultPrefs.get(originKey, null);
169+
if (overriddenDefault != null && currentDefault != null
170+
&& !currentDefault.equals(overriddenDefault)) {
171+
defaultPrefs.put(originKey, overriddenDefault);
172+
}
173+
}
174+
preferences.remove(key);
175+
}
176+
}
177+
} catch (BackingStoreException e) { // silently ignored
178+
}
179+
}
137180
}

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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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/Eclipse UI/org/eclipse/ui/internal/themes/ColorsAndFontsPreferencePage.java

Lines changed: 55 additions & 6 deletions
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,22 @@ private class PresentationLabelProvider extends LabelProvider implements IFontPr
331334

332335
private int usableImageSize = -1;
333336

337+
private final Runnable updateControlsAndRefreshTreeRunnable = new Runnable() {
338+
339+
@Override
340+
public void run() {
341+
if (fontChangeButton == null || fontChangeButton.isDisposed()) {
342+
return;
343+
}
344+
updateControls();
345+
tree.getViewer().refresh();
346+
}
347+
};
348+
334349
private IPropertyChangeListener listener = event -> {
335350
if (event.getNewValue() != null) {
336351
fireLabelProviderChanged(new LabelProviderChangedEvent(PresentationLabelProvider.this));
352+
Display.getDefault().timerExec(300, updateControlsAndRefreshTreeRunnable);
337353
} else {
338354
// Some theme definition element has been modified and we
339355
// need to refresh the viewer
@@ -1304,6 +1320,27 @@ private void updateThemeInfo(IThemeManager manager) {
13041320
labelProvider.hookListeners(); // rehook the listeners
13051321
}
13061322

1323+
private RGB getColorTakingOverriddenCssValueIntoAccount(ColorDefinition definition) {
1324+
RGB value = definition.getValue();
1325+
IPreferenceStore store = getPreferenceStore();
1326+
if (store != null) {
1327+
String id = definition.getId();
1328+
if (id != null && id.length() > 0) {
1329+
String storeDefault = store.getDefaultString(id);
1330+
if (storeDefault != null) {
1331+
try {
1332+
RGB defaultRGB = ColorUtil.getColorValue(storeDefault);
1333+
if (defaultRGB != null && !defaultRGB.equals(value)) {
1334+
value = defaultRGB;
1335+
}
1336+
} catch (DataFormatException e) { // silently ignored
1337+
}
1338+
}
1339+
}
1340+
}
1341+
return value;
1342+
}
1343+
13071344
/**
13081345
* Answers whether the definition is currently set to the default value.
13091346
*
@@ -1314,23 +1351,34 @@ private void updateThemeInfo(IThemeManager manager) {
13141351
*/
13151352
private boolean isDefault(ColorDefinition definition) {
13161353
String id = definition.getId();
1317-
13181354
if (colorPreferencesToSet.containsKey(definition)) {
13191355
if (definition.getValue() != null) { // value-based color
1320-
if (colorPreferencesToSet.get(definition).equals(definition.getValue()))
1356+
if (colorPreferencesToSet.get(definition)
1357+
.equals(getColorTakingOverriddenCssValueIntoAccount(definition)))
13211358
return true;
13221359
} else if (colorPreferencesToSet.get(definition).equals(getColorAncestorValue(definition)))
13231360
return true;
13241361
} else if (colorValuesToSet.containsKey(id)) {
13251362
if (definition.getValue() != null) { // value-based color
1326-
if (colorValuesToSet.get(id).equals(definition.getValue()))
1363+
if (colorValuesToSet.get(id).equals(getColorTakingOverriddenCssValueIntoAccount(definition)))
13271364
return true;
13281365
} else {
13291366
if (colorValuesToSet.get(id).equals(getColorAncestorValue(definition)))
13301367
return true;
13311368
}
13321369
} else if (definition.getValue() != null) { // value-based color
1333-
if (getPreferenceStore().isDefault(createPreferenceKey(definition)))
1370+
IPreferenceStore store = getPreferenceStore();
1371+
String defaultString = store.getDefaultString(id);
1372+
String string = store.getString(id);
1373+
if (defaultString != null && string != null && defaultString.equals(string)) {
1374+
return true;
1375+
}
1376+
// String overriddenByCss = store.getString(EclipsePreferencesHelper.PROPS_OVERRIDDEN_BY_CSS_PROP);
1377+
// if (overriddenByCss != null && overriddenByCss.contains(EclipsePreferencesHelper.SEPARATOR
1378+
// + id + EclipsePreferencesHelper.SEPARATOR)) {
1379+
// return true;
1380+
// }
1381+
if (store.isDefault(createPreferenceKey(definition)))
13341382
return true;
13351383
} else {
13361384
// a descendant is default if it's the same value as its ancestor
@@ -1516,8 +1564,9 @@ private void refreshCategory() {
15161564
private boolean resetColor(ColorDefinition definition, boolean force) {
15171565
if (force || !isDefault(definition)) {
15181566
RGB newRGB;
1519-
if (definition.getValue() != null)
1520-
newRGB = definition.getValue();
1567+
if (definition.getValue() != null) {
1568+
newRGB = getColorTakingOverriddenCssValueIntoAccount(definition);
1569+
}
15211570
else
15221571
newRGB = getColorAncestorValue(definition);
15231572

0 commit comments

Comments
 (0)