VsixCommunity/Community.VisualStudio.Toolkit

Get the users current theme programatically (VS2019-2022)

Tim-Maes opened this issue · 5 comments

Hi,

I maintain a VSIX extension that provides syntax highlighting provided through classifiers. When running the extensions for the first time I ask the user what VS theme they are currently using and apply colors based on their input.

Now I want to do this automatically without user input - get the users current theme (Light/Dark). I found some online information about this but it seems deprecated or outdated - or I'm missing something

How can this be achieved?

Thanks

If you can get the editor background color, you can then figure out if you need to use light or dark colors using this utility function:

ContrastComparisonResult contrast = ColorUtilities.CompareContrastWithBlackAndWhite(brush.Color);

Now I want to do this automatically without user input - get the users current theme (Light/Dark). I found some online information about this but it seems deprecated or outdated - or I'm missing something

Defining a color category with color definitions might be able to handle this, and would allow users to modify the colors.

Take a look at #414 (yet to be merged).

If you used "automatic" color that was backed by a VisualStudioColor, then it should handle selecting a color that's suitable for the current light/dark theme.

Also note that there are more theme options than just light and dark.
VS itself provides Blue & Blue (High Contrast)
And, obviously, there can be custom ones as well. Don't only support dark&light as this frequently (I use a blue high contrast theme and so have seen this many times) leads to some things not showing up correctly for anyone who uses something else.

I'm not sure if this fits your problem, but in using Microsoft.VisualStudio.PlatformUI; there is VSColorTheme.

You can listen on

    VSColorTheme.ThemeChanged += VSColorTheme_ThemeChanged;

and easily read some variables in there via

private void VSColorTheme_ThemeChanged(ThemeChangedEventArgs e)
{
    var themedColor = VSColorTheme.GetThemedColor(EnvironmentColors.ToolWindowBackgroundColorKey);
}

@Matt-17 That helped, thanks!