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

Commit 0ea5701

Browse files
committed
Merge pull request #112 from github/feature/pr/menu-refactoring
Turn menu creation methods into extension methods
2 parents 6061741 + cc671bf commit 0ea5701

File tree

6 files changed

+59
-68
lines changed

6 files changed

+59
-68
lines changed

src/GitHub.Exports/Extensions/VSExtensions.cs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
using System;
2+
using System.ComponentModel.Design;
23
using System.Diagnostics;
4+
using GitHub.Primitives;
35
using GitHub.Services;
6+
using LibGit2Sharp;
47
using Microsoft.TeamFoundation.Controls;
8+
using Microsoft.VisualStudio;
9+
using Microsoft.VisualStudio.Shell;
10+
using Microsoft.VisualStudio.Shell.Interop;
511

612
namespace GitHub.Extensions
713
{
8-
public static class VSExtensions
14+
public static class IServiceProviderExtensions
915
{
1016
static IUIProvider cachedUIProvider = null;
1117

@@ -75,5 +81,54 @@ static T GetExportedValueAndCache<T, CacheType>(ref CacheType cache)
7581
cache = (CacheType)(object)ret;
7682
return ret;
7783
}
84+
85+
public static void AddTopLevelMenuItem(this IServiceProvider provider,
86+
Guid guid,
87+
int cmdId,
88+
EventHandler eventHandler)
89+
{
90+
var mcs = provider.GetService(typeof(IMenuCommandService)) as IMenuCommandService;
91+
Debug.Assert(mcs != null, "No IMenuCommandService? Something is wonky");
92+
if (mcs == null)
93+
return;
94+
var id = new CommandID(guid, cmdId);
95+
var item = new MenuCommand(eventHandler, id);
96+
mcs.AddCommand(item);
97+
}
98+
99+
public static void AddDynamicMenuItem(this IServiceProvider provider,
100+
Guid guid,
101+
int cmdId,
102+
Func<bool> canEnable,
103+
Action execute)
104+
{
105+
var mcs = provider.GetService(typeof(IMenuCommandService)) as IMenuCommandService;
106+
Debug.Assert(mcs != null, "No IMenuCommandService? Something is wonky");
107+
if (mcs == null)
108+
return;
109+
var id = new CommandID(guid, cmdId);
110+
var item = new OleMenuCommand(
111+
(s, e) => execute(),
112+
(s, e) => { },
113+
(s, e) =>
114+
{
115+
((OleMenuCommand)s).Visible = canEnable();
116+
},
117+
id);
118+
mcs.AddCommand(item);
119+
}
120+
}
121+
122+
public static class ISolutionExtensions
123+
{
124+
public static IRepository GetRepoFromSolution(this IVsSolution solution)
125+
{
126+
string solutionDir, solutionFile, userFile;
127+
if (!ErrorHandler.Succeeded(solution.GetSolutionInfo(out solutionDir, out solutionFile, out userFile)))
128+
return null;
129+
if (solutionDir == null)
130+
return null;
131+
return GitService.GitServiceHelper.GetRepo(solutionDir);
132+
}
78133
}
79134
}

src/GitHub.Exports/Services/Services.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,5 @@ public static UriString GetRepoUrlFromSolution(IVsSolution solution)
100100
return null;
101101
return GitService.GitServiceHelper.GetUri(solutionDir);
102102
}
103-
104-
public static IRepository GetRepoFromSolution(this IVsSolution solution)
105-
{
106-
string solutionDir, solutionFile, userFile;
107-
if (!ErrorHandler.Succeeded(solution.GetSolutionInfo(out solutionDir, out solutionFile, out userFile)))
108-
return null;
109-
if (solutionDir == null)
110-
return null;
111-
return GitService.GitServiceHelper.GetRepo(solutionDir);
112-
}
113103
}
114104
}

src/GitHub.VisualStudio/Base/PackageBase.cs

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -24,58 +24,5 @@ protected PackageBase(IServiceProvider serviceProvider)
2424
{
2525
ServiceProvider = serviceProvider;
2626
}
27-
28-
protected void AddTopLevelMenuItem(
29-
Guid guid,
30-
uint packageCommandId,
31-
EventHandler eventHandler)
32-
{
33-
var mcs = GetService(typeof(IMenuCommandService)) as IMenuCommandService;
34-
Debug.Assert(mcs != null, "No IMenuCommandService? Something is wonky");
35-
if (mcs == null)
36-
return;
37-
var cmdId = new CommandID(guid, (int)packageCommandId);
38-
var item = new MenuCommand(eventHandler, cmdId);
39-
mcs.AddCommand(item);
40-
}
41-
42-
protected void AddDynamicMenuItem(
43-
Guid guid,
44-
uint id,
45-
Func<bool> canEnable,
46-
Action execute)
47-
{
48-
var mcs = GetService(typeof(IMenuCommandService)) as IMenuCommandService;
49-
Debug.Assert(mcs != null, "No IMenuCommandService? Something is wonky");
50-
if (mcs == null)
51-
return;
52-
var cmdId = new CommandID(guid, (int)id);
53-
var item = new OleMenuCommand(
54-
(s, e) => execute(),
55-
(s, e) => { },
56-
(s, e) =>
57-
{
58-
((OleMenuCommand)s).Visible = canEnable();
59-
},
60-
cmdId);
61-
mcs.AddCommand(item);
62-
}
63-
64-
public T GetService<T>()
65-
{
66-
Debug.Assert(this.serviceProvider != null, "GetService<T> called before service provider is set");
67-
if (serviceProvider == null)
68-
return default(T);
69-
return (T)serviceProvider.GetService(typeof(T));
70-
}
71-
72-
public T GetExportedValue<T>()
73-
{
74-
var componentModel = (IComponentModel)GetService<SComponentModel>();
75-
if (componentModel == null)
76-
return default(T);
77-
var exportProvider = componentModel.DefaultExportProvider;
78-
return exportProvider.GetExportedValue<T>();
79-
}
8027
}
8128
}

src/GitHub.VisualStudio/GitHub.VisualStudio.vsct

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
<IDSymbol name="GitHubMenu" value="0x1010"/>
6565
<IDSymbol name="idGitHubMenuGroup" value="0x1020"/>
6666
<IDSymbol name="addConnectionCommand" value="0x110"/>
67-
<IDSymbol name="addConnectionCommandDark" value="0x111"/>
6867
</GuidSymbol>
6968

7069
<!-- This is the Manage Connections menu -->

src/GitHub.VisualStudio/GitHubPackage.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ public GitHubPackage(IServiceProvider serviceProvider)
4747

4848
protected override void Initialize()
4949
{
50+
5051
base.Initialize();
5152

52-
AddTopLevelMenuItem(GuidList.guidGitHubCmdSet, PkgCmdIDList.addConnectionCommand, (s, e) => StartFlow(UIControllerFlow.Authentication));
53+
ServiceProvider.AddTopLevelMenuItem(GuidList.guidGitHubCmdSet, PkgCmdIDList.addConnectionCommand, (s, e) => StartFlow(UIControllerFlow.Authentication));
5354
}
5455

5556
void StartFlow(UIControllerFlow controllerFlow)

src/GitHub.VisualStudio/PkgCmdID.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ namespace GitHub.VisualStudio
44
{
55
static class PkgCmdIDList
66
{
7-
public const uint addConnectionCommand = 0x110;
8-
public const uint addConnectionCommandDark = 0x111;
7+
public const int addConnectionCommand = 0x110;
98
};
109
}

0 commit comments

Comments
 (0)