-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTabbedWebViewWindow.xaml.cs
512 lines (446 loc) · 16.3 KB
/
TabbedWebViewWindow.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
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.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace Trivial.UI;
/// <summary>
/// The browser window.
/// </summary>
public sealed partial class TabbedWebViewWindow : Window
{
private readonly SystemBackdropClient backdrop;
/// <summary>
/// Initializes a new instance of the TabbedWebViewWindow class.
/// </summary>
public TabbedWebViewWindow()
{
InitializeComponent();
try
{
ExtendsContentIntoTitleBar = true;
SetTitleBar(CustomDragRegion);
}
catch (ArgumentException)
{
}
catch (NullReferenceException)
{
}
catch (NotImplementedException)
{
}
catch (NotSupportedException)
{
}
catch (InvalidOperationException)
{
}
catch (ExternalException)
{
}
var theme = HostElement.RequestedTheme;
backdrop = new();
backdrop.UpdateWindowBackground(this, theme);
HostElement.WindowController = new Web.BasicWindowStateController(this);
}
/// <summary>
/// Occurs on the tab is closed.
/// </summary>
public event TypedEventHandler<TabbedWebViewWindow, TabViewTabCloseRequestedEventArgs> TabCloseRequested;
/// <summary>
/// Occurs on the tab drags starting.
/// </summary>
public event TypedEventHandler<TabbedWebViewWindow, TabViewTabDragStartingEventArgs> TabDragStarting;
/// <summary>
/// Occurs on the tab drags completed.
/// </summary>
public event TypedEventHandler<TabbedWebViewWindow, TabViewTabDragCompletedEventArgs> TabDragCompleted;
/// <summary>
/// Occurs on the tab is dropped outside.
/// </summary>
public event TypedEventHandler<TabbedWebViewWindow, TabViewTabDroppedOutsideEventArgs> TabDroppedOutside;
/// <summary>
/// Occurs on the web view tab has created.
/// </summary>
public event EventHandler<TabbedWebView.WebViewTabEventArgs> WebViewTabCreated;
/// <summary>
/// Occurs on the web view tab has created.
/// </summary>
public event EventHandler<TabbedWebView.LocalWebAppTabEventArgs> LocalWebAppTabCreated;
/// <summary>
/// Occurs when the webpage send request to close itself.
/// </summary>
public event EventHandler<TabbedWebView.WebViewTabEventArgs> WebViewPageCloseRequested;
/// <summary>
/// Occurs when the webpage send request to close itself.
/// </summary>
public event EventHandler<TabbedWebView.LocalWebAppTabEventArgs> LocalWebAppPageCloseRequested;
/// <summary>
/// Occurs on the selection has changed.
/// </summary>
public event SelectionChangedEventHandler SelectionChanged;
/// <summary>
/// Gets the download list.
/// </summary>
public List<CoreWebView2DownloadOperation> DownloadList
{
get => HostElement.DownloadList;
internal set => HostElement.DownloadList = value;
}
/// <summary>
/// Gets or sets the source.
/// </summary>
public Uri Source
{
get => HostElement.Source;
set => HostElement.Source = value;
}
/// <summary>
/// Gets or sets a value indicating whether the navigation input box is read-only..
/// </summary>
public bool IsReadOnly
{
get => HostElement.IsReadOnly;
set => HostElement.IsReadOnly = value;
}
/// <summary>
/// Gets or sets the tab width mode.
/// </summary>
public TabViewWidthMode TabWidthMode
{
get => HostElement.TabWidthMode;
set => HostElement.TabWidthMode = value;
}
/// <summary>
/// Gets the tabbed web view element.
/// </summary>
public TabbedWebView TabView => HostElement;
/// <summary>
/// Gets all web view instances.
/// </summary>
public IReadOnlyList<SingleWebView> WebViews => HostElement.WebViews;
/// <summary>
/// Gets all local web app page instances.
/// </summary>
public IReadOnlyList<LocalWebAppPage> LocalWebApps => HostElement.LocalWebApps;
/// <summary>
/// Gets all tab view items with web view.
/// </summary>
public IReadOnlyList<TabViewItem> ItemsWithWebView => HostElement.ItemsWithWebView;
/// <summary>
/// Gets or sets the selected item.
/// </summary>
public object SelectedItem
{
get => HostElement.SelectedItem;
set => HostElement.SelectedItem = value;
}
/// <summary>
/// Gets the selected web view.
/// </summary>
public SingleWebView SelectedWebView => HostElement.SelectedItem as SingleWebView;
/// <summary>
/// Gets the selected local web app page.
/// </summary>
public LocalWebAppPage SelectedLocalWebApp => HostElement.SelectedItem as LocalWebAppPage;
/// <summary>
/// Gets or sets the selected index.
/// </summary>
public int SelectedIndex
{
get => HostElement.SelectedIndex;
set => HostElement.SelectedIndex = value;
}
/// <summary>
/// Gets or sets the tab strip header
/// </summary>
public object TabStripHeader
{
get => HostElement.TabStripHeader;
set => HostElement.TabStripHeader = value;
}
/// <summary>
/// Gets or sets the tab strip footer
/// </summary>
public object TabStripFooter
{
get => HostElement.TabStripFooter;
set => HostElement.TabStripFooter = value;
}
/// <summary>
/// Gets or sets the handler to create default URI.
/// </summary>
public Func<Uri> DefaultUriCreator
{
get => HostElement.DefaultUriCreator;
set => HostElement.DefaultUriCreator = value;
}
/// <summary>
/// Gets the the web view in first tab.
/// </summary>
/// <returns>The web view instance.</returns>
public SingleWebView GetFirstWebView()
=> WebViews.FirstOrDefault() ?? Add(null as Uri);
/// <summary>
/// Gets the the web view in first tab.
/// </summary>
/// <returns>The web view instance.</returns>
public SingleWebView GetLastWebView()
=> WebViews.LastOrDefault() ?? Add(null as Uri);
/// <summary>
/// Gets a specific local web app page.
/// </summary>
/// <param name="resourcePackageIdentifier">The identifier of the resource package.</param>
/// <returns>The local web app page; or null, if does not exist.</returns>
public LocalWebAppPage GetLocalWebAppPage(string resourcePackageIdentifier)
=> HostElement.GetLocalWebAppPage(resourcePackageIdentifier);
/// <summary>
/// Gets the tab view item.
/// </summary>
/// <param name="webview">The web view.</param>
/// <returns>The tab view item which contains the web view; or null, if non-exists.</returns>
public TabViewItem GetTabItem(SingleWebView webview)
=> HostElement.GetTabItem(webview);
/// <summary>
/// Gets the tab view item.
/// </summary>
/// <param name="webview">The local web app page.</param>
/// <returns>The tab view item which contains the web view; or null, if non-exists.</returns>
public TabViewItem GetTabItem(LocalWebAppPage webview)
=> HostElement.GetTabItem(webview);
/// <summary>
/// Adds a new tab.
/// </summary>
/// <param name="tab">The tab view item to add.</param>
public void Add(TabViewItem tab)
=> HostElement.Add(tab);
/// <summary>
/// Adds a new tab.
/// </summary>
/// <param name="title">The tab header.</param>
/// <param name="content">The content of the tab.</param>
/// <param name="icon">An optional icon.</param>
/// <returns>The tab view item instance.</returns>
public TabViewItem Add(string title, UIElement content, IconSource icon = null)
=> HostElement.Add(title, content, icon);
/// <summary>
/// Adds a new web view.
/// </summary>
/// <param name="source">The source URI.</param>
/// <param name="callback">The callback.</param>
/// <returns>The web view instance.</returns>
public SingleWebView Add(Uri source, Action<TabViewItem> callback = null)
=> HostElement.Add(source, callback);
/// <summary>
/// Adds a new web view.
/// </summary>
/// <param name="host">The local web app host.</param>
/// <param name="callback">The callback.</param>
/// <returns>The web view instance.</returns>
public Task<LocalWebAppPage> AddAsync(Web.LocalWebAppHost host, Action<TabViewItem, LocalWebAppPage> callback = null)
=> HostElement.AddAsync(host, callback);
/// <summary>
/// Adds a new web view.
/// </summary>
/// <param name="host">The local web app host.</param>
/// <param name="callback">The callback.</param>
/// <returns>The web view instance.</returns>
public Task<LocalWebAppPage> AddAsync(Task<Web.LocalWebAppHost> host, Action<TabViewItem, LocalWebAppPage> callback = null)
=> HostElement.AddAsync(host, callback);
/// <summary>
/// Adds a new web view.
/// </summary>
/// <param name="host">The local web app host.</param>
/// <param name="showInfo">true if show the information before loading; otherwise, false.</param>
/// <param name="callback">The callback.</param>
/// <returns>The web view instance.</returns>
public Task<LocalWebAppPage> AddAsync(Task<Web.LocalWebAppHost> host, bool showInfo, Action<TabViewItem, LocalWebAppPage> callback = null)
=> HostElement.AddAsync(host, showInfo, callback);
/// <summary>
/// Adds a new web view.
/// </summary>
/// <param name="tab">The tab view item.</param>
/// <param name="source">The source URI.</param>
/// <param name="callback">The callback.</param>
/// <returns>The web view instance.</returns>
public SingleWebView NavigateTo(TabViewItem tab, Uri source, Action<TabViewItem> callback = null)
=> HostElement.NavigateTo(tab, source, callback);
/// <summary>
/// Inserts a new tab.
/// </summary>
/// <param name="index">The index.</param>
/// <param name="tab">The tab view item to insert.</param>
public void Insert(int index, TabViewItem tab)
=> HostElement.Insert(index, tab);
/// <summary>
/// Removes a specific tab.
/// </summary>
/// <param name="item">The tab view item to remove.</param>
/// <returns>true if remove succeeded; otherwise, false.</returns>
public bool Remove(object item)
=> HostElement.Remove(item);
/// <summary>
/// Removes a specific tab.
/// </summary>
/// <param name="index">The index to remove.</param>
/// <returns>true if remove succeeded; otherwise, false.</returns>
public void RemoveAt(int index)
=> HostElement.RemoveAt(index);
/// <summary>
/// Clears all tabs.
/// </summary>
public void Clear()
=> HostElement.Clear();
/// <summary>
/// Determines whether the tabs contains a specific value.
/// </summary>
/// <param name="tab">The tab view item to test.</param>
/// <returns>true if contains; otherwise, false.</returns>
public bool Contains(TabViewItem tab)
=> HostElement.Contains(tab);
/// <summary>
/// Determines whether the tabs contains a specific value.
/// </summary>
/// <param name="webview">The web view to test.</param>
/// <returns>true if contains; otherwise, false.</returns>
public bool Contains(SingleWebView webview)
=> HostElement.Contains(webview);
/// <summary>
/// Determines whether the tabs contains a specific value.
/// </summary>
/// <param name="webview">The web view to test.</param>
/// <returns>true if contains; otherwise, false.</returns>
public bool Contains(LocalWebAppPage webview)
=> HostElement.Contains(webview);
internal async Task OpenLocalWebApp(LocalWebAppHubPage p, Web.LocalWebAppInfo info, bool dev)
{
var id = info?.ResourcePackageId;
var page = GetLocalWebAppPage(id);
if (page != null)
{
var tab = GetTabItem(page);
if (tab != null)
{
tab.IsSelected = true;
return;
}
}
if (dev)
{
var dir = IO.FileSystemInfoUtility.TryGetDirectoryInfo(info.LocalPath);
if (dir != null && dir.Exists)
{
LocalWebAppPage p2 = null;
try
{
await AddAsync(Web.LocalWebAppHost.LoadDevPackageAsync(dir), true, (tab, page) =>
{
p2 = page;
page.IsDevEnvironmentEnabled = true;
});
}
catch (InvalidOperationException ex)
{
if (ex.Message != "Miss config file.") throw;
if (p.CreateDevAppHandler?.Invoke(p, p2) != true) throw;
}
return;
}
}
if (string.IsNullOrWhiteSpace(id)) return;
await AddAsync(Web.LocalWebAppHost.LoadAsync(id));
}
private void OnClosed(object sender, WindowEventArgs args)
{
if (backdrop != null) backdrop.Dispose();
try
{
var col = HostElement.WebViews.ToList();
foreach (var wv in col)
{
wv.Close();
}
}
catch (InvalidOperationException)
{
}
catch (NullReferenceException)
{
}
try
{
var col2 = HostElement.LocalWebApps.ToList();
foreach (var wv in col2)
{
wv.Close();
}
}
catch (InvalidOperationException)
{
}
catch (NullReferenceException)
{
}
}
private void OnTabCloseRequested(TabbedWebView sender, TabViewTabCloseRequestedEventArgs args)
{
TabCloseRequested?.Invoke(this, args);
if (HostElement.TabItems.Count < 1) Close();
}
private void OnActivated(object sender, WindowActivatedEventArgs args)
{
if (backdrop == null) return;
var isActive = args.WindowActivationState != WindowActivationState.Deactivated;
backdrop.IsInputActive = isActive;
}
private void OnWebViewTabCreated(object sender, TabbedWebView.WebViewTabEventArgs e)
=> WebViewTabCreated?.Invoke(this, e);
private void OnLocalWebAppTabCreated(object sender, TabbedWebView.LocalWebAppTabEventArgs e)
=> LocalWebAppTabCreated?.Invoke(this, e);
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
=> SelectionChanged?.Invoke(this, e);
private void HostElement_TabDragStarting(TabbedWebView sender, TabViewTabDragStartingEventArgs args)
=> TabDragStarting?.Invoke(this, args);
private void HostElement_TabDragCompleted(TabbedWebView sender, TabViewTabDragCompletedEventArgs args)
=> TabDragCompleted?.Invoke(this, args);
private void HostElement_TabDroppedOutside(TabbedWebView sender, TabViewTabDroppedOutsideEventArgs args)
=> TabDroppedOutside?.Invoke(this, args);
private void HostElement_ContainsFullScreenElementChanged(object sender, Data.DataEventArgs<int> e)
=> VisualUtility.SetFullScreenMode(e.Data > 0, this);
private void HostElement_WebViewPageCloseRequested(object sender, TabbedWebView.WebViewTabEventArgs e)
{
WebViewPageCloseRequested?.Invoke(this, e);
_ = TestWindowCloseAsync();
}
private void HostElement_LocalWebAppPageCloseRequested(object sender, TabbedWebView.LocalWebAppTabEventArgs e)
{
LocalWebAppPageCloseRequested?.Invoke(this, e);
_ = TestWindowCloseAsync();
}
private async Task TestWindowCloseAsync()
{
await Task.Delay(400);
try
{
if (HostElement.TabItems.Count < 1) Close();
}
catch (InvalidOperationException)
{
}
}
}