Skip to content

Commit

Permalink
Some thread-safety modifications in MacOSThemeDetector
Browse files Browse the repository at this point in the history
  • Loading branch information
Dansoftowner committed Apr 16, 2021
1 parent 40564b9 commit bb40a52
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/main/java/com/jthemedetecor/MacOSThemeDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ class MacOSThemeDetector extends OsThemeDetector {

private final Set<Consumer<Boolean>> listeners = Collections.synchronizedSet(new HashSet<>());
private final Pattern themeNamePattern = Pattern.compile(".*dark.*", Pattern.CASE_INSENSITIVE);
private final Object iterateLock = new Object();

private final Callback themeChangedCallback = new Callback() {
@SuppressWarnings("unused")
public void callback() {
notifyListeners(isDark());
synchronized (iterateLock) {
notifyListeners(isDark());
}
}
};

Expand Down Expand Up @@ -97,12 +100,16 @@ private boolean isDarkTheme(String themeName) {

@Override
public void registerListener(@NotNull Consumer<Boolean> darkThemeListener) {
listeners.add(darkThemeListener);
synchronized (iterateLock) {
listeners.add(darkThemeListener);
}
}

@Override
public void removeListener(@Nullable Consumer<Boolean> darkThemeListener) {
listeners.remove(darkThemeListener);
synchronized (iterateLock) {
listeners.remove(darkThemeListener);
}
}

private void notifyListeners(boolean isDark) {
Expand Down

0 comments on commit bb40a52

Please sign in to comment.