Separate preferences keys for dark theme colors

This commit is contained in:
Peter Dell 2023-02-26 20:53:14 +01:00
parent 62102443b5
commit 3667bfb22b
10 changed files with 140 additions and 33 deletions

View File

@ -121,6 +121,7 @@ public final class Texts extends NLS {
* Preferences: syntax highlighting.
*/
public static String PREFERENCES_SYNTAX_HIGHLIGHTING_GROUP_TITLE;
public static String PREFERENCES_SYNTAX_HIGHLIGHTING_GROUP_DARK_THEME_TITLE;
public static String PREFERENCES_TEXT_ATTRIBUTE_COMMENT_NAME;
public static String PREFERENCES_TEXT_ATTRIBUTE_STRING_NAME;
@ -237,7 +238,7 @@ public final class Texts extends NLS {
public static String TOC_COMPILER_SYNTAX_SINGLE_LINE_COMMENT_DELIMITERS;
public static String TOC_COMPILER_SYNTAX_SOURCE_INCLUDE_DEFAULT_EXTENSION;
public static String TOC_COMPILER_SYNTAX_STRING_DELIMITERS;
public static String TOC_HARDWARES_TOPIC_LABEL;
public static String TOC_HARDWARE_NAME_LABEL;
public static String TOC_HARDWARE_ID_LABEL;

View File

@ -61,6 +61,7 @@ COMPILER_SYMBOLS_VIEW_DECIMAL_VALUE_COLUMN_LABEL=Decimal Value
COMPILER_SYMBOLS_VIEW_STRING_VALUE_COLUMN_LABEL=String Value
PREFERENCES_SYNTAX_HIGHLIGHTING_GROUP_TITLE=Syntax Highlighting
PREFERENCES_SYNTAX_HIGHLIGHTING_GROUP_DARK_THEME_TITLE=Syntax Highlighting for Dark Theme
PREFERENCES_TEXT_ATTRIBUTE_COMMENT_NAME=Comments
PREFERENCES_TEXT_ATTRIBUTE_STRING_NAME=Strings

View File

@ -60,6 +60,7 @@ COMPILER_SYMBOLS_VIEW_DECIMAL_VALUE_COLUMN_LABEL=Dezimalwert
COMPILER_SYMBOLS_VIEW_STRING_VALUE_COLUMN_LABEL=Textwert
PREFERENCES_SYNTAX_HIGHLIGHTING_GROUP_TITLE=Syntax Hervorhebung
PREFERENCES_SYNTAX_HIGHLIGHTING_GROUP_DARK_THEME_TITLE=Syntax Hervorhebung für dunkles Theme
PREFERENCES_TEXT_ATTRIBUTE_COMMENT_NAME=Kommentare
PREFERENCES_TEXT_ATTRIBUTE_STRING_NAME=Textkonstanten

View File

@ -61,6 +61,7 @@ COMPILER_SYMBOLS_VIEW_DECIMAL_VALUE_COLUMN_LABEL=Decimal Value
COMPILER_SYMBOLS_VIEW_STRING_VALUE_COLUMN_LABEL=String Value
PREFERENCES_SYNTAX_HIGHLIGHTING_GROUP_TITLE=Syntax Highlighting
PREFERENCES_SYNTAX_HIGHLIGHTING_GROUP_DARK_THEME_TITLE=Syntax Highlighting for Dark Theme
PREFERENCES_TEXT_ATTRIBUTE_COMMENT_NAME=Comments
PREFERENCES_TEXT_ATTRIBUTE_STRING_NAME=Strings

View File

@ -31,9 +31,9 @@ import com.wudsn.ide.lng.LanguagePlugin;
import com.wudsn.ide.lng.compiler.CompilerDefinition;
import com.wudsn.ide.lng.compiler.CompilerPaths;
import com.wudsn.ide.lng.compiler.CompilerPaths.CompilerPath;
import com.wudsn.ide.lng.preferences.LanguagePreferencesConstants.EditorConstants;
import com.wudsn.ide.lng.editor.LanguageContentAssistProcessorDefaultCase;
import com.wudsn.ide.lng.editor.LanguageEditorCompileCommandPositioningMode;
import com.wudsn.ide.lng.preferences.LanguagePreferencesConstants.EditorConstants;
/**
* Facade class for typed access to the plugin preferences.

View File

@ -109,7 +109,6 @@ public final class LanguagePreferencesConstants {
*/
public static final String EDITOR_TEXT_ATTRIBUTE_IDENTIFIER_PROCEDURE_DEFINITION_SECTION = "editor.text.attribute.identifier.procedure"; //$NON-NLS-1$
/**
* Preference key for positioning for for compiling.
*
@ -120,16 +119,17 @@ public final class LanguagePreferencesConstants {
/**
* Gets preference key name for a editor attribute.
*
* @param language The language <code>null</code>.
* @param language The language, not <code>null</code>.
* @attributeName The attribute name, not <code>null</code>.
*
* @return The preference key name for the compiler executable path, not empty
* and not <code>null</code>.
*/
public static String getEditorAttributeKey(Language language, String textAttributeName) {
public static String getEditorAttributeKey(Language language, String attributeName) {
if (language == null) {
throw new IllegalArgumentException("Parameter 'language' must not be null.");
}
String preferencesKey = getLanguagePreferencesKey(language, textAttributeName);
String preferencesKey = getLanguagePreferencesKey(language, attributeName);
return preferencesKey;
}
@ -148,7 +148,7 @@ public final class LanguagePreferencesConstants {
return getEditorAttributeKey(language, EditorConstants.EDITOR_COMPILE_COMMAND_POSITIONING_MODE);
}
/**
* Gets the list of all preferences keys that depend on the global JFact text
* font setting.

View File

@ -57,13 +57,23 @@ public final class LanguagePreferencesInitializer extends AbstractPreferenceInit
store.setDefault(key, value);
}
private void setLanguageTextAttributeDefault(Language language, String textAttributeName, int r, int g, int b,
private void setLanguageTextAttributeDefault(Language language, String textAttributeName, int rgb, int rgbDarkTheme,
int style) {
setLanguageTextAttributeDefault(language, false, textAttributeName, rgb, style);
setLanguageTextAttributeDefault(language, true, textAttributeName, rgbDarkTheme, style);
}
private void setLanguageTextAttributeDefault(Language language, boolean darkTheme, String textAttributeName,
int rgb, int style) {
// Editor.
var display = Display.getCurrent();
int b = (rgb) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int r = (rgb >> 16) & 0xFF;
var textAttribute = new TextAttribute(new Color(display, r, g, b), null, style);
var preferencesKey = LanguagePreferencesConstants.EditorConstants.getEditorAttributeKey(language,
textAttributeName);
preferencesKey = LanguagesPreferences.getThemeTextAttributePreferencesKey(darkTheme, preferencesKey);
setDefault(preferencesKey, TextAttributeConverter.toString(textAttribute));
}
@ -89,16 +99,41 @@ public final class LanguagePreferencesInitializer extends AbstractPreferenceInit
if (language == null) {
throw new IllegalArgumentException("Parameter 'language' must not be null.");
}
setLanguageTextAttributeDefault(language, EditorConstants.EDITOR_TEXT_ATTRIBUTE_COMMENT, 0, 128, 0, SWT.ITALIC);
setLanguageTextAttributeDefault(language, EditorConstants.EDITOR_TEXT_ATTRIBUTE_DIRECTVE, 128, 64, 0, SWT.BOLD);
setLanguageTextAttributeDefault(language, EditorConstants.EDITOR_TEXT_ATTRIBUTE_NUMBER, 0, 0, 255, SWT.BOLD);
setLanguageTextAttributeDefault(language, EditorConstants.EDITOR_TEXT_ATTRIBUTE_OPCODE_LEGAL, 0, 0, 128,
final int black = 0x00000;
final int white = 0xffffff;
setLanguageTextAttributeDefault(language, EditorConstants.EDITOR_TEXT_ATTRIBUTE_COMMENT, 0x008000, 0x80ff80,
SWT.ITALIC);
setLanguageTextAttributeDefault(language, EditorConstants.EDITOR_TEXT_ATTRIBUTE_DIRECTVE, 0x804000, 0xffc080,
SWT.BOLD);
setLanguageTextAttributeDefault(language, EditorConstants.EDITOR_TEXT_ATTRIBUTE_OPCODE_ILLEGAL, 255, 32, 32,
setLanguageTextAttributeDefault(language, EditorConstants.EDITOR_TEXT_ATTRIBUTE_NUMBER, 0x0000ff, 0x0080c0,
SWT.BOLD);
setLanguageTextAttributeDefault(language, EditorConstants.EDITOR_TEXT_ATTRIBUTE_OPCODE_PSEUDO, 32, 128, 32,
SWT.BOLD);
setLanguageTextAttributeDefault(language, EditorConstants.EDITOR_TEXT_ATTRIBUTE_STRING, 0, 0, 255, SWT.NORMAL);
// Identifiers
setLanguageTextAttributeDefault(language,
EditorConstants.EDITOR_TEXT_ATTRIBUTE_IDENTIFIER_ENUM_DEFINITION_SECTION, black, white, SWT.NORMAL);
setLanguageTextAttributeDefault(language, EditorConstants.EDITOR_TEXT_ATTRIBUTE_IDENTIFIER_EQUATE, black, white,
SWT.NORMAL);
setLanguageTextAttributeDefault(language, EditorConstants.EDITOR_TEXT_ATTRIBUTE_IDENTIFIER_LABEL, black, white,
SWT.NORMAL);
setLanguageTextAttributeDefault(language, EditorConstants.EDITOR_TEXT_ATTRIBUTE_IDENTIFIER_LOCAL_SECTION, black,
white, SWT.NORMAL);
setLanguageTextAttributeDefault(language,
EditorConstants.EDITOR_TEXT_ATTRIBUTE_IDENTIFIER_MACRO_DEFINITION_SECTION, black, white, SWT.NORMAL);
setLanguageTextAttributeDefault(language,
EditorConstants.EDITOR_TEXT_ATTRIBUTE_IDENTIFIER_PROCEDURE_DEFINITION_SECTION, black, white,
SWT.NORMAL);
setLanguageTextAttributeDefault(language,
EditorConstants.EDITOR_TEXT_ATTRIBUTE_IDENTIFIER_STRUCTURE_DEFINITION_SECTION, black, white,
SWT.NORMAL);
setLanguageTextAttributeDefault(language, EditorConstants.EDITOR_TEXT_ATTRIBUTE_OPCODE_LEGAL, 0x000080,
0x8080ff, SWT.BOLD);
setLanguageTextAttributeDefault(language, EditorConstants.EDITOR_TEXT_ATTRIBUTE_OPCODE_ILLEGAL, 0xff2020,
0xd00000, SWT.BOLD);
setLanguageTextAttributeDefault(language, EditorConstants.EDITOR_TEXT_ATTRIBUTE_OPCODE_PSEUDO, 0x208020,
0xc0ffc0, SWT.BOLD);
setLanguageTextAttributeDefault(language, EditorConstants.EDITOR_TEXT_ATTRIBUTE_STRING, 0x0000ff, 0x00ff40,
SWT.NORMAL);
// Content assist.
var preferencesKey = EditorConstants.getEditorContentProcessorDefaultCaseKey(language);

View File

@ -31,6 +31,7 @@ import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.RadioGroupFieldEditor;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
@ -57,6 +58,7 @@ import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.model.WorkbenchViewerComparator;
import com.wudsn.ide.base.common.HexUtility;
import com.wudsn.ide.base.common.ProcessWithLogs;
import com.wudsn.ide.base.gui.SWTFactory;
import com.wudsn.ide.lng.Language;
@ -211,8 +213,12 @@ public abstract class LanguagePreferencesPage extends FieldEditorPreferencePage
if (parent == null) {
throw new IllegalArgumentException("Parameter 'parent' must not be null.");
}
Group group = SWTFactory.createGroup(parent, Texts.PREFERENCES_SYNTAX_HIGHLIGHTING_GROUP_TITLE, 2, 1,
GridData.FILL_HORIZONTAL);
var title = Texts.PREFERENCES_SYNTAX_HIGHLIGHTING_GROUP_TITLE;
if (LanguagesPreferences.isDarkThemeActive()) {
title = Texts.PREFERENCES_SYNTAX_HIGHLIGHTING_GROUP_DARK_THEME_TITLE;
}
Group group = SWTFactory.createGroup(parent, title, 2, 1, GridData.FILL_HORIZONTAL);
Label label;
GridLayout layout;
GridData gd;
@ -245,9 +251,19 @@ public abstract class LanguagePreferencesPage extends FieldEditorPreferencePage
label.setLayoutData(gd);
textAttributeForegroundColorSelector = new ColorSelector(stylesComposite);
Button foregroundColorButton = textAttributeForegroundColorSelector.getButton();
final var foregroundColorButton = textAttributeForegroundColorSelector.getButton();
gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
foregroundColorButton.setLayoutData(gd);
textAttributeForegroundColorSelector.addListener(new IPropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
if (event.getProperty().equals(ColorSelector.PROP_COLORCHANGE)) {
updatetextAttributeForegroundColor();
}
}
});
textAttributeBoldCheckBox = new Button(stylesComposite, SWT.CHECK);
textAttributeBoldCheckBox.setText(Texts.PREFERENCES_BOLD_LABEL);
@ -362,8 +378,9 @@ public abstract class LanguagePreferencesPage extends FieldEditorPreferencePage
List<TextAttributeDefinition> textAttributeDefinitions = EditorConstants.getTextAttributeDefinitions(language);
textAttributeListItems = new ArrayList<TextAttributeListItem>(textAttributeDefinitions.size());
for (TextAttributeDefinition textAttributeDefinition : textAttributeDefinitions) {
String data = getPreferenceStore().getString(textAttributeDefinition.getPreferencesKey());
for (var textAttributeDefinition : textAttributeDefinitions) {
var preferencesKey = LanguagesPreferences.getThemeTextAttributePreferencesKey(textAttributeDefinition);
String data = getPreferenceStore().getString(preferencesKey);
TextAttribute textAttribute = TextAttributeConverter.fromString(data);
TextAttributeListItem item = new TextAttributeListItem(textAttributeDefinition);
@ -376,10 +393,10 @@ public abstract class LanguagePreferencesPage extends FieldEditorPreferencePage
if (parent == null) {
throw new IllegalArgumentException("Parameter 'parent' must not be null.");
}
Group group = SWTFactory.createGroup(parent, Texts.PREFERENCES_EDITOR_GROUP_TITLE, 1, 1,
var group = SWTFactory.createGroup(parent, Texts.PREFERENCES_EDITOR_GROUP_TITLE, 1, 1,
GridData.FILL_HORIZONTAL);
Composite space = SWTFactory.createComposite(group, 2, 1, GridData.FILL_HORIZONTAL);
var space = SWTFactory.createComposite(group, 2, 1, GridData.FILL_HORIZONTAL);
String[][] labelsAndValues;
labelsAndValues = new String[][] { {
@ -498,9 +515,9 @@ public abstract class LanguagePreferencesPage extends FieldEditorPreferencePage
IPreferenceStore preferencesStore = getPreferenceStore();
for (TextAttributeListItem listItem : textAttributeListItems) {
String key = listItem.getDefinition().getPreferencesKey();
preferencesStore.setValue(key, preferencesStore.getDefaultString(key));
addChangedProperty(key);
String preferencesKey = LanguagesPreferences.getThemeTextAttributePreferencesKey(listItem.getDefinition());
preferencesStore.setValue(preferencesKey, preferencesStore.getDefaultString(preferencesKey));
addChangedProperty(preferencesKey);
}
disposeTextAttributesList();
@ -512,12 +529,12 @@ public abstract class LanguagePreferencesPage extends FieldEditorPreferencePage
* Saves all changes to the {@link IPreferenceStore}.
*/
private void saveChanges() {
String data;
IPreferenceStore store = getPreferenceStore();
var store = getPreferenceStore();
for (TextAttributeListItem listItem : textAttributeListItems) {
data = TextAttributeConverter.toString(listItem.getTextAttribute());
store.setValue(listItem.getDefinition().getPreferencesKey(), data);
var data = TextAttributeConverter.toString(listItem.getTextAttribute());
var preferencesKey = LanguagesPreferences.getThemeTextAttributePreferencesKey(listItem.getDefinition());
store.setValue(preferencesKey, data);
}
@ -554,6 +571,7 @@ public abstract class LanguagePreferencesPage extends FieldEditorPreferencePage
italic = (textAttribute.getStyle() & SWT.ITALIC) == SWT.ITALIC;
textAttributeForegroundColorSelector.setColorValue(color.getRGB());
updatetextAttributeForegroundColor();
textAttributeBoldCheckBox.setSelection(bold);
textAttributeItalicCheckBox.setSelection(italic);
@ -562,6 +580,14 @@ public abstract class LanguagePreferencesPage extends FieldEditorPreferencePage
textAttributeItalicCheckBox.setEnabled(true);
}
private void updatetextAttributeForegroundColor() {
var rgb = textAttributeForegroundColorSelector.getColorValue();
String rgbString = ("0x" + HexUtility.getLongValueHexString(rgb.red)
+ HexUtility.getLongValueHexString(rgb.green) + HexUtility.getLongValueHexString(rgb.blue))
.toLowerCase();
textAttributeForegroundColorSelector.getButton().setToolTipText(rgbString);
}
/**
* Returns the current highlighting color list item.
*

View File

@ -19,8 +19,12 @@
package com.wudsn.ide.lng.preferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.preferences.ScopedPreferenceStore;
import com.wudsn.ide.base.common.AbstractIDEPlugin;
import com.wudsn.ide.lng.Language;
@ -33,6 +37,11 @@ import com.wudsn.ide.lng.LanguagePlugin;
*/
public final class LanguagesPreferences {
/**
* Cached theme ID.
*/
static String themeID = null;
/**
* The preference store to which all calls are delegated.
*/
@ -107,6 +116,40 @@ public final class LanguagesPreferences {
return result;
}
/**
* Returns <code>true</code> if the current OS theme has a dark appearance, else
* returns <code>false</code>.
*/
static boolean isDarkThemeActive() {
if (themeID == null) {
var store = new ScopedPreferenceStore(InstanceScope.INSTANCE, "org.eclipse.e4.ui.css.swt.theme");
themeID = store.getString("themeid");
if (themeID == null) {
themeID = "undefined";
}
}
return themeID.equals("org.eclipse.e4.ui.css.theme.e4_dark");
}
/**
* Gets the text attribute preferences key based on the current theme. Dark
* themes have a separate set of color.
*
* @param textAttributeDefinition The text attribute definition, not
* <code>null</code>.
* @return The theme specific key of the preference, not <code>null</code>.
*/
static String getThemeTextAttributePreferencesKey(TextAttributeDefinition textAttributeDefinition) {
return getThemeTextAttributePreferencesKey(isDarkThemeActive(), textAttributeDefinition.getPreferencesKey());
}
static String getThemeTextAttributePreferencesKey(boolean darkTheme, String preferencesKey) {
if (darkTheme) {
preferencesKey += ".darkTheme";
}
return preferencesKey;
}
/**
* Gets the text attribute for a token type.
*
@ -120,6 +163,7 @@ public final class LanguagesPreferences {
throw new IllegalArgumentException("Parameter 'preferencesKey' must not be null.");
}
preferencesKey = getThemeTextAttributePreferencesKey(isDarkThemeActive(), preferencesKey);
return TextAttributeConverter.fromString(getString(preferencesKey));
}

View File

@ -24,7 +24,7 @@ import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
/**
* Structural page
* Structural page without editors.
*
* @author Peter Dell
*/
@ -32,13 +32,11 @@ public final class LanguagesPreferencesPage extends FieldEditorPreferencePage im
@Override
public void init(IWorkbench workbench) {
// TODO Auto-generated method stub
}
@Override
protected void createFieldEditors() {
// TODO Auto-generated method stub
}