Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/GitHub.Exports/Models/UsageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class MeasuresModel
public int NumberOfPRReviewDiffViewInlineCommentStartReview { get; set; }
public int NumberOfPRReviewPosts { get; set; }
public int NumberOfShowCurrentPullRequest { get; set; }
public int NumberOfStatusBarOpenPullRequestList { get; set; }
public int NumberOfTeamExplorerHomeOpenPullRequestList { get; set; }
}
}
Expand Down
93 changes: 79 additions & 14 deletions src/GitHub.InlineReviews/Services/PullRequestStatusBarManager.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.ComponentModel.Composition;
using System.Reactive.Linq;
using System.Linq.Expressions;
using github.202132.xyzmands;
using GitHub.InlineReviews.Views;
using GitHub.InlineReviews.ViewModels;
using GitHub.Services;
using GitHub.Models;
using GitHub.Logging;
using GitHub.Extensions;
using Serilog;
using ReactiveUI;

Expand All @@ -23,19 +27,31 @@ public class PullRequestStatusBarManager
static readonly ILogger log = LogManager.ForContext<PullRequestStatusBarManager>();
const string StatusBarPartName = "PART_SccStatusBarHost";

readonly IShowCurrentPullRequestCommand showCurrentPullRequestCommand;
readonly ICommand openPullRequestsCommand;
readonly ICommand showCurrentPullRequestCommand;

// At the moment this must be constructed on the main thread.
// At the moment these must be constructed on the main thread.
// TeamExplorerContext needs to retrieve DTE using GetService.
readonly Lazy<IPullRequestSessionManager> pullRequestSessionManager;
readonly Lazy<ITeamExplorerContext> teamExplorerContext;

IDisposable currentSessionSubscription;

[ImportingConstructor]
public PullRequestStatusBarManager(
IUsageTracker usageTracker,
IOpenPullRequestsCommand openPullRequestsCommand,
IShowCurrentPullRequestCommand showCurrentPullRequestCommand,
Lazy<IPullRequestSessionManager> pullRequestSessionManager)
Lazy<IPullRequestSessionManager> pullRequestSessionManager,
Lazy<ITeamExplorerContext> teamExplorerContext)
{
this.showCurrentPullRequestCommand = showCurrentPullRequestCommand;
this.openPullRequestsCommand = new UsageTrackingCommand(openPullRequestsCommand,
usageTracker, x => x.NumberOfStatusBarOpenPullRequestList);
this.showCurrentPullRequestCommand = new UsageTrackingCommand(showCurrentPullRequestCommand,
usageTracker, x => x.NumberOfShowCurrentPullRequest);

this.pullRequestSessionManager = pullRequestSessionManager;
this.teamExplorerContext = teamExplorerContext;
}

/// <summary>
Expand All @@ -48,27 +64,44 @@ public void StartShowingStatus()
{
try
{
pullRequestSessionManager.Value.WhenAnyValue(x => x.CurrentSession)
.Subscribe(x => RefreshCurrentSession());
teamExplorerContext.Value.WhenAnyValue(x => x.ActiveRepository)
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(x => RefreshActiveRepository(x));
}
catch (Exception e)
{
log.Error(e, "Error initializing");
}
}

void RefreshCurrentSession()
void RefreshActiveRepository(ILocalRepositoryModel repository)
{
currentSessionSubscription?.Dispose();
currentSessionSubscription = pullRequestSessionManager.Value.WhenAnyValue(x => x.CurrentSession)
.Subscribe(x => RefreshCurrentSession(repository, x));
}

void RefreshCurrentSession(ILocalRepositoryModel repository, IPullRequestSession session)
{
var pullRequest = pullRequestSessionManager.Value.CurrentSession?.PullRequest;
var viewModel = pullRequest != null ? CreatePullRequestStatusViewModel(pullRequest) : null;
ShowStatus(viewModel);
var cloneUrl = repository?.CloneUrl;
if (cloneUrl != null)
{
// Only show PR status bar if repo has remote
var viewModel = CreatePullRequestStatusViewModel(session);
ShowStatus(viewModel);
}
else
{
ShowStatus(null);
}
}

PullRequestStatusViewModel CreatePullRequestStatusViewModel(IPullRequestModel pullRequest)
PullRequestStatusViewModel CreatePullRequestStatusViewModel(IPullRequestSession session)
{
var pullRequestStatusViewModel = new PullRequestStatusViewModel(showCurrentPullRequestCommand);
pullRequestStatusViewModel.Number = pullRequest.Number;
pullRequestStatusViewModel.Title = pullRequest.Title;
var pullRequestStatusViewModel = new PullRequestStatusViewModel(openPullRequestsCommand, showCurrentPullRequestCommand);
var pullRequest = session?.PullRequest;
pullRequestStatusViewModel.Number = pullRequest?.Number;
pullRequestStatusViewModel.Title = pullRequest?.Title;
return pullRequestStatusViewModel;
}

Expand Down Expand Up @@ -110,5 +143,37 @@ StatusBar FindSccStatusBar(Window mainWindow)
var contentControl = mainWindow?.Template?.FindName(StatusBarPartName, mainWindow) as ContentControl;
return contentControl?.Content as StatusBar;
}

class UsageTrackingCommand : ICommand
{
readonly ICommand command;
readonly IUsageTracker usageTracker;
readonly Expression<Func<UsageModel.MeasuresModel, int>> counter;

internal UsageTrackingCommand(ICommand command, IUsageTracker usageTracker,
Expression<Func<UsageModel.MeasuresModel, int>> counter)
{
this.command = command;
this.usageTracker = usageTracker;
this.counter = counter;
}

public event EventHandler CanExecuteChanged
{
add { command.CanExecuteChanged += value; }
remove { command.CanExecuteChanged -= value; }
}

public bool CanExecute(object parameter)
{
return command.CanExecute(parameter);
}

public void Execute(object parameter)
{
command.Execute(parameter);
usageTracker.IncrementCounter(counter).Forget();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ public class PullRequestStatusViewModel : INotifyPropertyChanged
int? number;
string title;

public PullRequestStatusViewModel(ICommand showCurrentPullRequestCommand)
public PullRequestStatusViewModel(ICommand openPullRequestsCommand, ICommand showCurrentPullRequestCommand)
{
OpenPullRequestsCommand = openPullRequestsCommand;
ShowCurrentPullRequestCommand = showCurrentPullRequestCommand;
}

Expand Down Expand Up @@ -40,6 +41,7 @@ public string Title
}
}

public ICommand OpenPullRequestsCommand { get; }
public ICommand ShowCurrentPullRequestCommand { get; }

public event PropertyChangedEventHandler PropertyChanged;
Expand Down
64 changes: 45 additions & 19 deletions src/GitHub.InlineReviews/Views/PullRequestStatusView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,55 @@
</Style>
</UserControl.Resources>

<Grid Visibility="{Binding Number, TargetNullValue=Collapsed}">
<Button
<StackPanel Orientation="Horizontal">
<Grid Visibility="{Binding Number, Converter={ui:EqualsToVisibilityConverter {x:Null}}}">
<Button
Foreground="{DynamicResource VsBrush.StatusBarText}"
BorderThickness="0"
Padding="4 0"
Command="{Binding ShowCurrentPullRequestCommand}">
Command="{Binding OpenPullRequestsCommand}">

<StackPanel Orientation="Horizontal">
<ui:OcticonPath
Fill="White"
VerticalAlignment="Bottom"
Margin="0 0 4 0 "
Icon="git_pull_request" />
<TextBlock
VerticalAlignment="Center">
#<Run Text="{Binding Number}" />
<StackPanel Orientation="Horizontal">
<ui:OcticonPath
Fill="White"
VerticalAlignment="Bottom"
Margin="0 0 4 0 "
Icon="git_pull_request" />
<TextBlock VerticalAlignment="Center">
Pull requests
</TextBlock>
</StackPanel>
</Button>
<Grid.ToolTip>
<TextBlock VerticalAlignment="Center">
Open or Create a Pull request
</TextBlock>
</StackPanel>
</Button>
<Grid.ToolTip>
<TextBlock VerticalAlignment="Center">
</Grid.ToolTip>
</Grid>

<Grid Visibility="{Binding Number, Converter={ui:NotEqualsToVisibilityConverter {x:Null}}}">
<Button
Foreground="{DynamicResource VsBrush.StatusBarText}"
BorderThickness="0"
Padding="4 0"
Command="{Binding ShowCurrentPullRequestCommand}">

<StackPanel Orientation="Horizontal">
<ui:OcticonPath
Fill="White"
VerticalAlignment="Bottom"
Margin="0 0 4 0 "
Icon="git_pull_request" />
<TextBlock VerticalAlignment="Center">
#<Run Text="{Binding Number}" />
</TextBlock>
</StackPanel>
</Button>
<Grid.ToolTip>
<TextBlock VerticalAlignment="Center">
#<Run Text="{Binding Number}" /> - <Run Text="{Binding Title}" />
</TextBlock>
</Grid.ToolTip>
</Grid>
</TextBlock>
</Grid.ToolTip>
</Grid>
</StackPanel>
</UserControl>
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ public class ShowCurrentPullRequestCommand : VsCommand, IShowCurrentPullRequestC
{
static readonly ILogger log = LogManager.ForContext<ShowCurrentPullRequestCommand>();
readonly IGitHubServiceProvider serviceProvider;
readonly Lazy<IUsageTracker> usageTracker;

[ImportingConstructor]
protected ShowCurrentPullRequestCommand(IGitHubServiceProvider serviceProvider, Lazy<IUsageTracker> usageTracker)
protected ShowCurrentPullRequestCommand(IGitHubServiceProvider serviceProvider)
: base(CommandSet, CommandId)
{
this.serviceProvider = serviceProvider;
this.usageTracker = usageTracker;
}

/// <summary>
Expand Down Expand Up @@ -61,8 +59,6 @@ public override async Task Execute()
var manager = serviceProvider.TryGetService<IGitHubToolWindowManager>();
var host = await manager.ShowGitHubPane();
await host.ShowPullRequest(session.RepositoryOwner, host.LocalRepository.Name, pullRequest.Number);

usageTracker.Value.IncrementCounter(x => x.NumberOfShowCurrentPullRequest).Forget();
}
catch (Exception ex)
{
Expand Down