Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
🐛 Fix occasional problem where in the theme only partially updates (#18)
Browse files Browse the repository at this point in the history
Use IntelliJ's Alarm class to schedule updates on the UI thread instead of the async parameter in switchLafAndUpdateUI method
  • Loading branch information
gilday committed Aug 10, 2020
1 parent bfde6a7 commit 5a90440
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "com.github.gilday"
version = "1.2.4"
version = "1.2.5"

repositories {
mavenCentral()
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/com/github/gilday/darkmode/DarkModeSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.util.Alarm;
import com.intellij.util.Alarm.ThreadToUse;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
Expand All @@ -28,6 +30,7 @@ public final class DarkModeSync implements Disposable {

private final LafManager lafManager;
private final DarkModeSyncThemes themes;
private final Alarm updateOnUIThreadAlarm;

/** @param lafManager IDEA look-and-feel manager for getting and setting the current theme */
public DarkModeSync(final LafManager lafManager) {
Expand All @@ -37,17 +40,25 @@ public DarkModeSync(final LafManager lafManager) {
if (!(SystemInfo.isMacOSMojave || SystemInfo.isWin10OrNewer)) {
logger.error("Plugin only supports macOS Mojave and greater or Windows 10 and greater");
scheduledFuture = null;
updateOnUIThreadAlarm = null;
return;
}
ScheduledExecutorService executor = JobScheduler.getScheduler();
final ScheduledExecutorService executor = JobScheduler.getScheduler();
scheduledFuture =
executor.scheduleWithFixedDelay(this::updateLafIfNecessary, 0, 3, TimeUnit.SECONDS);
// initialize this last because it publishes "this"
updateOnUIThreadAlarm = new Alarm(ThreadToUse.SWING_THREAD, this);
}

/** cancels the scheduled task if one exists */
@Override
public void dispose() {
if (scheduledFuture != null) scheduledFuture.cancel(true);
if (scheduledFuture != null) {
scheduledFuture.cancel(true);
}
if (updateOnUIThreadAlarm != null) {
updateOnUIThreadAlarm.cancelAllRequests();
}
}

private void updateLafIfNecessary() {
Expand All @@ -71,8 +82,13 @@ private void updateLafIfNecessary() {
}
}

/**
* Uses the {@link #updateOnUIThreadAlarm} to schedule a look and feel update on the Swing UI
* thread
*/
private void updateLaf(final LookAndFeelInfo newLaf) {
QuickChangeLookAndFeel.switchLafAndUpdateUI(lafManager, newLaf, true);
updateOnUIThreadAlarm.addRequest(
() -> QuickChangeLookAndFeel.switchLafAndUpdateUI(lafManager, newLaf, false), 0);
}

private static final Logger logger = Logger.getInstance(DarkModeSync.class);
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
</vendor>

<change-notes><![CDATA[
v1.2.5
<ul>
<li>Fixed a problem wherein the theme would only partially update on occasion.</li>
</ul>
v1.2.4
<ul>
<li>Fixed incompatibilities with IDEs running on JetBrains Runtime 8 such as Android Studio.</li>
</ul>
v1.2.3
<ul>
<li>Added support IDEA 2020.2.</li>
Expand Down

0 comments on commit 5a90440

Please # to comment.