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}
begin
GlobalCEFApp := TCefApplication.Create;
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 := TCefApplication.Create;
GlobalCEFApp.FlashEnabled := False;
GlobalCEFApp.FastUnload := True;
GlobalCEFApp.OnContextInitialized := GlobalCEFApp_OnContextInitialized;
// 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
Left = 0
Top = 0
Caption = 'Tab Browser'
Caption = 'Initializing browser. Please wait...'
ClientHeight = 573
ClientWidth = 732
Color = clBtnFace
@@ -24,13 +24,13 @@ object MainForm: TMainForm
Align = alTop
BevelOuter = bvNone
Caption = 'ButtonPnl'
Enabled = False
Padding.Left = 5
Padding.Top = 5
Padding.Right = 5
Padding.Bottom = 5
ShowCaption = False
TabOrder = 0
ExplicitWidth = 684
object NavButtonPnl: TPanel
Left = 5
Top = 5
@@ -139,7 +139,6 @@ object MainForm: TMainForm
Align = alRight
BevelOuter = bvNone
TabOrder = 2
ExplicitLeft = 648
object GoBtn: TButton
Left = 6
Top = 0
@@ -166,8 +165,6 @@ object MainForm: TMainForm
Padding.Top = 2
ShowCaption = False
TabOrder = 0
ExplicitLeft = 190
ExplicitWidth = 458
object URLCbx: TComboBox
Left = 0
Top = 2
@@ -206,7 +203,5 @@ object MainForm: TMainForm
Align = alClient
TabOrder = 1
OnChange = PageControl1Change
ExplicitWidth = 684
ExplicitHeight = 497
end
end

View File

@@ -54,6 +54,7 @@ uses
const
CEFBROWSER_DESTROYWNDPARENT = WM_APP + $100;
CEFBROWSER_DESTROYTAB = WM_APP + $101;
CEFBROWSER_INITIALIZED = WM_APP + $102;
type
TMainForm = class(TForm)
@@ -93,6 +94,7 @@ type
procedure BrowserCreatedMsg(var aMessage : TMessage); message CEF_AFTERCREATED;
procedure BrowserDestroyWindowParentMsg(var aMessage : TMessage); message CEFBROWSER_DESTROYWNDPARENT;
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 WMMoving(var aMessage : TMessage); message WM_MOVING;
@@ -108,6 +110,8 @@ type
var
MainForm: TMainForm;
procedure GlobalCEFApp_OnContextInitialized;
implementation
{$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.
// 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);
var
TempSheet : TTabSheet;
@@ -175,7 +184,15 @@ end;
procedure TMainForm.FormShow(Sender: TObject);
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;
procedure TMainForm.ForwardBtnClick(Sender: TObject);
@@ -405,4 +422,15 @@ begin
URLCbx.Text := TempChromium.DocumentURL;
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.

View File

@@ -55,9 +55,10 @@ uses
{$SetPEFlags IMAGE_FILE_LARGE_ADDRESS_AWARE}
begin
GlobalCEFApp := TCefApplication.Create;
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 := TCefApplication.Create;
GlobalCEFApp.FlashEnabled := False;
GlobalCEFApp.FastUnload := True;
GlobalCEFApp.OnContextInitialized := GlobalCEFApp_OnContextInitialized;
// 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
BorderIcons = [biSystemMenu]
BorderStyle = bsSingle
Caption = 'Toolbox Browser demo'
Caption = 'Initializing browser. Please wait...'
ClientHeight = 37
ClientWidth = 357
Color = clBtnFace
@@ -15,23 +15,39 @@ object MainForm: TMainForm
OldCreateOrder = False
Position = poScreenCenter
OnCreate = FormCreate
OnShow = FormShow
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 300
Top = 6
Width = 51
Height = 25
Caption = 'Open'
object ButtonPnl: TPanel
Left = 0
Top = 0
Width = 357
Height = 37
Align = alClient
BevelOuter = bvNone
Enabled = False
ShowCaption = False
TabOrder = 0
OnClick = Button1Click
end
object Edit1: TEdit
Left = 8
Top = 8
Width = 286
Height = 21
TabOrder = 1
Text = 'https://www.google.com'
ExplicitLeft = 136
ExplicitTop = 8
ExplicitWidth = 185
ExplicitHeight = 41
object Edit1: TEdit
Left = 8
Top = 8
Width = 286
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

View File

@@ -54,13 +54,16 @@ const
CEFBROWSER_CREATED = WM_APP + $100;
CEFBROWSER_CHILDDESTROYED = WM_APP + $101;
CEFBROWSER_DESTROY = WM_APP + $102;
CEFBROWSER_INITIALIZED = WM_APP + $103;
type
TMainForm = class(TForm)
Button1: TButton;
ButtonPnl: TPanel;
Edit1: TEdit;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
private
// Variables to control when can we destroy the form safely
FCanClose : boolean; // Set to True when all the child forms are closed
@@ -73,6 +76,7 @@ type
protected
procedure ChildDestroyedMsg(var aMessage : TMessage); message CEFBROWSER_CHILDDESTROYED;
procedure CEFInitializedMsg(var aMessage : TMessage); message CEFBROWSER_INITIALIZED;
public
function CloseQuery: Boolean; override;
@@ -84,18 +88,25 @@ type
var
MainForm: TMainForm;
procedure GlobalCEFApp_OnContextInitialized;
implementation
{$R *.dfm}
uses
uChildForm;
uChildForm, uCEFApplication;
// Destruction steps
// =================
// 1. Destroy all child forms
// 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);
var
TempChild : TChildForm;
@@ -216,4 +227,21 @@ begin
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.