Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IllegalStateException in AbstractThemeManager.getDefaultTheme() #721

Closed
ghentschke opened this issue Mar 1, 2024 · 4 comments
Closed

IllegalStateException in AbstractThemeManager.getDefaultTheme() #721

ghentschke opened this issue Mar 1, 2024 · 4 comments

Comments

@ghentschke
Copy link

Due to a failed unit test in LSP4E it turns out that since this TM4E commit a IllegalStateException will be thrown by AbstractThemeManager.getDefaultTheme(), when this method gets called from within a non UI thread:

Exception in thread "org.eclipse.jface.text.reconciler.Reconciler" java.lang.IllegalStateException: No active shell found!
	at org.eclipse.tm4e.ui.internal.utils.UI.isDarkEclipseTheme(UI.java:175)
	at org.eclipse.tm4e.ui.internal.themes.AbstractThemeManager.getDefaultTheme(AbstractThemeManager.java:57)
	at org.eclipse.lsp4e.operations.semanticTokens.TokenTypeMapper.apply(TokenTypeMapper.java:45)
	at org.eclipse.lsp4e.operations.semanticTokens.TokenTypeMapper.apply(TokenTypeMapper.java:1)
	at org.eclipse.lsp4e.operations.semanticTokens.SemanticTokensDataStreamProcessor.textAttribute(SemanticTokensDataStreamProcessor.java:134)
	at org.eclipse.lsp4e.operations.semanticTokens.SemanticTokensDataStreamProcessor.getStyleRanges(SemanticTokensDataStreamProcessor.java:88)
	at org.eclipse.lsp4e.operations.semanticTokens.SemanticHighlightReconcilerStrategy.saveStyle(SemanticHighlightReconcilerStrategy.java:193)
	at org.eclipse.lsp4e.operations.semanticTokens.VersionedSemanticTokens.apply(VersionedSemanticTokens.java:40)
	at org.eclipse.lsp4e.operations.semanticTokens.SemanticHighlightReconcilerStrategy.lambda$5(SemanticHighlightReconcilerStrategy.java:276)
	at java.base/java.util.Optional.ifPresent(Optional.java:178)
	at org.eclipse.lsp4e.operations.semanticTokens.SemanticHighlightReconcilerStrategy.fullReconcile(SemanticHighlightReconcilerStrategy.java:275)
	at org.eclipse.lsp4e.operations.semanticTokens.SemanticHighlightReconcilerStrategy.initialReconcile(SemanticHighlightReconcilerStrategy.java:291)
	at org.eclipse.ui.internal.genericeditor.CompositeReconcilerStrategy.initialReconcile(CompositeReconcilerStrategy.java:48)
	at org.eclipse.jface.text.reconciler.Reconciler.initialProcess(Reconciler.java:223)
	at org.eclipse.jface.text.reconciler.AbstractReconciler$BackgroundThread.run(AbstractReconciler.java:177)

image

I am not sure if this should be fixed here or in the caller in LSP4E. Should the user be aware, that this method should be called from within a UI thread only?:

 TMUIPlugin.getThemeManager().getDefaultTheme().getToken(tokenType)
@sebthom
Copy link
Member

sebthom commented Mar 1, 2024

I am not sure if this should be fixed here or in the caller in LSP4E. Should the user be aware, that this method should be called from within a UI thread only?

That is nowhere specified. Most of the code in the tm4e.ui package expects to be running on the UI thread. Anyways, I added a workaround for this case. Can you please give the latest snapshot build a shot? https://download.eclipse.org/tm4e/snapshots/

@ghentschke
Copy link
Author

A first check seems very promising. @sebthom Thank you for the fast fix!

Has the default theme changed as well? TMUIPlugin.getThemeManager().getDefaultTheme() returns now the dark theme.

@sebthom
Copy link
Member

sebthom commented Mar 2, 2024

The default theme depends on the Eclipse UI theme and should only return the dark theme if Eclipse itself is dark.

This is the integration test:

final ITheme theme = manager.getDefaultTheme();
assertEquals(SolarizedLight, theme.getId());

What has changed is, that we now don't try to guess the Eclipse theme based on theme names but by measuring brightness of the active shell's background color.

private static int getBrightness(final int red, final int green, final int blue) {
// https://www.w3.org/TR/AERT/#color-contrast
return (int) (0.299 * red + 0.587 * green + 0.114 * blue);
}
public static boolean isDarkColor(final RGB color) {
return getBrightness(color.red, color.green, color.blue) < 128;
}
public static boolean isDarkColor(final Color color) {
return getBrightness(color.getRed(), color.getGreen(), color.getBlue()) < 128;
}
public static boolean isDarkEclipseTheme() {
final var shell = getActiveShell();
if (shell == null)
throw new IllegalStateException("No active shell found!");
return isDarkColor(shell.getBackground());
}

Maybe this does not work in some scenarios. It is working fine for me on Windows 10.

@sebthom
Copy link
Member

sebthom commented Mar 4, 2024

I released TM4E 0.10.3 which contains the fix. Thanks for reporting the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants