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

Commit 052f25f

Browse files
committed
wip abstracting more things
1 parent 20290b8 commit 052f25f

File tree

2 files changed

+73
-38
lines changed

2 files changed

+73
-38
lines changed
Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Microsoft.VisualStudio.Shell;
22
using System;
3-
using System.ComponentModel.Composition;
43

54
namespace GitHub.Services
65
{
@@ -9,43 +8,14 @@ public interface IVSUIContextFactory
98
IVSUIContext GetUIContext(Guid contextGuid);
109
}
1110

12-
[Export(typeof(IVSUIContextFactory))]
13-
[PartCreationPolicy(CreationPolicy.Shared)]
14-
public class VSUIContextFactory : IVSUIContextFactory
11+
public interface IVSUIContextChangedEventArgs
1512
{
16-
public IVSUIContext GetUIContext(Guid contextGuid)
17-
{
18-
return new VSUIContext(UIContext.FromUIContextGuid(contextGuid));
19-
}
13+
bool Activated { get; }
2014
}
2115

2216
public interface IVSUIContext
2317
{
2418
bool IsActive { get; }
25-
event EventHandler<UIContextChangedEventArgs> UIContextChanged;
26-
}
27-
28-
public class VSUIContext : IVSUIContext
29-
{
30-
readonly UIContext context;
31-
32-
public VSUIContext(UIContext context)
33-
{
34-
this.context = context;
35-
}
36-
37-
public bool IsActive { get { return context.IsActive; } }
38-
39-
public event EventHandler<UIContextChangedEventArgs> UIContextChanged
40-
{
41-
add
42-
{
43-
context.UIContextChanged += value;
44-
}
45-
remove
46-
{
47-
context.UIContextChanged -= value;
48-
}
49-
}
19+
event EventHandler<IVSUIContextChangedEventArgs> UIContextChanged;
5020
}
5121
}

src/GitHub.TeamFoundation.14/Base/TeamExplorerServiceHolder.cs

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace GitHub.VisualStudio.Base
2121
public class TeamExplorerServiceHolder : ITeamExplorerServiceHolder
2222
{
2323
static readonly ILogger log = LogManager.ForContext<TeamExplorerServiceHolder>();
24-
readonly IVSUIContextFactory uiContextFactory;
24+
readonly IVSUIContextFactory uiContextFactory = null;
2525
readonly Dictionary<object, Action<ILocalRepositoryModel>> activeRepoHandlers = new Dictionary<object, Action<ILocalRepositoryModel>>();
2626
ILocalRepositoryModel activeRepo;
2727
bool activeRepoNotified = false;
@@ -33,9 +33,12 @@ public class TeamExplorerServiceHolder : ITeamExplorerServiceHolder
3333
// ActiveRepositories PropertyChanged event comes in on a non-main thread
3434
readonly SynchronizationContext syncContext;
3535

36-
public TeamExplorerServiceHolder(IVSUIContextFactory uiContextFactory, IVSGitExt gitService)
36+
[ImportingConstructor]
37+
//public TeamExplorerServiceHolder(IVSUIContextFactory uiContextFactory, IVSGitExt gitService)
38+
public TeamExplorerServiceHolder(IVSGitExt gitService)
3739
{
38-
this.uiContextFactory = uiContextFactory;
40+
Debugger.Break();
41+
//this.uiContextFactory = uiContextFactory;
3942
this.GitService = gitService;
4043
syncContext = SynchronizationContext.Current;
4144
}
@@ -80,7 +83,7 @@ public void Subscribe(object who, Action<ILocalRepositoryModel> handler)
8083

8184
bool notificationsExist;
8285
ILocalRepositoryModel repo;
83-
lock(activeRepoHandlers)
86+
lock (activeRepoHandlers)
8487
{
8588
repo = ActiveRepo;
8689
notificationsExist = activeRepoNotified;
@@ -142,7 +145,7 @@ void NotifyActiveRepo()
142145
}
143146
}
144147

145-
void UIContextChanged(object sender, UIContextChangedEventArgs e)
148+
void UIContextChanged(object sender, IVSUIContextChangedEventArgs e)
146149
{
147150
Guard.ArgumentNotNull(e, nameof(e));
148151

@@ -247,6 +250,68 @@ IVSGitExt GitService
247250
}
248251
}
249252

253+
[Export(typeof(IVSUIContextFactory))]
254+
[PartCreationPolicy(CreationPolicy.Shared)]
255+
public class VSUIContextFactory : IVSUIContextFactory
256+
{
257+
[ImportingConstructor]
258+
public VSUIContextFactory()
259+
{
260+
Debugger.Break();
261+
}
262+
263+
public IVSUIContext GetUIContext(Guid contextGuid)
264+
{
265+
return new VSUIContext(UIContext.FromUIContextGuid(contextGuid));
266+
}
267+
}
268+
269+
public class VSUIContextChangedEventArgs : IVSUIContextChangedEventArgs
270+
{
271+
public bool Activated { get; }
272+
273+
public VSUIContextChangedEventArgs(bool activated)
274+
{
275+
Activated = activated;
276+
}
277+
}
278+
279+
public class VSUIContext : IVSUIContext
280+
{
281+
readonly UIContext context;
282+
readonly Dictionary<EventHandler<IVSUIContextChangedEventArgs>, EventHandler<UIContextChangedEventArgs>> handlers =
283+
new Dictionary<EventHandler<IVSUIContextChangedEventArgs>, EventHandler<UIContextChangedEventArgs>>();
284+
public VSUIContext(UIContext context)
285+
{
286+
this.context = context;
287+
}
288+
289+
public bool IsActive { get { return context.IsActive; } }
290+
291+
public event EventHandler<IVSUIContextChangedEventArgs> UIContextChanged
292+
{
293+
add
294+
{
295+
EventHandler<UIContextChangedEventArgs> handler = null;
296+
if (!handlers.TryGetValue(value, out handler))
297+
{
298+
handler = (s, e) => value.Invoke(s, new VSUIContextChangedEventArgs(e.Activated));
299+
handlers.Add(value, handler);
300+
}
301+
context.UIContextChanged += handler;
302+
}
303+
remove
304+
{
305+
EventHandler<UIContextChangedEventArgs> handler = null;
306+
if (handlers.TryGetValue(value, out handler))
307+
{
308+
handlers.Remove(value);
309+
context.UIContextChanged -= handler;
310+
}
311+
}
312+
}
313+
}
314+
250315
static class IGitRepositoryInfoExtensions
251316
{
252317
/// <summary>

0 commit comments

Comments
 (0)