-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathApp.xaml.cs
133 lines (117 loc) · 4.34 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
using Microsoft.UI.Dispatching;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Windows.AppLifecycle;
using Microsoft.Windows.AppNotifications;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Text.Json;
namespace Edge
{
public partial class App : Application
{
public static List<MainWindow> mainWindows = [];
public static string LatestVersion = null;
public static Settings settings;
public static WebView2 webView2 = new();
public static ObservableCollection<WebViewHistory> Histories = [];
public static ObservableCollection<DownloadObject> DownloadList = [];
public static WordSearchEngine searchEngine;
public App()
{
this.InitializeComponent();
searchEngine = new("./Assets/words.txt");
EnsureWebView2Async();
}
public async void EnsureWebView2Async()
{
await webView2.EnsureCoreWebView2Async();
}
public static MainWindow CreateNewWindow()
{
MainWindow window = new();
window.Closed += (sender, e) =>
{
mainWindows.Remove(window);
File.WriteAllText("./settings.json", JsonSerializer.Serialize(settings, JsonContext.Default.Settings));
};
mainWindows.Add(window);
return window;
}
public static MainWindow GetWindowForElement(UIElement element)
{
if (element.XamlRoot != null)
{
foreach (MainWindow window in mainWindows)
{
if (element.XamlRoot == window.Content.XamlRoot)
{
return window;
}
}
}
return null;
}
public static WebView2 GetWebView2(UIElement element)
{
WebViewPage page = GetWindowForElement(element).SelectedItem as WebViewPage;
return page.WebView2;
}
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
settings = Info.LoadSettings();
window = CreateNewWindow();
string[] arguments = Environment.GetCommandLineArgs()[1..];
if (arguments.Length > 0)
{
foreach (string arg in arguments)
{
WebSearch.StartSearch(arg, window);
}
}
else
{
window.AddHomePage();
}
window.Activate();
AppNotificationManager notificationManager = AppNotificationManager.Default;
notificationManager.NotificationInvoked += NotificationManager_NotificationInvoked;
notificationManager.Register();
var activatedArgs = AppInstance.GetCurrent().GetActivatedEventArgs();
var activationKind = activatedArgs.Kind;
if (activationKind != ExtendedActivationKind.AppNotification)
{
OverlappedPresenter presenter = window.AppWindow.Presenter as OverlappedPresenter;
presenter.Restore(true);
}
else
{
HandleNotification((AppNotificationActivatedEventArgs)activatedArgs.Data);
}
}
private void HandleNotification(AppNotificationActivatedEventArgs args)
{
var dispatcherQueue = window?.DispatcherQueue ?? DispatcherQueue.GetForCurrentThread();
dispatcherQueue.TryEnqueue(async delegate
{
switch (args.Arguments["Notification"])
{
case "LaunchReleaseWebsite":
await Windows.System.Launcher.LaunchUriAsync(new Uri("https://github.com/wtcpython/WinUIEdge/releases/latest/"));
break;
case "ChangeStartUri":
window.Content.Focus(FocusState.Programmatic);
break;
}
});
}
private void NotificationManager_NotificationInvoked(AppNotificationManager sender, AppNotificationActivatedEventArgs args)
{
HandleNotification(args);
}
private static MainWindow window;
}
}