1
0
mirror of https://github.com/salvadordf/CEF4Delphi.git synced 2025-11-23 21:34:53 +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';