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

Commit a7d66f8

Browse files
committed
Consolidate the 3 SharedDictionaryManager classes
Everywhere now uses `GitHub.UI.ThemeDictionaryManager`. SharedDictionaryManager can be used as base for added functionality (e.g. ThemeDictionaryManager).
1 parent c3fd4c7 commit a7d66f8

36 files changed

+171
-195
lines changed

src/GitHub.UI/GitHub.UI.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
<Compile Include="Converters\StickieListItemConverter.cs" />
9999
<Compile Include="Helpers\LoadingResourceDictionary.cs" />
100100
<Compile Include="Helpers\ScrollViewerUtilities.cs" />
101-
<Compile Include="Helpers\SharedDictionaryManagerBase.cs" />
101+
<Compile Include="Helpers\SharedDictionaryManager.cs" />
102102
<Compile Include="Resources.Designer.cs">
103103
<AutoGen>True</AutoGen>
104104
<DesignTime>True</DesignTime>
@@ -163,7 +163,6 @@
163163
<Compile Include="Converters\VerticalOffsetToVisibilityConverter.cs" />
164164
<Compile Include="Helpers\BindingProxy.cs" />
165165
<Compile Include="Helpers\GitHubBrushes.cs" />
166-
<Compile Include="Helpers\SharedDictionaryManager.cs" />
167166
</ItemGroup>
168167
<ItemGroup>
169168
<Page Include="Assets\Controls\GitHubLinkButton.xaml">
Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,139 @@
1-
namespace GitHub.UI.Helpers
1+
using System;
2+
using System.Windows;
3+
using System.Collections.Generic;
4+
using NullGuard;
5+
6+
namespace GitHub.UI.Helpers
27
{
3-
public class SharedDictionaryManager : SharedDictionaryManagerBase
8+
public class SharedDictionaryManager : ResourceDictionary
49
{
10+
CachingFactory factory;
11+
Uri source;
12+
13+
public SharedDictionaryManager() : this(GetCurrentDomainCachingFactory())
14+
{
15+
}
16+
17+
public SharedDictionaryManager(CachingFactory factory)
18+
{
19+
this.factory = factory;
20+
}
21+
22+
public virtual new Uri Source
23+
{
24+
// Just in case the designer checks this property.
25+
[return: AllowNull]
26+
get
27+
{
28+
return source;
29+
}
30+
31+
set
32+
{
33+
source = value;
34+
35+
value = FixDesignTimeUri(value);
36+
var rd = factory.GetOrCreateResourceDictionary(this, value);
37+
MergedDictionaries.Clear();
38+
MergedDictionaries.Add(rd);
39+
}
40+
}
41+
42+
public static CachingFactory GetCurrentDomainCachingFactory()
43+
{
44+
var dataName = typeof(CachingFactory).FullName;
45+
var data = AppDomain.CurrentDomain.GetData(dataName);
46+
47+
var cachingFactory = data as CachingFactory;
48+
if(cachingFactory != null)
49+
{
50+
return cachingFactory;
51+
}
52+
53+
var disposable = data as IDisposable;
54+
if(disposable != null)
55+
{
56+
disposable.Dispose();
57+
}
58+
59+
cachingFactory = new CachingFactory();
60+
AppDomain.CurrentDomain.SetData(dataName, cachingFactory);
61+
return cachingFactory;
62+
}
63+
64+
public class CachingFactory : IDisposable
65+
{
66+
IDictionary<Uri, ResourceDictionary> sharedDictionaries;
67+
ISet<IDisposable> disposables;
68+
69+
public CachingFactory()
70+
{
71+
sharedDictionaries = new Dictionary<Uri, ResourceDictionary>();
72+
disposables = new HashSet<IDisposable>();
73+
}
74+
75+
public void Dispose()
76+
{
77+
foreach (var disposable in disposables)
78+
{
79+
disposable.Dispose();
80+
}
81+
82+
disposables.Clear();
83+
sharedDictionaries.Clear();
84+
}
85+
86+
public ResourceDictionary GetOrCreateResourceDictionary(ResourceDictionary owner, Uri uri)
87+
{
88+
TryAddDisposable(owner);
89+
90+
ResourceDictionary rd;
91+
if (!sharedDictionaries.TryGetValue(uri, out rd))
92+
{
93+
rd = new LoadingResourceDictionary { Source = uri };
94+
sharedDictionaries[uri] = rd;
95+
}
96+
97+
return rd;
98+
}
99+
100+
// Remember subtypes that need disposing of.
101+
public void TryAddDisposable(object owner)
102+
{
103+
var disposable = owner as IDisposable;
104+
if (disposable != null)
105+
{
106+
disposables.Add(disposable);
107+
}
108+
}
109+
}
110+
111+
public static Uri FixDesignTimeUri(Uri inUri)
112+
{
113+
if (inUri.Scheme != "file")
114+
{
115+
return inUri;
116+
}
117+
118+
var url = inUri.ToString();
119+
var assemblyPrefix = "/src/";
120+
var assemblyIndex = url.LastIndexOf(assemblyPrefix);
121+
if (assemblyIndex == -1)
122+
{
123+
return inUri;
124+
}
125+
126+
assemblyIndex += assemblyPrefix.Length;
127+
var pathIndex = url.IndexOf('/', assemblyIndex);
128+
if (pathIndex == -1)
129+
{
130+
return inUri;
131+
}
132+
133+
var assemblyName = url.Substring(assemblyIndex, pathIndex - assemblyIndex);
134+
var path = url.Substring(pathIndex + 1);
135+
136+
return new Uri($"pack://application:,,,/{assemblyName};component/{path}");
137+
}
5138
}
6139
}

src/GitHub.UI/Helpers/SharedDictionaryManagerBase.cs

Lines changed: 0 additions & 139 deletions
This file was deleted.

src/GitHub.VisualStudio.UI/GitHub.VisualStudio.UI.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
<Compile Include="Colors.cs" />
7575
<Compile Include="Constants.cs" />
7676
<Compile Include="Helpers\ThemeDictionaryManager.cs" />
77-
<Compile Include="Helpers\SharedDictionaryManager.cs" />
7877
<Compile Include="RepositoryOrigin.cs" />
7978
<Compile Include="Resources.Designer.cs">
8079
<DependentUpon>Resources.resx</DependentUpon>

src/GitHub.VisualStudio.UI/Helpers/SharedDictionaryManager.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/GitHub.VisualStudio.UI/Helpers/ThemeDictionaryManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace GitHub.VisualStudio.UI.Helpers
99
{
10-
public sealed class ThemeDictionaryManager : SharedDictionaryManagerBase, IDisposable
10+
public sealed class ThemeDictionaryManager : SharedDictionaryManager, IDisposable
1111
{
1212
static bool isInDesignMode;
1313
Uri baseThemeUri;

src/GitHub.VisualStudio.UI/SharedDictionary.xaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
4-
xmlns:cache="clr-namespace:GitHub.VisualStudio.UI.Helpers"
4+
xmlns:theme="clr-namespace:GitHub.VisualStudio.UI.Helpers"
5+
xmlns:cache="clr-namespace:GitHub.UI.Helpers;assembly=GitHub.UI"
56
xmlns:ui="clr-namespace:GitHub.UI;assembly=GitHub.UI">
67

78
<ResourceDictionary.MergedDictionaries>
8-
<cache:ThemeDictionaryManager Source="pack://application:,,,/GitHub.VisualStudio.UI;component/Styles/ThemeDesignTime.xaml"/>
9+
<theme:ThemeDictionaryManager Source="pack://application:,,,/GitHub.VisualStudio.UI;component/Styles/ThemeDesignTime.xaml"/>
910
<cache:SharedDictionaryManager Source="pack://application:,,,/GitHub.VisualStudio.UI;component/Styles/ActionLinkButton.xaml"/>
1011
<cache:SharedDictionaryManager Source="pack://application:,,,/GitHub.VisualStudio.UI;component/Styles/GitHubComboBox.xaml"/>
1112
<cache:SharedDictionaryManager Source="pack://application:,,,/GitHub.VisualStudio.UI;component/Styles/Buttons.xaml"/>

src/GitHub.VisualStudio.UI/Styles/GitHubComboBox.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3-
xmlns:cache="clr-namespace:GitHub.VisualStudio.UI.Helpers"
3+
xmlns:cache="clr-namespace:GitHub.UI.Helpers;assembly=GitHub.UI"
44
xmlns:ui="clr-namespace:GitHub.UI;assembly=GitHub.UI">
55

66
<ResourceDictionary.MergedDictionaries>

src/GitHub.VisualStudio.UI/Styles/LinkDropDown.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3-
xmlns:cache="clr-namespace:GitHub.VisualStudio.UI.Helpers"
3+
xmlns:cache="clr-namespace:GitHub.UI.Helpers;assembly=GitHub.UI"
44
xmlns:ui="clr-namespace:GitHub.UI;assembly=GitHub.UI">
55

66
<ResourceDictionary.MergedDictionaries>

src/GitHub.VisualStudio.UI/UI/Controls/InfoPanel.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<UserControl x:Class="GitHub.VisualStudio.UI.Controls.InfoPanel"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4-
xmlns:cache="clr-namespace:GitHub.VisualStudio.UI.Helpers"
4+
xmlns:cache="clr-namespace:GitHub.UI.Helpers;assembly=GitHub.UI"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:local="clr-namespace:GitHub.VisualStudio.UI.Controls"
77
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

0 commit comments

Comments
 (0)