10
10
import com .intellij .openapi .components .ServiceManager ;
11
11
import com .intellij .openapi .diagnostic .Logger ;
12
12
import com .intellij .openapi .util .SystemInfo ;
13
+ import com .intellij .util .Alarm ;
14
+ import com .intellij .util .Alarm .ThreadToUse ;
13
15
import java .util .concurrent .ScheduledExecutorService ;
14
16
import java .util .concurrent .ScheduledFuture ;
15
17
import java .util .concurrent .TimeUnit ;
@@ -28,6 +30,7 @@ public final class DarkModeSync implements Disposable {
28
30
29
31
private final LafManager lafManager ;
30
32
private final DarkModeSyncThemes themes ;
33
+ private final Alarm updateOnUIThreadAlarm ;
31
34
32
35
/** @param lafManager IDEA look-and-feel manager for getting and setting the current theme */
33
36
public DarkModeSync (final LafManager lafManager ) {
@@ -37,17 +40,25 @@ public DarkModeSync(final LafManager lafManager) {
37
40
if (!(SystemInfo .isMacOSMojave || SystemInfo .isWin10OrNewer )) {
38
41
logger .error ("Plugin only supports macOS Mojave and greater or Windows 10 and greater" );
39
42
scheduledFuture = null ;
43
+ updateOnUIThreadAlarm = null ;
40
44
return ;
41
45
}
42
- ScheduledExecutorService executor = JobScheduler .getScheduler ();
46
+ final ScheduledExecutorService executor = JobScheduler .getScheduler ();
43
47
scheduledFuture =
44
48
executor .scheduleWithFixedDelay (this ::updateLafIfNecessary , 0 , 3 , TimeUnit .SECONDS );
49
+ // initialize this last because it publishes "this"
50
+ updateOnUIThreadAlarm = new Alarm (ThreadToUse .SWING_THREAD , this );
45
51
}
46
52
47
53
/** cancels the scheduled task if one exists */
48
54
@ Override
49
55
public void dispose () {
50
- if (scheduledFuture != null ) scheduledFuture .cancel (true );
56
+ if (scheduledFuture != null ) {
57
+ scheduledFuture .cancel (true );
58
+ }
59
+ if (updateOnUIThreadAlarm != null ) {
60
+ updateOnUIThreadAlarm .cancelAllRequests ();
61
+ }
51
62
}
52
63
53
64
private void updateLafIfNecessary () {
@@ -71,8 +82,13 @@ private void updateLafIfNecessary() {
71
82
}
72
83
}
73
84
85
+ /**
86
+ * Uses the {@link #updateOnUIThreadAlarm} to schedule a look and feel update on the Swing UI
87
+ * thread
88
+ */
74
89
private void updateLaf (final LookAndFeelInfo newLaf ) {
75
- QuickChangeLookAndFeel .switchLafAndUpdateUI (lafManager , newLaf , true );
90
+ updateOnUIThreadAlarm .addRequest (
91
+ () -> QuickChangeLookAndFeel .switchLafAndUpdateUI (lafManager , newLaf , false ), 0 );
76
92
}
77
93
78
94
private static final Logger logger = Logger .getInstance (DarkModeSync .class );
0 commit comments