VsixCommunity / Community.VisualStudio.Toolkit

Making it easier to write Visual Studio extensions
Other
256 stars 44 forks source link

Get the users current theme programatically (VS2019-2022) #460

Closed Tim-Maes closed 10 months ago

Tim-Maes commented 1 year ago

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

madskristensen commented 1 year ago

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);
reduckted commented 1 year ago

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.

mrlacey commented 12 months ago

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.

Matt-17 commented 10 months ago

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);
}
Tim-Maes commented 10 months ago

@Matt-17 That helped, thanks!