From b31a365dd41585ba24433dc2655dbfa03ea18e1a Mon Sep 17 00:00:00 2001 From: amaitland Date: Thu, 3 Oct 2019 21:02:25 +1000 Subject: [PATCH] WPF - Add basic touch support to ChromiumWebBrowser - Touch support moved from ChromiumWebBrowserWithTouchSupport - Stylus support was left in ChromiumWebBrowserWithTouchSupport as needs more testing The CefSharp.Wpf.Example still has System.AppContext.SetSwitch("Switch.System.Windows.Input.Stylus.DisableStylusAndTouchSupport", true); You will need to remove this for testing Issue #228 --- CefSharp.Wpf/ChromiumWebBrowser.cs | 130 +++++++++++++- .../ChromiumWebBrowserWithTouchSupport.cs | 165 +----------------- 2 files changed, 126 insertions(+), 169 deletions(-) diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index 240c17fa0..f263a1da9 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -2125,7 +2125,13 @@ namespace CefSharp.Wpf /// The that contains the event data. protected override void OnMouseMove(MouseEventArgs e) { - if (!e.Handled && browser != null) + //Mouse, touch, and stylus will raise mouse event. + //For mouse events from an actual mouse, e.StylusDevice will be null. + //For mouse events from touch and stylus, e.StylusDevice will not be null. + //We only handle event from mouse here. + //If not, touch will cause duplicate events (mousemove and touchmove) and so does stylus. + //Use e.StylusDevice == null to ensure only mouse. + if (!e.Handled && browser != null && e.StylusDevice == null) { var point = e.GetPosition(this); var modifiers = e.GetModifiers(); @@ -2171,8 +2177,17 @@ namespace CefSharp.Wpf /// This event data reports details about the mouse button that was pressed and the handled state. protected override void OnMouseDown(MouseButtonEventArgs e) { - Focus(); - OnMouseButton(e); + //Mouse, touch, and stylus will raise mouse event. + //For mouse events from an actual mouse, e.StylusDevice will be null. + //For mouse events from touch and stylus, e.StylusDevice will not be null. + //We only handle event from mouse here. + //If not, touch will cause duplicate events (mouseup and touchup) and so does stylus. + //Use e.StylusDevice == null to ensure only mouse. + if (e.StylusDevice == null) + { + Focus(); + OnMouseButton(e); + } base.OnMouseDown(e); } @@ -2183,9 +2198,18 @@ namespace CefSharp.Wpf /// The that contains the event data. The event data reports that the mouse button was released. protected override void OnMouseUp(MouseButtonEventArgs e) { - OnMouseButton(e); + //Mouse, touch, and stylus will raise mouse event. + //For mouse events from an actual mouse, e.StylusDevice will be null. + //For mouse events from touch and stylus, e.StylusDevice will not be null. + //We only handle event from mouse here. + //If not, touch will cause duplicate events (mouseup and touchup) and so does stylus. + //Use e.StylusDevice == null to ensure only mouse. + if (e.StylusDevice == null) + { + OnMouseButton(e); - base.OnMouseUp(e); + base.OnMouseUp(e); + } } /// @@ -2194,7 +2218,13 @@ namespace CefSharp.Wpf /// The that contains the event data. protected override void OnMouseLeave(MouseEventArgs e) { - if (!e.Handled && browser != null) + //Mouse, touch, and stylus will raise mouse event. + //For mouse events from an actual mouse, e.StylusDevice will be null. + //For mouse events from touch and stylus, e.StylusDevice will not be null. + //We only handle event from mouse here. + //OnMouseLeave event from touch or stylus needn't to be handled. + //Use e.StylusDevice == null to ensure only mouse. + if (!e.Handled && browser != null && e.StylusDevice == null) { var modifiers = e.GetModifiers(); var point = e.GetPosition(this); @@ -2251,6 +2281,94 @@ namespace CefSharp.Wpf } } + /// + /// Provides class handling for the routed event that occurs when a touch presses inside this element. + /// + /// The that contains the event data. + protected override void OnTouchDown(TouchEventArgs e) + { + Focus(); + // Capture touch so touch events are still pushed to CEF even if the touch leaves the control before a TouchUp. + // This behavior is similar to how other browsers handle touch input. + CaptureTouch(e.TouchDevice); + OnTouch(e); + base.OnTouchDown(e); + } + + /// + /// Provides class handling for the routed event that occurs when a touch moves while inside this element. + /// + /// The that contains the event data. + protected override void OnTouchMove(TouchEventArgs e) + { + OnTouch(e); + base.OnTouchMove(e); + } + + /// + /// Provides class handling for the routed event that occurs when a touch is released inside this element. + /// + /// The that contains the event data. + protected override void OnTouchUp(TouchEventArgs e) + { + ReleaseTouchCapture(e.TouchDevice); + OnTouch(e); + base.OnTouchUp(e); + } + + /// + /// Handles a event. + /// + /// The instance containing the event data. + private void OnTouch(TouchEventArgs e) + { + var browser = GetBrowser(); + + if (!e.Handled && browser != null) + { + var modifiers = WpfExtensions.GetModifierKeys(); + var touchPoint = e.GetTouchPoint(this); + var touchEventType = TouchEventType.Cancelled; + switch (touchPoint.Action) + { + case TouchAction.Down: + { + touchEventType = TouchEventType.Pressed; + break; + } + case TouchAction.Move: + { + touchEventType = TouchEventType.Moved; + break; + } + case TouchAction.Up: + { + touchEventType = TouchEventType.Released; + break; + } + default: + { + touchEventType = TouchEventType.Cancelled; + break; + } + } + + var touchEvent = new TouchEvent() + { + Id = e.TouchDevice.Id, + X = (float)touchPoint.Position.X, + Y = (float)touchPoint.Position.Y, + PointerType = PointerType.Touch, + Type = touchEventType, + Modifiers = modifiers, + }; + + browser.GetHost().SendTouchEvent(touchEvent); + + e.Handled = true; + } + } + /// /// Loads the specified URL. /// diff --git a/CefSharp.Wpf/Experimental/ChromiumWebBrowserWithTouchSupport.cs b/CefSharp.Wpf/Experimental/ChromiumWebBrowserWithTouchSupport.cs index 16e7b7fc5..874ad018e 100644 --- a/CefSharp.Wpf/Experimental/ChromiumWebBrowserWithTouchSupport.cs +++ b/CefSharp.Wpf/Experimental/ChromiumWebBrowserWithTouchSupport.cs @@ -10,178 +10,17 @@ using CefSharp.Wpf.Internals; namespace CefSharp.Wpf.Experimental { /// - /// An Experimental ChromiumWebBrowser implementation that includes support for Touch/Stylus + /// 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 OnMouseMove(MouseEventArgs e) - { - //Mouse, touch, and stylus will raise mouse event. - //For mouse events from an actual mouse, e.StylusDevice will be null. - //For mouse events from touch and stylus, e.StylusDevice will not be null. - //We only handle event from mouse here. - //If not, touch will cause duplicate events (mousemove and touchmove) and so does stylus. - //Use e.StylusDevice == null to ensure only mouse. - if (e.StylusDevice == null) - { - base.OnMouseMove(e); - } - } - - /// - /// Invoked when an unhandled  routed 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. The event data reports that the mouse button was released. - protected override void OnMouseUp(MouseButtonEventArgs e) - { - //Mouse, touch, and stylus will raise mouse event. - //For mouse events from an actual mouse, e.StylusDevice will be null. - //For mouse events from touch and stylus, e.StylusDevice will not be null. - //We only handle event from mouse here. - //If not, touch will cause duplicate events (mouseup and touchup) and so does stylus. - //Use e.StylusDevice == null to ensure only mouse. - if (e.StylusDevice == null) - { - base.OnMouseUp(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. - /// This event data reports details about the mouse button that was pressed and the handled state. - protected override void OnMouseDown(MouseButtonEventArgs e) - { - //Mouse, touch, and stylus will raise mouse event. - //For mouse events from an actual mouse, e.StylusDevice will be null. - //For mouse events from touch and stylus, e.StylusDevice will not be null. - //We only handle event from mouse here. - //If not, touch will cause duplicate events (mouseup and touchup) and so does stylus. - //Use e.StylusDevice == null to ensure only mouse. - if (e.StylusDevice == null) - { - base.OnMouseDown(e); - } - } - - /// - /// Invoked when an unhandled  attached event is raised on this element. Implement this method to add class handling for this event. - /// - /// The that contains the event data. - protected override void OnMouseLeave(MouseEventArgs e) - { - //Mouse, touch, and stylus will raise mouse event. - //For mouse events from an actual mouse, e.StylusDevice will be null. - //For mouse events from touch and stylus, e.StylusDevice will not be null. - //We only handle event from mouse here. - //OnMouseLeave event from touch or stylus needn't to be handled. - //Use e.StylusDevice == null to ensure only mouse. - if (e.StylusDevice == null) - { - base.OnMouseLeave(e); - } - } - - /// - /// Provides class handling for the routed event that occurs when a touch presses inside this element. - /// - /// The that contains the event data. - protected override void OnTouchDown(TouchEventArgs e) - { - Focus(); - // Capture touch so touch events are still pushed to CEF even if the touch leaves the control before a TouchUp. - // This behavior is similar to how other browsers handle touch input. - CaptureTouch(e.TouchDevice); - OnTouch(e); - base.OnTouchDown(e); - } - - /// - /// Provides class handling for the routed event that occurs when a touch moves while inside this element. - /// - /// The that contains the event data. - protected override void OnTouchMove(TouchEventArgs e) - { - OnTouch(e); - base.OnTouchMove(e); - } - - /// - /// Provides class handling for the routed event that occurs when a touch is released inside this element. - /// - /// The that contains the event data. - protected override void OnTouchUp(TouchEventArgs e) - { - ReleaseTouchCapture(e.TouchDevice); - OnTouch(e); - base.OnTouchUp(e); - } - - /// - /// Handles a event. - /// - /// The instance containing the event data. - private void OnTouch(TouchEventArgs e) - { - var browser = GetBrowser(); - - if (!e.Handled && browser != null) - { - var modifiers = WpfExtensions.GetModifierKeys(); - var touchPoint = e.GetTouchPoint(this); - var touchEventType = TouchEventType.Cancelled; - switch (touchPoint.Action) - { - case TouchAction.Down: - { - touchEventType = TouchEventType.Pressed; - break; - } - case TouchAction.Move: - { - touchEventType = TouchEventType.Moved; - break; - } - case TouchAction.Up: - { - touchEventType = TouchEventType.Released; - break; - } - default: - { - touchEventType = TouchEventType.Cancelled; - break; - } - } - - var touchEvent = new TouchEvent() - { - Id = e.TouchDevice.Id, - X = (float)touchPoint.Position.X, - Y = (float)touchPoint.Position.Y, - PointerType = PointerType.Touch, - Type = touchEventType, - Modifiers = modifiers, - }; - - browser.GetHost().SendTouchEvent(touchEvent); - - e.Handled = true; - } - } - /// /// 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. ///