11using System ;
22using System . Windows ;
33using System . Windows . Media ;
4+ using System . Windows . Input ;
45using System . Windows . Controls ;
56using System . Windows . Controls . Primitives ;
7+ using System . ComponentModel ;
68using System . ComponentModel . Composition ;
79using GitHub . InlineReviews . Views ;
810using GitHub . InlineReviews . ViewModels ;
911using GitHub . Services ;
10- using System . ComponentModel ;
12+ using GitHub . VisualStudio ;
13+ using Microsoft . VisualStudio . Shell ;
1114
1215namespace GitHub . InlineReviews . Services
1316{
@@ -22,29 +25,15 @@ public class PullRequestStatusManager : IPullRequestStatusManager
2225 readonly PullRequestStatusViewModel pullRequestStatusViewModel ;
2326
2427 [ ImportingConstructor ]
25- public PullRequestStatusManager ( IPullRequestSessionManager pullRequestSessionManager ) : this ( new PullRequestStatusViewModel ( ) )
28+ public PullRequestStatusManager ( SVsServiceProvider serviceProvider , IPullRequestSessionManager pullRequestSessionManager )
29+ : this ( CreatePullRequestStatusViewModel ( serviceProvider ) )
2630 {
2731 this . pullRequestSessionManager = pullRequestSessionManager ;
2832
2933 RefreshCurrentSession ( ) ;
3034 pullRequestSessionManager . PropertyChanged += PullRequestSessionManager_PropertyChanged ;
3135 }
3236
33- void PullRequestSessionManager_PropertyChanged ( object sender , PropertyChangedEventArgs e )
34- {
35- if ( e . PropertyName == nameof ( PullRequestSessionManager . CurrentSession ) )
36- {
37- RefreshCurrentSession ( ) ;
38- }
39- }
40-
41- void RefreshCurrentSession ( )
42- {
43- var pullRequest = pullRequestSessionManager . CurrentSession ? . PullRequest ;
44- pullRequestStatusViewModel . Number = pullRequest ? . Number ;
45- pullRequestStatusViewModel . Title = pullRequest ? . Title ;
46- }
47-
4837 public PullRequestStatusManager ( PullRequestStatusViewModel pullRequestStatusViewModel )
4938 {
5039 this . pullRequestStatusViewModel = pullRequestStatusViewModel ;
@@ -78,6 +67,27 @@ public void HideStatus()
7867 }
7968 }
8069
70+ static PullRequestStatusViewModel CreatePullRequestStatusViewModel ( IServiceProvider serviceProvider )
71+ {
72+ var command = new RaiseVsCommand ( serviceProvider , Guids . guidGitHubCmdSetString , PkgCmdIDList . showCurrentPullRequestCommand ) ;
73+ return new PullRequestStatusViewModel ( command ) ;
74+ }
75+
76+ void PullRequestSessionManager_PropertyChanged ( object sender , PropertyChangedEventArgs e )
77+ {
78+ if ( e . PropertyName == nameof ( PullRequestSessionManager . CurrentSession ) )
79+ {
80+ RefreshCurrentSession ( ) ;
81+ }
82+ }
83+
84+ void RefreshCurrentSession ( )
85+ {
86+ var pullRequest = pullRequestSessionManager . CurrentSession ? . PullRequest ;
87+ pullRequestStatusViewModel . Number = pullRequest ? . Number ;
88+ pullRequestStatusViewModel . Title = pullRequest ? . Title ;
89+ }
90+
8191 static UserControl FindPullRequestStatusView ( StatusBar statusBar )
8292 {
8393 return FindChild < UserControl > ( statusBar , GitHubStatusName ) ;
@@ -132,12 +142,46 @@ static T FindChild<T>(DependencyObject parent, string childName) where T : Depen
132142 return foundChild ;
133143 }
134144
145+ class RaiseVsCommand : ICommand
146+ {
147+ readonly IServiceProvider serviceProvider ;
148+ readonly string guid ;
149+ readonly int id ;
150+
151+ internal RaiseVsCommand ( IServiceProvider serviceProvider , string guid , int id )
152+ {
153+ this . serviceProvider = serviceProvider ;
154+ this . guid = guid ;
155+ this . id = id ;
156+ }
157+
158+ public bool CanExecute ( object parameter ) => true ;
159+
160+ public void Execute ( object parameter )
161+ {
162+ try
163+ {
164+ var dte = serviceProvider . GetService ( typeof ( EnvDTE . DTE ) ) as EnvDTE . DTE ;
165+ object customIn = null ;
166+ object customOut = null ;
167+ dte . Commands . Raise ( guid , id , ref customIn , ref customOut ) ;
168+ }
169+ catch ( Exception e )
170+ {
171+ VsOutputLogger . WriteLine ( "Couldn't raise {0}: {1}" , nameof ( PkgCmdIDList . openPullRequestsCommand ) , e ) ;
172+ System . Diagnostics . Trace . WriteLine ( e ) ;
173+ }
174+ }
175+
176+ public event EventHandler CanExecuteChanged ;
177+ }
178+
135179 class PullRequestStatusManagerInstaller
136180 {
137181 [ STAThread ]
138182 void Install ( )
139183 {
140- var viewModel = new PullRequestStatusViewModel { Number = 666 , Title = "A beast of a PR" } ;
184+ var viewModel = new PullRequestStatusViewModel ( null ) { Number = 666 , Title = "A beast of a PR" } ;
141185 var provider = new PullRequestStatusManager ( viewModel ) ;
142186 provider . HideStatus ( ) ;
143187 provider . ShowStatus ( ) ;
0 commit comments