// Copyright © 2019 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System.Windows.Input;
using CefSharp.Enums;
using CefSharp.Structs;
using CefSharp.Wpf.Internals;
namespace CefSharp.Wpf.Experimental
{
///
/// An Experimental ChromiumWebBrowser implementation that includes support for Stylus
/// using the default WPF touch implementation. There are known performance problems with
/// this default implementation, workarounds such as https://github.com/jaytwo/WmTouchDevice
/// may need to be considered. .Net 4.7 supports the newer WM_Pointer implementation which
/// should resolve the issue see https://github.com/dotnet/docs/blob/master/docs/framework/migration-guide/mitigation-pointer-based-touch-and-stylus-support.md
/// Original PR https://github.com/cefsharp/CefSharp/pull/2745
/// Original Author https://github.com/GSonofNun
/// Touch support was merged into ChromiumWebBrowser, only Style support still exists in this class
///
public class ChromiumWebBrowserWithTouchSupport : ChromiumWebBrowser
{
///
/// Invoked when an unhandled attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
///
/// The that contains the event data.
protected override void OnStylusDown(StylusDownEventArgs e)
{
//Both touch and stylus will raise stylus event.
//We use the OnTouchXXX methods which contain more intermediate points to handle the user's touch so that we can track the user's fingers faster.
//Use e.StylusDevice.TabletDevice.Type == TabletDeviceType.Stylus to ensure only stylus pen.
if (e.StylusDevice.TabletDevice.Type == TabletDeviceType.Stylus)
{
Focus();
// Capture stylus so stylus events are still pushed to CEF even if the stylus leaves the control before a StylusUp.
// This behavior is similar to how other browsers handle stylus input.
CaptureStylus();
OnStylus(e, TouchEventType.Pressed);
}
base.OnStylusDown(e);
}
///
/// Invoked when an unhandled attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
///
/// The that contains the event data.
protected override void OnStylusMove(StylusEventArgs e)
{
//Both touch and stylus will raise stylus event.
//We use the OnTouchXXX methods which contain more intermediate points to handle the user's touch so that we can track the user's fingers faster.
//Use e.StylusDevice.TabletDevice.Type == TabletDeviceType.Stylus to ensure only stylus pen.
if (e.StylusDevice.TabletDevice.Type == TabletDeviceType.Stylus)
{
OnStylus(e, TouchEventType.Moved);
}
base.OnStylusMove(e);
}
///
/// Invoked when an unhandled attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
///
/// The that contains the event data.
protected override void OnStylusUp(StylusEventArgs e)
{
//Both touch and stylus will raise stylus event.
//We use the OnTouchXXX methods which contain more intermediate points to handle the user's touch so that we can track the user's fingers faster.
//Use e.StylusDevice.TabletDevice.Type == TabletDeviceType.Stylus to ensure only stylus pen.
if (e.StylusDevice.TabletDevice.Type == TabletDeviceType.Stylus)
{
ReleaseStylusCapture();
OnStylus(e, TouchEventType.Released);
}
base.OnStylusUp(e);
}
///
/// Handles a event.
///
/// The instance containing the event data.
/// The event type
private void OnStylus(StylusEventArgs e, TouchEventType touchEventType)
{
var browser = GetBrowser();
if (!e.Handled && browser != null)
{
var modifiers = WpfExtensions.GetModifierKeys();
var pointerType = e.StylusDevice.Inverted ? PointerType.Eraser : PointerType.Pen;
//Send all points to host
foreach (var stylusPoint in e.GetStylusPoints(this))
{
var touchEvent = new TouchEvent()
{
Id = e.StylusDevice.Id,
X = (float)stylusPoint.X,
Y = (float)stylusPoint.Y,
PointerType = pointerType,
Pressure = stylusPoint.PressureFactor,
Type = touchEventType,
Modifiers = modifiers,
};
browser.GetHost().SendTouchEvent(touchEvent);
}
e.Handled = true;
}
}
}
}