using System.Runtime.InteropServices; using System.Windows.Interop; using Febe.Desktop.Native; using Microsoft.Web.WebView2.Core; namespace Febe.Desktop.Browser; /// /// The second attempt of S2 (spec §6.6, «composizione»): WebView2 hosted as a DirectComposition visual of the window /// «Browser di Febe» instead of a child window. The window owns the visual tree (a topmost target with one visual, /// the WebView at its origin over the page area); WebView2 gets no window of its own to be occluded or hidden, but /// then the host forwards the mouse (SendMouseInput) and the cursor. The keyboard reaches the page through the /// focus WebView2 keeps (MoveFocus). The bar of the window stays below the page and keeps its own input. /// /// To be tested on Windows (docs/windows.md, S2): it cannot run here. internal sealed class CompositionHost : IDisposable { private readonly IntPtr _window; private readonly Func _page; private readonly IDCompositionDevice _device; private readonly IDCompositionTarget _target; private readonly IDCompositionVisual _visual; private HwndSource? _source; private CoreWebView2CompositionController? _controller; private bool _tracking; private bool _captured; public CompositionHost(IntPtr window, Func pageBounds) { _window = window; _page = pageBounds ?? throw new ArgumentNullException(nameof(pageBounds)); var iid = typeof(IDCompositionDevice).GUID; Marshal.ThrowExceptionForHR(Win32.DCompositionCreateDevice2(IntPtr.Zero, ref iid, out var device)); _device = (IDCompositionDevice)device; _target = _device.CreateTargetForHwnd(window, topmost: true); _visual = _device.CreateVisual(); _target.SetRoot(_visual); _device.Commit(); } /// Gives the visual to WebView2 and starts forwarding the mouse of the page area. public void Attach(CoreWebView2CompositionController controller) { _controller = controller ?? throw new ArgumentNullException(nameof(controller)); controller.RootVisualTarget = _visual; _device.Commit(); _source = HwndSource.FromHwnd(_window); _source?.AddHook(Hook); } public void Dispose() { _source?.RemoveHook(Hook); _source = null; if (_controller is { } controller) { try { controller.RootVisualTarget = null; } catch (Exception ex) when (ex is InvalidOperationException or COMException) { // The WebView is already closed. } } _controller = null; Marshal.ReleaseComObject(_visual); Marshal.ReleaseComObject(_target); Marshal.ReleaseComObject(_device); } private IntPtr Hook(IntPtr window, int message, IntPtr wParam, IntPtr lParam, ref bool handled) { if (_controller is not { } controller) { return IntPtr.Zero; } if (message == Win32.WmSetCursor) { if (Win32.LowWord(lParam) == Win32.HtClient && CursorInPage(window, out _)) { Win32.SetCursor(controller.Cursor); handled = true; return new IntPtr(1); } return IntPtr.Zero; } var isMouse = message is >= Win32.WmMouseFirst and <= Win32.WmMouseHWheel || message == Win32.WmMouseLeave; if (!isMouse) { return IntPtr.Zero; } var point = new Win32.Point { X = Win32.LowWord(lParam), Y = Win32.HighWord(lParam) }; if (message is Win32.WmMouseWheel or Win32.WmMouseHWheel) { // Wheel messages carry screen coordinates. Win32.ScreenToClient(window, ref point); } var page = _page(); var inside = point.X >= page.Left && point.X < page.Right && point.Y >= page.Top && point.Y < page.Bottom; if (message != Win32.WmMouseLeave && !inside && !_captured) { return IntPtr.Zero; } var kind = (CoreWebView2MouseEventKind)message; var keys = message == Win32.WmMouseLeave ? CoreWebView2MouseEventVirtualKeys.None : (CoreWebView2MouseEventVirtualKeys)(Win32.LowWord(wParam) & 0xFFFF); uint data = message switch { Win32.WmMouseWheel or Win32.WmMouseHWheel => unchecked((uint)Win32.HighWord(wParam)), 0x020B or 0x020C or 0x020D => unchecked((uint)Win32.HighWord(wParam)), // X buttons: which one _ => 0, }; switch (message) { case 0x0201 or 0x0204 or 0x0207 or 0x020B: Win32.SetCapture(window); _captured = true; break; case 0x0202 or 0x0205 or 0x0208 or 0x020C: Win32.ReleaseCapture(); _captured = false; break; case Win32.WmMouseLeave: _tracking = false; break; } if (message == Win32.WmMouseFirst && !_tracking) { var track = new Win32.TrackMouseEventInfo { Size = (uint)Marshal.SizeOf(), Flags = Win32.TmeLeave, Window = window, }; _tracking = Win32.TrackMouseEvent(ref track); } controller.SendMouseInput(kind, keys, data, new System.Drawing.Point(point.X - page.Left, point.Y - page.Top)); handled = true; return IntPtr.Zero; } private bool CursorInPage(IntPtr window, out Win32.Point point) { point = default; if (!GetCursorPos(out var screen)) { return false; } point = screen; Win32.ScreenToClient(window, ref point); var page = _page(); return point.X >= page.Left && point.X < page.Right && point.Y >= page.Top && point.Y < page.Bottom; } [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool GetCursorPos(out Win32.Point point); }