-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModelViewerControl.cs
185 lines (151 loc) · 5.85 KB
/
ModelViewerControl.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
using Autofac;
using HgmViewer.Classes;
using HgmViewer.Service;
using Krypton.Navigator;
using Microsoft.Web.WebView2.Core;
using System;
using System.Windows.Forms;
namespace HgmViewer
{
/// <summary>
/// 3D-Model Preview Control
/// </summary>
public partial class ModelViewerControl : UserControl
{
private readonly ILifetimeScope _scope;
internal readonly WebViewContext _webViewContext;
private const bool _acceleratorKeysEnabled = false;
private const bool _webViewNavigationBarDisabled = true;
public ModelViewerControl(ILifetimeScope scope)
{
InitializeComponent();
_scope = scope;
_webViewContext = _scope.Resolve<WebViewContext>(new NamedParameter("modelViewerControl", this));
if (_webViewNavigationBarDisabled)
{
splitContainer.Panel1Collapsed = true;
}
btnGo.Text = "\uE017";
AttachControlEventHandlers(webView2Control);
Load += ModelViewerControl_Load;
}
private void ModelViewerControl_Load(object sender, EventArgs e)
{
webView2Control.Dock = DockStyle.Fill;
}
private void AddressBarNavigate()
{
var rawUrl = txtUrl.Text;
Uri uri;
if (Uri.IsWellFormedUriString(rawUrl, UriKind.Absolute))
{
uri = new Uri(rawUrl);
}
else if (!rawUrl.Contains(' ') && rawUrl.Contains('.'))
{
uri = new Uri("http://" + rawUrl);
}
else
{
uri = new Uri("https://www.google.com/search?q=" +
String.Join("+", Uri.EscapeDataString(rawUrl).Split(new string[] { "%20" }, StringSplitOptions.RemoveEmptyEntries)));
}
webView2Control.Source = uri;
}
private void AttachControlEventHandlers(Microsoft.Web.WebView2.WinForms.WebView2 control)
{
control.CoreWebView2InitializationCompleted += WebView2Control_CoreWebView2InitializationCompleted;
control.NavigationCompleted += WebView2Control_NavigationCompleted;
control.SourceChanged += CoreWebView2_SourceChanged;
control.KeyDown += WebView2Control_KeyDown;
control.KeyUp += WebView2Control_KeyUp;
}
private void WebView2Control_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
if (!e.IsSuccess)
{
MessageBox.Show($"WebView2 creation failed with exception = {e.InitializationException}");
return;
}
webView2Control.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
webView2Control.CoreWebView2.SourceChanged += CoreWebView2_SourceChanged;
webView2Control.CoreWebView2.HistoryChanged += CoreWebView2_HistoryChanged;
_webViewContext.InitializationCompleted();
}
private void WebView2Control_KeyUp(object sender, KeyEventArgs e)
{
if (ConfigService.IsDebug && e.KeyCode == Keys.F12)
return;
if (!_acceleratorKeysEnabled)
e.Handled = true;
}
private void WebView2Control_KeyDown(object sender, KeyEventArgs e)
{
if (ConfigService.IsDebug && e.KeyCode == Keys.F12)
return;
if (!_acceleratorKeysEnabled)
e.Handled = true;
}
private async void WebView2Control_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
{
_webViewContext.NavigationCompleted(e);
await webView2Control.CoreWebView2.Profile.ClearBrowsingDataAsync(CoreWebView2BrowsingDataKinds.BrowsingHistory);
}
private void CoreWebView2_HistoryChanged(object sender, object e)
{
btnBack.Enabled = webView2Control.CoreWebView2.CanGoBack;
btnForward.Enabled = webView2Control.CoreWebView2.CanGoForward;
}
private void CoreWebView2_SourceChanged(object sender, CoreWebView2SourceChangedEventArgs e)
{
txtUrl.Text = webView2Control.Source.AbsoluteUri;
}
private void BtnRefresh_Click(object sender, EventArgs e)
{
webView2Control.Reload();
}
private void BtnGo_Click(object sender, EventArgs e)
{
AddressBarNavigate();
}
private void btnBack_Click(object sender, EventArgs e)
{
webView2Control.GoBack();
}
private void btnForward_Click(object sender, EventArgs e)
{
webView2Control.GoForward();
}
private void txtUrl_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
{
AddressBarNavigate();
e.Handled = true;
}
}
private void txtUrl_TextChanged(object sender, EventArgs e)
{
if (txtUrl.Text.IndexOf('\r') != -1 || txtUrl.Text.IndexOf('\n') != -1)
{
txtUrl.Text = txtUrl.Text.Replace("\r", "").Replace("\n", "");
}
}
/// <summary>
/// Krypton Docking page for <see cref="ModelViewerControl"/>
/// </summary>
public class ModelViewerPage : KryptonPage
{
private readonly ILifetimeScope _scope;
private readonly ModelViewerControl _ctrl;
public ModelViewerControl Ctrl => _ctrl;
public ModelViewerPage(ILifetimeScope scope)
{
_scope = scope;
_ctrl = scope.Resolve<ModelViewerControl>();
_ctrl.Dock = DockStyle.Fill;
Controls.Add(_ctrl);
}
}
}
}