diff --git a/CefSharp.OffScreen/CefSharp.OffScreen.csproj b/CefSharp.OffScreen/CefSharp.OffScreen.csproj index b23606934..6f8cd436c 100644 --- a/CefSharp.OffScreen/CefSharp.OffScreen.csproj +++ b/CefSharp.OffScreen/CefSharp.OffScreen.csproj @@ -75,6 +75,7 @@ + diff --git a/CefSharp.OffScreen/ChromiumWebBrowser.cs b/CefSharp.OffScreen/ChromiumWebBrowser.cs index 19203d797..94e723c9f 100644 --- a/CefSharp.OffScreen/ChromiumWebBrowser.cs +++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs @@ -280,8 +280,19 @@ namespace CefSharp.OffScreen /// Fired by a separate thread when Chrome has re-rendered. /// This means that a Bitmap will be returned by ScreenshotOrNull(). /// + [Obsolete("Subscribe to OnPaint instead as this event will be removed in future versions.")] public event EventHandler NewScreenshot; + /// + /// Fired on the CEF UI thread, which by default is not the same as your application main thread. + /// Called when an element should be painted. Pixel values passed to this method are scaled relative to view coordinates + /// based on the value of ScreenInfo.DeviceScaleFactor returned from GetScreenInfo. |type| indicates whether the element + /// is the view or the popup widget. |buffer| contains the pixel data for the whole image. |dirtyRects| contains the set + /// of rectangles in pixel coordinates that need to be repainted. |buffer| will be |width|*|height|*4 bytes in size and + /// represents a BGRA image with an upper-left origin. + /// + public event EventHandler OnPaint; + /// /// A flag that indicates if you can execute javascript in the main frame. /// Flag is set to true in IRenderProcessMessageHandler.OnContextCreated. @@ -444,7 +455,7 @@ namespace CefSharp.OffScreen /// /// Get/set the size of the Chromium viewport, in pixels. - /// This also changes the size of the next screenshot. + /// This also changes the size of the next rendered bitmap. /// /// The size. public Size Size @@ -468,13 +479,14 @@ namespace CefSharp.OffScreen /// Immediately returns a copy of the last rendering from Chrome, /// or null if no rendering has occurred yet. /// Chrome also renders the page loading, so if you want to see a complete rendering, - /// only start this task once your page is loaded (which you can detect via FrameLoadEnd + /// only start this task once your page is loaded (which you can detect via LoadingStateChanged/FrameLoadEnd /// or your own heuristics based on evaluating JavaScript). /// It is your responsibility to dispose the returned Bitmap. - /// The bitmap size is determined by the Size property set earlier. + /// The bitmap size is determined by the property set earlier. /// /// Choose which bitmap to retrieve, choose for a merged bitmap. /// Bitmap. + [Obsolete("Use ScreenshotAsync as this method will be removed in future versions.")] public Bitmap ScreenshotOrNull(PopupBlending blend = PopupBlending.Main) { lock (BitmapLock) @@ -502,15 +514,6 @@ namespace CefSharp.OffScreen } } - /// - /// Sets the Bitmap to render with a copy of bitmap in parameter. - /// - /// The bitmap which will be copied - protected void SetBitmap(Bitmap bitmap) - { - Bitmap = (Bitmap)bitmap.Clone(); - } - /// /// Starts a task that waits for the next rendering from Chrome. /// Chrome also renders the page loading, so if you want to see a complete rendering, @@ -531,17 +534,17 @@ namespace CefSharp.OffScreen if (screenshot == null || ignoreExistingScreenshot) { - EventHandler newScreenshot = null; // otherwise we cannot reference ourselves in the anonymous method below + EventHandler onPaint = null; // otherwise we cannot reference ourselves in the anonymous method below - newScreenshot = (sender, e) => + onPaint = (sender, e) => { - // Chromium has rendered. Tell the task about it. - NewScreenshot -= newScreenshot; + // Chromium has rendered. Tell the task about it. + OnPaint -= onPaint; completionSource.TrySetResultAsync(ScreenshotOrNull()); }; - NewScreenshot += newScreenshot; + OnPaint += onPaint; } else { @@ -631,28 +634,67 @@ namespace CefSharp.OffScreen } /// - /// Gets the screen information. + /// Gets the screen information (scale factor). /// /// ScreenInfo. ScreenInfo IRenderWebBrowser.GetScreenInfo() { - var screenInfo = new ScreenInfo(scaleFactor : 1.0F); - + return GetScreenInfo(); + } + + /// + /// Gets the screen information (scale factor). + /// + /// ScreenInfo. + protected virtual ScreenInfo GetScreenInfo() + { + var screenInfo = new ScreenInfo(scaleFactor: 1.0F); + return screenInfo; } /// - /// Gets the view rect. + /// Gets the view rect (width, height) /// /// ViewRect. ViewRect IRenderWebBrowser.GetViewRect() + { + return GetViewRect(); + } + + /// + /// Gets the view rect (width, height) + /// + /// ViewRect. + protected virtual ViewRect GetViewRect() { var viewRect = new ViewRect(size.Width, size.Height); - + return viewRect; } + /// + /// Called to retrieve the translation from view coordinates to actual screen coordinates. + /// + /// x + /// y + /// screen x + /// screen y + /// Return true if the screen coordinates were provided. bool IRenderWebBrowser.GetScreenPoint(int viewX, int viewY, out int screenX, out int screenY) + { + return GetScreenPoint(viewX, viewY, out screenX, out screenY); + } + + /// + /// Called to retrieve the translation from view coordinates to actual screen coordinates. + /// + /// x + /// y + /// screen x + /// screen y + /// Return true if the screen coordinates were provided. + protected virtual bool GetScreenPoint(int viewX, int viewY, out int screenX, out int screenY) { screenX = 0; screenY = 0; @@ -685,12 +727,27 @@ namespace CefSharp.OffScreen /// information about the bitmap to be rendered void IRenderWebBrowser.OnPaint(BitmapInfo bitmapInfo) { - InvokeRenderAsync(bitmapInfo); + var handled = false; - var handler = NewScreenshot; + var handler = OnPaint; if (handler != null) { - handler(this, EventArgs.Empty); + var args = new OnPaintEventArgs(bitmapInfo.IsPopup, bitmapInfo.DirtyRect, bitmapInfo.BackBufferHandle, + bitmapInfo.Width, bitmapInfo.Height, bitmapInfo.BytesPerPixel, bitmapInfo.NumberOfBytes); + handler(this, args); + + handled = args.Handled; + } + + if(!handled) + { + var newScreenshotHandler = NewScreenshot; + if (newScreenshotHandler != null) + { + newScreenshotHandler(this, EventArgs.Empty); + } + + InvokeRenderAsync(bitmapInfo); } } @@ -738,10 +795,20 @@ namespace CefSharp.OffScreen /// The type. void IRenderWebBrowser.SetCursor(IntPtr handle, CursorType type) { + SetCursor(handle, type); } /// - /// Starts the dragging. + /// Sets the cursor. + /// + /// The handle. + /// The type. + protected virtual void SetCursor(IntPtr handle, CursorType type) + { + } + + /// + /// Starts dragging. /// /// The drag data. /// The mask. @@ -749,13 +816,31 @@ namespace CefSharp.OffScreen /// The y. /// true if XXXX, false otherwise. bool IRenderWebBrowser.StartDragging(IDragData dragData, DragOperationsMask mask, int x, int y) + { + return StartDragging(dragData, mask, x, y); + } + + /// + /// Starts dragging. + /// + /// The drag data. + /// The mask. + /// The x. + /// The y. + /// true if XXXX, false otherwise. + protected virtual bool StartDragging(IDragData dragData, DragOperationsMask mask, int x, int y) { return false; } void IRenderWebBrowser.UpdateDragCursor(DragOperationsMask operation) { - //TODO: Someone should implement this + UpdateDragCursor(operation); + } + + protected virtual void UpdateDragCursor(DragOperationsMask operation) + { + } /// @@ -763,6 +848,11 @@ namespace CefSharp.OffScreen /// /// if set to true [show]. void IRenderWebBrowser.SetPopupIsOpen(bool show) + { + SetPopupIsOpen(show); + } + + protected virtual void SetPopupIsOpen(bool show) { PopupOpen = show; @@ -782,6 +872,18 @@ namespace CefSharp.OffScreen /// The x. /// The y. void IRenderWebBrowser.SetPopupSizeAndPosition(int width, int height, int x, int y) + { + SetPopupSizeAndPosition(width, height, x, y); + } + + /// + /// Sets the popup size and position. + /// + /// The width. + /// The height. + /// The x. + /// The y. + protected virtual void SetPopupSizeAndPosition(int width, int height, int x, int y) { popupPosition.X = x; popupPosition.Y = y; @@ -791,7 +893,12 @@ namespace CefSharp.OffScreen void IRenderWebBrowser.OnImeCompositionRangeChanged(Range selectedRange, Rect[] characterBounds) { - //TODO: Implement this + OnImeCompositionRangeChanged(selectedRange, characterBounds); + } + + protected virtual void OnImeCompositionRangeChanged(Range selectedRange, Rect[] characterBounds) + { + } /// diff --git a/CefSharp.OffScreen/OnPaintEventArgs.cs b/CefSharp.OffScreen/OnPaintEventArgs.cs new file mode 100644 index 000000000..1734fbcf1 --- /dev/null +++ b/CefSharp.OffScreen/OnPaintEventArgs.cs @@ -0,0 +1,45 @@ +// Copyright © 2010-2017 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; + +namespace CefSharp +{ + /// + /// Event arguments to the OnPaint event handler. + /// + public class OnPaintEventArgs : EventArgs + { + public bool Handled { get; set; } + public bool IsPopup { get; private set; } + public int Width { get; private set; } + public int Height { get; private set; } + public IntPtr BufferHandle { get; private set; } + public int BytesPerPixel { get; private set; } + public int NumberOfBytes { get; set; } + public CefDirtyRect DirtyRect { get; private set; } + + /// + /// Creates a new OnPaint event arg + /// + /// is popup + /// dirty rect if enabled + /// buffer handle (back buffer) + /// width + /// height + /// bytes per pixel + /// number of bytes in the buffer + public OnPaintEventArgs(bool isPopup, CefDirtyRect dirtyRect, IntPtr bufferHandle, int width, int height, int bytesPerPixel, int numberOfBytes) + { + IsPopup = isPopup; + DirtyRect = dirtyRect; + BufferHandle = bufferHandle; + Width = width; + Height = height; + BytesPerPixel = bytesPerPixel; + NumberOfBytes = numberOfBytes; + } + } +} +