Skip to content

Commit

Permalink
fix: Exception using IThemeManager#getDefaultTheme() from non-UI thread
Browse files Browse the repository at this point in the history
This addresses #721
  • Loading branch information
sebthom committed Mar 1, 2024
1 parent 74cdc9d commit e99eda0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ITheme[] getThemes() {

@Override
public ITheme getDefaultTheme() {
return getDefaultTheme(UI.isDarkEclipseTheme());
return getDefaultTheme(UI.runSync(UI::isDarkEclipseTheme));
}

@Override
Expand Down Expand Up @@ -87,7 +87,7 @@ public ITheme[] getThemes(final boolean dark) {

@Override
public ITheme getThemeForScope(final String scopeName) {
return getThemeForScope(scopeName, UI.isDarkEclipseTheme());
return getThemeForScope(scopeName, UI.runSync(UI::isDarkEclipseTheme));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
*******************************************************************************/
package org.eclipse.tm4e.ui.internal.utils;

import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Color;
Expand Down Expand Up @@ -176,6 +180,36 @@ public static boolean isDarkEclipseTheme() {
return isDarkColor(shell.getBackground());
}

public static boolean isUIThread() {
return Display.getCurrent() != null;
}

/**
* Runs the given runnable synchronously on the UI thread
*
* @throws SWTException if the {@link Display} has been disposed
*/
public static <T> T runSync(final Supplier<T> runnable) {
if (isUIThread())
return runnable.get();

final var resultRef = new AtomicReference<T>();
final var exRef = new AtomicReference<@Nullable RuntimeException>();
getDisplay().syncExec(() -> {
try {
resultRef.set(runnable.get());
} catch (final RuntimeException ex) {
exRef.set(ex);
}
});

final @Nullable RuntimeException ex = exRef.get();
if (ex != null) {
throw ex;
}
return resultRef.get();
}

private UI() {
}
}

0 comments on commit e99eda0

Please sign in to comment.