Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit a9c0614

Browse files
committed
Add theming
Any xaml file that includes a SharedDictionaryManager resource dictionary with the uri "Theme.xaml" will get a themed dictionary included instead that tracks the VS theme and gets switched at runtime whenever the VS theme changes.
1 parent 006eb83 commit a9c0614

File tree

6 files changed

+98
-5
lines changed

6 files changed

+98
-5
lines changed

src/GitHub.VisualStudio/GitHub.VisualStudio.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,18 @@
372372
<SubType>Designer</SubType>
373373
<Generator>MSBuild:Compile</Generator>
374374
</Page>
375+
<Page Include="Styles\ThemeBlue.xaml">
376+
<Generator>MSBuild:Compile</Generator>
377+
<SubType>Designer</SubType>
378+
</Page>
379+
<Page Include="Styles\ThemeLight.xaml">
380+
<Generator>MSBuild:Compile</Generator>
381+
<SubType>Designer</SubType>
382+
</Page>
383+
<Page Include="Styles\ThemeDark.xaml">
384+
<Generator>MSBuild:Compile</Generator>
385+
<SubType>Designer</SubType>
386+
</Page>
375387
<Page Include="SharedDictionary.xaml">
376388
<Generator>MSBuild:Compile</Generator>
377389
</Page>

src/GitHub.VisualStudio/Helpers/Colors.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Microsoft.VisualStudio.PlatformUI;
2+
using System;
23
using System.Windows.Media;
34

45
namespace GitHub.VisualStudio.Helpers
@@ -25,5 +26,25 @@ public static Color ToColor(this System.Drawing.Color color)
2526
{
2627
return Color.FromArgb(color.A, color.R, color.G, color.B);
2728
}
29+
30+
31+
static Color AccentMediumDarkTheme = Color.FromRgb(45, 45, 48);
32+
static Color AccentMediumLightTheme = Color.FromRgb(238, 238, 242);
33+
static Color AccentMediumBlueTheme = Color.FromRgb(255, 236, 181);
34+
35+
public static string DetectTheme()
36+
{
37+
var color = VSColorTheme.GetThemedColor(EnvironmentColors.AccentMediumColorKey);
38+
var cc = color.ToColor();
39+
if (cc == AccentMediumBlueTheme)
40+
return "Blue";
41+
if (cc == AccentMediumLightTheme)
42+
return "Light";
43+
if (cc == AccentMediumDarkTheme)
44+
return "Dark";
45+
var brightness = color.GetBrightness();
46+
var dark = brightness > 0.5f;
47+
return dark ? "Dark" : "Light";
48+
}
2849
}
2950
}

src/GitHub.VisualStudio/Helpers/SharedDictionaryManager.cs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Reflection;
66
using System.IO;
77
using System.Linq;
8+
using Microsoft.VisualStudio.PlatformUI;
89

910
namespace GitHub.VisualStudio.Helpers
1011
{
@@ -29,6 +30,11 @@ static SharedDictionaryManager()
2930
AppDomain.CurrentDomain.AssemblyResolve += LoadAssemblyFromRunDir;
3031
}
3132

33+
public SharedDictionaryManager()
34+
{
35+
currentTheme = Colors.DetectTheme();
36+
}
37+
3238
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods")]
3339
static Assembly LoadAssemblyFromRunDir(object sender, ResolveEventArgs e)
3440
{
@@ -53,24 +59,51 @@ static Assembly LoadAssemblyFromRunDir(object sender, ResolveEventArgs e)
5359

5460
#region ResourceDictionaryImplementation
5561
static readonly Dictionary<Uri, ResourceDictionary> resourceDicts = new Dictionary<Uri, ResourceDictionary>();
62+
static string baseThemeUri = "pack://application:,,,/GitHub.VisualStudio;component/Styles/";
5663

5764
Uri sourceUri;
65+
string currentTheme;
66+
bool themed = false;
5867
public new Uri Source
5968
{
6069
get { return sourceUri; }
6170
set
6271
{
72+
if (value.ToString() == "Theme.xaml")
73+
{
74+
if (!themed)
75+
{
76+
themed = true;
77+
VSColorTheme.ThemeChanged += OnThemeChange;
78+
}
79+
value = new Uri(baseThemeUri + "Theme" + currentTheme + ".xaml");
80+
}
81+
6382
sourceUri = value;
6483
ResourceDictionary ret;
6584
if (resourceDicts.TryGetValue(value, out ret))
6685
{
67-
MergedDictionaries.Add(ret);
68-
return;
86+
if (ret != this)
87+
{
88+
MergedDictionaries.Add(ret);
89+
return;
90+
}
6991
}
7092
base.Source = value;
71-
resourceDicts.Add(value, this);
93+
if (ret == null)
94+
resourceDicts.Add(value, this);
7295
}
7396
}
74-
#endregion
97+
98+
void OnThemeChange(ThemeChangedEventArgs e)
99+
{
100+
var uri = new Uri(baseThemeUri + "Theme" + currentTheme + ".xaml");
101+
ResourceDictionary ret;
102+
if (resourceDicts.TryGetValue(uri, out ret))
103+
MergedDictionaries.Remove(ret);
104+
currentTheme = Colors.DetectTheme();
105+
Source = uri;
106+
}
107+
#endregion
75108
}
76109
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<ResourceDictionary
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options">
5+
6+
<Color x:Key="GitHubContextMenuIconColor">#424242</Color>
7+
<SolidColorBrush x:Key="GitHubContextMenuIconBrush" Color="{StaticResource GitHubContextMenuIconColor}" PresentationOptions:Freeze="true" />
8+
9+
</ResourceDictionary>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<ResourceDictionary
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options">
5+
6+
<Color x:Key="GitHubContextMenuIconColor">#424242</Color>
7+
<SolidColorBrush x:Key="GitHubContextMenuIconBrush" Color="{StaticResource GitHubContextMenuIconColor}" PresentationOptions:Freeze="true" />
8+
9+
</ResourceDictionary>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<ResourceDictionary
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options">
5+
6+
<Color x:Key="GitHubContextMenuIconColor">#424242</Color>
7+
<SolidColorBrush x:Key="GitHubContextMenuIconBrush" Color="{StaticResource GitHubContextMenuIconColor}" PresentationOptions:Freeze="true" />
8+
9+
</ResourceDictionary>

0 commit comments

Comments
 (0)