Skip to content

Commit

Permalink
Adds SendNotification operation and forwards VL.CEF from sub packages
Browse files Browse the repository at this point in the history
  • Loading branch information
azeno committed Apr 11, 2024
1 parent ec44660 commit 03f5a4d
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<!-- Version information -->
<Year>$([System.DateTime]::Now.ToString('yyyy'))</Year>
<Version>0.5.5</Version>
<Version>0.5.6</Version>

<!-- Package properties -->
<Authors>vvvv group</Authors>
Expand Down
2 changes: 1 addition & 1 deletion VL.CEF.Skia/VL.CEF.Skia.vl
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,5 @@
</Patch>
<NugetDependency Id="Pa4n9gMiHfyPAmP1MTQUEf" Location="VL.Skia" Version="2022.5.0-0683-g91567c285a" />
<PlatformDependency Id="V00Dw32yWnSOJVwytTbLQb" Location="./lib/net6.0-windows/VL.CEF.Skia.dll" />
<NugetDependency Id="Knu810rkN45OuFmlmqfa99" Location="VL.CEF" Version="0.0.0" />
<NugetDependency Id="Knu810rkN45OuFmlmqfa99" Location="VL.CEF" IsForward="true" Version="0.0.0" />
</Document>
2 changes: 1 addition & 1 deletion VL.CEF.Stride/VL.CEF.Stride.vl
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,5 @@
<NugetDependency Id="HPbFYrmwgPhPvXgZqFEIvs" Location="VL.Stride" Version="2022.5.0-0683-g91567c285a" />
<NugetDependency Id="IB2zAnzlQFnPHn5Uo120Vw" Location="Stride.Rendering" Version="4.1.0.1805" />
<PlatformDependency Id="V00Dw32yWnSOJVwytTbLQb" Location="./lib/net6.0-windows/VL.CEF.Stride.dll" />
<NugetDependency Id="B2j9g6TO1esQANElMPYnOG" Location="VL.CEF" Version="0.0.0" />
<NugetDependency Id="B2j9g6TO1esQANElMPYnOG" Location="VL.CEF" IsForward="true" Version="0.0.0" />
</Document>
213 changes: 213 additions & 0 deletions VL.CEF/src/WebBrowser.InputHandling.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
using Stride.Core.Mathematics;
using System;
using System.Diagnostics;
using VL.Lib.IO;
using VL.Lib.IO.Notifications;
using Xilium.CefGlue;

namespace VL.CEF
{
partial class WebBrowser
{
CefEventFlags mouseModifiers;

public bool SendNotification(INotification notification)
{
if (notification is MouseNotification mouseNotification)
{
HandleMouseNotification(mouseNotification);
return true;
}
else if (notification is KeyNotification keyNotification)
{
HandleKeyNotification(keyNotification);
return true;
}
else if (notification is TouchNotification touchNotification)
{
HandleTouchNotification(touchNotification);
return true;
}
return false;
}

private void HandleTouchNotification(TouchNotification n)
{
var position = n.Position.DeviceToLogical(ScaleFactor);
var touchEvent = new CefTouchEvent()
{
Id = n.Id,
Modifiers = GetModifiers(n),
PointerType = CefPointerType.Touch,
Pressure = 1f,
RadiusX = n.ContactArea.X,
RadiusY = n.ContactArea.Y,
RotationAngle = 0,
Type = GetTouchType(n.Kind),
X = position.X,
Y = position.Y
};
BrowserHost.SendTouchEvent(touchEvent);

CefTouchEventType GetTouchType(TouchNotificationKind kind)
{
switch (kind)
{
case TouchNotificationKind.TouchDown:
return CefTouchEventType.Pressed;
case TouchNotificationKind.TouchUp:
return CefTouchEventType.Released;
case TouchNotificationKind.TouchMove:
return CefTouchEventType.Moved;
default:
return CefTouchEventType.Cancelled;
}
}
}

private void HandleKeyNotification(KeyNotification n)
{
var keyEvent = new CefKeyEvent()
{
Modifiers = GetModifiers(n)
};
switch (n.Kind)
{
case KeyNotificationKind.KeyDown:
var keyDown = n as KeyDownNotification;
keyEvent.EventType = CefKeyEventType.KeyDown;
keyEvent.WindowsKeyCode = (int)keyDown.KeyCode;
keyEvent.NativeKeyCode = (int)keyDown.KeyCode;
break;
case KeyNotificationKind.KeyPress:
var keyPress = n as KeyPressNotification;
keyEvent.EventType = CefKeyEventType.Char;
keyEvent.Character = keyPress.KeyChar;
keyEvent.UnmodifiedCharacter = keyPress.KeyChar;
keyEvent.WindowsKeyCode = (int)keyPress.KeyChar;
keyEvent.NativeKeyCode = (int)keyPress.KeyChar;
break;
case KeyNotificationKind.KeyUp:
var keyUp = n as KeyUpNotification;
keyEvent.EventType = CefKeyEventType.KeyUp;
keyEvent.WindowsKeyCode = (int)keyUp.KeyCode;
keyEvent.NativeKeyCode = (int)keyUp.KeyCode;
break;
default:
break;
}
BrowserHost.SendKeyEvent(keyEvent);
}

int clickCount = 1;
MouseButtons? lastButton;
Vector2 lastPosition;
Stopwatch stopwatch = Stopwatch.StartNew();

private void HandleMouseNotification(MouseNotification n)
{
if (n is MouseButtonNotification buttonNotification)
{
var position = n.Position;
var delta = lastPosition - position;
var deltaTime = stopwatch.ElapsedMilliseconds;
stopwatch.Restart();

if (n is MouseDownNotification)
{
if (buttonNotification.Buttons == lastButton &&
Math.Abs(delta.X) < Mouse.DoubleClickSize.Width / 2 &&
Math.Abs(delta.Y) < Mouse.DoubleClickSize.Height / 2 &&
deltaTime < Mouse.DoubleClickTime)
{
clickCount++;
}
else
{
clickCount = 1;
}
}
else if (buttonNotification.Buttons != lastButton)
{
clickCount = 1;
}

lastButton = buttonNotification.Buttons;
lastPosition = position;

if (n is MouseDownNotification mouseDown)
mouseModifiers |= ToCefEventFlags(mouseDown.Buttons);
else if (n is MouseUpNotification mouseUp)
mouseModifiers &= ~ToCefEventFlags(mouseUp.Buttons);
}

{
var position = n.Position.DeviceToLogical(ScaleFactor);
var mouseEvent = new CefMouseEvent((int)position.X, (int)position.Y, GetModifiers(n));
var browserHost = BrowserHost;
switch (n.Kind)
{
case MouseNotificationKind.MouseDown:
var mouseDown = n as MouseDownNotification;
browserHost.SendMouseClickEvent(mouseEvent, GetMouseButtonType(mouseDown.Buttons), mouseUp: false, clickCount: clickCount);
break;
case MouseNotificationKind.MouseUp:
var mouseUp = n as MouseUpNotification;
browserHost.SendMouseClickEvent(mouseEvent, GetMouseButtonType(mouseUp.Buttons), mouseUp: true, clickCount: clickCount);
break;
case MouseNotificationKind.MouseMove:
browserHost.SendMouseMoveEvent(mouseEvent, mouseLeave: false);
break;
case MouseNotificationKind.MouseWheel:
var mouseWheel = n as MouseWheelNotification;
browserHost.SendMouseWheelEvent(mouseEvent, 0, mouseWheel.WheelDelta);
break;
case MouseNotificationKind.MouseHorizontalWheel:
var mouseHWheel = n as MouseHorizontalWheelNotification;
browserHost.SendMouseWheelEvent(mouseEvent, mouseHWheel.WheelDelta, 0);
break;
case MouseNotificationKind.DeviceLost:
browserHost.SendMouseMoveEvent(mouseEvent, mouseLeave: true);
break;
}
}

CefMouseButtonType GetMouseButtonType(MouseButtons buttons)
{
if ((buttons & MouseButtons.Left) != 0)
return CefMouseButtonType.Left;
if ((buttons & MouseButtons.Middle) != 0)
return CefMouseButtonType.Middle;
if ((buttons & MouseButtons.Right) != 0)
return CefMouseButtonType.Right;
return default;
}

static CefEventFlags ToCefEventFlags(MouseButtons buttons)
{
switch (buttons)
{
case MouseButtons.Left:
return CefEventFlags.LeftMouseButton;
case MouseButtons.Middle:
return CefEventFlags.MiddleMouseButton;
case MouseButtons.Right:
return CefEventFlags.RightMouseButton;
}
return default;
}
}

CefEventFlags GetModifiers(NotificationBase n)
{
var result = CefEventFlags.None;
if (n.AltKey)
result |= CefEventFlags.AltDown | CefEventFlags.IsLeft;
if (n.ShiftKey)
result |= CefEventFlags.ShiftDown | CefEventFlags.IsLeft;
if (n.CtrlKey)
result |= CefEventFlags.ControlDown | CefEventFlags.IsLeft;
return result | mouseModifiers;
}
}
}

0 comments on commit 03f5a4d

Please sign in to comment.