|
| 1 | +using System; |
| 2 | +using System.Windows; |
| 3 | +using System.Windows.Input; |
| 4 | +using System.Windows.Controls; |
| 5 | +using System.Windows.Controls.Primitives; |
| 6 | +using System.ComponentModel; |
| 7 | +using System.ComponentModel.Composition; |
| 8 | +using GitHub.InlineReviews.Views; |
| 9 | +using GitHub.InlineReviews.ViewModels; |
| 10 | +using GitHub.Services; |
| 11 | +using GitHub.VisualStudio; |
| 12 | +using GitHub.Models; |
| 13 | +using GitHub.Logging; |
| 14 | +using GitHub.Extensions; |
| 15 | +using Serilog; |
| 16 | + |
| 17 | +namespace GitHub.InlineReviews.Services |
| 18 | +{ |
| 19 | + public class PullRequestStatusBarManager |
| 20 | + { |
| 21 | + static readonly ILogger log = LogManager.ForContext<PullRequestStatusBarManager>(); |
| 22 | + const string StatusBarPartName = "PART_SccStatusBarHost"; |
| 23 | + |
| 24 | + readonly IVSGitExt gitExt; |
| 25 | + readonly IUsageTracker usageTracker; |
| 26 | + readonly IGitHubServiceProvider serviceProvider; |
| 27 | + |
| 28 | + IPullRequestSessionManager pullRequestSessionManager; |
| 29 | + |
| 30 | + [ImportingConstructor] |
| 31 | + public PullRequestStatusBarManager(IVSGitExt gitExt, IUsageTracker usageTracker, IGitHubServiceProvider serviceProvider) |
| 32 | + { |
| 33 | + this.gitExt = gitExt; |
| 34 | + this.usageTracker = usageTracker; |
| 35 | + this.serviceProvider = serviceProvider; |
| 36 | + |
| 37 | + OnActiveRepositoriesChanged(); |
| 38 | + gitExt.ActiveRepositoriesChanged += OnActiveRepositoriesChanged; |
| 39 | + } |
| 40 | + |
| 41 | + void OnActiveRepositoriesChanged() |
| 42 | + { |
| 43 | + if (gitExt.ActiveRepositories.Count > 0) |
| 44 | + { |
| 45 | + gitExt.ActiveRepositoriesChanged -= OnActiveRepositoriesChanged; |
| 46 | + Application.Current.Dispatcher.Invoke(() => StartShowingStatus()); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + void StartShowingStatus() |
| 51 | + { |
| 52 | + try |
| 53 | + { |
| 54 | + // Create just in time on Main thread. |
| 55 | + pullRequestSessionManager = serviceProvider.GetService<IPullRequestSessionManager>(); |
| 56 | + |
| 57 | + RefreshCurrentSession(); |
| 58 | + pullRequestSessionManager.PropertyChanged += PullRequestSessionManager_PropertyChanged; |
| 59 | + } |
| 60 | + catch (Exception e) |
| 61 | + { |
| 62 | + log.Error(e, "Error initializing"); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + void PullRequestSessionManager_PropertyChanged(object sender, PropertyChangedEventArgs e) |
| 67 | + { |
| 68 | + if (e.PropertyName == nameof(PullRequestSessionManager.CurrentSession)) |
| 69 | + { |
| 70 | + RefreshCurrentSession(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + void RefreshCurrentSession() |
| 75 | + { |
| 76 | + var pullRequest = pullRequestSessionManager.CurrentSession?.PullRequest; |
| 77 | + var viewModel = pullRequest != null ? CreatePullRequestStatusViewModel(pullRequest) : null; |
| 78 | + ShowStatus(viewModel); |
| 79 | + } |
| 80 | + |
| 81 | + PullRequestStatusViewModel CreatePullRequestStatusViewModel(IPullRequestModel pullRequest) |
| 82 | + { |
| 83 | + var dte = serviceProvider.TryGetService<EnvDTE.DTE>(); |
| 84 | + var command = new RaisePullRequestCommand(dte, usageTracker); |
| 85 | + var pullRequestStatusViewModel = new PullRequestStatusViewModel(command); |
| 86 | + pullRequestStatusViewModel.Number = pullRequest.Number; |
| 87 | + pullRequestStatusViewModel.Title = pullRequest.Title; |
| 88 | + return pullRequestStatusViewModel; |
| 89 | + } |
| 90 | + |
| 91 | + void ShowStatus(PullRequestStatusViewModel pullRequestStatusViewModel = null) |
| 92 | + { |
| 93 | + var statusBar = FindSccStatusBar(Application.Current.MainWindow); |
| 94 | + if (statusBar != null) |
| 95 | + { |
| 96 | + var githubStatusBar = Find<PullRequestStatusView>(statusBar); |
| 97 | + if (githubStatusBar != null) |
| 98 | + { |
| 99 | + // Replace to ensure status shows up. |
| 100 | + statusBar.Items.Remove(githubStatusBar); |
| 101 | + } |
| 102 | + |
| 103 | + if (pullRequestStatusViewModel != null) |
| 104 | + { |
| 105 | + githubStatusBar = new PullRequestStatusView { DataContext = pullRequestStatusViewModel }; |
| 106 | + statusBar.Items.Insert(0, githubStatusBar); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + static T Find<T>(StatusBar statusBar) |
| 112 | + { |
| 113 | + foreach (var item in statusBar.Items) |
| 114 | + { |
| 115 | + if (item is T) |
| 116 | + { |
| 117 | + return (T)item; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return default(T); |
| 122 | + } |
| 123 | + |
| 124 | + StatusBar FindSccStatusBar(Window mainWindow) |
| 125 | + { |
| 126 | + var contentControl = mainWindow?.Template?.FindName(StatusBarPartName, mainWindow) as ContentControl; |
| 127 | + return contentControl?.Content as StatusBar; |
| 128 | + } |
| 129 | + |
| 130 | + class RaisePullRequestCommand : ICommand |
| 131 | + { |
| 132 | + readonly string guid = Guids.guidGitHubCmdSetString; |
| 133 | + readonly int id = PkgCmdIDList.showCurrentPullRequestCommand; |
| 134 | + |
| 135 | + readonly EnvDTE.DTE dte; |
| 136 | + readonly IUsageTracker usageTracker; |
| 137 | + |
| 138 | + internal RaisePullRequestCommand(EnvDTE.DTE dte, IUsageTracker usageTracker) |
| 139 | + { |
| 140 | + this.dte = dte; |
| 141 | + this.usageTracker = usageTracker; |
| 142 | + } |
| 143 | + |
| 144 | + public bool CanExecute(object parameter) => true; |
| 145 | + |
| 146 | + public void Execute(object parameter) |
| 147 | + { |
| 148 | + try |
| 149 | + { |
| 150 | + object customIn = null; |
| 151 | + object customOut = null; |
| 152 | + dte?.Commands.Raise(guid, id, ref customIn, ref customOut); |
| 153 | + } |
| 154 | + catch (Exception e) |
| 155 | + { |
| 156 | + log.Error(e, "Couldn't raise {Guid}:{ID}", guid, id); |
| 157 | + } |
| 158 | + |
| 159 | + usageTracker.IncrementCounter(x => x.NumberOfShowCurrentPullRequest).Forget(); |
| 160 | + } |
| 161 | + |
| 162 | + public event EventHandler CanExecuteChanged |
| 163 | + { |
| 164 | + add { } |
| 165 | + remove { } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments