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

Commit c8152df

Browse files
authored
Merge pull request #382 from github/backport/context-menu-visibility
[Backport] Context menu visibility
2 parents 04d89a2 + f53776a commit c8152df

File tree

5 files changed

+71
-18
lines changed

5 files changed

+71
-18
lines changed

src/GitHub.VisualStudio/Base/MenuBase.cs

+46-2
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,50 @@
33
using GitHub.Extensions;
44
using GitHub.Models;
55
using GitHub.Services;
6+
using System.Threading.Tasks;
7+
using GitHub.Api;
8+
using NullGuard;
9+
using System.Diagnostics;
10+
using GitHub.Primitives;
611

712
namespace GitHub.VisualStudio
813
{
914
public abstract class MenuBase
1015
{
1116
readonly IServiceProvider serviceProvider;
17+
readonly ISimpleApiClientFactory apiFactory;
18+
1219
protected IServiceProvider ServiceProvider { get { return serviceProvider; } }
1320

1421
protected ISimpleRepositoryModel ActiveRepo { get; private set; }
1522

23+
protected ISimpleApiClient simpleApiClient;
24+
25+
[AllowNull]
26+
protected ISimpleApiClient SimpleApiClient
27+
{
28+
[return: AllowNull]
29+
get { return simpleApiClient; }
30+
set
31+
{
32+
if (simpleApiClient != value && value == null)
33+
apiFactory.ClearFromCache(simpleApiClient);
34+
simpleApiClient = value;
35+
}
36+
}
37+
38+
protected ISimpleApiClientFactory ApiFactory => apiFactory;
39+
1640
protected MenuBase()
1741
{
1842
}
1943

44+
protected MenuBase(IServiceProvider serviceProvider, ISimpleApiClientFactory apiFactory)
45+
{
46+
this.serviceProvider = serviceProvider;
47+
this.apiFactory = apiFactory;
48+
}
49+
2050
protected MenuBase(IServiceProvider serviceProvider)
2151
{
2252
this.serviceProvider = serviceProvider;
@@ -41,10 +71,24 @@ void RefreshRepo()
4171
}
4272
}
4373

44-
protected bool IsGitHubRepo()
74+
protected async Task<bool> IsGitHubRepo()
4575
{
4676
RefreshRepo();
47-
return ActiveRepo?.CloneUrl?.RepositoryName != null;
77+
78+
var uri = ActiveRepo.CloneUrl;
79+
if (uri == null)
80+
return false;
81+
82+
Debug.Assert(apiFactory != null, "apiFactory cannot be null. Did you call the right constructor?");
83+
SimpleApiClient = apiFactory.Create(uri);
84+
85+
var isdotcom = HostAddress.IsGitHubDotComUri(uri.ToRepositoryUrl());
86+
if (!isdotcom)
87+
{
88+
var repo = await SimpleApiClient.GetRepository();
89+
return (repo.FullName == ActiveRepo.Name || repo.Id == 0) && SimpleApiClient.IsEnterprise();
90+
}
91+
return isdotcom;
4892
}
4993
}
5094
}

src/GitHub.VisualStudio/Menus/AddConnection.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
using GitHub.Services;
55
using GitHub.UI;
66
using GitHub.Extensions;
7+
using GitHub.Api;
78

89
namespace GitHub.VisualStudio.Menus
910
{
1011
[Export(typeof(IMenuHandler))]
1112
[PartCreationPolicy(CreationPolicy.Shared)]
12-
public class AddConnection: MenuBase, IMenuHandler
13+
public class AddConnection : MenuBase, IMenuHandler
1314
{
1415
[ImportingConstructor]
15-
public AddConnection([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
16-
: base(serviceProvider)
16+
public AddConnection([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider, ISimpleApiClientFactory apiFactory)
17+
: base(serviceProvider, apiFactory)
1718
{
1819
}
1920

src/GitHub.VisualStudio/Menus/CopyLink.cs

+9-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using GitHub.Extensions;
66
using GitHub.Services;
77
using GitHub.VisualStudio.UI;
8+
using GitHub.Api;
89

910
namespace GitHub.VisualStudio.Menus
1011
{
@@ -13,17 +14,18 @@ namespace GitHub.VisualStudio.Menus
1314
public class CopyLink : LinkMenuBase, IDynamicMenuHandler
1415
{
1516
[ImportingConstructor]
16-
public CopyLink([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
17-
: base(serviceProvider)
17+
public CopyLink([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider, ISimpleApiClientFactory apiFactory)
18+
: base(serviceProvider, apiFactory)
1819
{
1920
}
2021

2122
public Guid Guid => GuidList.guidContextMenuSet;
2223
public int CmdId => PkgCmdIDList.copyLinkCommand;
2324

24-
public void Activate()
25+
public async void Activate()
2526
{
26-
if (!IsGitHubRepo())
27+
var isgithub = await IsGitHubRepo();
28+
if (!isgithub)
2729
return;
2830

2931
var link = GenerateLink();
@@ -44,7 +46,9 @@ public void Activate()
4446

4547
public bool CanShow()
4648
{
47-
return IsGitHubRepo();
49+
var githubRepoCheckTask = IsGitHubRepo();
50+
return githubRepoCheckTask.Wait(250) ? githubRepoCheckTask.Result : false;
4851
}
52+
4953
}
5054
}

src/GitHub.VisualStudio/Menus/LinkMenuBase.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
using GitHub.Extensions;
1+
using GitHub.Api;
2+
using GitHub.Extensions;
23
using GitHub.Primitives;
34
using System;
45

56
namespace GitHub.VisualStudio.Menus
67
{
78
public class LinkMenuBase: MenuBase
89
{
9-
public LinkMenuBase(IServiceProvider serviceProvider)
10-
: base(serviceProvider)
10+
public LinkMenuBase(IServiceProvider serviceProvider, ISimpleApiClientFactory apiFactory)
11+
: base(serviceProvider, apiFactory)
1112
{
1213
}
1314

src/GitHub.VisualStudio/Menus/OpenLink.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.VisualStudio.Shell;
44
using System;
55
using System.ComponentModel.Composition;
6+
using GitHub.Api;
67

78
namespace GitHub.VisualStudio.Menus
89
{
@@ -11,17 +12,18 @@ namespace GitHub.VisualStudio.Menus
1112
public class OpenLink: LinkMenuBase, IDynamicMenuHandler
1213
{
1314
[ImportingConstructor]
14-
public OpenLink([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
15-
: base(serviceProvider)
15+
public OpenLink([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider, ISimpleApiClientFactory apiFactory)
16+
: base(serviceProvider, apiFactory)
1617
{
1718
}
1819

1920
public Guid Guid => GuidList.guidContextMenuSet;
2021
public int CmdId => PkgCmdIDList.openLinkCommand;
2122

22-
public void Activate()
23+
public async void Activate()
2324
{
24-
if (!IsGitHubRepo())
25+
var isgithub = await IsGitHubRepo();
26+
if (!isgithub)
2527
return;
2628

2729
var link = GenerateLink();
@@ -33,7 +35,8 @@ public void Activate()
3335

3436
public bool CanShow()
3537
{
36-
return IsGitHubRepo();
38+
var githubRepoCheckTask = IsGitHubRepo();
39+
return githubRepoCheckTask.Wait(250) ? githubRepoCheckTask.Result : false;
3740
}
3841
}
3942
}

0 commit comments

Comments
 (0)