-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathuChildForm.pas
174 lines (141 loc) · 4.88 KB
/
uChildForm.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
unit uChildForm;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
uWVBrowser, uWVWinControl, uWVWindowParent, uWVTypes, uWVTypeLibrary,
uWVBrowserBase, uWVCoreWebView2Args, uWVCoreWebView2Deferral, Vcl.ComCtrls;
type
TChildForm = class(TForm)
WVWindowParent1: TWVWindowParent;
WVBrowser1: TWVBrowser;
StatusBar1: TStatusBar;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormShow(Sender: TObject);
procedure WVBrowser1AfterCreated(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure WVBrowser1WindowCloseRequested(Sender: TObject);
procedure WVBrowser1NewWindowRequested(Sender: TObject;
const aWebView: ICoreWebView2;
const aArgs: ICoreWebView2NewWindowRequestedEventArgs);
procedure FormCreate(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure WVBrowser1DocumentTitleChanged(Sender: TObject);
procedure StatusBar1DblClick(Sender: TObject);
private
FArgs : TCoreWebView2NewWindowRequestedEventArgs;
FDeferral : TCoreWebView2Deferral;
FURL : string;
public
constructor Create(AOwner: TComponent; const aArgs : ICoreWebView2NewWindowRequestedEventArgs); reintroduce; overload;
constructor Create(AOwner: TComponent; const aURL: string); reintroduce; overload;
end;
var
ChildForm: TChildForm;
implementation
{$R *.dfm}
uses
uWVCoreWebView2WindowFeatures, menu, functions, Winapi.ShellAPI;
constructor TChildForm.Create(AOwner: TComponent; const aArgs : ICoreWebView2NewWindowRequestedEventArgs);
begin
inherited Create(AOwner);
FArgs := TCoreWebView2NewWindowRequestedEventArgs.Create(aArgs);
FDeferral := TCoreWebView2Deferral.Create(FArgs.Deferral);
end;
constructor TChildForm.Create(AOwner: TComponent; const aURL: string);
begin
inherited Create(AOwner);
FArgs := nil; // No event args in this constructor
FDeferral := nil;
FURL := aURL;
end;
procedure TChildForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
procedure TChildForm.FormCreate(Sender: TObject);
begin
EnableNCShadow(Handle);
if frmMenu.PopupWindowRect.Width > 100 then
begin
BoundsRect := frmMenu.PopupWindowRect;
end;
end;
procedure TChildForm.FormDestroy(Sender: TObject);
begin
if assigned(FDeferral) then
FreeAndNil(FDeferral);
if assigned(FArgs) then
FreeAndNil(FArgs);
end;
procedure TChildForm.FormResize(Sender: TObject);
begin
frmMenu.PopupWindowRect := BoundsRect;
end;
procedure TChildForm.FormShow(Sender: TObject);
var
TempWindowFeatures : TCoreWebView2WindowFeatures;
begin
TempWindowFeatures := nil;
if assigned(FArgs) then
try
TempWindowFeatures := TCoreWebView2WindowFeatures.Create(FArgs.WindowFeatures);
if TempWindowFeatures.HasPosition then
begin
WVBrowser1.SetFormLeftTo(TempWindowFeatures.Left);
WVBrowser1.SetFormTopTo(TempWindowFeatures.Top);
end;
if TempWindowFeatures.HasSize then
begin
WVBrowser1.ResizeFormWidthTo(TempWindowFeatures.width);
WVBrowser1.ResizeFormHeightTo(TempWindowFeatures.height);
end;
finally
if assigned(TempWindowFeatures) then
FreeAndNil(TempWindowFeatures);
end;
// WVBrowser1.UserAgent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.0.0';
WVBrowser1.CreateBrowser(WVWindowParent1.Handle);
end;
procedure TChildForm.StatusBar1DblClick(Sender: TObject);
var
url: string;
begin
url := StatusBar1.Panels[0].Text;
if url.StartsWith('http') then
ShellExecute(0, 'OPEN', PChar(url), nil, nil, SW_SHOWNORMAL);
end;
procedure TChildForm.WVBrowser1AfterCreated(Sender: TObject);
begin
if assigned(FArgs) and assigned(FDeferral) then
try
FArgs.NewWindow := WVBrowser1.CoreWebView2.BaseIntf;
FArgs.Handled := True;
FDeferral.Complete;
finally
FreeAndNil(FDeferral);
FreeAndNil(FArgs);
end;
WVWindowParent1.UpdateSize;
if FURL <> '' then
WVBrowser1.Navigate(FURL);
end;
procedure TChildForm.WVBrowser1DocumentTitleChanged(Sender: TObject);
begin
Caption := 'AIChatBar - ' + WVBrowser1.DocumentTitle;
StatusBar1.Panels[0].Text := WVBrowser1.Source;
end;
procedure TChildForm.WVBrowser1NewWindowRequested(Sender: TObject;
const aWebView: ICoreWebView2;
const aArgs: ICoreWebView2NewWindowRequestedEventArgs);
var
TempChildForm : TChildForm;
begin
TempChildForm := TChildForm.Create(Self, aArgs);
TempChildForm.Show;
end;
procedure TChildForm.WVBrowser1WindowCloseRequested(Sender: TObject);
begin
PostMessage(Handle, WM_CLOSE, 0, 0);
end;
end.