9
9
package net .certiv .tools .indentguide ;
10
10
11
11
import org .eclipse .core .runtime .IStatus ;
12
+ import org .eclipse .core .runtime .Platform ;
12
13
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 ;
13
21
import org .eclipse .swt .graphics .Color ;
22
+ import org .eclipse .swt .graphics .RGB ;
23
+ import org .eclipse .ui .IWorkbench ;
14
24
import org .eclipse .ui .PlatformUI ;
15
25
import org .eclipse .ui .plugin .AbstractUIPlugin ;
26
+ import org .eclipse .ui .texteditor .AbstractTextEditor ;
16
27
import org .eclipse .ui .themes .ColorUtil ;
17
28
import org .osgi .framework .BundleContext ;
29
+ import org .osgi .service .event .EventHandler ;
18
30
19
31
import net .certiv .tools .indentguide .preferences .Settings ;
20
32
33
+ @ SuppressWarnings ("restriction" )
21
34
public class Activator extends AbstractUIPlugin {
22
35
23
36
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$
24
38
25
39
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
+
26
47
private Color color ;
27
48
28
- public Activator () {}
49
+ public Activator () {
50
+ super ();
51
+ }
29
52
30
53
/** Returns the shared instance */
31
54
public static Activator getDefault () {
@@ -36,35 +59,79 @@ public static Activator getDefault() {
36
59
public void start (BundleContext context ) throws Exception {
37
60
super .start (context );
38
61
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
+ }
39
71
}
40
72
41
73
@ Override
42
74
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 );
46
79
}
80
+
81
+ disposeLineColor ();
82
+ scopes [0 ] = scopes [1 ] = null ;
83
+
47
84
plugin = null ;
48
85
super .stop (context );
49
86
}
50
87
51
88
public Color getColor () {
52
89
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 ));
55
97
}
56
98
return color ;
57
99
}
58
100
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
+
59
121
public void setColor (Color color ) {
60
- if (this .color != null ) {
61
- this .color .dispose ();
62
- }
122
+ disposeLineColor ();
63
123
this .color = color ;
64
124
}
65
125
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 )));
68
135
}
69
136
70
137
public static void log (Throwable e ) {
0 commit comments