From d37e72c010bd7d89596d195228270bfef4531caa Mon Sep 17 00:00:00 2001 From: martin Date: Mon, 1 Mar 2021 20:05:39 +0100 Subject: [PATCH] Changed Demo BrowserWindow (Lazarus_any_OS) to use ContextInitialized Handler --- .../BrowserWindow/globalcefapplication.pas | 5 ++-- source/uceflazarusbrowserwindow.pas | 30 +++++++++++++++---- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/demos/Lazarus_any_OS/BrowserWindow/globalcefapplication.pas b/demos/Lazarus_any_OS/BrowserWindow/globalcefapplication.pas index 4e97ab04..64a101b3 100644 --- a/demos/Lazarus_any_OS/BrowserWindow/globalcefapplication.pas +++ b/demos/Lazarus_any_OS/BrowserWindow/globalcefapplication.pas @@ -51,7 +51,7 @@ unit GlobalCefApplication; interface uses - uCEFApplication, uCEFWorkScheduler, FileUtil; + uCEFApplication, uCEFWorkScheduler, uCEFLazApplication, FileUtil; procedure CreateGlobalCEFApp; @@ -75,7 +75,8 @@ begin GlobalCEFWorkScheduler := TCEFWorkScheduler.Create(nil); {$ENDIF} - GlobalCEFApp := TCefApplication.Create; + GlobalCEFApp := TCefLazApplication.Create; + GlobalCEFApp.CheckCEFFiles := False; {$IFDEF USE_MULTI_THREAD_LOOP} // On Windows/Linux CEF can use threads for the message-loop GlobalCEFApp.MultiThreadedMessageLoop := True; diff --git a/source/uceflazarusbrowserwindow.pas b/source/uceflazarusbrowserwindow.pas index aa1b4c66..97c85df9 100644 --- a/source/uceflazarusbrowserwindow.pas +++ b/source/uceflazarusbrowserwindow.pas @@ -47,7 +47,7 @@ uses LResources, {$ENDIF} uCEFApplication, uCEFChromiumWindow, uCEFTypes, uCEFInterfaces, uCEFChromium, - uCEFLinkedWinControlBase, Forms, ExtCtrls, Classes, sysutils; + uCEFLinkedWinControlBase, uCEFLazApplication, Forms, ExtCtrls, Classes, sysutils; type @@ -112,6 +112,7 @@ type FTimer : TTimer; procedure DoCreateBrowser(Sender: TObject); + procedure DoCreateBrowserAfterContext(Sender: TObject); protected function GetChromium: TChromium; override; procedure DestroyHandle; override; @@ -330,7 +331,8 @@ end; procedure TLazarusBrowserWindow.DoCreateBrowser(Sender: TObject); begin - FTimer.Enabled := False; + if FTimer <> nil then + FTimer.Enabled := False; case FChromiumWrapper.FChromiumState of csCreatingBrowser, csHasBrowser: begin @@ -351,11 +353,26 @@ begin if GlobalCEFApp.ExternalMessagePump then GlobalCEFApp.DoMessageLoopWork; + if FTimer = nil then + FTimer := TTimer.Create(Self); + FTimer.OnTimer := @DoCreateBrowser; FTimer.Interval := 100; FTimer.Enabled := True; end; end; +procedure TLazarusBrowserWindow.DoCreateBrowserAfterContext(Sender: TObject); +begin + {$IFDEF LINUX} + FTimer := TTimer.Create(Self); + FTimer.Interval := 20; + FTimer.OnTimer := @DoCreateBrowser; + FTimer.Enabled := True; + {$ELSE} + DoCreateBrowser(nil); + {$ENDIF} +end; + function TLazarusBrowserWindow.GetChromium: TChromium; begin Result := FChromiumWrapper.FChromium; @@ -368,10 +385,11 @@ begin (* On Windows we can create the browser immediately. But at least on Linux, we need to wait *) - FTimer := TTimer.Create(Self); - FTimer.Interval := 20; - FTimer.OnTimer := @DoCreateBrowser; - FTimer.Enabled := True; + + if GlobalCEFApp is TCefLazApplication then + TCefLazApplication(GlobalCEFApp).AddContextInitializedHandler(@DoCreateBrowserAfterContext) + else + DoCreateBrowserAfterContext(nil); end; end;