-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUltralightWebView.xaml.cs
More file actions
353 lines (317 loc) · 13.7 KB
/
UltralightWebView.xaml.cs
File metadata and controls
353 lines (317 loc) · 13.7 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
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
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using UltralightNet;
using UltralightWPF.Handlers;
namespace UltralightWPF
{
public readonly struct LoadingStateResult(string _Url, ulong _FrameId, bool _IsMainFrame, bool _IsLoading)
{
public string Url { get; } = _Url;
public ulong FrameId { get; } = _FrameId;
public bool IsMainFrame { get; } = _IsMainFrame;
public bool IsLoading { get; } = _IsLoading;
}
/// <summary>
/// Interaction logic for UltralightWebView.xaml
/// </summary>
public partial class UltralightWebView : UserControl, IDisposable
{
private Renderer? _Renderer;
private View? _View;
private bool _Disposed;
private IRenderHandler RenderHandler;
public string Title => _View?.Title ?? "";
public bool CanGoBack => _View?.CanGoBack ?? false;
public bool CanGoForward => _View?.CanGoForward ?? false;
public bool CanReload => IsBrowserInitialized;
public bool IsLoading => _View?.IsLoading ?? false;
public bool IsBrowserInitialized => _View != null;
public string Url
{
get => _View?.URL ?? "about:blank";
set => Navigate(value);
}
public void Navigate(string Url)
{
if (_View != null)
_View.URL = Url;
}
public void NavigateToString(string Text)
{
if (_View != null)
_View.HTML = Text;
}
public void GoBack() => _View?.GoBack();
public void GoForward() => _View?.GoForward();
public void Reload() => _View?.Reload();
public void Stop() => _View?.Stop();
public View? GetView() => _View;
public double ZoomLevel
{
get => _View?.DeviceScale ?? 1;
set
{
if (_View != null)
{
if (value < 0.25 || value > 5.25)
return;
_View.DeviceScale = value;
}
}
}
public double ZoomFactor = 1.1;
public void ZoomIn() =>
ZoomLevel *= ZoomFactor;
public void ZoomOut() =>
ZoomLevel /= ZoomFactor;
public void ZoomReset() =>
ZoomLevel = 1;
public UltralightWebView()
{
InitializeComponent();
//RenderHandler = new InteropBitmapRenderHandler();
RenderHandler = new WriteableBitmapRenderHandler();
//RenderHandler = new D3DImageRenderHandler();
}
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
int _Width = (int)Math.Max(1, sizeInfo.NewSize.Width);
int _Height = (int)Math.Max(1, sizeInfo.NewSize.Height);
_View?.Resize((uint)_Width, (uint)_Height);
RenderHandler.AllocateBitmap(RenderImage, _Width, _Height);
}
public void UpdateSurfaceTexture()
{
if (_View == null) return;
//TODO: Utilize GPU _View.RenderTarget.
RenderHandler.UpdateBitmap(_View!);
}
public void Initialize(ULViewConfig? Config = null, View? InitialView = null)
{
if (_Disposed)
throw new Exception("Disposed");
if (UltralightManager.Instance == null)
new UltralightManager().Initialize();
Config ??= UltralightManager.DefaultViewConfig;
_Renderer = UltralightManager.Instance.GlobalRenderer!;
_View = InitialView ?? _Renderer.CreateView((uint)Math.Max(1, ActualWidth), (uint)Math.Max(1, ActualHeight), Config);
_View.OnChangeTitle += View_OnChangeTitle;
_View.OnChangeURL += View_OnChangeURL;
//_View.OnChangeTooltip += View_OnChangeTooltip;
_View.OnChangeCursor += View_OnChangeCursor;
_View.OnBeginLoading += View_OnBeginLoading;
_View.OnFinishLoading += View_OnFinishLoading;
_View.OnFailLoading += View_OnFailLoading;
UltralightManager.Instance.RegisterView(this);
Focus();
}
private void View_OnFailLoading(ulong frameId, bool isMainFrame, string url, string description, string errorDomain, int errorCode)
{
LoadingStateChanged.RaiseUIAsync(this, new LoadingStateResult(url, frameId, isMainFrame, false));
}
private void View_OnFinishLoading(ulong frameId, bool isMainFrame, string url)
{
LoadingStateChanged.RaiseUIAsync(this, new LoadingStateResult(url, frameId, isMainFrame, false));
}
private void View_OnBeginLoading(ulong frameId, bool isMainFrame, string url)
{
LoadingStateChanged.RaiseUIAsync(this, new LoadingStateResult(url, frameId, isMainFrame, true));
}
private void View_OnChangeURL(string e)
{
UrlChanged.RaiseUIAsync(this, e);
}
private void View_OnChangeCursor(ULCursor e)
{
Cursor = e.ToCursor();
}
public event EventHandler<LoadingStateResult> LoadingStateChanged;
public event EventHandler<string> UrlChanged;
public event EventHandler<string> TitleChanged;
/*public event EventHandler<string> ToolTipChanged;
private void View_OnChangeTooltip(string e)
{
ToolTipChanged.RaiseUIAsync(this, e);
}*/
private void View_OnChangeTitle(string e)
{
TitleChanged.RaiseUIAsync(this, e);
}
//TODO: Investigate inoperability.
/*public void ShowInspector(UltralightWebView InspectorWebView, ULViewConfig? Config = null)
{
View? InspectorView = _View?.CreateLocalInspectorView();
Debug.WriteLine(InspectorView.URL);
if (InspectorView != null)
InspectorWebView.Initialize(Config, InspectorView);*/
/*Window InspectorWindow = new()
{
Title = $"Ultralight Developer Tools - {InspectedUrl}",
Width = 800,
Height = 600
};*/
//}
protected override void OnMouseWheel(MouseWheelEventArgs e)
{
if (!e.Handled && _View != null)
{
bool IsShiftKeyDown = Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift);
_View?.FireScrollEvent(new ULScrollEvent() { DeltaX = IsShiftKeyDown ? e.Delta : 0, DeltaY = IsShiftKeyDown ? 0 : e.Delta, Type = ULScrollEventType.ByPixel });
e.Handled = true;
}
base.OnMouseWheel(e);
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
if (!e.Handled && _View != null)
{
string Text = Helpers.KeyToString(e.Key, Keyboard.Modifiers);
string Unmodified = Helpers.KeyToString(e.Key, ModifierKeys.None);
int KeyCode = KeyInterop.VirtualKeyFromKey(e.Key);
//_View.FireKeyEvent(ULKeyEvent.Create(ULKeyEventType.RawKeyDown, Keyboard.Modifiers.ToULKeyEventModifiers(), KeyCode, Helpers.GetNativeScanCode((uint)KeyCode), Text, Unmodified, e.Key >= Key.NumPad0 && e.Key <= Key.Divide, e.IsRepeat, e.Key == Key.System));
_View.FireKeyEvent(ULKeyEvent.Create(ULKeyEventType.RawKeyDown, Keyboard.Modifiers.ToULKeyEventModifiers(), KeyCode, 0, Text, Unmodified, e.Key >= Key.NumPad0 && e.Key <= Key.Divide, e.IsRepeat, e.Key == Key.System));
if (e.Key == Key.Tab || e.Key == Key.Home || e.Key == Key.End || e.Key == Key.Up || e.Key == Key.Down || e.Key == Key.Left || e.Key == Key.Right || (e.Key == Key.A && Keyboard.Modifiers == ModifierKeys.Control))
e.Handled = true;
}
base.OnPreviewKeyDown(e);
}
protected override void OnPreviewKeyUp(KeyEventArgs e)
{
if (!e.Handled && _View != null)
{
string Text = Helpers.KeyToString(e.Key, Keyboard.Modifiers);
string Unmodified = Helpers.KeyToString(e.Key, ModifierKeys.None);
int KeyCode = KeyInterop.VirtualKeyFromKey(e.Key);
//_View.FireKeyEvent(ULKeyEvent.Create(ULKeyEventType.KeyUp, Keyboard.Modifiers.ToULKeyEventModifiers(), KeyCode, Helpers.GetNativeScanCode((uint)KeyCode), Text, Unmodified, e.Key >= Key.NumPad0 && e.Key <= Key.Divide, e.IsRepeat, e.Key == Key.System));
_View.FireKeyEvent(ULKeyEvent.Create(ULKeyEventType.KeyUp, Keyboard.Modifiers.ToULKeyEventModifiers(), KeyCode, 0, Text, Unmodified, e.Key >= Key.NumPad0 && e.Key <= Key.Divide, e.IsRepeat, e.Key == Key.System));
if (e.Key == Key.Tab || e.Key == Key.Home || e.Key == Key.End || e.Key == Key.Up || e.Key == Key.Down || e.Key == Key.Left || e.Key == Key.Right || (e.Key == Key.A && Keyboard.Modifiers == ModifierKeys.Control))
e.Handled = true;
}
base.OnPreviewKeyUp(e);
}
protected override void OnPreviewTextInput(TextCompositionEventArgs e)
{
if (!e.Handled && _View != null)
{
OnTextInput(e.Text);
e.Handled = true;
}
base.OnPreviewTextInput(e);
}
private void OnTextInput(string Text)
{
for (int i = 0; i < Text.Length; i++)
{
//char Character = Text[i];
string CharString = Text[i].ToString();
//int KeyCode = Helpers.CharToKeyCode(Character);
_View.FireKeyEvent(ULKeyEvent.Create(ULKeyEventType.Char, Keyboard.Modifiers.ToULKeyEventModifiers(), 0, 0, CharString, CharString, false, false, false));
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (!e.Handled && _View != null && e.StylusDevice == null)
{
Point Coordinate = e.GetPosition(this);
_View.FireMouseEvent(new ULMouseEvent() { Button = e.GetMouseEvents(), Type = ULMouseEventType.MouseMoved, X = (int)(Coordinate.X / _View.DeviceScale), Y = (int)(Coordinate.Y / _View.DeviceScale) });
}
base.OnMouseMove(e);
}
protected override void OnGotFocus(RoutedEventArgs e)
{
_View?.Focus();
base.OnGotFocus(e);
}
protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
_View?.Focus();
base.OnGotKeyboardFocus(e);
}
protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
_View?.Unfocus();
base.OnLostKeyboardFocus(e);
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
if (e.StylusDevice == null)
{
Focus();
OnMouseButton(e);
if (e.ChangedButton == MouseButton.Left && e.LeftButton == MouseButtonState.Pressed)
CaptureMouse();
}
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseButtonEventArgs e)
{
if (e.StylusDevice == null)
{
OnMouseButton(e);
if (e.ChangedButton == MouseButton.Left && e.LeftButton == MouseButtonState.Released)
ReleaseMouseCapture();
}
base.OnMouseUp(e);
}
private void OnMouseButton(MouseButtonEventArgs e)
{
if (!e.Handled && _View != null)
{
bool MouseUp = e.ButtonState == MouseButtonState.Released;
if (e.ChangedButton == MouseButton.XButton1)
{
if (CanGoBack && MouseUp)
GoBack();
}
else if (e.ChangedButton == MouseButton.XButton2)
{
if (CanGoForward && MouseUp)
GoForward();
}
else
{
Point Coordinate = e.GetPosition(this);
_View.FireMouseEvent(new ULMouseEvent() { Button = e.GetMouseEvents(), Type = MouseUp ? ULMouseEventType.MouseUp : ULMouseEventType.MouseDown, X = (int)(Coordinate.X / _View.DeviceScale), Y = (int)(Coordinate.Y / _View.DeviceScale) });
}
e.Handled = true;
}
}
protected override void OnMouseLeave(MouseEventArgs e)
{
if (!e.Handled && _View != null && e.StylusDevice == null)
{
Point Coordinate = e.GetPosition(this);
_View.FireMouseEvent(new ULMouseEvent() { Button = ULMouseEventButton.None, Type = ULMouseEventType.MouseMoved, X = (int)(Coordinate.X / _View.DeviceScale), Y = (int)(Coordinate.Y / _View.DeviceScale) });
}
base.OnMouseLeave(e);
}
protected virtual void Dispose(bool disposing)
{
if (!_Disposed)
{
UltralightManager.Instance.UnregisterView(this);
if (_View != null)
{
_View.OnChangeTitle -= View_OnChangeTitle;
_View.OnChangeURL -= View_OnChangeURL;
_View.OnChangeCursor -= View_OnChangeCursor;
_View.Dispose();
_View = null;
}
RenderHandler.Dispose();
_Renderer = null;
_Disposed = true;
}
}
~UltralightWebView()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}