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

Commit db3a26b

Browse files
authored
Merge pull request #950 from github/jcansdale/xaml-designer
Make `SharedDictionaryManager` work inside XAML Designer
2 parents 90ae092 + bf3e472 commit db3a26b

File tree

51 files changed

+631
-481
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+631
-481
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<ProjectConfiguration>
2+
<Settings>
3+
<CopyReferencedAssembliesToWorkspace>True</CopyReferencedAssembliesToWorkspace>
4+
<PreviouslyBuiltSuccessfully>True</PreviouslyBuiltSuccessfully>
5+
</Settings>
6+
</ProjectConfiguration>

GitHubVS.sln

Lines changed: 0 additions & 116 deletions
Large diffs are not rendered by default.

src/DesignTimeStyleHelper/DesignTimeStyleHelper.csproj

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,6 @@
3535
<WarningLevel>4</WarningLevel>
3636
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
3737
</PropertyGroup>
38-
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'XamlDesign|AnyCPU'">
39-
<PlatformTarget>AnyCPU</PlatformTarget>
40-
<DebugSymbols>true</DebugSymbols>
41-
<DebugType>full</DebugType>
42-
<Optimize>false</Optimize>
43-
<DefineConstants>DEBUG;TRACE;XAML_DESIGNER</DefineConstants>
44-
<ErrorReport>prompt</ErrorReport>
45-
<WarningLevel>4</WarningLevel>
46-
</PropertyGroup>
4738
<ItemGroup>
4839
<Reference Include="Microsoft.VisualStudio.ComponentModelHost, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
4940
<HintPath>..\..\packages\VSSDK.ComponentModelHost.12.0.4\lib\net45\Microsoft.VisualStudio.ComponentModelHost.dll</HintPath>

src/GitHub.UI.Reactive/GitHub.UI.Reactive.csproj

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,6 @@
4545
<CodeAnalysisIgnoreGeneratedCode>true</CodeAnalysisIgnoreGeneratedCode>
4646
<CodeAnalysisRuleSet>..\common\GitHubVS.ruleset</CodeAnalysisRuleSet>
4747
</PropertyGroup>
48-
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'XamlDesign|AnyCPU'">
49-
<DebugSymbols>true</DebugSymbols>
50-
<DebugType>full</DebugType>
51-
<Optimize>false</Optimize>
52-
<DefineConstants>DEBUG;TRACE;XAML_DESIGNER</DefineConstants>
53-
<ErrorReport>prompt</ErrorReport>
54-
<WarningLevel>4</WarningLevel>
55-
<RunCodeAnalysis>false</RunCodeAnalysis>
56-
<CodeAnalysisRuleSet>..\common\GitHubVS.ruleset</CodeAnalysisRuleSet>
57-
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
58-
<CodeAnalysisIgnoreGeneratedCode>true</CodeAnalysisIgnoreGeneratedCode>
59-
</PropertyGroup>
6048
<PropertyGroup Condition="$(Buildtype) == 'Internal'">
6149
<AssemblyOriginatorKeyFile>..\..\script\Key.snk</AssemblyOriginatorKeyFile>
6250
<SignAssembly>true</SignAssembly>

src/GitHub.UI/GitHub.UI.csproj

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,6 @@
4141
<CodeAnalysisIgnoreGeneratedCode>true</CodeAnalysisIgnoreGeneratedCode>
4242
<CodeAnalysisRuleSet>..\common\GitHubVS.ruleset</CodeAnalysisRuleSet>
4343
</PropertyGroup>
44-
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'XamlDesign|AnyCPU'">
45-
<DebugSymbols>true</DebugSymbols>
46-
<DebugType>full</DebugType>
47-
<Optimize>false</Optimize>
48-
<DefineConstants>DEBUG;TRACE;XAML_DESIGNER</DefineConstants>
49-
<ErrorReport>prompt</ErrorReport>
50-
<WarningLevel>4</WarningLevel>
51-
<RunCodeAnalysis>false</RunCodeAnalysis>
52-
<CodeAnalysisRuleSet>..\common\GitHubVS.ruleset</CodeAnalysisRuleSet>
53-
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
54-
<CodeAnalysisIgnoreGeneratedCode>true</CodeAnalysisIgnoreGeneratedCode>
55-
</PropertyGroup>
5644
<PropertyGroup Condition="$(Buildtype) == 'Internal'">
5745
<AssemblyOriginatorKeyFile>..\..\script\Key.snk</AssemblyOriginatorKeyFile>
5846
<SignAssembly>true</SignAssembly>
@@ -110,6 +98,7 @@
11098
<Compile Include="Converters\StickieListItemConverter.cs" />
11199
<Compile Include="Helpers\LoadingResourceDictionary.cs" />
112100
<Compile Include="Helpers\ScrollViewerUtilities.cs" />
101+
<Compile Include="Helpers\SharedDictionaryManager.cs" />
113102
<Compile Include="Resources.Designer.cs">
114103
<AutoGen>True</AutoGen>
115104
<DesignTime>True</DesignTime>
@@ -174,7 +163,6 @@
174163
<Compile Include="Converters\VerticalOffsetToVisibilityConverter.cs" />
175164
<Compile Include="Helpers\BindingProxy.cs" />
176165
<Compile Include="Helpers\GitHubBrushes.cs" />
177-
<Compile Include="Helpers\SharedDictionaryManager.cs" />
178166
</ItemGroup>
179167
<ItemGroup>
180168
<Page Include="Assets\Controls\GitHubLinkButton.xaml">

src/GitHub.UI/Helpers/LoadingResourceDictionary.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ namespace GitHub
99
{
1010
public class LoadingResourceDictionary : ResourceDictionary
1111
{
12-
#if !XAML_DESIGNER // XAML Designer doesn't work if `Source` property has been replaced.
1312
static Dictionary<string, Assembly> assemblyDicts = new Dictionary<string, Assembly>();
1413

1514
public new Uri Source
@@ -72,6 +71,5 @@ static string FindAssemblyNameFromPackUri(Uri packUri)
7271

7372
return path.Substring(1, componentIndex - 1);
7473
}
75-
#endif
7674
}
7775
}
Lines changed: 135 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,151 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Windows;
3+
using System.Collections.Generic;
4+
using NullGuard;
45

56
namespace GitHub.UI.Helpers
67
{
7-
public class SharedDictionaryManager : LoadingResourceDictionary
8+
public class SharedDictionaryManager : ResourceDictionary
89
{
9-
#if !XAML_DESIGNER // XAML Designer doesn't work if `Source` property has been replaced.
10-
static readonly Dictionary<Uri, ResourceDictionary> resourceDicts = new Dictionary<Uri, ResourceDictionary>();
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+
}
1121

12-
Uri sourceUri;
13-
public new Uri Source
22+
public virtual new Uri Source
1423
{
15-
get { return sourceUri; }
24+
// Just in case the designer checks this property.
25+
[return: AllowNull]
26+
get
27+
{
28+
return source;
29+
}
30+
1631
set
1732
{
18-
sourceUri = value;
19-
ResourceDictionary ret;
20-
if (resourceDicts.TryGetValue(value, out ret))
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 ResourceDictionary GetOrCreateResourceDictionary(ResourceDictionary owner, Uri uri)
76+
{
77+
TryAddDisposable(owner);
78+
79+
ResourceDictionary rd;
80+
if (!sharedDictionaries.TryGetValue(uri, out rd))
81+
{
82+
rd = new LoadingResourceDictionary { Source = uri };
83+
sharedDictionaries[uri] = rd;
84+
}
85+
86+
return rd;
87+
}
88+
89+
// Remember subtypes that need disposing of.
90+
public void TryAddDisposable(object owner)
91+
{
92+
var disposable = owner as IDisposable;
93+
if (disposable != null)
2194
{
22-
MergedDictionaries.Add(ret);
23-
return;
95+
disposables.Add(disposable);
2496
}
25-
base.Source = value;
26-
resourceDicts.Add(value, this);
2797
}
98+
99+
bool disposed;
100+
void Dispose(bool disposing)
101+
{
102+
if (disposed) return;
103+
if (disposing)
104+
{
105+
disposed = true;
106+
foreach (var disposable in disposables)
107+
{
108+
disposable.Dispose();
109+
}
110+
111+
disposables.Clear();
112+
sharedDictionaries.Clear();
113+
}
114+
}
115+
116+
public void Dispose()
117+
{
118+
Dispose(true);
119+
GC.SuppressFinalize(this);
120+
}
121+
}
122+
123+
public static Uri FixDesignTimeUri(Uri inUri)
124+
{
125+
if (inUri.Scheme != "file")
126+
{
127+
return inUri;
128+
}
129+
130+
var url = inUri.ToString();
131+
var assemblyPrefix = "/src/";
132+
var assemblyIndex = url.LastIndexOf(assemblyPrefix);
133+
if (assemblyIndex == -1)
134+
{
135+
return inUri;
136+
}
137+
138+
assemblyIndex += assemblyPrefix.Length;
139+
var pathIndex = url.IndexOf('/', assemblyIndex);
140+
if (pathIndex == -1)
141+
{
142+
return inUri;
143+
}
144+
145+
var assemblyName = url.Substring(assemblyIndex, pathIndex - assemblyIndex);
146+
var path = url.Substring(pathIndex + 1);
147+
148+
return new Uri($"pack://application:,,,/{assemblyName};component/{path}");
28149
}
29-
#endif
30150
}
31151
}

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,6 @@
3434
<DefineConstants>TRACE</DefineConstants>
3535
<ErrorReport>prompt</ErrorReport>
3636
</PropertyGroup>
37-
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'XamlDesign|AnyCPU'">
38-
<DebugSymbols>true</DebugSymbols>
39-
<DebugType>full</DebugType>
40-
<Optimize>false</Optimize>
41-
<DefineConstants>TRACE;DEBUG;CODE_ANALYSIS;XAML_DESIGNER</DefineConstants>
42-
<ErrorReport>prompt</ErrorReport>
43-
<CreateVsixContainer>True</CreateVsixContainer>
44-
<DeployExtension>True</DeployExtension>
45-
<IncludeDebugSymbolsInVSIXContainer>true</IncludeDebugSymbolsInVSIXContainer>
46-
</PropertyGroup>
4737
<PropertyGroup Condition="$(Buildtype) == 'Internal'">
4838
<AssemblyOriginatorKeyFile>..\..\script\Key.snk</AssemblyOriginatorKeyFile>
4939
<SignAssembly>true</SignAssembly>
@@ -83,7 +73,7 @@
8373
<Compile Include="Base\TeamExplorerItemBase.cs" />
8474
<Compile Include="Colors.cs" />
8575
<Compile Include="Constants.cs" />
86-
<Compile Include="Helpers\SharedDictionaryManager.cs" />
76+
<Compile Include="Helpers\ThemeDictionaryManager.cs" />
8777
<Compile Include="RepositoryOrigin.cs" />
8878
<Compile Include="Resources.Designer.cs">
8979
<DependentUpon>Resources.resx</DependentUpon>

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

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

0 commit comments

Comments
 (0)