-
Notifications
You must be signed in to change notification settings - Fork 6
/
uBrowserCard.pas
156 lines (124 loc) · 4.25 KB
/
uBrowserCard.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
unit uBrowserCard;
interface
uses
Winapi.Windows, System.Classes, Winapi.Messages, Vcl.ComCtrls, Vcl.Controls,
Vcl.Forms, System.SysUtils, uBrowserFrame, uWVCoreWebView2Args, Vcl.WinXPanels,
Net.HttpClient;
type
TBrowserCard = class(TCard)
private
FCardCtrlPEvent: TNotifyEvent;
FIsChatGPT: Boolean;
function GetRamUsage: Int64;
function GetCookies: TCookieManager;
protected
FBrowserFrame: TBrowserFrame;
FCardID: Cardinal;
function GetInitialized: Boolean;
procedure CreateFrame(const aHomepage, aUA : string); overload;
procedure CreateFrame(const aArgs : TCoreWebView2NewWindowRequestedEventArgs); overload;
procedure BrowserFrame_OnBrowserTitleChange(Sender: TObject; const aTitle : string);
procedure CtrlPEvent(Sender: TObject);
public
constructor Create(AOwner: TComponent; aCardID : cardinal; const aCaption : string); reintroduce;
procedure NotifyParentWindowPositionChanged;
procedure CreateBrowser(const aHomepage, aUA: string); overload;
procedure CreateBrowser(const aArgs : TCoreWebView2NewWindowRequestedEventArgs); overload;
procedure Navigate(const url: string);
procedure FocusBrowser;
property CardID : cardinal read FCardID;
property Initialized : boolean read GetInitialized;
property MemoryUsage : Int64 read GetRamUsage;
property CardCtrlPEvent : TNotifyEvent read FCardCtrlPEvent write FCardCtrlPEvent;
property Cookies : TCookieManager read GetCookies;
property IsChatGPT : Boolean read FIsChatGPT write FIsChatGPT;
end;
implementation
uses
frmChatWebView, menu;
{ TBrowserCard }
procedure TBrowserCard.BrowserFrame_OnBrowserTitleChange(Sender: TObject;
const aTitle: string);
begin
Caption := aTitle;
end;
constructor TBrowserCard.Create(AOwner: TComponent; aCardID: cardinal;
const aCaption: string);
begin
inherited Create(AOwner);
FCardID := aCardID;
Caption := aCaption;
FBrowserFrame := nil;
FIsChatGPT := False;
end;
procedure TBrowserCard.CreateBrowser(
const aArgs: TCoreWebView2NewWindowRequestedEventArgs);
begin
CreateFrame(aArgs);
if (FBrowserFrame <> nil) then FBrowserFrame.CreateBrowser;
end;
procedure TBrowserCard.CreateBrowser(const aHomepage, aUA: string);
begin
CreateFrame(aHomepage, aUA);
if (FBrowserFrame <> nil) then FBrowserFrame.CreateBrowser;
end;
procedure TBrowserCard.CreateFrame(
const aArgs: TCoreWebView2NewWindowRequestedEventArgs);
begin
CreateFrame('', '');
FBrowserFrame.Args := aArgs;
end;
procedure TBrowserCard.CtrlPEvent(Sender: TObject);
begin
if Assigned(FCardCtrlPEvent) then
FCardCtrlPEvent(Self);
end;
procedure TBrowserCard.FocusBrowser;
begin
if (FBrowserFrame <> nil) then
begin
Winapi.Windows.SetFocus(FBrowserFrame.ChildHandle);
end;
end;
procedure TBrowserCard.CreateFrame(const aHomepage, aUA: string);
begin
if (FBrowserFrame = nil) then
begin
FBrowserFrame := TBrowserFrame.Create(self);
FBrowserFrame.Name := 'BrowserFrame' + IntToStr(CardID);
FBrowserFrame.Parent := self;
FBrowserFrame.Align := alClient;
FBrowserFrame.Visible := True;
FBrowserFrame.OnBrowserTitleChange := BrowserFrame_OnBrowserTitleChange;
FBrowserFrame.CreateAllHandles;
end;
FBrowserFrame.UA := aUA;
FBrowserFrame.Homepage := aHomepage;
FBrowserFrame.CtrlPEvent := CtrlPEvent;
end;
function TBrowserCard.GetCookies: TCookieManager;
begin
Result := nil;
if (FBrowserFrame <> nil) then
Result := FBrowserFrame.Cookies;
end;
function TBrowserCard.GetInitialized: Boolean;
begin
Result := (FBrowserFrame <> nil) and
FBrowserFrame.Initialized;
end;
function TBrowserCard.GetRamUsage: Int64;
begin
Result := 0;
if (FBrowserFrame <> nil) then
Result := FBrowserFrame.MemoryUsage;
end;
procedure TBrowserCard.Navigate(const url: string);
begin
FBrowserFrame.WVBrowser1.Navigate(url);
end;
procedure TBrowserCard.NotifyParentWindowPositionChanged;
begin
FBrowserFrame.NotifyParentWindowPositionChanged;
end;
end.