diff --git a/src/Cody.Core/Infrastructure/IColorThemeService.cs b/src/Cody.Core/Infrastructure/IColorThemeService.cs index d5906bc0..7501a301 100644 --- a/src/Cody.Core/Infrastructure/IColorThemeService.cs +++ b/src/Cody.Core/Infrastructure/IColorThemeService.cs @@ -6,10 +6,27 @@ namespace Cody.Core.Infrastructure { - public interface IColorThemeService + public interface IThemeService { bool IsDarkTheme(); - IReadOnlyDictionary GetThemedColors(); + IReadOnlyDictionary GetColors(); + + FontInformation GetEditorFont(); + + FontInformation GetUIFont(); + } + + public class FontInformation + { + public FontInformation(string fontName, float size) + { + FontName = fontName; + Size = size; + } + + public string FontName { get; protected set; } + + public float Size { get; protected set; } } } diff --git a/src/Cody.VisualStudio/Cody.VisualStudio.csproj b/src/Cody.VisualStudio/Cody.VisualStudio.csproj index c3fc7ea0..339f1fec 100644 --- a/src/Cody.VisualStudio/Cody.VisualStudio.csproj +++ b/src/Cody.VisualStudio/Cody.VisualStudio.csproj @@ -59,7 +59,7 @@ Component - + diff --git a/src/Cody.VisualStudio/CodyPackage.cs b/src/Cody.VisualStudio/CodyPackage.cs index 71765dad..88a2b7f5 100644 --- a/src/Cody.VisualStudio/CodyPackage.cs +++ b/src/Cody.VisualStudio/CodyPackage.cs @@ -64,7 +64,7 @@ public sealed class CodyPackage : AsyncPackage public IUserSettingsService UserSettingsService; public InitializeCallback InitializeService; public IStatusbarService StatusbarService; - public IColorThemeService ColorThemeService; + public IThemeService ThemeService; public NotificationHandlers NotificationHandlers; public IVsEditorAdaptersFactoryService VsEditorAdaptersFactoryService; public IVsUIShell VsUIShell; @@ -102,7 +102,7 @@ private void InitializeServices() UserSettingsService = new UserSettingsService(new UserSettingsProvider(this), Logger); StatusbarService = new StatusbarService(); InitializeService = new InitializeCallback(UserSettingsService, VersionService, VsVersionService, StatusbarService, Logger); - ColorThemeService = new ColorThemeService(this); + ThemeService = new ThemeService(this); var runningDocumentTable = this.GetService(); var componentModel = this.GetService(); diff --git a/src/Cody.VisualStudio/Services/ColorThemeService.cs b/src/Cody.VisualStudio/Services/ColorThemeService.cs deleted file mode 100644 index d0852d03..00000000 --- a/src/Cody.VisualStudio/Services/ColorThemeService.cs +++ /dev/null @@ -1,70 +0,0 @@ -using Cody.Core.Infrastructure; -using Microsoft.VisualStudio.PlatformUI; -using Microsoft.VisualStudio.Settings; -using Microsoft.VisualStudio.Shell; -using Microsoft.VisualStudio.Shell.Settings; -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Cody.VisualStudio.Services -{ - public class ColorThemeService : IColorThemeService - { - private ThemeResourceKey[] colorsList = new ThemeResourceKey[] - { - EnvironmentColors.ToolWindowBackgroundColorKey, - EnvironmentColors.ToolWindowTextColorKey, - EnvironmentColors.ToolWindowBorderColorKey, - }; - - private IServiceProvider serviceProvider; - - public ColorThemeService(IServiceProvider serviceProvider) - { - this.serviceProvider = serviceProvider; - } - - public IReadOnlyDictionary GetThemedColors() - { - var result = new Dictionary(); - foreach (var colorKey in colorsList) - { - var color = VSColorTheme.GetThemedColor(colorKey); - result.Add(ToCssVariableName(colorKey.Name), ToCssColor(color)); - } - - return result; - } - - public bool IsDarkTheme() - { - const string darkTheme = "{1ded0138-47ce-435e-84ef-9ec1f439b749}"; - const string systemTheme = "{619dac1e-8220-4bd9-96fb-75ceb61a6107}"; - - var settingsManager = new ShellSettingsManager(serviceProvider); - var store = settingsManager.GetReadOnlySettingsStore(SettingsScope.UserSettings); - var themeId = store.GetString("Theme", "BackupThemeId"); - - if(themeId == darkTheme) return true; - else if(themeId == systemTheme) - { - var colorMode = (int)Microsoft.Win32.Registry.GetValue( - @"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize", - "AppsUseLightTheme", - -1); - - if(colorMode == 0) return true; - } - - return false; - } - - private string ToCssColor(Color color) => $"rgb({color.R}, {color.G}, {color.B}, {color.A / 255f})"; - - private string ToCssVariableName(string name) => $"--visualstudio-{name.ToLower()}"; - } -} diff --git a/src/Cody.VisualStudio/Services/ThemeService.cs b/src/Cody.VisualStudio/Services/ThemeService.cs new file mode 100644 index 00000000..fc01488a --- /dev/null +++ b/src/Cody.VisualStudio/Services/ThemeService.cs @@ -0,0 +1,121 @@ +using Cody.Core.Infrastructure; +using Microsoft.VisualStudio.PlatformUI; +using Microsoft.VisualStudio.Settings; +using Microsoft.VisualStudio.Shell; +using Microsoft.VisualStudio.Shell.Interop; +using Microsoft.VisualStudio.Shell.Settings; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Drawing; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Cody.VisualStudio.Services +{ + public class ThemeService : IThemeService + { + private ThemeResourceKey[] colorsList = new ThemeResourceKey[] + { + EnvironmentColors.ToolWindowBackgroundColorKey, + EnvironmentColors.ToolWindowTextColorKey, + EnvironmentColors.ToolWindowBorderColorKey, + }; + + private IServiceProvider serviceProvider; + + public ThemeService(IServiceProvider serviceProvider) + { + this.serviceProvider = serviceProvider; + } + + public IReadOnlyDictionary GetColors() + { + var result = new Dictionary(); + foreach (var colorKey in colorsList) + { + var color = VSColorTheme.GetThemedColor(colorKey); + result.Add(ToCssVariableName(colorKey.Name), ToCssColor(color)); + } + + return result; + } + + public FontInformation GetEditorFont() + { + const string textEditorCategory = "{A27B4E24-A735-4D1D-B8E7-9716E1E3D8E0}"; + return GetFontInfo(new Guid(textEditorCategory)); + } + + public FontInformation GetUIFont() + { + const string environmentCategory = "{1F987C00-E7C4-4869-8A17-23FD602268B0}"; + return GetFontInfo(new Guid(environmentCategory)); + } + + private FontInformation GetFontInfo(Guid categoryGuid) + { + var storage = (IVsFontAndColorStorage)serviceProvider.GetService(typeof(SVsFontAndColorStorage)); + storage.OpenCategory(categoryGuid, (uint)(__FCSTORAGEFLAGS.FCSF_LOADDEFAULTS | __FCSTORAGEFLAGS.FCSF_READONLY)); + + var logFont = new LOGFONTW[1]; + var pInfo = new FontInfo[1]; + storage.GetFont(logFont, pInfo); + + var result = new FontInformation(pInfo[0].bstrFaceName, pInfo[0].wPointSize); + + storage.CloseCategory(); + + return result; + } + + public bool IsDarkTheme() + { + const string darkTheme = "{1ded0138-47ce-435e-84ef-9ec1f439b749}"; + const string systemTheme = "{619dac1e-8220-4bd9-96fb-75ceb61a6107}"; + + var settingsManager = new ShellSettingsManager(serviceProvider); + var store = settingsManager.GetReadOnlySettingsStore(SettingsScope.UserSettings); + var themeId = store.GetString("Theme", "BackupThemeId"); + + if (themeId == darkTheme) return true; + else if (themeId == systemTheme) + { + var colorMode = (int)Microsoft.Win32.Registry.GetValue( + @"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize", + "AppsUseLightTheme", + -1); + + if (colorMode == 0) return true; + } + + return false; + } + + private string ToCssColor(Color color) => $"rgb({color.R}, {color.G}, {color.B}, {color.A / 255f})"; + + private string ToCssVariableName(string name) => $"--visualstudio-{name.ToLower()}"; + + [Conditional("DEBUG")] + public static void GetAllColors() + { + var list = new List(); + var properties = typeof(EnvironmentColors).GetProperties(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public); + foreach (var property in properties) + { + if (property.Name.EndsWith("ColorKey")) + { + var value = (ThemeResourceKey)property.GetValue(null); + var color = VSColorTheme.GetThemedColor(value); + var line = $"{value.Name}\t{color.R}\t{color.G}\t{color.B}\t{color.A / 255f}"; + + list.Add(line); + } + } + + File.WriteAllLines("colors.txt", list); + } + } +}