Skip to content

Commit d49f975

Browse files
committed
OffScreen - IRenderHandler now implements all methods
DefaultRenderHandler handles all the calls required by the OffScreen ChromiumWebBrowser implementation
1 parent 93fdd63 commit d49f975

File tree

3 files changed

+206
-66
lines changed

3 files changed

+206
-66
lines changed

CefSharp.OffScreen/ChromiumWebBrowser.cs

Lines changed: 23 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,6 @@ public class ChromiumWebBrowser : IRenderWebBrowser
7979
/// binding.</remarks>
8080
public bool CanGoBack { get; private set; }
8181
/// <summary>
82-
/// Gets or sets a value indicating whether the popup is open.
83-
/// </summary>
84-
/// <value>
85-
/// <c>true</c> if popup is opened; otherwise, <c>false</c>.
86-
/// </value>
87-
public bool PopupOpen { get; protected set; }
88-
/// <summary>
8982
/// A flag that indicates whether the state of the control currently supports the GoForward action (true) or not (false).
9083
/// </summary>
9184
/// <value><c>true</c> if this instance can go forward; otherwise, <c>false</c>.</value>
@@ -102,7 +95,6 @@ public class ChromiumWebBrowser : IRenderWebBrowser
10295
/// </summary>
10396
/// <value>The request context.</value>
10497
public IRequestContext RequestContext { get; private set; }
105-
10698
/// <summary>
10799
/// Implement <see cref="IJsDialogHandler" /> and assign to handle events related to JavaScript Dialogs.
108100
/// </summary>
@@ -275,16 +267,6 @@ public class ChromiumWebBrowser : IRenderWebBrowser
275267
/// </summary>
276268
public bool CanExecuteJavascriptInMainFrame { get; private set; }
277269

278-
/// <summary>
279-
/// Top Left position of the popup.
280-
/// </summary>
281-
private Point popupPosition;
282-
283-
/// <summary>
284-
/// Size of the popup.
285-
/// </summary>
286-
private Size popupSize;
287-
288270
/// <summary>
289271
/// Create a new OffScreen Chromium Browser
290272
/// </summary>
@@ -315,9 +297,6 @@ public ChromiumWebBrowser(string address = "", BrowserSettings browserSettings =
315297
CreateBrowser(IntPtr.Zero);
316298
}
317299

318-
popupPosition = new Point();
319-
popupSize = new Size();
320-
321300
RenderHandler = new DefaultRenderHandler(this);
322301
}
323302

@@ -385,22 +364,6 @@ protected virtual void Dispose(bool disposing)
385364
this.SetHandlersToNull();
386365
}
387366

388-
/// <summary>
389-
/// Gets the size of the popup.
390-
/// </summary>
391-
public Size PopupSize
392-
{
393-
get { return popupSize; }
394-
}
395-
396-
/// <summary>
397-
/// Gets the popup position.
398-
/// </summary>
399-
public Point PopupPosition
400-
{
401-
get { return popupPosition; }
402-
}
403-
404367
/// <summary>
405368
/// Create the underlying browser. The instance address, browser settings and request context will be used.
406369
/// </summary>
@@ -475,20 +438,20 @@ public Bitmap ScreenshotOrNull(PopupBlending blend = PopupBlending.Main)
475438

476439
if (blend == PopupBlending.Popup)
477440
{
478-
return PopupOpen ? renderHandler.PopupBuffer.CreateBitmap() : null;
441+
return renderHandler.PopupOpen ? renderHandler.PopupBuffer.CreateBitmap() : null;
479442
}
480443

481444

482445
var bitmap = renderHandler.BitmapBuffer.CreateBitmap();
483446

484-
if (PopupOpen && bitmap != null)
447+
if (renderHandler.PopupOpen && bitmap != null)
485448
{
486449
var popup = renderHandler.PopupBuffer.CreateBitmap();
487450
if (popup == null)
488451
{
489452
return bitmap;
490453
}
491-
return MergeBitmaps(bitmap, popup);
454+
return MergeBitmaps(bitmap, popup, renderHandler.PopupPosition);
492455
}
493456

494457
return bitmap;
@@ -667,9 +630,7 @@ public IBrowser GetBrowser()
667630
/// <returns>ScreenInfo.</returns>
668631
protected virtual ScreenInfo? GetScreenInfo()
669632
{
670-
var screenInfo = new ScreenInfo(scaleFactor: 1.0F);
671-
672-
return screenInfo;
633+
return RenderHandler?.GetScreenInfo();
673634
}
674635

675636
/// <summary>
@@ -687,9 +648,7 @@ public IBrowser GetBrowser()
687648
/// <returns>ViewRect.</returns>
688649
protected virtual ViewRect? GetViewRect()
689650
{
690-
var viewRect = new ViewRect(size.Width, size.Height);
691-
692-
return viewRect;
651+
return RenderHandler?.GetViewRect();
693652
}
694653

695654
/// <summary>
@@ -718,7 +677,7 @@ protected virtual bool GetScreenPoint(int viewX, int viewY, out int screenX, out
718677
screenX = 0;
719678
screenY = 0;
720679

721-
return false;
680+
return RenderHandler?.GetScreenPoint(viewX, viewY, out screenX, out screenY) ?? false;
722681
}
723682

724683
/// <summary>
@@ -755,11 +714,10 @@ void IRenderWebBrowser.OnPaint(PaintElementType type, Rect dirtyRect, IntPtr buf
755714
/// <param name="buffer">The bitmap will be will be width * height *4 bytes in size and represents a BGRA image with an upper-left origin</param>
756715
/// <param name="width">width</param>
757716
/// <param name="height">height</param>
717+
[Obsolete("This method will be removed, implement IRenderHandler and assign browser.RenderHandler")]
758718
protected virtual void OnPaint(PaintElementType type, Rect dirtyRect, IntPtr buffer, int width, int height)
759719
{
760-
var isPopup = type == PaintElementType.Popup;
761-
762-
RenderHandler?.OnPaint(isPopup, dirtyRect, buffer, width, height);
720+
RenderHandler?.OnPaint(type, dirtyRect, buffer, width, height);
763721
}
764722

765723
/// <summary>
@@ -779,8 +737,10 @@ void IRenderWebBrowser.OnCursorChange(IntPtr cursor, CursorType type, CursorInfo
779737
/// <param name="cursor">If type is Custom then customCursorInfo will be populated with the custom cursor information</param>
780738
/// <param name="type">cursor type</param>
781739
/// <param name="customCursorInfo">custom cursor Information</param>
740+
[Obsolete("This method will be removed, implement IRenderHandler and assign browser.RenderHandler")]
782741
protected virtual void OnCursorChange(IntPtr cursor, CursorType type, CursorInfo customCursorInfo)
783742
{
743+
RenderHandler?.OnCursorChange(cursor, type, customCursorInfo);
784744
}
785745

786746
/// <summary>
@@ -804,19 +764,21 @@ bool IRenderWebBrowser.StartDragging(IDragData dragData, DragOperationsMask mask
804764
/// <param name="x">The x.</param>
805765
/// <param name="y">The y.</param>
806766
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
767+
[Obsolete("This method will be removed, implement IRenderHandler and assign browser.RenderHandler")]
807768
protected virtual bool StartDragging(IDragData dragData, DragOperationsMask mask, int x, int y)
808769
{
809-
return false;
770+
return RenderHandler?.StartDragging(dragData, mask, x, y) ?? false;
810771
}
811772

812773
void IRenderWebBrowser.UpdateDragCursor(DragOperationsMask operation)
813774
{
814775
UpdateDragCursor(operation);
815776
}
816777

778+
[Obsolete("This method will be removed, implement IRenderHandler and assign browser.RenderHandler")]
817779
protected virtual void UpdateDragCursor(DragOperationsMask operation)
818780
{
819-
781+
RenderHandler?.UpdateDragCursor(operation);
820782
}
821783

822784
/// <summary>
@@ -828,9 +790,10 @@ void IRenderWebBrowser.OnPopupShow(bool show)
828790
OnPopupShow(show);
829791
}
830792

793+
[Obsolete("This method will be removed, implement IRenderHandler and assign browser.RenderHandler")]
831794
protected virtual void OnPopupShow(bool show)
832795
{
833-
PopupOpen = show;
796+
RenderHandler?.OnPopupShow(show);
834797
}
835798

836799
/// <summary>
@@ -846,22 +809,21 @@ void IRenderWebBrowser.OnPopupSize(Rect rect)
846809
/// Called when the browser wants to move or resize the popup widget.
847810
/// </summary>
848811
/// <param name="rect">contains the new location and size in view coordinates. </param>
812+
[Obsolete("This method will be removed, implement IRenderHandler and assign browser.RenderHandler")]
849813
protected virtual void OnPopupSize(Rect rect)
850814
{
851-
popupPosition.X = rect.X;
852-
popupPosition.Y = rect.Y;
853-
popupSize.Width = rect.Width;
854-
popupSize.Height = rect.Height;
815+
RenderHandler?.OnPopupSize(rect);
855816
}
856817

857818
void IRenderWebBrowser.OnImeCompositionRangeChanged(Range selectedRange, Rect[] characterBounds)
858819
{
859820
OnImeCompositionRangeChanged(selectedRange, characterBounds);
860821
}
861822

823+
[Obsolete("This method will be removed, implement IRenderHandler and assign browser.RenderHandler")]
862824
protected virtual void OnImeCompositionRangeChanged(Range selectedRange, Rect[] characterBounds)
863825
{
864-
826+
RenderHandler?.OnImeCompositionRangeChanged(selectedRange, characterBounds);
865827
}
866828

867829
/// <summary>
@@ -990,14 +952,15 @@ void IWebBrowserInternal.SetCanExecuteJavascriptOnMainFrame(bool canExecute)
990952
/// </summary>
991953
/// <param name="firstBitmap">First bitmap, this will be the first image drawn</param>
992954
/// <param name="secondBitmap">Second bitmap, this image will be drawn on the first</param>
955+
/// <param name="secondBitmapPosition">Position of the second bitmap</param>
993956
/// <returns>The merged bitmap, size of firstBitmap</returns>
994-
private Bitmap MergeBitmaps(Bitmap firstBitmap, Bitmap secondBitmap)
957+
private Bitmap MergeBitmaps(Bitmap firstBitmap, Bitmap secondBitmap, Point secondBitmapPosition)
995958
{
996959
var mergedBitmap = new Bitmap(firstBitmap.Width, firstBitmap.Height, PixelFormat.Format32bppPArgb);
997960
using (var g = Graphics.FromImage(mergedBitmap))
998961
{
999962
g.DrawImage(firstBitmap, new Rectangle(0, 0, firstBitmap.Width, firstBitmap.Height));
1000-
g.DrawImage(secondBitmap, new Rectangle(popupPosition.X, popupPosition.Y, secondBitmap.Width, secondBitmap.Height));
963+
g.DrawImage(secondBitmap, new Rectangle(secondBitmapPosition.X, secondBitmapPosition.Y, secondBitmap.Width, secondBitmap.Height));
1001964
}
1002965
return mergedBitmap;
1003966
}

CefSharp.OffScreen/DefaultRenderHandler.cs

Lines changed: 98 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,35 @@
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

55
using System;
6+
using CefSharp.Enums;
67
using CefSharp.Structs;
78

9+
using Point = System.Drawing.Point;
10+
using Size = System.Drawing.Size;
11+
812
namespace CefSharp.OffScreen
913
{
1014
public class DefaultRenderHandler : IRenderHandler
1115
{
1216
private ChromiumWebBrowser browser;
1317

18+
private Size popupSize;
19+
private Point popupPosition;
20+
21+
/// <summary>
22+
/// Need a lock because the caller may be asking for the bitmap
23+
/// while Chromium async rendering has returned on another thread.
24+
/// </summary>
25+
public readonly object BitmapLock = new object();
26+
27+
/// <summary>
28+
/// Gets or sets a value indicating whether the popup is open.
29+
/// </summary>
30+
/// <value>
31+
/// <c>true</c> if popup is opened; otherwise, <c>false</c>.
32+
/// </value>
33+
public bool PopupOpen { get; protected set; }
34+
1435
/// <summary>
1536
/// Contains the last bitmap buffer. Direct access
1637
/// to the underlying buffer - there is no locking when trying
@@ -25,30 +46,102 @@ public class DefaultRenderHandler : IRenderHandler
2546
public BitmapBuffer PopupBuffer { get; private set; }
2647

2748
/// <summary>
28-
/// Need a lock because the caller may be asking for the bitmap
29-
/// while Chromium async rendering has returned on another thread.
49+
/// Gets the size of the popup.
3050
/// </summary>
31-
public readonly object BitmapLock = new object();
51+
public Size PopupSize
52+
{
53+
get { return popupSize; }
54+
}
55+
56+
/// <summary>
57+
/// Gets the popup position.
58+
/// </summary>
59+
public Point PopupPosition
60+
{
61+
get { return popupPosition; }
62+
}
3263

3364
public DefaultRenderHandler(ChromiumWebBrowser browser)
3465
{
3566
this.browser = browser;
3667

68+
popupPosition = new Point();
69+
popupSize = new Size();
70+
3771
BitmapBuffer = new BitmapBuffer(BitmapLock);
3872
PopupBuffer = new BitmapBuffer(BitmapLock);
39-
4073
}
4174

4275
public void Dispose()
4376
{
4477
browser = null;
4578
}
4679

47-
public virtual void OnPaint(bool isPopup, Rect dirtyRect, IntPtr buffer, int width, int height)
80+
public virtual ScreenInfo? GetScreenInfo()
4881
{
82+
var screenInfo = new ScreenInfo(scaleFactor: 1.0F);
83+
84+
return screenInfo;
85+
}
86+
87+
public virtual ViewRect? GetViewRect()
88+
{
89+
//TODO: See if this can be refactored and remove browser reference
90+
var size = browser.Size;
91+
92+
var viewRect = new ViewRect(size.Width, size.Height);
93+
94+
return viewRect;
95+
}
96+
97+
public virtual bool GetScreenPoint(int viewX, int viewY, out int screenX, out int screenY)
98+
{
99+
screenX = 0;
100+
screenY = 0;
101+
102+
return false;
103+
}
104+
105+
public virtual void OnPaint(PaintElementType type, Rect dirtyRect, IntPtr buffer, int width, int height)
106+
{
107+
var isPopup = type == PaintElementType.Popup;
108+
49109
var bitmapBuffer = isPopup ? PopupBuffer : BitmapBuffer;
50110

51111
bitmapBuffer.UpdateBuffer(width, height, buffer, dirtyRect);
52112
}
113+
114+
public virtual void OnCursorChange(IntPtr cursor, CursorType type, CursorInfo customCursorInfo)
115+
{
116+
117+
}
118+
119+
public virtual bool StartDragging(IDragData dragData, DragOperationsMask mask, int x, int y)
120+
{
121+
return false;
122+
}
123+
124+
public virtual void UpdateDragCursor(DragOperationsMask operation)
125+
{
126+
127+
}
128+
129+
public virtual void OnPopupShow(bool show)
130+
{
131+
PopupOpen = show;
132+
}
133+
134+
public virtual void OnPopupSize(Rect rect)
135+
{
136+
popupPosition.X = rect.X;
137+
popupPosition.Y = rect.Y;
138+
popupSize.Width = rect.Width;
139+
popupSize.Height = rect.Height;
140+
}
141+
142+
public virtual void OnImeCompositionRangeChanged(Range selectedRange, Rect[] characterBounds)
143+
{
144+
145+
}
53146
}
54147
}

0 commit comments

Comments
 (0)