-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathApp.xaml.cs
244 lines (203 loc) · 9.33 KB
/
App.xaml.cs
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using CommunityToolkit.WinUI;
using DevHome.Activation;
using DevHome.Common.Contracts;
using DevHome.Common.Contracts.Services;
using DevHome.Common.Environments.Services;
using DevHome.Common.Extensions;
using DevHome.Common.Models;
using DevHome.Common.Services;
using DevHome.Contracts.Services;
using DevHome.Customization.Extensions;
using DevHome.Dashboard.Extensions;
using DevHome.Database.Extensions;
using DevHome.ExtensionLibrary.Extensions;
using DevHome.Helpers;
using DevHome.RepositoryManagement.Extensions;
using DevHome.RepositoryManagement.ViewModels;
using DevHome.Services;
using DevHome.Services.Core.Extensions;
using DevHome.Services.DesiredStateConfiguration.Extensions;
using DevHome.Services.WindowsPackageManager.Extensions;
using DevHome.Settings.Extensions;
using DevHome.SetupFlow.Extensions;
using DevHome.SetupFlow.Services;
using DevHome.Telemetry;
using DevHome.TelemetryEvents;
using DevHome.Utilities.Extensions;
using DevHome.ViewModels;
using DevHome.Views;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using Microsoft.Windows.AppLifecycle;
using Serilog;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.WindowsAndMessaging;
namespace DevHome;
public partial class App : Application, IApp
{
private readonly DispatcherQueue _dispatcherQueue;
// The .NET Generic Host provides dependency injection, configuration, logging, and other services.
// https://docs.microsoft.com/dotnet/core/extensions/generic-host
// https://docs.microsoft.com/dotnet/core/extensions/dependency-injection
// https://docs.microsoft.com/dotnet/core/extensions/configuration
// https://docs.microsoft.com/dotnet/core/extensions/logging
public IHost Host
{
get;
}
public T GetService<T>()
where T : class => Host.GetService<T>();
public static Window MainWindow { get; } = new MainWindow();
private static string RemoveComments(string text)
{
var start = text.IndexOf("/*", StringComparison.Ordinal);
while (start >= 0)
{
var end = text.IndexOf("*/", start + 2, StringComparison.Ordinal);
if (end < 0)
{
end = text.Length;
}
text = text.Remove(start, end - start + 2);
start = text.IndexOf("/*", start, StringComparison.Ordinal);
}
return text;
}
internal static NavConfig NavConfig { get; } = System.Text.Json.JsonSerializer.Deserialize(
RemoveComments(File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "navConfig.jsonc"))),
SourceGenerationContext.Default.NavConfig)!;
public App()
{
// TODO: Add database migration.
InitializeComponent();
#if DEBUG_FAILFAST
DebugSettings.FailFastOnErrors = true;
#endif
_dispatcherQueue = DispatcherQueue.GetForCurrentThread();
Host = Microsoft.Extensions.Hosting.Host.
CreateDefaultBuilder().
UseContentRoot(AppContext.BaseDirectory).
UseDefaultServiceProvider((context, options) =>
{
options.ValidateOnBuild = true;
}).
ConfigureServices((context, services) =>
{
// Add Serilog logging for ILogger.
services.AddLogging(lb => lb.AddSerilog(dispose: true));
// Add databse connection
services.AddDatabase(context);
// Default Activation Handler
services.AddTransient<ActivationHandler<LaunchActivatedEventArgs>, DefaultActivationHandler>();
// Other Activation Handlers
services.AddTransient<IActivationHandler, ProtocolActivationHandler>();
services.AddTransient<IActivationHandler, DSCFileActivationHandler>();
services.AddTransient<IActivationHandler, AppInstallActivationHandler>();
// Service projects
services.AddCore();
services.AddWinGet();
services.AddDSC();
// Services
services.AddSingleton<ILocalSettingsService, LocalSettingsService>();
services.AddSingleton<IExperimentationService, ExperimentationService>();
services.AddSingleton<IThemeSelectorService, ThemeSelectorService>();
services.AddTransient<INavigationViewService, NavigationViewService>();
services.AddSingleton<IActivationService, ActivationService>();
services.AddSingleton<IExtensionService, ExtensionService>();
services.AddSingleton<IPageService, PageService>();
services.AddSingleton<INavigationService, NavigationService>();
services.AddSingleton<IAccountsService, AccountsService>();
services.AddSingleton<IInfoBarService, InfoBarService>();
services.AddSingleton<IAppInfoService, AppInfoService>();
services.AddSingleton<ITelemetry>(TelemetryFactory.Get<ITelemetry>());
services.AddSingleton<IStringResource, StringResource>();
services.AddSingleton<IScreenReaderService, ScreenReaderService>();
services.AddSingleton<IComputeSystemService, ComputeSystemService>();
services.AddSingleton<IComputeSystemManager, ComputeSystemManager>();
services.AddTransient<AdaptiveCardRenderingService>();
// Core Services
services.AddSingleton<IFileService, FileService>();
// Main window: Allow access to the main window
// from anywhere in the application.
services.AddSingleton(_ => MainWindow);
// DispatcherQueue: Allow access to the DispatcherQueue for
// the main window for general purpose UI thread access.
services.AddSingleton(_ => MainWindow.DispatcherQueue);
// Views and ViewModels
services.AddTransient<ShellPage>();
services.AddTransient<InitializationPage>();
services.AddTransient<ShellViewModel>();
services.AddTransient<InitializationViewModel>();
// Settings
services.AddSettings(context);
// Configuration
services.Configure<LocalSettingsOptions>(context.Configuration.GetSection(nameof(LocalSettingsOptions)));
// Setup flow
services.AddSetupFlow(context);
// Repository Management
services.AddRepositoryManagement(context);
// Dashboard
services.AddDashboard(context);
// ExtensionLibrary
services.AddExtensionLibrary(context);
// Environments
services.AddEnvironments(context);
// Windows customization
services.AddWindowsCustomization(context);
// Utilities
services.AddUtilities(context);
}).
Build();
UnhandledException += App_UnhandledException;
AppInstance.GetCurrent().Activated += OnActivated;
TelemetryFactory.Get<ITelemetry>().Log("DevHome_Started_Event", LogLevel.Critical, new DevHomeStartedEvent());
Log.Information("Dev Home Started.");
}
public void ShowMainWindow()
{
_dispatcherQueue.TryEnqueue(() =>
{
var hWnd = new HWND(WinRT.Interop.WindowNative.GetWindowHandle(MainWindow));
if (PInvoke.IsIconic(hWnd))
{
PInvoke.ShowWindow(hWnd, SHOW_WINDOW_CMD.SW_RESTORE);
}
else
{
PInvoke.SetForegroundWindow(hWnd);
}
});
}
private async void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
{
// https://docs.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.application.unhandledexception.
Log.Fatal(e.Exception, $"Unhandled exception: {e.Message}");
// We are about to crash, so signal the extensions to stop.
await GetService<IExtensionService>().SignalStopExtensionsAsync();
Log.CloseAndFlush();
// We are very likely in a bad and unrecoverable state, so ensure Dev Home crashes w/ the exception info.
Environment.FailFast(e.Message, e.Exception);
}
protected async override void OnLaunched(LaunchActivatedEventArgs args)
{
base.OnLaunched(args);
await Task.WhenAll(
GetService<IActivationService>().ActivateAsync(AppInstance.GetCurrent().GetActivatedEventArgs().Data),
GetService<IAccountsService>().InitializeAsync(),
GetService<IAppManagementInitializer>().InitializeAsync());
}
private async void OnActivated(object? sender, AppActivationArguments args)
{
ShowMainWindow();
// Note: Keep the reference to 'args.Data' object, as 'args' may be
// disposed before the async operation completes (RpcCallFailed: 0x800706be)
var localArgsDataReference = args.Data;
// Activate the app and ensure the appropriate handlers are called.
await _dispatcherQueue.EnqueueAsync(async () => await GetService<IActivationService>().ActivateAsync(localArgsDataReference));
}
}