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

Commit

Permalink
Merge pull request #382 from github/backport/context-menu-visibility
Browse files Browse the repository at this point in the history
[Backport] Context menu visibility
  • Loading branch information
shana authored Jun 23, 2016
2 parents 04d89a2 + f53776a commit c8152df
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 18 deletions.
48 changes: 46 additions & 2 deletions src/GitHub.VisualStudio/Base/MenuBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,50 @@
using GitHub.Extensions;
using GitHub.Models;
using GitHub.Services;
using System.Threading.Tasks;
using GitHub.Api;
using NullGuard;
using System.Diagnostics;
using GitHub.Primitives;

namespace GitHub.VisualStudio
{
public abstract class MenuBase
{
readonly IServiceProvider serviceProvider;
readonly ISimpleApiClientFactory apiFactory;

protected IServiceProvider ServiceProvider { get { return serviceProvider; } }

protected ISimpleRepositoryModel ActiveRepo { get; private set; }

protected ISimpleApiClient simpleApiClient;

[AllowNull]
protected ISimpleApiClient SimpleApiClient
{
[return: AllowNull]
get { return simpleApiClient; }
set
{
if (simpleApiClient != value && value == null)
apiFactory.ClearFromCache(simpleApiClient);
simpleApiClient = value;
}
}

protected ISimpleApiClientFactory ApiFactory => apiFactory;

protected MenuBase()
{
}

protected MenuBase(IServiceProvider serviceProvider, ISimpleApiClientFactory apiFactory)
{
this.serviceProvider = serviceProvider;
this.apiFactory = apiFactory;
}

protected MenuBase(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
Expand All @@ -41,10 +71,24 @@ void RefreshRepo()
}
}

protected bool IsGitHubRepo()
protected async Task<bool> IsGitHubRepo()
{
RefreshRepo();
return ActiveRepo?.CloneUrl?.RepositoryName != null;

var uri = ActiveRepo.CloneUrl;
if (uri == null)
return false;

Debug.Assert(apiFactory != null, "apiFactory cannot be null. Did you call the right constructor?");
SimpleApiClient = apiFactory.Create(uri);

var isdotcom = HostAddress.IsGitHubDotComUri(uri.ToRepositoryUrl());
if (!isdotcom)
{
var repo = await SimpleApiClient.GetRepository();
return (repo.FullName == ActiveRepo.Name || repo.Id == 0) && SimpleApiClient.IsEnterprise();
}
return isdotcom;
}
}
}
7 changes: 4 additions & 3 deletions src/GitHub.VisualStudio/Menus/AddConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
using GitHub.Services;
using GitHub.UI;
using GitHub.Extensions;
using GitHub.Api;

namespace GitHub.VisualStudio.Menus
{
[Export(typeof(IMenuHandler))]
[PartCreationPolicy(CreationPolicy.Shared)]
public class AddConnection: MenuBase, IMenuHandler
public class AddConnection : MenuBase, IMenuHandler
{
[ImportingConstructor]
public AddConnection([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
: base(serviceProvider)
public AddConnection([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider, ISimpleApiClientFactory apiFactory)
: base(serviceProvider, apiFactory)
{
}

Expand Down
14 changes: 9 additions & 5 deletions src/GitHub.VisualStudio/Menus/CopyLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using GitHub.Extensions;
using GitHub.Services;
using GitHub.VisualStudio.UI;
using GitHub.Api;

namespace GitHub.VisualStudio.Menus
{
Expand All @@ -13,17 +14,18 @@ namespace GitHub.VisualStudio.Menus
public class CopyLink : LinkMenuBase, IDynamicMenuHandler
{
[ImportingConstructor]
public CopyLink([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
: base(serviceProvider)
public CopyLink([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider, ISimpleApiClientFactory apiFactory)
: base(serviceProvider, apiFactory)
{
}

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

public void Activate()
public async void Activate()
{
if (!IsGitHubRepo())
var isgithub = await IsGitHubRepo();
if (!isgithub)
return;

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

public bool CanShow()
{
return IsGitHubRepo();
var githubRepoCheckTask = IsGitHubRepo();
return githubRepoCheckTask.Wait(250) ? githubRepoCheckTask.Result : false;
}

}
}
7 changes: 4 additions & 3 deletions src/GitHub.VisualStudio/Menus/LinkMenuBase.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using GitHub.Extensions;
using GitHub.Api;
using GitHub.Extensions;
using GitHub.Primitives;
using System;

namespace GitHub.VisualStudio.Menus
{
public class LinkMenuBase: MenuBase
{
public LinkMenuBase(IServiceProvider serviceProvider)
: base(serviceProvider)
public LinkMenuBase(IServiceProvider serviceProvider, ISimpleApiClientFactory apiFactory)
: base(serviceProvider, apiFactory)
{
}

Expand Down
13 changes: 8 additions & 5 deletions src/GitHub.VisualStudio/Menus/OpenLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.VisualStudio.Shell;
using System;
using System.ComponentModel.Composition;
using GitHub.Api;

namespace GitHub.VisualStudio.Menus
{
Expand All @@ -11,17 +12,18 @@ namespace GitHub.VisualStudio.Menus
public class OpenLink: LinkMenuBase, IDynamicMenuHandler
{
[ImportingConstructor]
public OpenLink([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
: base(serviceProvider)
public OpenLink([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider, ISimpleApiClientFactory apiFactory)
: base(serviceProvider, apiFactory)
{
}

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

public void Activate()
public async void Activate()
{
if (!IsGitHubRepo())
var isgithub = await IsGitHubRepo();
if (!isgithub)
return;

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

public bool CanShow()
{
return IsGitHubRepo();
var githubRepoCheckTask = IsGitHubRepo();
return githubRepoCheckTask.Wait(250) ? githubRepoCheckTask.Result : false;
}
}
}

0 comments on commit c8152df

Please sign in to comment.