This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathAsyncPaneBase.cs
More file actions
98 lines (88 loc) · 3.22 KB
/
Copy pathAsyncPaneBase.cs
File metadata and controls
98 lines (88 loc) · 3.22 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
using System;
using System.ComponentModel.Composition;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using GitHub.Factories;
using GitHub.Services;
using GitHub.ViewModels;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Threading;
using ReactiveUI;
namespace GitHub.VisualStudio.UI
{
public class AsyncPaneBase : ToolWindowPane
{
/// <summary>
/// Gets or sets an ID string that identifies the content of the pane.
/// </summary>
public string Id { get; set; }
}
public class AsyncPaneBase<TViewModel> : AsyncPaneBase
where TViewModel : IPaneViewModel
{
readonly ContentPresenter contentPresenter;
IDisposable subscription;
JoinableTask<TViewModel> viewModelTask;
public AsyncPaneBase()
{
Content = contentPresenter = new ContentPresenter();
}
public virtual FrameworkElement View
{
get => (FrameworkElement)contentPresenter.Content;
set => contentPresenter.Content = value;
}
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<TViewModel> GetViewModelAsync() => viewModelTask.JoinAsync();
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
subscription?.Dispose();
subscription = null;
}
}
async Task<TViewModel> InitializeAsync(AsyncPackage asyncPackage)
{
try
{
// 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<TViewModel>();
await viewModel.InitializeAsync(this);
View = factory.CreateView<TViewModel>();
if (View == null)
{
throw new CompositionException("Could not find view for " + typeof(TViewModel).FullName);
}
View.DataContext = viewModel;
subscription = viewModel.WhenAnyValue(x => x.PaneCaption).Subscribe(x => Caption = x);
return viewModel;
}
catch (Exception e)
{
ShowError(e);
throw;
}
}
void ShowError(Exception e)
{
View = new TextBox
{
Text = e.ToString(),
IsReadOnly = true,
};
}
}
}