-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLocalWebAppWindow.xaml.cs
513 lines (439 loc) · 17.6 KB
/
LocalWebAppWindow.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.Web.WebView2.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using System.Threading.Tasks;
using Trivial.Data;
using Trivial.Text;
using Trivial.UI;
using Trivial.Web;
using Windows.Foundation;
using Windows.Foundation.Collections;
namespace Trivial.UI;
/// <summary>
/// The local standalone local web app window.
/// </summary>
public sealed partial class LocalWebAppWindow : Window
{
private readonly SystemBackdropClient backdrop;
private double titleHeight = 28;
/// <summary>
/// Initializes a new instance of the LocalWebAppWindow class.
/// </summary>
public LocalWebAppWindow()
{
InitializeComponent();
Title = TitleElement.Text = string.IsNullOrWhiteSpace(LocalWebAppSettings.CustomizedLocaleStrings.Loading) ? "Loading…" : LocalWebAppSettings.CustomizedLocaleStrings.Loading;
try
{
ExtendsContentIntoTitleBar = true;
SetTitleBar(TitleBackground);
}
catch (ArgumentException)
{
}
catch (NullReferenceException)
{
}
catch (NotImplementedException)
{
}
catch (NotSupportedException)
{
}
catch (InvalidOperationException)
{
}
catch (ExternalException)
{
}
var theme = MainElement.RequestedTheme;
backdrop = new();
backdrop.UpdateWindowBackground(this, theme);
MainElement.WindowController = new BasicWindowStateController(this);
}
/// <summary>
/// Initializes a new instance of the LocalWebAppWindow class.
/// </summary>
public LocalWebAppWindow(LocalWebAppOptions options) : this()
=> _ = MainElement.LoadAsync(options);
/// <summary>
/// Occurs when the web view core processes failed.
/// </summary>
public event TypedEventHandler<LocalWebAppWindow, CoreWebView2ProcessFailedEventArgs> CoreProcessFailed;
/// <summary>
/// Occurs when the web view core has initialized.
/// </summary>
public event TypedEventHandler<LocalWebAppWindow, CoreWebView2InitializedEventArgs> CoreWebView2Initialized;
/// <summary>
/// Occurs when the navigation is completed.
/// </summary>
public event TypedEventHandler<LocalWebAppWindow, CoreWebView2NavigationCompletedEventArgs> NavigationCompleted;
/// <summary>
/// Occurs when the navigation is starting.
/// </summary>
public event TypedEventHandler<LocalWebAppWindow, CoreWebView2NavigationStartingEventArgs> NavigationStarting;
/// <summary>
/// Occurs when the new window request sent.
/// </summary>
public event TypedEventHandler<LocalWebAppWindow, CoreWebView2NewWindowRequestedEventArgs> NewWindowRequested;
/// <summary>
/// Occurs when the webpage send request to close itself.
/// </summary>
public event TypedEventHandler<LocalWebAppWindow, object> WindowCloseRequested;
/// <summary>
/// Occurs when the downloading is starting.
/// </summary>
public event TypedEventHandler<LocalWebAppWindow, CoreWebView2DownloadStartingEventArgs> DownloadStarting;
/// <summary>
/// Gets or sets the title height.
/// </summary>
public double TitleHeight
{
get
{
return titleHeight;
}
set
{
titleHeight = value;
TitleRow.Height = new(value);
}
}
/// <summary>
/// Gets the identifier of the resource package.
/// </summary>
public string ResourcePackageId => MainElement.ResourcePackageId;
/// <summary>
/// Gets the display name of the resource package.
/// </summary>
public string ResourcePackageDisplayName => MainElement.ResourcePackageDisplayName;
/// <summary>
/// Gets the version string of the resource package.
/// </summary>
public string ResourcePackageVersion => MainElement.ResourcePackageVersion;
/// <summary>
/// Gets the copyright string of the resource package.
/// </summary>
public string ResourcePackageCopyright => MainElement.ResourcePackageCopyright;
/// <summary>
/// Gets the publisher name of the resource package.
/// </summary>
public string ResourcePackagePublisherName => MainElement.ResourcePackagePublisherName;
/// <summary>
/// Gets the description of the resource package.
/// </summary>
public string ResourcePackageDescription => MainElement.ResourcePackageDescription;
/// <summary>
/// Gets the relative icon path of the resource package.
/// </summary>
public string ResourcePackageIcon => MainElement.ResourcePackageIcon;
/// <summary>
/// Gets the website URL of the resource package.
/// </summary>
public string ResourcePackageWebsite => MainElement.ResourcePackageWebsite;
/// <summary>
/// Gets the download list.
/// </summary>
public List<CoreWebView2DownloadOperation> DownloadList => MainElement.DownloadList;
/// <summary>
/// Gets or sets a value indicating whether it is in debug mode to ignore any signature verification and enable Microsoft Edge DevTools.
/// </summary>
public bool IsDevEnvironmentEnabled
{
get => MainElement.IsDevEnvironmentEnabled;
set => MainElement.IsDevEnvironmentEnabled = value;
}
/// <summary>
/// Gets the available new version to update.
/// </summary>
public string NewVersionAvailable => MainElement.NewVersionAvailable;
/// <summary>
/// Gets or sets a value indicating whether disable to create a default tabbed browser for new window request.
/// </summary>
public bool DisableNewWindowRequestHandling
{
get => MainElement.DisableNewWindowRequestHandling;
set => MainElement.DisableNewWindowRequestHandling = value;
}
/// <summary>
/// Gets or sets the default background color of the browser.
/// </summary>
public Windows.UI.Color BrowserBackgroundColor
{
get => MainElement.BrowserBackgroundColor;
set => MainElement.BrowserBackgroundColor = value;
}
/// <summary>
/// Gets the a value indicating whether contains the full screen element.
/// </summary>
public bool ContainsFullScreenElement => MainElement.ContainsFullScreenElement;
/// <summary>
/// Gets a value indicating whether the browser can go back.
/// </summary>
public bool CanGoBack => MainElement.CanGoBack;
/// <summary>
/// Gets a value indicating whether the browser can go forward.
/// </summary>
public bool CanGoForward => MainElement.CanGoForward;
/// <summary>
/// Gets or sets the image source of the icon.
/// </summary>
public ImageSource IconImageSource
{
get
{
return IconElement.Source;
}
set
{
TitleElement.Margin = new Thickness(value != null ? 28 : 10, 0, 32, 0);
IconElement.Source = value;
}
}
/// <summary>
/// Gets or sets the handler occuring on the default browser window is created.
/// </summary>
public Action<TabbedWebViewWindow> OnWindowCreate
{
get => MainElement.OnWindowCreate;
set => MainElement.OnWindowCreate = value;
}
/// <summary>
/// Gets the options.
/// </summary>
public LocalWebAppOptions Options => MainElement.Options;
/// <summary>
/// Gets a value indicating whether the app is verified.
/// </summary>
public bool IsVerified => MainElement.IsVerified;
/// <summary>
/// Goes back.
/// </summary>
public void GoBack()
=> MainElement.GoBack();
/// <summary>
/// Goes back.
/// </summary>
public void GoForward()
=> MainElement.GoForward();
/// <summary>
/// Goes back.
/// </summary>
public void ReloadPage()
=> MainElement.ReloadPage();
/// <summary>
/// Executes JavaScript.
/// </summary>
/// <param name="javascriptCode">The code to execute.</param>
/// <returns>Value returned.</returns>
public Task<string> ExecuteScriptAsync(string javascriptCode)
=> MainElement.ExecuteScriptAsync(javascriptCode);
/// <summary>
/// Load a dev local web app.
/// </summary>
/// <returns>The async task.</returns>
/// <param name="cancellationToken">The optional cancellation token to cancel operation.</param>
public Task LoadDevPackageAsync(CancellationToken cancellationToken = default)
=> MainElement.LoadDevPackageAsync(this, cancellationToken);
/// <summary>
/// Load a dev local web app.
/// </summary>
/// <param name="dir">The root directory.</param>
/// <param name="cancellationToken">The optional cancellation token to cancel operation.</param>
public Task LoadDevPackageAsync(DirectoryInfo dir, CancellationToken cancellationToken = default)
=> MainElement.LoadDevPackageAsync(dir, cancellationToken);
/// <summary>
/// Loads data.
/// </summary>
/// <param name="resourcePackageId">The identifier of the resource package.</param>
public Task LoadAsync(string resourcePackageId)
=> MainElement.LoadAsync(resourcePackageId);
/// <summary>
/// Loads data.
/// </summary>
/// <param name="options">The options of the standalone local web app.</param>
public Task LoadAsync(LocalWebAppOptions options)
=> MainElement.LoadAsync(options);
/// <summary>
/// Loads data.
/// </summary>
/// <param name="host">The standalone local web app host.</param>
public Task LoadAsync(LocalWebAppHost host)
=> MainElement.LoadAsync(host);
/// <summary>
/// Loads data.
/// </summary>
/// <param name="host">The standalone local web app host.</param>
/// <param name="callback">The callback.</param>
/// <param name="error">The error handling.</param>
public Task LoadAsync(Task<LocalWebAppHost> host, Action<LocalWebAppHost> callback = null, Action<Exception> error = null)
=> MainElement.LoadAsync(host, callback, error);
/// <summary>
/// Updates the resource package.
/// </summary>
/// <param name="cancellationToken">The optional cancellation token to cancel operation.</param>
/// <returns>The new version.</returns>
public Task UpdateAsync(CancellationToken cancellationToken)
=> MainElement.UpdateAsync(cancellationToken);
/// <summary>
/// Sends a notification message to webpage.
/// </summary>
/// <param name="handler">The sender.</param>
/// <param name="type">The message type.</param>
/// <param name="message">The message body to send.</param>
public void Notify(ILocalWebAppCommandHandler handler, string type, LocalWebAppNotificationMessage message)
=> MainElement.Notify(handler, type, message);
/// <summary>
/// Stops progress ring.
/// </summary>
public void StopLoading()
=> MainElement.StopLoading();
/// <summary>
/// Shows notification.
/// </summary>
/// <param name="title">The title.</param>
/// <param name="message">The message.</param>
/// <param name="severity">The severity.</param>
public void ShowNotification(string title, string message, InfoBarSeverity severity)
=> MainElement.ShowNotification(title, message, severity);
/// <summary>
/// Focuses the browser.
/// </summary>
/// <param name="value">The focus state.</param>
public void FocusBrowser(FocusState value)
=> MainElement.Focus(value);
/// <summary>
/// Tests if there is the command handler of given identifier.
/// </summary>
/// <param name="id">The handler identifier.</param>
/// <returns>true if exists; otherwise, false.</returns>
/// <exception cref="ArgumentNullException">id was null.</exception>
public bool ContainsCommandHandler(string id)
=> MainElement.ContainsCommandHandler(id);
/// <summary>
/// Tries to get the specific command handler.
/// </summary>
/// <param name="id">The handler identifier.</param>
/// <param name="handler">The process handler.</param>
/// <returns>true if exists; otherwise, false.</returns>
/// <exception cref="ArgumentNullException">id was null.</exception>
public bool TryGetCommandHandler(string id, out ILocalWebAppCommandHandler handler)
=> MainElement.TryGetCommandHandler(id, out handler);
/// <summary>
/// Tries to get the specific command handler.
/// </summary>
/// <param name="id">The handler identifier.</param>
/// <returns>The command handler..</returns>
/// <exception cref="ArgumentNullException">id was null.</exception>
public ILocalWebAppCommandHandler TryGetCommandHandler(string id)
=> MainElement.TryGetCommandHandler(id);
/// <summary>
/// Registers a command handler. It will override the existed one.
/// </summary>
/// <param name="handler">The process handler to add.</param>
/// <exception cref="ArgumentNullException">id was null.</exception>
public void RegisterCommandHandler(ILocalWebAppCommandHandler handler)
=> MainElement.RegisterCommandHandler(handler);
/// <summary>
/// Removes the command handler.
/// </summary>
/// <param name="id">The handler identifier.</param>
/// <returns>true if remove succeeded; otherwise, false.</returns>
public bool RemoveCommandHandler(string id)
=> MainElement.RemoveCommandHandler(id);
/// <summary>
/// Removes the command handler.
/// </summary>
/// <param name="handler">The process handler to remove.</param>
/// <returns>true if remove succeeded; otherwise, false.</returns>
public bool RemoveCommandHandler(ILocalWebAppCommandHandler handler)
=> MainElement.RemoveCommandHandler(handler);
/// <summary>
/// Maps a folder as a virtual host name.
/// </summary>
/// <param name="hostName">The virtual host name.</param>
/// <param name="folderPath">The folder path to map.</param>
/// <param name="accessKind">The access kind.</param>
public void SetVirtualHostNameToFolderMapping(string hostName, string folderPath, CoreWebView2HostResourceAccessKind accessKind)
=> MainElement.SetVirtualHostNameToFolderMapping(hostName, folderPath, accessKind);
/// <summary>
/// Clears the mapping of the specific virtual host name.
/// </summary>
/// <param name="hostName">The virtual host name.</param>
public void ClearVirtualHostNameToFolderMapping(string hostName)
=> MainElement.ClearVirtualHostNameToFolderMapping(hostName);
/// <summary>
/// Prints to PDF format file.
/// </summary>
/// <param name="path">The output path of the PDF format file.</param>
/// <param name="printSettings">The print settings.</param>
/// <returns>true if print succeeded; otherwise, false.</returns>
public Task<bool> PrintToPdfAsync(string path, CoreWebView2PrintSettings printSettings)
=> MainElement.PrintToPdfAsync(path, printSettings);
/// <summary>
/// Opens the default download dialog.
/// </summary>
public void OpenDefaultDownloadDialog()
=> MainElement.OpenDefaultDownloadDialog();
/// <summary>
/// Closes the default download dialog.
/// </summary>
public void CloseDefaultDownloadDialog()
=> MainElement.CloseDefaultDownloadDialog();
internal void SetFullScreen(bool value)
{
VisualUtility.SetFullScreenMode(value, this);
TitleRow.Height = value ? new(0) : new(titleHeight);
}
private void OnClosed(object sender, WindowEventArgs args)
{
if (backdrop != null) backdrop.Dispose();
MainElement.Close();
MainElement.WindowController = null;
}
private void OnTitleChanged(object sender, DataEventArgs<string> e)
=> Title = TitleElement.Text = e.Data;
private void OnActivated(object sender, WindowActivatedEventArgs args)
{
if (backdrop == null) return;
var isActive = args.WindowActivationState != WindowActivationState.Deactivated;
backdrop.IsInputActive = isActive;
TitleElement.Opacity = isActive ? 1 : 0.6;
}
private void OnCoreProcessFailed(LocalWebAppPage sender, CoreWebView2ProcessFailedEventArgs args)
=> CoreProcessFailed?.Invoke(this, args);
private void OnCoreWebViewInitialized(LocalWebAppPage sender, CoreWebView2InitializedEventArgs args)
=> CoreWebView2Initialized?.Invoke(this, args);
private void OnNavigationCompleted(LocalWebAppPage sender, CoreWebView2NavigationCompletedEventArgs args)
=> NavigationCompleted?.Invoke(this, args);
private void OnNavigationStarting(LocalWebAppPage sender, CoreWebView2NavigationStartingEventArgs args)
=> NavigationStarting?.Invoke(this, args);
private void OnNewWindowRequested(LocalWebAppPage sender, CoreWebView2NewWindowRequestedEventArgs args)
=> NewWindowRequested?.Invoke(this, args);
private void OnWindowCloseRequested(LocalWebAppPage sender, object args)
{
_ = OnWindowCloseRequestedAsync();
WindowCloseRequested?.Invoke(this, args);
}
private async Task OnWindowCloseRequestedAsync()
{
await Task.Delay(600);
Close();
}
private void OnDownloadStarting(LocalWebAppPage sender, CoreWebView2DownloadStartingEventArgs args)
=> DownloadStarting?.Invoke(this, args);
private void MainElement_ContainsFullScreenElementChanged(object sender, DataEventArgs<bool> e)
=> SetFullScreen(e.Data);
}