-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
140 lines (124 loc) · 4.96 KB
/
MainWindow.xaml.cs
File metadata and controls
140 lines (124 loc) · 4.96 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
using Microsoft.Web.WebView2.Core;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using WinRT.Interop;
using Windows.ApplicationModel.Core;
using Windows.UI.ViewManagement;
using Windows.UI.Core;
using Microsoft.UI.Windowing;
using Microsoft.UI;
using Microsoft.UI.Xaml.Controls.Primitives;
using System.Numerics;
namespace Project_Narc
{
public sealed partial class MainWindow : Window
{
private readonly AppWindow m_AppWindow;
public MainWindow()
{
this.InitializeComponent();
MyWebView.NavigationStarting += EnsureHttps;
m_AppWindow = GetAppWindowForCurrentWindow();
m_AppWindow.Title = "";
PopupRectangle.Translation += new Vector3(0, 0, 32);
var titlebar = m_AppWindow.TitleBar;
titlebar.IconShowOptions = IconShowOptions.HideIconAndSystemMenu;
titlebar.ButtonBackgroundColor = Colors.Transparent;
titlebar.ButtonInactiveBackgroundColor = Colors.Transparent;
if (AppWindowTitleBar.IsCustomizationSupported())
{
var titleBar = m_AppWindow.TitleBar;
// Hide default title bar.
titleBar.ExtendsContentIntoTitleBar = true;
}
else
{
AppTitleBar.Visibility = Visibility.Collapsed;
}
}
private AppWindow GetAppWindowForCurrentWindow()
{
IntPtr hWnd = WindowNative.GetWindowHandle(this);
WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd);
return AppWindow.GetFromWindowId(wndId);
}
private void ExtendAcrylicIntoTitleBar()
{
CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
ApplicationViewTitleBar titleBar = ApplicationView.GetForCurrentView().TitleBar;
titleBar.ButtonBackgroundColor = Colors.Transparent;
titleBar.ButtonInactiveBackgroundColor = Colors.Transparent;
}
private async void EnsureHttps(WebView2 sender, CoreWebView2NavigationStartingEventArgs args)
{
string uri = args.Uri;
// Remove "https://" and "www." prefixes from the displayed URL
string displayedUrl = uri.Replace("https://", string.Empty);
displayedUrl = displayedUrl.Replace("www.", string.Empty);
// Check if there is a trailing slash and hide it if present
if (displayedUrl.EndsWith("/"))
{
displayedUrl = displayedUrl.Substring(0, displayedUrl.Length - 1);
}
addressBar.Text = displayedUrl;
}
private void AddressBarKeyDown(object sender, KeyRoutedEventArgs e){
if (e.Key == Windows.System.VirtualKey.Enter)
{
NavigateToAddress(sender, e);
e.Handled = true; // Mark the event as handled to prevent further processing
}
}
private void NavigateToAddress(object sender, RoutedEventArgs e)
{
string uri = addressBar.Text;
if (!uri.StartsWith("https://") && !uri.StartsWith("http://"))
{
// Check if uri starts with "www."
if (uri.StartsWith("www."))
{
bool isValidHTTPWebsite = Uri.TryCreate("http://" + uri, UriKind.Absolute, out Uri validatedHTTPUri);
if (isValidHTTPWebsite)
{
MyWebView.Source = validatedHTTPUri;
} else
{
bool isValidHTPPSWebsite = Uri.TryCreate("https://" + uri, UriKind.Absolute, out Uri validatedHTTPSUri);
if (isValidHTPPSWebsite)
{
MyWebView.Source = validatedHTTPSUri;
}
}
}
else if (uri.EndsWith(".com"))
{
bool isValidHTTPSWWWWebsite = Uri.TryCreate("https://www." + uri, UriKind.Absolute, out Uri validatedHTTPSWWWUri);
if (isValidHTTPSWWWWebsite)
{
MyWebView.Source = validatedHTTPSWWWUri;
}
} else
{
string searchQuery = Uri.EscapeDataString(addressBar.Text);
string googleSearchUrl = $"https://www.google.com/search?q={searchQuery}";
MyWebView.Source = new Uri(googleSearchUrl);
}
}
else
{
MyWebView.Source = new Uri(uri);
}
}
}
}