1
0
mirror of https://github.com/salvadordf/CEF4Delphi.git synced 2025-08-14 21:42:50 +02:00

TabBrowser and ToolBoxBrowser demos : Added CEF initialization checks before enabling the GUI

This commit is contained in:
Salvador Díaz Fau
2017-12-14 12:09:13 +01:00
parent 8319df160d
commit f871755249
6 changed files with 101 additions and 32 deletions

View File

@@ -54,9 +54,10 @@ uses
{$SetPEFlags IMAGE_FILE_LARGE_ADDRESS_AWARE} {$SetPEFlags IMAGE_FILE_LARGE_ADDRESS_AWARE}
begin begin
GlobalCEFApp := TCefApplication.Create; GlobalCEFApp := TCefApplication.Create;
GlobalCEFApp.FlashEnabled := False; GlobalCEFApp.FlashEnabled := False;
GlobalCEFApp.FastUnload := True; // Enable the fast unload controller, which speeds up tab/window close by running a tab's onunload js handler independently of the GUI GlobalCEFApp.FastUnload := True;
GlobalCEFApp.OnContextInitialized := GlobalCEFApp_OnContextInitialized;
// In case you want to use custom directories for the CEF3 binaries, cache, cookies and user data. // In case you want to use custom directories for the CEF3 binaries, cache, cookies and user data.
{ {

View File

@@ -1,7 +1,7 @@
object MainForm: TMainForm object MainForm: TMainForm
Left = 0 Left = 0
Top = 0 Top = 0
Caption = 'Tab Browser' Caption = 'Initializing browser. Please wait...'
ClientHeight = 573 ClientHeight = 573
ClientWidth = 732 ClientWidth = 732
Color = clBtnFace Color = clBtnFace
@@ -24,13 +24,13 @@ object MainForm: TMainForm
Align = alTop Align = alTop
BevelOuter = bvNone BevelOuter = bvNone
Caption = 'ButtonPnl' Caption = 'ButtonPnl'
Enabled = False
Padding.Left = 5 Padding.Left = 5
Padding.Top = 5 Padding.Top = 5
Padding.Right = 5 Padding.Right = 5
Padding.Bottom = 5 Padding.Bottom = 5
ShowCaption = False ShowCaption = False
TabOrder = 0 TabOrder = 0
ExplicitWidth = 684
object NavButtonPnl: TPanel object NavButtonPnl: TPanel
Left = 5 Left = 5
Top = 5 Top = 5
@@ -139,7 +139,6 @@ object MainForm: TMainForm
Align = alRight Align = alRight
BevelOuter = bvNone BevelOuter = bvNone
TabOrder = 2 TabOrder = 2
ExplicitLeft = 648
object GoBtn: TButton object GoBtn: TButton
Left = 6 Left = 6
Top = 0 Top = 0
@@ -166,8 +165,6 @@ object MainForm: TMainForm
Padding.Top = 2 Padding.Top = 2
ShowCaption = False ShowCaption = False
TabOrder = 0 TabOrder = 0
ExplicitLeft = 190
ExplicitWidth = 458
object URLCbx: TComboBox object URLCbx: TComboBox
Left = 0 Left = 0
Top = 2 Top = 2
@@ -206,7 +203,5 @@ object MainForm: TMainForm
Align = alClient Align = alClient
TabOrder = 1 TabOrder = 1
OnChange = PageControl1Change OnChange = PageControl1Change
ExplicitWidth = 684
ExplicitHeight = 497
end end
end end

View File

@@ -54,6 +54,7 @@ uses
const const
CEFBROWSER_DESTROYWNDPARENT = WM_APP + $100; CEFBROWSER_DESTROYWNDPARENT = WM_APP + $100;
CEFBROWSER_DESTROYTAB = WM_APP + $101; CEFBROWSER_DESTROYTAB = WM_APP + $101;
CEFBROWSER_INITIALIZED = WM_APP + $102;
type type
TMainForm = class(TForm) TMainForm = class(TForm)
@@ -93,6 +94,7 @@ type
procedure BrowserCreatedMsg(var aMessage : TMessage); message CEF_AFTERCREATED; procedure BrowserCreatedMsg(var aMessage : TMessage); message CEF_AFTERCREATED;
procedure BrowserDestroyWindowParentMsg(var aMessage : TMessage); message CEFBROWSER_DESTROYWNDPARENT; procedure BrowserDestroyWindowParentMsg(var aMessage : TMessage); message CEFBROWSER_DESTROYWNDPARENT;
procedure BrowserDestroyTabMsg(var aMessage : TMessage); message CEFBROWSER_DESTROYTAB; procedure BrowserDestroyTabMsg(var aMessage : TMessage); message CEFBROWSER_DESTROYTAB;
procedure CEFInitializedMsg(var aMessage : TMessage); message CEFBROWSER_INITIALIZED;
procedure WMMove(var aMessage : TWMMove); message WM_MOVE; procedure WMMove(var aMessage : TWMMove); message WM_MOVE;
procedure WMMoving(var aMessage : TMessage); message WM_MOVING; procedure WMMoving(var aMessage : TMessage); message WM_MOVING;
@@ -108,6 +110,8 @@ type
var var
MainForm: TMainForm; MainForm: TMainForm;
procedure GlobalCEFApp_OnContextInitialized;
implementation implementation
{$R *.dfm} {$R *.dfm}
@@ -127,6 +131,11 @@ implementation
// 2. TChromium.OnClose sends a CEFBROWSER_DESTROYWNDPARENT message to destroy TCEFWindowParent in the main thread which triggers a TChromium.OnBeforeClose event. // 2. TChromium.OnClose sends a CEFBROWSER_DESTROYWNDPARENT message to destroy TCEFWindowParent in the main thread which triggers a TChromium.OnBeforeClose event.
// 3. TChromium.OnBeforeClose sends a CEFBROWSER_DESTROYTAB message to destroy the tab in the main thread. // 3. TChromium.OnBeforeClose sends a CEFBROWSER_DESTROYTAB message to destroy the tab in the main thread.
procedure GlobalCEFApp_OnContextInitialized;
begin
if (MainForm <> nil) then PostMessage(MainForm.Handle, CEFBROWSER_INITIALIZED, 0, 0);
end;
procedure TMainForm.AddTabBtnClick(Sender: TObject); procedure TMainForm.AddTabBtnClick(Sender: TObject);
var var
TempSheet : TTabSheet; TempSheet : TTabSheet;
@@ -175,7 +184,15 @@ end;
procedure TMainForm.FormShow(Sender: TObject); procedure TMainForm.FormShow(Sender: TObject);
begin begin
AddTabBtn.Click; if (GlobalCEFApp <> nil) and
GlobalCEFApp.GlobalContextInitialized and
not(ButtonPnl.Enabled) then
begin
ButtonPnl.Enabled := True;
Caption := 'Tab Browser';
cursor := crDefault;
if (PageControl1.PageCount = 0) then AddTabBtn.Click;
end;
end; end;
procedure TMainForm.ForwardBtnClick(Sender: TObject); procedure TMainForm.ForwardBtnClick(Sender: TObject);
@@ -405,4 +422,15 @@ begin
URLCbx.Text := TempChromium.DocumentURL; URLCbx.Text := TempChromium.DocumentURL;
end; end;
procedure TMainForm.CEFInitializedMsg(var aMessage : TMessage);
begin
if not(ButtonPnl.Enabled) then
begin
ButtonPnl.Enabled := True;
Caption := 'Tab Browser';
cursor := crDefault;
if (PageControl1.PageCount = 0) then AddTabBtn.Click;
end;
end;
end. end.

View File

@@ -55,9 +55,10 @@ uses
{$SetPEFlags IMAGE_FILE_LARGE_ADDRESS_AWARE} {$SetPEFlags IMAGE_FILE_LARGE_ADDRESS_AWARE}
begin begin
GlobalCEFApp := TCefApplication.Create; GlobalCEFApp := TCefApplication.Create;
GlobalCEFApp.FlashEnabled := False; GlobalCEFApp.FlashEnabled := False;
GlobalCEFApp.FastUnload := True; // Enable the fast unload controller, which speeds up tab/window close by running a tab's onunload js handler independently of the GUI GlobalCEFApp.FastUnload := True;
GlobalCEFApp.OnContextInitialized := GlobalCEFApp_OnContextInitialized;
// In case you want to use custom directories for the CEF3 binaries, cache, cookies and user data. // In case you want to use custom directories for the CEF3 binaries, cache, cookies and user data.
{ {

View File

@@ -3,7 +3,7 @@ object MainForm: TMainForm
Top = 0 Top = 0
BorderIcons = [biSystemMenu] BorderIcons = [biSystemMenu]
BorderStyle = bsSingle BorderStyle = bsSingle
Caption = 'Toolbox Browser demo' Caption = 'Initializing browser. Please wait...'
ClientHeight = 37 ClientHeight = 37
ClientWidth = 357 ClientWidth = 357
Color = clBtnFace Color = clBtnFace
@@ -15,23 +15,39 @@ object MainForm: TMainForm
OldCreateOrder = False OldCreateOrder = False
Position = poScreenCenter Position = poScreenCenter
OnCreate = FormCreate OnCreate = FormCreate
OnShow = FormShow
PixelsPerInch = 96 PixelsPerInch = 96
TextHeight = 13 TextHeight = 13
object Button1: TButton object ButtonPnl: TPanel
Left = 300 Left = 0
Top = 6 Top = 0
Width = 51 Width = 357
Height = 25 Height = 37
Caption = 'Open' Align = alClient
BevelOuter = bvNone
Enabled = False
ShowCaption = False
TabOrder = 0 TabOrder = 0
OnClick = Button1Click ExplicitLeft = 136
end ExplicitTop = 8
object Edit1: TEdit ExplicitWidth = 185
Left = 8 ExplicitHeight = 41
Top = 8 object Edit1: TEdit
Width = 286 Left = 8
Height = 21 Top = 8
TabOrder = 1 Width = 286
Text = 'https://www.google.com' Height = 21
TabOrder = 0
Text = 'https://www.google.com'
end
object Button1: TButton
Left = 300
Top = 6
Width = 51
Height = 25
Caption = 'Open'
TabOrder = 1
OnClick = Button1Click
end
end end
end end

View File

@@ -54,13 +54,16 @@ const
CEFBROWSER_CREATED = WM_APP + $100; CEFBROWSER_CREATED = WM_APP + $100;
CEFBROWSER_CHILDDESTROYED = WM_APP + $101; CEFBROWSER_CHILDDESTROYED = WM_APP + $101;
CEFBROWSER_DESTROY = WM_APP + $102; CEFBROWSER_DESTROY = WM_APP + $102;
CEFBROWSER_INITIALIZED = WM_APP + $103;
type type
TMainForm = class(TForm) TMainForm = class(TForm)
Button1: TButton; ButtonPnl: TPanel;
Edit1: TEdit; Edit1: TEdit;
Button1: TButton;
procedure FormCreate(Sender: TObject); procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject); procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
private private
// Variables to control when can we destroy the form safely // Variables to control when can we destroy the form safely
FCanClose : boolean; // Set to True when all the child forms are closed FCanClose : boolean; // Set to True when all the child forms are closed
@@ -73,6 +76,7 @@ type
protected protected
procedure ChildDestroyedMsg(var aMessage : TMessage); message CEFBROWSER_CHILDDESTROYED; procedure ChildDestroyedMsg(var aMessage : TMessage); message CEFBROWSER_CHILDDESTROYED;
procedure CEFInitializedMsg(var aMessage : TMessage); message CEFBROWSER_INITIALIZED;
public public
function CloseQuery: Boolean; override; function CloseQuery: Boolean; override;
@@ -84,18 +88,25 @@ type
var var
MainForm: TMainForm; MainForm: TMainForm;
procedure GlobalCEFApp_OnContextInitialized;
implementation implementation
{$R *.dfm} {$R *.dfm}
uses uses
uChildForm; uChildForm, uCEFApplication;
// Destruction steps // Destruction steps
// ================= // =================
// 1. Destroy all child forms // 1. Destroy all child forms
// 2. Wait until all the child forms are closed before closing the main form and terminating the application. // 2. Wait until all the child forms are closed before closing the main form and terminating the application.
procedure GlobalCEFApp_OnContextInitialized;
begin
if (MainForm <> nil) then PostMessage(MainForm.Handle, CEFBROWSER_INITIALIZED, 0, 0);
end;
procedure TMainForm.CreateToolboxChild(const ChildCaption, URL: string); procedure TMainForm.CreateToolboxChild(const ChildCaption, URL: string);
var var
TempChild : TChildForm; TempChild : TChildForm;
@@ -216,4 +227,21 @@ begin
end; end;
end; end;
procedure TMainForm.CEFInitializedMsg(var aMessage : TMessage);
begin
Caption := 'ToolBox Browser';
ButtonPnl.Enabled := True;
cursor := crDefault;
end;
procedure TMainForm.FormShow(Sender: TObject);
begin
if (GlobalCEFApp <> nil) and GlobalCEFApp.GlobalContextInitialized then
begin
Caption := 'ToolBox Browser';
ButtonPnl.Enabled := True;
cursor := crDefault;
end;
end;
end. end.