Skip to content

Commit

Permalink
Merge 82c4d19 into c303e1c
Browse files Browse the repository at this point in the history
  • Loading branch information
João Neves authored Jan 8, 2018
2 parents c303e1c + 82c4d19 commit 31f21ef
Showing 1 changed file with 33 additions and 90 deletions.
123 changes: 33 additions & 90 deletions CefSharp.Wpf/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using CefSharp.Internals;
using CefSharp.Wpf.Internals;
using CefSharp.Wpf.Rendering;
using Microsoft.Win32.SafeHandles;
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
Expand All @@ -17,10 +14,12 @@
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Threading;
using CefSharp.ModelBinding;
using System.Runtime.CompilerServices;
using CefSharp.Internals;
using CefSharp.Wpf.Internals;
using CefSharp.Wpf.Rendering;
using Microsoft.Win32.SafeHandles;

namespace CefSharp.Wpf
namespace CefSharp.Wpf
{
/// <summary>
/// ChromiumWebBrowser is the WPF web browser control
Expand All @@ -35,10 +34,6 @@ public class ChromiumWebBrowser : ContentControl, IRenderWebBrowser, IWpfWebBrow
/// </summary>
private HwndSource source;
/// <summary>
/// The source hook
/// </summary>
private HwndSourceHook sourceHook;
/// <summary>
/// The tooltip timer
/// </summary>
private DispatcherTimer tooltipTimer;
Expand Down Expand Up @@ -582,7 +577,7 @@ protected virtual void Dispose(bool isDisposing)

Cef.RemoveDisposable(this);

RemoveSourceHook();
source = null;
}
}

Expand Down Expand Up @@ -1559,8 +1554,6 @@ private void PresentationSourceChangedHandler(object sender, SourceChangedEventA
var notifyDpiChanged = DpiScaleFactor > 0 && !DpiScaleFactor.Equals(matrix.M11);

DpiScaleFactor = source.CompositionTarget.TransformToDevice.M11;
sourceHook = SourceHook;
source.AddHook(sourceHook);

if (notifyDpiChanged && browser != null)
{
Expand All @@ -1579,8 +1572,6 @@ private void PresentationSourceChangedHandler(object sender, SourceChangedEventA
}
else if (args.OldSource != null)
{
RemoveSourceHook();

var window = args.OldSource.RootVisual as Window;
if (window != null)
{
Expand Down Expand Up @@ -1631,18 +1622,6 @@ private void OnWindowLocationChanged(object sender, EventArgs e)
UpdateBrowserScreenLocation();
}

/// <summary>
/// Removes the source hook.
/// </summary>
private void RemoveSourceHook()
{
if (source != null && sourceHook != null)
{
source.RemoveHook(sourceHook);
source = null;
}
}

/// <summary>
/// Create the underlying Browser instance, can be overriden to defer control creation
/// The browser will only be created when size &gt; Size(0,0). If you specify a positive
Expand Down Expand Up @@ -1842,60 +1821,6 @@ private Popup CreatePopup()
return newPopup;
}

/// <summary>
/// WindowProc callback interceptor. Handles Windows messages intended for the source hWnd, and passes them to the
/// contained browser as needed.
/// </summary>
/// <param name="hWnd">The source handle.</param>
/// <param name="message">The message.</param>
/// <param name="wParam">Additional message info.</param>
/// <param name="lParam">Even more message info.</param>
/// <param name="handled">if set to <c>true</c>, the event has already been handled by someone else.</param>
/// <returns>IntPtr.</returns>
protected virtual IntPtr SourceHook(IntPtr hWnd, int message, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (handled)
{
return IntPtr.Zero;
}

switch ((WM)message)
{
case WM.SYSCHAR:
case WM.SYSKEYDOWN:
case WM.SYSKEYUP:
case WM.KEYDOWN:
case WM.KEYUP:
case WM.CHAR:
case WM.IME_CHAR:
{
if (!IsKeyboardFocused)
{
break;
}

if (message == (int)WM.SYSKEYDOWN &&
wParam.ToInt32() == KeyInterop.VirtualKeyFromKey(Key.F4))
{
// We don't want CEF to receive this event (and mark it as handled), since that makes it impossible to
// shut down a CefSharp-based app by pressing Alt-F4, which is kind of bad.
return IntPtr.Zero;
}

if (browser != null)
{
browser.GetHost().SendKeyEvent(message, wParam.CastToInt32(), lParam.CastToInt32());
handled = true;
}

break;
}
}

return IntPtr.Zero;
}

/// <summary>
/// Converts a .NET Drag event to a CefSharp MouseEvent
/// </summary>
/// <param name="e">The <see cref="DragEventArgs"/> instance containing the event data.</param>
Expand Down Expand Up @@ -2036,25 +1961,43 @@ protected override void OnPreviewKeyUp(KeyEventArgs e)
/// <param name="e">The <see cref="KeyEventArgs"/> instance containing the event data.</param>
private void OnPreviewKey(KeyEventArgs e)
{
// As KeyDown and KeyUp bubble, it appears they're being handled before they get a chance to
// trigger the appropriate WM_ messages handled by our SourceHook, so we have to handle these extra keys here.
if (browser != null)
{
var modifiers = e.GetModifiers();
var message = (int)(e.IsDown ? WM.KEYDOWN : WM.KEYUP);
var virtualKey = KeyInterop.VirtualKeyFromKey(e.Key);

browser.GetHost().SendKeyEvent(message, virtualKey, (int)modifiers);
}

// Hooking the Tab key like this makes the tab focusing in essence work like
// KeyboardNavigation.TabNavigation="Cycle"; you will never be able to Tab out of the web browser control.
// We also add the condition to allow ctrl+a to work when the web browser control is put inside listbox.
// Prevent keyboard navigation using arrows and home and end keys
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))
{
var modifiers = e.GetModifiers();
var message = (int)(e.IsDown ? WM.KEYDOWN : WM.KEYUP);
var virtualKey = KeyInterop.VirtualKeyFromKey(e.Key);
e.Handled = true;
}
}

if(browser != null)
/// <summary>
/// Handles the <see cref="E:PreviewTextInput" /> event.
/// </summary>
/// <param name="e">The <see cref="TextCompositionEventArgs"/> instance containing the event data.</param>
protected override void OnPreviewTextInput(TextCompositionEventArgs e)
{
if (!e.Handled && browser != null)
{
var browserHost = browser.GetHost();
for (int i = 0; i < e.Text.Length; i++)
{
browser.GetHost().SendKeyEvent(message, virtualKey, (int)modifiers);
e.Handled = true;
browserHost.SendKeyEvent((int)WM.CHAR, e.Text[i], 0);
}
e.Handled = true;
}
base.OnPreviewTextInput(e);
}

/// <summary>
Expand Down

0 comments on commit 31f21ef

Please sign in to comment.