-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTabbedWebView.xaml.cs
872 lines (783 loc) · 27.8 KB
/
TabbedWebView.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
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 Trivial.Data;
using Windows.Foundation;
using Windows.Foundation.Collections;
namespace Trivial.UI;
using DependencyObjectProxy = DependencyObjectProxy<TabbedWebView>;
/// <summary>
/// The tabbed web view.
/// </summary>
public sealed partial class TabbedWebView : UserControl
{
/// <summary>
/// The event arguments of web view tab.
/// </summary>
public class WebViewTabEventArgs : EventArgs
{
/// <summary>
/// Initializes a new instance of the WebViewTabEventArgs class.
/// </summary>
/// <param name="tab">The tab view item instance.</param>
/// <param name="webview">The single web view instance.</param>
/// <param name="source">The initialized source URI to navigate.</param>
public WebViewTabEventArgs(TabViewItem tab, SingleWebView webview, Uri source)
{
Tab = tab;
WebView = webview;
Source = source;
}
/// <summary>
/// Gets or sets the tab view item instance.
/// </summary>
public TabViewItem Tab { get; }
/// <summary>
/// Gets or sets the web view instance.
/// </summary>
public SingleWebView WebView { get; }
/// <summary>
/// Gets or sets the initializied source URI to navigate.
/// </summary>
public Uri Source { get; }
}
/// <summary>
/// The event arguments of local web app tab.
/// </summary>
public class LocalWebAppTabEventArgs : EventArgs
{
/// <summary>
/// Initializes a new instance of the LocalWebAppTabEventArgs class.
/// </summary>
/// <param name="tab">The tab view item instance.</param>
/// <param name="page">The single web view instance.</param>
public LocalWebAppTabEventArgs(TabViewItem tab, LocalWebAppPage page)
{
Tab = tab;
Page = page;
}
/// <summary>
/// Gets or sets the tab view item instance.
/// </summary>
public TabViewItem Tab { get; }
/// <summary>
/// Gets or sets the web view instance.
/// </summary>
public LocalWebAppPage Page { get; }
}
/// <summary>
/// The dependency property of item style.
/// </summary>
public static readonly DependencyProperty TabViewStyleProperty = DependencyObjectProxy.RegisterProperty<Style>(nameof(TabViewStyle));
/// <summary>
/// The dependency property of source
/// </summary>
public static readonly DependencyProperty SourceProperty = DependencyObjectProxy.RegisterProperty<Uri>(nameof(Source), (c, e, p) => c.NavigateTo(c.ItemsWithWebView.FirstOrDefault(), e.NewValue));
/// <summary>
/// The dependency property of the flag indicating whether the navigation input box is read-only.
/// </summary>
public static readonly DependencyProperty IsReadOnlyProperty = DependencyObjectProxy.RegisterProperty(nameof(IsReadOnly), UpdateReadOnly, false);
/// <summary>
/// The dependency property of the tab width mode.
/// </summary>
public static readonly DependencyProperty TabWidthModeProperty = DependencyObjectProxy.RegisterProperty<TabViewWidthMode>(nameof(TabWidthMode));
/// <summary>
/// Initialized a new instance of the TabbedWebView class.
/// </summary>
public TabbedWebView()
{
InitializeComponent();
}
/// <summary>
/// Occurs when fullscreen request sent including to enable and disable.
/// </summary>
public event DataEventHandler<int> ContainsFullScreenElementChanged;
/// <summary>
/// Occurs on the tab is closed.
/// </summary>
public event TypedEventHandler<TabbedWebView, TabViewTabCloseRequestedEventArgs> TabCloseRequested;
/// <summary>
/// Occurs on the tab drags starting.
/// </summary>
public event TypedEventHandler<TabbedWebView, TabViewTabDragStartingEventArgs> TabDragStarting;
/// <summary>
/// Occurs on the tab drags completed.
/// </summary>
public event TypedEventHandler<TabbedWebView, TabViewTabDragCompletedEventArgs> TabDragCompleted;
/// <summary>
/// Occurs on the tab is dropped outside.
/// </summary>
public event TypedEventHandler<TabbedWebView, TabViewTabDroppedOutsideEventArgs> TabDroppedOutside;
/// <summary>
/// Occurs on the web view tab has created.
/// </summary>
public event EventHandler<WebViewTabEventArgs> WebViewTabCreated;
/// <summary>
/// Occurs on the local web app tab has created.
/// </summary>
public event EventHandler<LocalWebAppTabEventArgs> LocalWebAppTabCreated;
/// <summary>
/// Occurs when the webpage send request to close itself.
/// </summary>
public event EventHandler<WebViewTabEventArgs> WebViewPageCloseRequested;
/// <summary>
/// Occurs when the webpage send request to close itself.
/// </summary>
public event EventHandler<LocalWebAppTabEventArgs> LocalWebAppPageCloseRequested;
/// <summary>
/// Occurs on the selection has changed.
/// </summary>
public event SelectionChangedEventHandler SelectionChanged;
/// <summary>
/// Gets or sets the style of tab view.
/// </summary>
public Style TabViewStyle
{
get => (Style)GetValue(TabViewStyleProperty);
set => SetValue(TabViewStyleProperty, value);
}
/// <summary>
/// Gets the download list.
/// </summary>
public List<CoreWebView2DownloadOperation> DownloadList { get; internal set; } = new();
/// <summary>
/// Gets or sets the source.
/// </summary>
public Uri Source
{
get => (Uri)GetValue(SourceProperty);
set => SetValue(SourceProperty, value);
}
/// <summary>
/// Gets or sets a value indicating whether the navigation input box is read-only..
/// </summary>
public bool IsReadOnly
{
get => (bool)GetValue(IsReadOnlyProperty);
set => SetValue(IsReadOnlyProperty, value);
}
/// <summary>
/// Gets or sets the tab width mode.
/// </summary>
public TabViewWidthMode TabWidthMode
{
get => (TabViewWidthMode)GetValue(TabWidthModeProperty);
set => SetValue(TabWidthModeProperty, value);
}
/// <summary>
/// Gets all web view instances.
/// </summary>
public IList<object> TabItems => HostElement.TabItems;
/// <summary>
/// Gets all web view instances.
/// </summary>
public IReadOnlyList<SingleWebView> WebViews => HostElement.TabItems?.OfType<TabViewItem>()?.Select(ele => ele?.Content as SingleWebView)?.Where(ele => ele != null)?.ToList() ?? new List<SingleWebView>();
/// <summary>
/// Gets all local web app page instances.
/// </summary>
public IReadOnlyList<LocalWebAppPage> LocalWebApps => HostElement.TabItems?.OfType<TabViewItem>()?.Select(ele => ele?.Content as LocalWebAppPage)?.Where(ele => ele != null)?.ToList() ?? new List<LocalWebAppPage>();
/// <summary>
/// Gets all tab view items with web view.
/// </summary>
public IReadOnlyList<TabViewItem> ItemsWithWebView => HostElement.TabItems?.OfType<TabViewItem>()?.Where(ele => ele?.Content is SingleWebView)?.ToList() ?? new List<TabViewItem>();
/// <summary>
/// Gets all tab view items with local web app page.
/// </summary>
public IReadOnlyList<TabViewItem> ItemsWithLocalWebApp => HostElement.TabItems?.OfType<TabViewItem>()?.Where(ele => ele?.Content is LocalWebAppPage)?.ToList() ?? new List<TabViewItem>();
/// <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 an instance style that is applied for the tab view during layout and rendering.
/// </summary>
public Style TabStyle
{
get => HostElement.Style;
set => HostElement.Style = value;
}
/// <summary>
/// Gets or sets the handler to create default URI.
/// </summary>
public Func<Uri> DefaultUriCreator { get; set; }
/// <summary>
/// Gets or sets the window controller.
/// </summary>
public Web.IBasicWindowStateController WindowController { get; set; }
/// <summary>
/// Gets 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 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)
=> string.IsNullOrWhiteSpace(resourcePackageIdentifier) ? null : LocalWebApps.FirstOrDefault(ele => ele?.ResourcePackageId == 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)
=> webview == null ? null : HostElement.TabItems?.OfType<TabViewItem>()?.FirstOrDefault(ele => ele?.Content is SingleWebView v && v == 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)
=> webview == null ? null : HostElement.TabItems?.OfType<TabViewItem>()?.FirstOrDefault(ele => ele?.Content is LocalWebAppPage v && v == webview);
/// <summary>
/// Gets the tab view item.
/// </summary>
/// <param name="host">The local web app host.</param>
/// <returns>The tab view item which contains the web view; or null, if non-exists.</returns>
public TabViewItem GetTabItem(Web.LocalWebAppHost host)
=> host == null ? null : HostElement.TabItems?.OfType<TabViewItem>()?.FirstOrDefault(ele => ele?.Content is LocalWebAppPage v && v.IsHost(host));
/// <summary>
/// Adds a new tab.
/// </summary>
/// <param name="tab">The tab view item to add.</param>
public void Add(TabViewItem tab)
{
if (tab != null) HostElement.TabItems.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)
{
var tab = new TabViewItem
{
Header = title,
Content = content,
IconSource = icon,
IsSelected = true
};
HostElement.TabItems.Add(tab);
return tab;
}
/// <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)
=> NavigateTo(null, source, callback);
/// <summary>
/// Adds a new tab.
/// </summary>
/// <param name="host">The host.</param>
/// <param name="callback">The callback.</param>
public async Task<LocalWebAppPage> AddAsync(Web.LocalWebAppHost host, Action<TabViewItem, LocalWebAppPage> callback = null)
{
if (host == null) return null;
foreach (var tabItem in HostElement.TabItems)
{
if (tabItem is not TabViewItem tabView || tabView.Content is not LocalWebAppPage page || !page.IsHost(host)) continue;
tabView.IsSelected = true;
try
{
page.FocusBrowser(FocusState.Programmatic);
}
catch (InvalidOperationException)
{
}
catch (ExternalException)
{
}
return page;
}
var c = AddLocalWebAppTab(callback);
await c.LoadAsync(host);
try
{
c.FocusBrowser(FocusState.Programmatic);
}
catch (InvalidOperationException)
{
}
catch (ExternalException)
{
}
return c;
}
/// <summary>
/// Adds a new tab.
/// </summary>
/// <param name="hostTask">The host.</param>
/// <param name="callback">The callback.</param>
public Task<LocalWebAppPage> AddAsync(Task<Web.LocalWebAppHost> hostTask, Action<TabViewItem, LocalWebAppPage> callback = null)
=> AddAsync(hostTask, false, callback);
/// <summary>
/// Adds a new tab.
/// </summary>
/// <param name="hostTask">The host.</param>
/// <param name="showInfo">true if show the information before loading; otherwise, false.</param>
/// <param name="callback">The callback.</param>
public async Task<LocalWebAppPage> AddAsync(Task<Web.LocalWebAppHost> hostTask, bool showInfo, Action<TabViewItem, LocalWebAppPage> callback = null)
{
if (hostTask == null) return null;
var c = AddLocalWebAppTab(callback);
await c.LoadAsync(hostTask, showInfo);
return c;
}
/// <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)
{
SingleWebView c;
var name = source == null ? "Blank" : (source.Host ?? "Loading…");
if (tab == null)
{
if (source == null)
{
foreach (var testTab in HostElement.TabItems)
{
if (testTab is not TabViewItem tabItem || tabItem.Content is not SingleWebView webview) continue;
if (webview.Source != null) continue;
HostElement.SelectedItem = tabItem;
return webview;
}
}
c = new SingleWebView
{
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
IsReadOnly = IsReadOnly,
DownloadList = DownloadList
};
tab = new TabViewItem
{
Header = name,
Content = c,
IsSelected = true,
};
callback?.Invoke(tab);
HostElement.TabItems.Add(tab);
WebViewTabCreated?.Invoke(this, new WebViewTabEventArgs(tab, c, source));
}
else
{
c = tab.Content as SingleWebView;
tab.Header = name;
if (c == null) return null;
callback?.Invoke(tab);
}
c.DocumentTitleChanged += (sender, e) =>
{
tab.Header = e.Data ?? string.Empty;
};
c.NewWindowRequested += OnNewWindowRequested;
c.ContainsFullScreenElementChanged += OnContainsFullScreenElementChanged;
c.WindowCloseRequested += (sender, e) =>
{
try
{
WebViewPageCloseRequested?.Invoke(this, new WebViewTabEventArgs(tab, c, source));
}
finally
{
try
{
c.Close();
}
catch (InvalidOperationException)
{
}
catch (NullReferenceException)
{
}
catch (ApplicationException)
{
}
catch (ExternalException)
{
}
try
{
HostElement.TabItems.Remove(tab);
}
catch (InvalidOperationException)
{
}
catch (ExternalException)
{
}
}
};
if (source != null) c.Source = source;
try
{
c.Focus(FocusState.Programmatic);
}
catch (InvalidOperationException)
{
}
catch (ExternalException)
{
}
return c;
}
/// <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)
{
if (tab == null) return;
HostElement.TabItems.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)
=> item is not null && HostElement.TabItems.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)
{
var item = HostElement.TabItems.Skip(index).FirstOrDefault();
if (item == null) return;
try
{
if (item is TabViewItem tab)
{
var webview = GetWebView(tab);
if (webview != null)
{
webview.Close();
}
else
{
GetLocalWebApp(tab)?.Close();
}
}
}
catch (InvalidOperationException)
{
}
catch (NullReferenceException)
{
}
catch (ApplicationException)
{
}
catch (ExternalException)
{
}
HostElement.TabItems.Remove(item);
}
/// <summary>
/// Clears all tabs.
/// </summary>
public void Clear()
{
var col = HostElement.TabItems.OfType<TabViewItem>().ToList();
foreach (var tab in col)
{
try
{
var webview = GetWebView(tab);
if (webview != null)
{
webview.Close();
continue;
}
GetLocalWebApp(tab)?.Close();
}
catch (InvalidOperationException)
{
}
catch (NullReferenceException)
{
}
catch (ApplicationException)
{
}
catch (ExternalException)
{
}
}
HostElement.TabItems.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)
=> tab != null && HostElement.TabItems.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)
=> webview != null && HostElement.TabItems.Any(ele => ele is TabViewItem tab && tab.Content is SingleWebView v && v == 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)
=> webview != null && HostElement.TabItems.Any(ele => ele is TabViewItem tab && tab.Content is LocalWebAppPage v && v == webview);
private void OnNewWindowRequested(SingleWebView sender, CoreWebView2NewWindowRequestedEventArgs e)
=> _ = OnNewWindowRequestedAsync(e);
private void OnNewWindowRequested2(LocalWebAppPage sender, CoreWebView2NewWindowRequestedEventArgs e)
=> _ = OnNewWindowRequestedAsync(e);
private LocalWebAppPage AddLocalWebAppTab(Action<TabViewItem, LocalWebAppPage> callback = null)
{
var c = new LocalWebAppPage
{
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
DownloadList = DownloadList,
DisableNewWindowRequestHandling = true,
WindowController = WindowController
};
var name = "Loading…";
var tab = new TabViewItem
{
Header = name,
Content = c,
IsSelected = true,
};
callback?.Invoke(tab, c);
HostElement.TabItems.Add(tab);
c.TitleChanged += (sender, e) =>
{
tab.Header = e.Data ?? string.Empty;
};
c.NewWindowRequested += OnNewWindowRequested2;
c.ContainsFullScreenElementChanged += OnContainsFullScreenElementChanged;
c.WindowCloseRequested += (sender, e) =>
{
try
{
LocalWebAppPageCloseRequested?.Invoke(this, new LocalWebAppTabEventArgs(tab, c));
}
finally
{
try
{
c.Close();
}
catch (InvalidOperationException)
{
}
catch (NullReferenceException)
{
}
catch (ApplicationException)
{
}
catch (ExternalException)
{
}
try
{
HostElement.TabItems.Remove(tab);
}
catch (InvalidOperationException)
{
}
catch (ExternalException)
{
}
}
};
LocalWebAppTabCreated?.Invoke(this, new LocalWebAppTabEventArgs(tab, c));
return c;
}
private async Task OnNewWindowRequestedAsync(CoreWebView2NewWindowRequestedEventArgs e)
{
if (e.WindowFeatures != null && (e.WindowFeatures.HasPosition || e.WindowFeatures.HasSize)) return;
e.Handled = true;
var deferral = e.GetDeferral();
var n = Add(null as Uri);
await n.EnsureCoreWebView2Async();
e.NewWindow = n.CoreWebView2;
deferral.Complete();
}
private void OnTabCloseRequested(TabView sender, TabViewTabCloseRequestedEventArgs args)
{
try
{
var tab = args.Tab;
var webview = GetWebView(tab);
if (webview != null)
{
webview.Close();
}
else
{
GetLocalWebApp(tab)?.Close();
}
}
catch (InvalidOperationException)
{
}
catch (NullReferenceException)
{
}
catch (ApplicationException)
{
}
catch (ExternalException)
{
}
sender.TabItems.Remove(args.Tab);
TabCloseRequested?.Invoke(this, args);
}
private void OnContainsFullScreenElementChanged(object sender, DataEventArgs<bool> e)
{
var i = 0;
foreach (var item in WebViews)
{
try
{
if (item.ContainsFullScreenElement) i++;
}
catch (InvalidOperationException)
{
}
catch (NullReferenceException)
{
}
catch (ApplicationException)
{
}
catch (ExternalException)
{
}
}
foreach (var item in LocalWebApps)
{
try
{
if (item.ContainsFullScreenElement) i++;
}
catch (InvalidOperationException)
{
}
catch (NullReferenceException)
{
}
catch (ApplicationException)
{
}
catch (ExternalException)
{
}
}
VisualStateManager.GoToState(HostElement, e.Data ? "FullscreenState" : "WindowState", true);
ContainsFullScreenElementChanged?.Invoke(this, new DataEventArgs<int>(i));
}
private void HostElement_AddTabButtonClick(TabView sender, object args)
=> Add(DefaultUriCreator?.Invoke());
private void HostElement_SelectionChanged(object sender, SelectionChangedEventArgs e)
=> SelectionChanged?.Invoke(this, e);
private static void UpdateReadOnly(TabbedWebView c, ChangeEventArgs<bool> e, DependencyProperty d)
{
c.HostElement.IsAddTabButtonVisible = !e.NewValue;
foreach (var webview in c.WebViews)
{
webview.IsReadOnly = e.NewValue;
}
}
/// <summary>
/// Gets the web view instance.
/// </summary>
/// <param name="tab">The tab view item.</param>
/// <returns>The web view contained by the tab view item; or null, if non-exists.</returns>
private static SingleWebView GetWebView(TabViewItem tab)
=> tab?.Content as SingleWebView;
/// <summary>
/// Gets the local web app instance.
/// </summary>
/// <param name="tab">The tab view item.</param>
/// <returns>The web view contained by the tab view item; or null, if non-exists.</returns>
private static LocalWebAppPage GetLocalWebApp(TabViewItem tab)
=> tab?.Content as LocalWebAppPage;
private void HostElement_TabDragStarting(TabView sender, TabViewTabDragStartingEventArgs args)
=> TabDragStarting?.Invoke(this, args);
private void HostElement_TabDragCompleted(TabView sender, TabViewTabDragCompletedEventArgs args)
=> TabDragCompleted?.Invoke(this, args);
private void HostElement_TabDroppedOutside(TabView sender, TabViewTabDroppedOutsideEventArgs args)
=> TabDroppedOutside?.Invoke(this, args);
}