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

Retrieving the font name and its size from VS settings #30

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/Cody.Core/Infrastructure/IColorThemeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,25 @@

namespace Cody.Core.Infrastructure
{
public interface IColorThemeService
public interface IThemeService
{
bool IsDarkTheme();

IReadOnlyDictionary<string, string> GetThemedColors();
IReadOnlyDictionary<string, string> GetColors();

FontInformation GetFont();
}

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; }
}
}
2 changes: 1 addition & 1 deletion src/Cody.VisualStudio/Cody.VisualStudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<Compile Include="Services\OptionsPage.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Services\ColorThemeService.cs" />
<Compile Include="Services\ThemeService.cs" />
<Compile Include="Services\StatusbarService.cs" />
<Compile Include="Services\UserSettingsProvider.cs" />
<Compile Include="Services\VsVersionService.cs" />
Expand Down
4 changes: 2 additions & 2 deletions src/Cody.VisualStudio/CodyPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<SVsRunningDocumentTable, IVsRunningDocumentTable>();
var componentModel = this.GetService<SComponentModel, IComponentModel>();
Expand Down
70 changes: 0 additions & 70 deletions src/Cody.VisualStudio/Services/ColorThemeService.cs

This file was deleted.

117 changes: 117 additions & 0 deletions src/Cody.VisualStudio/Services/ThemeService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
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<string, string> GetColors()
{
var result = new Dictionary<string, string>();
foreach (var colorKey in colorsList)
{
var color = VSColorTheme.GetThemedColor(colorKey);
result.Add(ToCssVariableName(colorKey.Name), ToCssColor(color));
}

return result;
}

public FontInformation GetFont()
{
const string environmentCategory = "{1F987C00-E7C4-4869-8A17-23FD602268B0}";

var storage = (IVsFontAndColorStorage)serviceProvider.GetService(typeof(SVsFontAndColorStorage));
storage.OpenCategory(new Guid(environmentCategory), (uint)(__FCSTORAGEFLAGS.FCSF_READONLY));

FontInformation uIFont;
var logFont = new LOGFONTW[1];
var pInfo = new FontInfo[1];
storage.GetFont(logFont , pInfo);
if (pInfo[0].bFaceNameValid == 1 && pInfo[0].bPointSizeValid == 1)
uIFont = new FontInformation(pInfo[0].bstrFaceName, pInfo[0].wPointSize);
else
{
var systemFont = SystemFonts.CaptionFont;
uIFont = new FontInformation(systemFont.Name, systemFont.SizeInPoints);
}

storage.CloseCategory();

return uIFont;
}

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<string>();
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);
}
}
}
Loading