This repository was archived by the owner on Jun 21, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathGitHubPane.cs
More file actions
215 lines (179 loc) · 7.17 KB
/
Copy pathGitHubPane.cs
File metadata and controls
215 lines (179 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using System;
using System.Threading.Tasks;
using System.ComponentModel.Design;
using System.Diagnostics.CodeAnalysis;
using System.Reactive.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using GitHub.Factories;
using GitHub.Services;
using GitHub.ViewModels.GitHubPane;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Threading;
using ReactiveUI;
namespace GitHub.VisualStudio.UI
{
/// <summary>
/// This class implements the tool window exposed by this package and hosts a user control.
/// </summary>
/// <remarks>
/// In Visual Studio tool windows are composed of a frame (implemented by the shell) and a pane,
/// usually implemented by the package implementer.
/// <para>
/// This class derives from the ToolWindowPane class provided from the MPF in order to use its
/// implementation of the IVsUIElementPane interface.
/// </para>
/// </remarks>
[Guid(GitHubPaneGuid)]
public class GitHubPane : ToolWindowPane
{
public const string GitHubPaneGuid = "6b0fdc0a-f28e-47a0-8eed-cc296beff6d2";
JoinableTask<IGitHubPaneViewModel> viewModelTask;
IDisposable viewSubscription;
ContentPresenter contentPresenter;
public FrameworkElement View
{
get { return contentPresenter.Content as FrameworkElement; }
set
{
viewSubscription?.Dispose();
viewSubscription = null;
contentPresenter.Content = value;
viewSubscription = value.WhenAnyValue(x => x.DataContext)
.SelectMany(x =>
{
var pane = x as IGitHubPaneViewModel;
return pane?.WhenAnyValue(p => p.IsSearchEnabled, p => p.SearchQuery)
?? Observable.Return(Tuple.Create<bool, string>(false, null));
})
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(x => UpdateSearchHost(x.Item1, x.Item2));
}
}
[SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public GitHubPane() : base(null)
{
Caption = "GitHub";
Content = contentPresenter = new ContentPresenter();
BitmapImageMoniker = new Microsoft.VisualStudio.Imaging.Interop.ImageMoniker()
{
Guid = Guids.guidImageMoniker,
Id = 1
};
ToolBar = new CommandID(Guids.guidGitHubToolbarCmdSet, PkgCmdIDList.idGitHubToolbar);
ToolBarLocation = (int)VSTWT_LOCATION.VSTWT_TOP;
}
public override bool SearchEnabled => true;
protected override void Initialize()
{
// Using JoinableTaskFactory from parent AsyncPackage. That way if VS shuts down before this
// work is done, we won't risk crashing due to arbitrary work going on in background threads.
var asyncPackage = (AsyncPackage)Package;
viewModelTask = asyncPackage.JoinableTaskFactory.RunAsync(() => InitializeAsync(asyncPackage));
}
public Task<IGitHubPaneViewModel> GetViewModelAsync() => viewModelTask.JoinAsync();
async Task<IGitHubPaneViewModel> InitializeAsync(AsyncPackage asyncPackage)
{
try
{
ShowInitializing();
// Allow MEF to initialize its cache asynchronously
var provider = (IGitHubServiceProvider)await asyncPackage.GetServiceAsync(typeof(IGitHubServiceProvider));
var teServiceHolder = provider.GetService<ITeamExplorerServiceHolder>();
teServiceHolder.ServiceProvider = this;
var factory = provider.GetService<IViewViewModelFactory>();
var viewModel = provider.ExportProvider.GetExportedValue<IGitHubPaneViewModel>();
await viewModel.InitializeAsync(this);
View = factory.CreateView<IGitHubPaneViewModel>();
View.DataContext = viewModel;
return viewModel;
}
catch (Exception e)
{
ShowError(e);
throw;
}
}
[SuppressMessage("Microsoft.Design", "CA1061:DoNotHideBaseClassMethods", Justification = "WTF CA, I'm overriding!")]
public override IVsSearchTask CreateSearch(uint dwCookie, IVsSearchQuery pSearchQuery, IVsSearchCallback pSearchCallback)
{
var pane = View?.DataContext as IGitHubPaneViewModel;
if (pane != null)
{
return new SearchTask(pane, dwCookie, pSearchQuery, pSearchCallback);
}
return null;
}
public override void ClearSearch()
{
var pane = View?.DataContext as IGitHubPaneViewModel;
if (pane != null)
{
pane.SearchQuery = null;
}
}
public override void OnToolWindowCreated()
{
base.OnToolWindowCreated();
Marshal.ThrowExceptionForHR(((IVsWindowFrame)Frame)?.SetProperty(
(int)__VSFPROPID5.VSFPROPID_SearchPlacement,
__VSSEARCHPLACEMENT.SP_STRETCH) ?? 0);
var pane = View?.DataContext as IGitHubPaneViewModel;
UpdateSearchHost(pane?.IsSearchEnabled ?? false, pane?.SearchQuery);
}
void ShowInitializing()
{
// This page is intentionally left blank.
}
void ShowError(Exception e)
{
View = new TextBox
{
Text = e.ToString(),
IsReadOnly = true,
};
}
void UpdateSearchHost(bool enabled, string query)
{
if (SearchHost != null)
{
SearchHost.IsEnabled = enabled;
if (SearchHost.SearchQuery?.SearchString != query)
{
SearchHost.SearchAsync(query != null ? new SearchQuery(query) : null);
}
}
}
class SearchTask : VsSearchTask
{
readonly IGitHubPaneViewModel viewModel;
public SearchTask(
IGitHubPaneViewModel viewModel,
uint dwCookie,
IVsSearchQuery pSearchQuery,
IVsSearchCallback pSearchCallback)
: base(dwCookie, pSearchQuery, pSearchCallback)
{
this.viewModel = viewModel;
}
protected override void OnStartSearch()
{
viewModel.SearchQuery = SearchQuery.SearchString;
base.OnStartSearch();
}
protected override void OnStopSearch() => viewModel.SearchQuery = null;
}
class SearchQuery : IVsSearchQuery
{
public SearchQuery(string query)
{
SearchString = query;
}
public uint ParseError => 0;
public string SearchString { get; }
public uint GetTokens(uint dwMaxTokens, IVsSearchToken[] rgpSearchTokens) => 0;
}
}
}