1
0
mirror of https://github.com/salvadordf/CEF4Delphi.git synced 2025-02-02 10:25:26 +02:00

Fixed ConsoleBrowser2 resize issues

Renamed TCEFBrowserBitmap.BeginBufferDraw to TCEFBrowserBitmap.BeginDraw
Renamed TCEFBrowserBitmap.EndBufferDraw to TCEFBrowserBitmap.EndDraw
Renamed TCEFBrowserBitmap.UpdateBufferDimensions to TCEFBrowserBitmap.UpdateDimensions
Removed TCEFBrowserBitmap.BufferIsResize
This commit is contained in:
Salvador Díaz Fau 2024-11-30 11:18:21 +01:00
parent 5d4705c34f
commit 6badc8f3bd
7 changed files with 108 additions and 87 deletions

View File

@ -62,6 +62,7 @@ type
procedure Resize;
function CreateBrowser : boolean;
procedure CloseBrowser;
procedure UpdateSize(aNewWidth, aNewHeight : integer);
procedure InitError;
procedure WebpagePostProcessing;
procedure WebpageError;
@ -76,6 +77,7 @@ type
function CopySnapshot(var aSnapshot : TBitmap) : boolean;
function SaveSnapshotToFile(const aPath : ustring) : boolean;
procedure LoadUrl(const aURL : ustring);
function UpdateBrowserSize(aNewWidth, aNewHeight : integer): boolean;
property ErrorCode : integer read GetErrorCode;
property ErrorText : ustring read GetErrorText write SetErrorText;
@ -91,10 +93,11 @@ type
implementation
const
CEF_WEBPAGE_LOADED_MSG = WM_APP + 1;
CEF_WEBPAGE_ERROR_MSG = WM_APP + 2;
CEF_CLOSE_BROWSER_MSG = WM_APP + 3;
CEF_LOAD_PENDING_URL_MSG = WM_APP + 4;
CEF_WEBPAGE_LOADED_MSG = WM_APP + 1;
CEF_WEBPAGE_ERROR_MSG = WM_APP + 2;
CEF_CLOSE_BROWSER_MSG = WM_APP + 3;
CEF_LOAD_PENDING_URL_MSG = WM_APP + 4;
CEF_UPDATEBROWSERSIZE_MSG = WM_APP + 5;
constructor TCEFBrowserThread.Create(const aDefaultURL : ustring; aWidth, aHeight, aDelayMs : integer; const aScreenScale : single);
begin
@ -317,6 +320,12 @@ begin
PostThreadMessage(ThreadID, CEF_CLOSE_BROWSER_MSG, 0, 0);
end;
function TCEFBrowserThread.UpdateBrowserSize(aNewWidth, aNewHeight : integer): boolean;
begin
Result := Initialized and
PostThreadMessage(ThreadID, CEF_UPDATEBROWSERSIZE_MSG, WPARAM(aNewWidth), LPARAM(aNewHeight));
end;
procedure TCEFBrowserThread.Browser_OnAfterCreated(Sender: TObject; const browser: ICefBrowser);
begin
if assigned(FBrowserInfoCS) then
@ -343,7 +352,7 @@ begin
FResizeCS.Acquire;
TempForcedResize := False;
if FBrowserBitmap.BeginBufferDraw then
if FBrowserBitmap.BeginDraw then
begin
if (kind = PET_POPUP) then
begin
@ -367,7 +376,7 @@ begin
end
else
begin
TempForcedResize := FBrowserBitmap.UpdateBufferDimensions(aWidth, aHeight) or not(FBrowserBitmap.BufferIsResized(False));
TempForcedResize := FBrowserBitmap.UpdateDimensions(aWidth, aHeight);
TempWidth := FBrowserBitmap.Width;
TempHeight := FBrowserBitmap.Height;
TempScanlineSize := FBrowserBitmap.ScanlineSize;
@ -423,7 +432,7 @@ begin
end;
end;
FBrowserBitmap.EndBufferDraw;
FBrowserBitmap.EndDraw;
if (kind = PET_VIEW) then
begin
@ -540,13 +549,10 @@ begin
if FResizing then
FPendingResize := True
else
if FBrowserBitmap.BufferIsResized then
FBrowser.Invalidate(PET_VIEW)
else
begin
FResizing := True;
FBrowser.WasResized;
end;
begin
FResizing := True;
FBrowser.WasResized;
end;
finally
FResizeCS.Release;
end;
@ -612,6 +618,36 @@ begin
end;
end;
procedure TCEFBrowserThread.UpdateSize(aNewWidth, aNewHeight : integer);
begin
if assigned(FResizeCS) then
try
FResizeCS.Acquire;
if assigned(FBrowserBitmap) and
FBrowserBitmap.BeginDraw then
try
if (FBrowserBitmap.Width <> aNewWidth) or
(FBrowserBitmap.Height <> aNewHeight) then
begin
{$IFDEF DELPHI16_UP}
FBrowserBitmap.SetSize(aNewWidth, aNewHeight);
{$ELSE}
FBrowserBitmap.Width := aNewWidth;
FBrowserBitmap.Height := aNewHeight;
{$ENDIF}
FResizing := True;
FBrowser.WasResized;
end;
finally
FBrowserBitmap.EndDraw;
end;
finally
FResizeCS.Release;
end;
end;
procedure TCEFBrowserThread.DoOnError;
begin
FOnError(self);
@ -645,12 +681,13 @@ begin
while TempCont and GetMessage(TempMsg, 0, 0, 0) and not(Terminated) do
begin
case TempMsg.Message of
CEF_PENDINGRESIZE : Resize;
CEF_CLOSE_BROWSER_MSG : CloseBrowser;
CEF_LOAD_PENDING_URL_MSG : LoadPendingURL;
CEF_WEBPAGE_LOADED_MSG : WebpagePostProcessing;
CEF_WEBPAGE_ERROR_MSG : WebpageError;
WM_QUIT : TempCont := False;
CEF_PENDINGRESIZE : Resize;
CEF_CLOSE_BROWSER_MSG : CloseBrowser;
CEF_LOAD_PENDING_URL_MSG : LoadPendingURL;
CEF_WEBPAGE_LOADED_MSG : WebpagePostProcessing;
CEF_WEBPAGE_ERROR_MSG : WebpageError;
CEF_UPDATEBROWSERSIZE_MSG : UpdateSize(integer(TempMsg.wParam), integer(TempMsg.lParam));
WM_QUIT : TempCont := False;
end;
DispatchMessage(TempMsg);

View File

@ -22,17 +22,19 @@ type
FScale : single;
FSnapshotPath : ustring;
FErrorText : ustring;
FFirst : boolean;
procedure Thread_OnError(Sender: TObject);
procedure Thread_OnSnapshotAvailable(Sender: TObject);
public
constructor Create;
constructor Create(aWidth, aHeight : integer);
destructor Destroy; override;
procedure LoadURL(const aURL : ustring);
function UpdateBrowserSize(aNewWidth, aNewHeight : integer): boolean;
property Width : integer read FWidth write FWidth;
property Height : integer read FHeight write FHeight;
property Width : integer read FWidth;
property Height : integer read FHeight;
property DelayMs : integer read FDelayMs write FDelayMs;
property Scale : single read FScale write FScale;
property SnapshotPath : ustring read FSnapshotPath write FSnapshotPath;
@ -77,7 +79,7 @@ begin
WriteLn('No URL has been specified. Using the default...');
end;
EncapsulatedBrowser := TEncapsulatedBrowser.Create;
EncapsulatedBrowser := TEncapsulatedBrowser.Create(1024, 768);
EncapsulatedBrowser.LoadURL(TempURL);
end;
@ -125,13 +127,14 @@ begin
GlobalCEFApp.StartMainProcess;
end;
constructor TEncapsulatedBrowser.Create;
constructor TEncapsulatedBrowser.Create(aWidth, aHeight : integer);
begin
inherited Create;
FFirst := True;
FThread := nil;
FWidth := 1024;
FHeight := 768;
FWidth := aWidth;
FHeight := aHeight;
FDelayMs := 500;
FScale := 1; // This is the relative scale to a 96 DPI screen. It's calculated with the formula : scale = custom_DPI / 96
FSnapshotPath := 'snapshot.bmp';
@ -164,6 +167,14 @@ begin
FThread.LoadUrl(aURL);
end;
function TEncapsulatedBrowser.UpdateBrowserSize(aNewWidth, aNewHeight : integer): boolean;
begin
FWidth := aNewWidth;
FHeight := aNewHeight;
Result := assigned(FThread) and
FThread.UpdateBrowserSize(aNewWidth, aNewHeight);
end;
procedure TEncapsulatedBrowser.Thread_OnError(Sender: TObject);
begin
// This code is executed in the TCEFBrowserThread thread context while the main application thread is waiting for MainAppEvent.
@ -186,6 +197,17 @@ procedure TEncapsulatedBrowser.Thread_OnSnapshotAvailable(Sender: TObject);
begin
// This code is executed in the TCEFBrowserThread thread context while the main application thread is waiting for MainAppEvent.
// Enable this block to test UpdateBrowserSize
{
if FFirst then
begin
FFirst := False;
if UpdateBrowserSize(800, 600) then
LoadURL('https://www.bing.com');
exit;
end;
}
if (FThread = nil) or not(FThread.SaveSnapshotToFile(FSnapshotPath)) then
FErrorText := 'There was an error copying the snapshot';

View File

@ -377,7 +377,7 @@ begin
FResizeCS.Acquire;
TempForcedResize := False;
if FBrowserBitmap.BeginBufferDraw then
if FBrowserBitmap.BeginDraw then
begin
if (kind = PET_POPUP) then
begin
@ -401,7 +401,7 @@ begin
end
else
begin
TempForcedResize := FBrowserBitmap.UpdateBufferDimensions(aWidth, aHeight) or not(FBrowserBitmap.BufferIsResized(False));
TempForcedResize := FBrowserBitmap.UpdateDimensions(aWidth, aHeight);
TempWidth := FBrowserBitmap.Width;
TempHeight := FBrowserBitmap.Height;
TempScanlineSize := FBrowserBitmap.ScanlineSize;
@ -457,7 +457,7 @@ begin
end;
end;
FBrowserBitmap.EndBufferDraw;
FBrowserBitmap.EndDraw;
if (kind = PET_VIEW) then
begin
@ -587,13 +587,10 @@ begin
if FResizing then
FPendingResize := True
else
if FBrowserBitmap.BufferIsResized then
FBrowser.Invalidate(PET_VIEW)
else
begin
FResizing := True;
FBrowser.WasResized;
end;
begin
FResizing := True;
FBrowser.WasResized;
end;
finally
FResizeCS.Release;
end;

View File

@ -343,7 +343,7 @@ begin
FResizeCS.Acquire;
TempForcedResize := False;
if FBrowserBitmap.BeginBufferDraw then
if FBrowserBitmap.BeginDraw then
begin
if (kind = PET_POPUP) then
begin
@ -367,7 +367,7 @@ begin
end
else
begin
TempForcedResize := FBrowserBitmap.UpdateBufferDimensions(aWidth, aHeight) or not(FBrowserBitmap.BufferIsResized(False));
TempForcedResize := FBrowserBitmap.UpdateDimensions(aWidth, aHeight);
TempWidth := FBrowserBitmap.Width;
TempHeight := FBrowserBitmap.Height;
TempScanlineSize := FBrowserBitmap.ScanlineSize;
@ -423,7 +423,7 @@ begin
end;
end;
FBrowserBitmap.EndBufferDraw;
FBrowserBitmap.EndDraw;
if (kind = PET_VIEW) then
begin
@ -540,13 +540,10 @@ begin
if FResizing then
FPendingResize := True
else
if FBrowserBitmap.BufferIsResized then
FBrowser.Invalidate(PET_VIEW)
else
begin
FResizing := True;
FBrowser.WasResized;
end;
begin
FResizing := True;
FBrowser.WasResized;
end;
finally
FResizeCS.Release;
end;

View File

@ -542,7 +542,7 @@ begin
FPopUpRect.Bottom := rect.y + rect.height - 1;
end;
procedure TCEFBrowserThread.Browser_OnBeforePopup(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const targetUrl, targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean; const popupFeatures: TCefPopupFeatures; var windowInfo: TCefWindowInfo; var client: ICefClient; var settings: TCefBrowserSettings; var extra_info: ICefDictionaryValue; var noJavascriptAccess: Boolean; var Result: Boolean);
procedure TCEFBrowserThread.Browser_OnBeforePopup(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; popup_id: Integer; const targetUrl, targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean; const popupFeatures: TCefPopupFeatures; var windowInfo: TCefWindowInfo; var client: ICefClient; var settings: TCefBrowserSettings; var extra_info: ICefDictionaryValue; var noJavascriptAccess: Boolean; var Result: Boolean);
begin
// For simplicity, this demo blocks all popup windows and new tabs
Result := (targetDisposition in [CEF_WOD_NEW_FOREGROUND_TAB, CEF_WOD_NEW_BACKGROUND_TAB, CEF_WOD_NEW_POPUP, CEF_WOD_NEW_WINDOW]);

View File

@ -36,10 +36,9 @@ type
public
constructor Create; override;
destructor Destroy; override;
function BeginBufferDraw : boolean;
procedure EndBufferDraw;
function UpdateBufferDimensions(aWidth, aHeight : integer) : boolean;
function BufferIsResized(aUseMutex : boolean = True) : boolean;
function BeginDraw : boolean;
procedure EndDraw;
function UpdateDimensions(aWidth, aHeight : integer) : boolean;
procedure BufferDraw(const aBitmap : TBitmap; const aSrcRect, aDstRect : TRect);
property ScanlineSize : integer read FScanlineSize;
@ -98,7 +97,7 @@ begin
Result := nil;
end;
function TCEFBrowserBitmap.BeginBufferDraw : boolean;
function TCEFBrowserBitmap.BeginDraw : boolean;
begin
{$IFDEF MSWINDOWS}
Result := (FSyncObj <> 0) and (WaitForSingleObject(FSyncObj, 5000) = WAIT_OBJECT_0);
@ -113,7 +112,7 @@ begin
{$ENDIF}
end;
procedure TCEFBrowserBitmap.EndBufferDraw;
procedure TCEFBrowserBitmap.EndDraw;
begin
{$IFDEF MSWINDOWS}
if (FSyncObj <> 0) then ReleaseMutex(FSyncObj);
@ -122,7 +121,7 @@ begin
{$ENDIF}
end;
function TCEFBrowserBitmap.UpdateBufferDimensions(aWidth, aHeight : integer) : boolean;
function TCEFBrowserBitmap.UpdateDimensions(aWidth, aHeight : integer) : boolean;
begin
Result := False;
FScanlineSize := aWidth * SizeOf(TRGBQuad);
@ -140,37 +139,6 @@ begin
end;
end;
function TCEFBrowserBitmap.BufferIsResized(aUseMutex : boolean) : boolean;
var
TempDevWidth, TempLogWidth, TempDevHeight, TempLogHeight : integer;
begin
Result := False;
if not(aUseMutex) or BeginBufferDraw then
begin
if (FDeviceScaleFactor = 1) then
Result := (Width = Width) and
(Height = Height)
else
begin
// CEF and Chromium use 'floor' to round the float values in Device <-> Logical unit conversions
// and Delphi uses MulDiv, which uses the bankers rounding, to resize the components in high DPI mode.
// This is the cause of slight differences in size between the buffer and the panel in some occasions.
TempLogWidth := DeviceToLogical(Width, FDeviceScaleFactor);
TempLogHeight := DeviceToLogical(Height, FDeviceScaleFactor);
TempDevWidth := LogicalToDevice(TempLogWidth, FDeviceScaleFactor);
TempDevHeight := LogicalToDevice(TempLogHeight, FDeviceScaleFactor);
Result := (Width = TempDevWidth) and
(Height = TempDevHeight);
end;
if aUseMutex then EndBufferDraw;
end;
end;
procedure TCEFBrowserBitmap.BufferDraw(const aBitmap : TBitmap; const aSrcRect, aDstRect : TRect);
begin
if (aBitmap <> nil) then

View File

@ -2,7 +2,7 @@
"UpdateLazPackages" : [
{
"ForceNotify" : true,
"InternalVersion" : 679,
"InternalVersion" : 680,
"Name" : "cef4delphi_lazarus.lpk",
"Version" : "131.2.7"
}