Skip to content

Commit e64ae63

Browse files
author
gbr
committed
Possible fix for dark mode problems.
1 parent 52f305f commit e64ae63

File tree

14 files changed

+110
-38
lines changed

14 files changed

+110
-38
lines changed

net.certiv.tools.indentguide.feature/feature.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<feature
33
id="net.certiv.tools.indentguide.feature"
44
label="IndentGuide"
5-
version="1.6.3.qualifier"
5+
version="1.7.0.qualifier"
66
provider-name="Certiv Analytis">
77

88
<description>

net.certiv.tools.indentguide.feature/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<parent>
1010
<groupId>net.certiv</groupId>
1111
<artifactId>net.certiv.tools.indentguide.parent</artifactId>
12-
<version>1.6.3-SNAPSHOT</version>
12+
<version>1.7.0-SNAPSHOT</version>
1313
</parent>
1414

1515
<artifactId>net.certiv.tools.indentguide.feature</artifactId>
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/target/
2+
/attic/

net.certiv.tools.indentguide.plugin/META-INF/MANIFEST.MF

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: IndentGuide
44
Bundle-SymbolicName: net.certiv.tools.indentguide;singleton:=true
5-
Bundle-Version: 1.6.3.qualifier
5+
Bundle-Version: 1.7.0.qualifier
66
Bundle-Activator: net.certiv.tools.indentguide.Activator
77
Require-Bundle: org.eclipse.ui;bundle-version="[3.109.100,4.0.0)",
88
org.eclipse.core.runtime;bundle-version="[3.14.0,4.0.0)",
99
org.eclipse.jface.text;bundle-version="[3.13.0,4.0.0)",
1010
org.eclipse.ui.workbench.texteditor;bundle-version="[3.11.0,4.0.0)",
11-
org.eclipse.e4.ui.css.swt.theme
11+
org.eclipse.e4.core.services;bundle-version="2.3.0",
12+
org.eclipse.e4.ui.css.swt.theme;bundle-version="0.13.0",
13+
org.eclipse.osgi.services
1214
Bundle-RequiredExecutionEnvironment: JavaSE-11
1315
Bundle-Vendor: Certiv Analytics
1416
Automatic-Module-Name: net.certiv.tools.indentguide

net.certiv.tools.indentguide.plugin/build.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ bin.includes = plugin.xml,\
44
.,\
55
META-INF/,\
66
OSGI-INF/,\
7-
css/
7+
attic/css/

net.certiv.tools.indentguide.plugin/css/dark.css

-5
This file was deleted.

net.certiv.tools.indentguide.plugin/plugin.xml

-9
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,5 @@
2525
</startup>
2626
</extension>
2727

28-
<extension
29-
point="org.eclipse.e4.ui.css.swt.theme">
30-
<stylesheet
31-
uri="css/dark.css">
32-
<themeid
33-
refid="org.eclipse.e4.ui.css.theme.e4_dark">
34-
</themeid>
35-
</stylesheet>
36-
</extension>
3728

3829
</plugin>

net.certiv.tools.indentguide.plugin/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<parent>
1010
<groupId>net.certiv</groupId>
1111
<artifactId>net.certiv.tools.indentguide.parent</artifactId>
12-
<version>1.6.3-SNAPSHOT</version>
12+
<version>1.7.0-SNAPSHOT</version>
1313
</parent>
1414

1515
<artifactId>net.certiv.tools.indentguide</artifactId>

net.certiv.tools.indentguide.plugin/src/net/certiv/tools/indentguide/Activator.java

+78-11
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,46 @@
99
package net.certiv.tools.indentguide;
1010

1111
import org.eclipse.core.runtime.IStatus;
12+
import org.eclipse.core.runtime.Platform;
1213
import org.eclipse.core.runtime.Status;
14+
import org.eclipse.core.runtime.preferences.DefaultScope;
15+
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
16+
import org.eclipse.core.runtime.preferences.InstanceScope;
17+
import org.eclipse.e4.core.services.events.IEventBroker;
18+
import org.eclipse.e4.ui.css.swt.theme.IThemeEngine;
19+
import org.eclipse.jface.preference.PreferenceConverter;
20+
import org.eclipse.jface.resource.StringConverter;
1321
import org.eclipse.swt.graphics.Color;
22+
import org.eclipse.swt.graphics.RGB;
23+
import org.eclipse.ui.IWorkbench;
1424
import org.eclipse.ui.PlatformUI;
1525
import org.eclipse.ui.plugin.AbstractUIPlugin;
26+
import org.eclipse.ui.texteditor.AbstractTextEditor;
1627
import org.eclipse.ui.themes.ColorUtil;
1728
import org.osgi.framework.BundleContext;
29+
import org.osgi.service.event.EventHandler;
1830

1931
import net.certiv.tools.indentguide.preferences.Settings;
2032

33+
@SuppressWarnings("restriction")
2134
public class Activator extends AbstractUIPlugin {
2235

2336
public static final String PLUGIN_ID = "net.certiv.tools.indentguide"; //$NON-NLS-1$
37+
private static final String EditorsID = "org.eclipse.ui.editors"; //$NON-NLS-1$
2438

2539
private static Activator plugin;
40+
41+
private final IEclipsePreferences[] scopes = new IEclipsePreferences[2];
42+
private final EventHandler themeEventHandler = event -> {
43+
disposeLineColor();
44+
log("Theme change '%s'", event);
45+
};
46+
2647
private Color color;
2748

28-
public Activator() {}
49+
public Activator() {
50+
super();
51+
}
2952

3053
/** Returns the shared instance */
3154
public static Activator getDefault() {
@@ -36,35 +59,79 @@ public static Activator getDefault() {
3659
public void start(BundleContext context) throws Exception {
3760
super.start(context);
3861
plugin = this;
62+
63+
scopes[0] = InstanceScope.INSTANCE.getNode(EditorsID);
64+
scopes[1] = DefaultScope.INSTANCE.getNode(EditorsID);
65+
66+
IWorkbench wb = PlatformUI.getWorkbench();
67+
IEventBroker broker = wb.getService(IEventBroker.class);
68+
if (broker != null) {
69+
broker.subscribe(IThemeEngine.Events.THEME_CHANGED, themeEventHandler);
70+
}
3971
}
4072

4173
@Override
4274
public void stop(BundleContext context) throws Exception {
43-
if (color != null) {
44-
color.dispose();
45-
color = null;
75+
IWorkbench wb = PlatformUI.getWorkbench();
76+
IEventBroker broker = wb.getService(IEventBroker.class);
77+
if (broker != null) {
78+
broker.unsubscribe(themeEventHandler);
4679
}
80+
81+
disposeLineColor();
82+
scopes[0] = scopes[1] = null;
83+
4784
plugin = null;
4885
super.stop(context);
4986
}
5087

5188
public Color getColor() {
5289
if (color == null) {
53-
String colorSpec = getPreferenceStore().getString(Settings.LINE_COLOR);
54-
color = new Color(PlatformUI.getWorkbench().getDisplay(), ColorUtil.getColorValue(colorSpec));
90+
String key = Settings.LINE_COLOR;
91+
if (isDarkTheme()) {
92+
key += Settings.DARK;
93+
}
94+
String spec = getPreferenceStore().getString(key);
95+
color = new Color(PlatformUI.getWorkbench().getDisplay(), ColorUtil.getColorValue(spec));
96+
log(String.format("Line color set %s -> %s", key, spec));
5597
}
5698
return color;
5799
}
58100

101+
/**
102+
* Returns {@code true} if the current theme is 'dark', defined as where the
103+
* foreground color is relatively darker than the background color. (black ->
104+
* '0'; white -> '255*3')
105+
*/
106+
public boolean isDarkTheme() {
107+
RGB fg = getRawRGB(AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND);
108+
RGB bg = getRawRGB(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND);
109+
return (fg.red + fg.blue + fg.green) > (bg.red + bg.blue + bg.green);
110+
}
111+
112+
private RGB getRawRGB(String key) {
113+
String value = Platform.getPreferencesService().get(key, null, scopes);
114+
if (value == null) return PreferenceConverter.COLOR_DEFAULT_DEFAULT;
115+
116+
RGB rgb = StringConverter.asRGB(value, null);
117+
if (rgb == null) return PreferenceConverter.COLOR_DEFAULT_DEFAULT;
118+
return rgb;
119+
}
120+
59121
public void setColor(Color color) {
60-
if (this.color != null) {
61-
this.color.dispose();
62-
}
122+
disposeLineColor();
63123
this.color = color;
64124
}
65125

66-
public static void log(String msg) {
67-
plugin.getLog().log(new Status(IStatus.INFO, PLUGIN_ID, msg));
126+
private void disposeLineColor() {
127+
if (color != null) {
128+
color.dispose();
129+
color = null;
130+
}
131+
}
132+
133+
public static void log(String fmt, Object... args) {
134+
plugin.getLog().log(new Status(IStatus.INFO, PLUGIN_ID, String.format(fmt, args)));
68135
}
69136

70137
public static void log(Throwable e) {

net.certiv.tools.indentguide.plugin/src/net/certiv/tools/indentguide/preferences/Initializer.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
/** Initialize default preference values. */
2121
public class Initializer extends AbstractPreferenceInitializer {
2222

23+
private static final String BLACK = "0,0,0"; // $NON-NLS-1$
24+
private static final String LIGHT = "192,192,192"; // $NON-NLS-1$
25+
private static final String PIPE = "|"; // $NON-NLS-1$
26+
2327
@Override
2428
public void initializeDefaultPreferences() {
2529
IPreferenceStore store = Activator.getDefault().getPreferenceStore();
@@ -28,7 +32,8 @@ public void initializeDefaultPreferences() {
2832
store.setDefault(Settings.LINE_STYLE, SWT.LINE_SOLID);
2933
store.setDefault(Settings.LINE_WIDTH, 1);
3034
store.setDefault(Settings.LINE_SHIFT, 2);
31-
store.setDefault(Settings.LINE_COLOR, "0,0,0"); //$NON-NLS-1$
35+
store.setDefault(Settings.LINE_COLOR, BLACK);
36+
store.setDefault(Settings.LINE_COLOR + Settings.DARK, LIGHT);
3237
store.setDefault(Settings.DRAW_LEFT_END, false);
3338
store.setDefault(Settings.DRAW_BLANK_LINE, true);
3439
store.setDefault(Settings.SKIP_COMMENT_BLOCK, false);
@@ -38,7 +43,7 @@ public void initializeDefaultPreferences() {
3843
StringBuilder sb = new StringBuilder();
3944
for (IContentType type : mgr.getAllContentTypes()) {
4045
if (type.isKindOf(textType)) {
41-
sb.append(type.getId() + "|");
46+
sb.append(type.getId() + PIPE);
4247
}
4348
}
4449
sb.setLength(sb.length() - 1);

net.certiv.tools.indentguide.plugin/src/net/certiv/tools/indentguide/preferences/Settings.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@
1212
public class Settings {
1313

1414
public static final String KEY = "indentguide.";
15+
public static final String DARK = ".dark";
1516

1617
public static final String ENABLED = KEY + "enabled"; //$NON-NLS-1$
18+
1719
public static final String LINE_ALPHA = KEY + "line_alpha"; //$NON-NLS-1$
1820
public static final String LINE_STYLE = KEY + "line_style"; //$NON-NLS-1$
1921
public static final String LINE_WIDTH = KEY + "line_width"; //$NON-NLS-1$
2022
public static final String LINE_SHIFT = KEY + "line_shift"; //$NON-NLS-1$
2123
public static final String LINE_COLOR = KEY + "line_color"; //$NON-NLS-1$
24+
2225
public static final String DRAW_LEFT_END = KEY + "draw_left_end"; //$NON-NLS-1$
2326
public static final String DRAW_BLANK_LINE = KEY + "draw_blank_line"; //$NON-NLS-1$
2427
public static final String SKIP_COMMENT_BLOCK = KEY + "skip_comment_block"; //$NON-NLS-1$
2528
public static final String CONTENT_TYPES = KEY + "content_types"; //$NON-NLS-1$
26-
2729
}

net.certiv.tools.indentguide.plugin/src/net/certiv/tools/indentguide/preferences/SettingsPage.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ public void widgetSelected(final SelectionEvent e) {
107107
lineShift = new Spinner(attributes, SWT.BORDER);
108108
lineShift.setMinimum(0);
109109
lineShift.setMaximum(8);
110-
colorFieldEditor = new ColorFieldEditor(Settings.LINE_COLOR, SettingsMessages.Settings_color_label,
110+
111+
colorFieldEditor = new ColorFieldEditor(colorKey(), SettingsMessages.Settings_color_label,
111112
attributes);
112113
colorFieldEditor.setPreferenceStore(getPreferenceStore());
113114

@@ -157,6 +158,14 @@ public void widgetDefaultSelected(final SelectionEvent e) {}
157158
return composite;
158159
}
159160

161+
private String colorKey() {
162+
String key = Settings.LINE_COLOR;
163+
if (Activator.getDefault().isDarkTheme()) {
164+
key += Settings.DARK;
165+
}
166+
return key;
167+
}
168+
160169
@Override
161170
protected void performDefaults() {
162171
super.performDefaults();

net.certiv.tools.indentguide.site/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<parent>
1010
<groupId>net.certiv</groupId>
1111
<artifactId>net.certiv.tools.indentguide.parent</artifactId>
12-
<version>1.6.3-SNAPSHOT</version>
12+
<version>1.7.0-SNAPSHOT</version>
1313
</parent>
1414

1515
<artifactId>net.certiv.tools.indentguide.site</artifactId>

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<groupId>net.certiv</groupId>
1010
<artifactId>net.certiv.tools.indentguide.parent</artifactId>
11-
<version>1.6.3-SNAPSHOT</version>
11+
<version>1.7.0-SNAPSHOT</version>
1212

1313
<name>IndentGuide</name>
1414
<description>IndentGuide</description>
@@ -27,7 +27,7 @@
2727
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
2828
<java.version>11</java.version>
2929
<tycho.version>2.0.0</tycho.version>
30-
<eclipse-repo.url>http://download.eclipse.org/releases/2021-03</eclipse-repo.url>
30+
<eclipse-repo.url>http://download.eclipse.org/releases/2021-09</eclipse-repo.url>
3131
</properties>
3232

3333
<modules>

0 commit comments

Comments
 (0)