-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathUtilities.cs
More file actions
170 lines (158 loc) · 5.53 KB
/
Utilities.cs
File metadata and controls
170 lines (158 loc) · 5.53 KB
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
using Microsoft.UI;
using Microsoft.UI.Composition.SystemBackdrops;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Edge
{
public enum UriType
{
WithProtocol,
WithoutProtocol,
PlainText
}
public static class Utilities
{
public static void SetBackdrop(this Window window)
{
Effect effect = App.settings.BackgroundEffect;
if (effect == Effect.Mica && MicaController.IsSupported())
{
window.SystemBackdrop = new MicaBackdrop();
}
else if (effect == Effect.MicaAlt && MicaController.IsSupported())
{
window.SystemBackdrop = new MicaBackdrop() { Kind = MicaKind.BaseAlt };
}
else if (effect == Effect.Acrylic)
{
window.SystemBackdrop = new DesktopAcrylicBackdrop();
}
else
{
window.SystemBackdrop = null;
}
}
public static void SetThemeColor(this Window window)
{
string appearance = App.settings.Appearance;
if (window.Content is FrameworkElement rootElement)
{
rootElement.RequestedTheme = Enum.Parse<ElementTheme>(appearance);
appearance = appearance == "Default" ? "UseDefaultAppMode" : appearance;
window.AppWindow.TitleBar.PreferredTheme = Enum.Parse<TitleBarTheme>(appearance);
}
}
public static IntPtr GetWindowHandle(this Window window)
{
return Win32Interop.GetWindowFromWindowId(window.AppWindow.Id);
}
public static IntPtr GetWindowHandle(this UIElement element)
{
Window window = App.GetWindowForElement(element);
return window.GetWindowHandle();
}
public static WindowId GetWindowId(this UIElement element)
{
Window window = App.GetWindowForElement(element);
return window.AppWindow.Id;
}
public static string ToGlyph(this string name)
{
return name switch
{
"back" => "\ue72b",
"forward" => "\ue72a",
"reload" => "\ue72c",
"saveAs" => "\ue792",
"print" => "\ue749",
"share" => "\ue72d",
"emoji" => "\ue899",
"undo" => "\ue7a7",
"redo" => "\ue7a6",
"cut" => "\ue8c6",
"copy" => "\ue8c8",
"paste" => "\ue77f",
"openLinkInNewWindow" => "\ue737",
"copyLinkLocation" => "\ue71b",
_ => string.Empty,
};
}
public static UriType DetectUri(this string text)
{
if (string.IsNullOrWhiteSpace(text))
{
return UriType.PlainText;
}
if (text.Contains("://"))
{
return Uri.IsWellFormedUriString(text, UriKind.Absolute) ? UriType.WithProtocol : UriType.PlainText;
}
string domainPattern = @"^(?!-)[A-Za-z0-9-]+(\.[A-Za-z]{2,})+$";
if (Regex.IsMatch(text, domainPattern))
{
return UriType.WithoutProtocol;
}
return UriType.PlainText;
}
public static async Task<string> GetBingImageUrlAsync()
{
using var client = new HttpClient();
var response = await client.GetStringAsync("https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1");
var json = JsonDocument.Parse(response);
return "https://cn.bing.com" + json.RootElement.GetProperty("images")[0].GetProperty("url").GetString();
}
public static void Search(string text, MainWindow mainWindow)
{
UriType uriType = text.DetectUri();
if (uriType == UriType.WithProtocol)
{
Navigate(text, mainWindow);
}
else if (uriType == UriType.WithoutProtocol)
{
Navigate("https://" + text, mainWindow);
}
else if (File.Exists(text))
{
FileInfo fileInfo = new(text);
string ext = fileInfo.Extension;
if (Info.LanguageDict.TryGetValue(ext, out var _))
{
mainWindow.AddNewTab(new TextFilePage(fileInfo), fileInfo.Name);
}
else if (Info.ImageDict.TryGetValue(ext, out var _))
{
mainWindow.AddNewTab(new ImageViewer(fileInfo), fileInfo.Name);
}
else
{
Navigate(text, mainWindow);
}
}
else
{
Navigate(Info.SearchEngineList.First(x => x.Name == App.settings.SearchEngine).Uri + text, mainWindow);
}
}
public static void Navigate(string site, MainWindow mainWindow)
{
Uri uri = new(site);
if ((mainWindow.TabView.SelectedItem != null) && (mainWindow.SelectedItem is WebViewPage webviewPage))
{
webviewPage.WebView2.Source = uri;
}
else
{
mainWindow.AddNewTab(new WebViewPage(uri));
}
}
}
}