-
Notifications
You must be signed in to change notification settings - Fork 2
/
lazunitmain.pas
449 lines (377 loc) · 10.6 KB
/
lazunitmain.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
unit LazUnitMain;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Menus,
Windows, LMessages, ActiveX,
ActnList, StdCtrls, ComCtrls, uWVBrowser, uWVWindowParent, uWVLoader,
uWVBrowserBase, uWVTypes, uWVEvents, uWVTypeLibrary,
UnitTools;
type
{ TEdgeBrowserUpdater }
TEdgeBrowserUpdater = class(TComponent)
private
FView: TWVBrowser;
FViewWnd: TWVWindowParent;
FTimer: TTimer;
FHtml: string;
FHtmlPending: Boolean;
procedure BrowserNavCompleted(Sender: TObject; const aWebView: ICoreWebView2;
const aArgs: ICoreWebView2NavigationCompletedEventArgs);
procedure BrowserAfterCreated(Sender: TObject);
procedure TimerTimer(Sender: TObject);
function GetBusy: Boolean;
procedure InitWebView;
procedure DoSetPage(const html: string);
public
constructor Create(AOwner: TComponent; View: TWVBrowser; ViewWnd: TWVWindowParent; InitialURL: string = 'about:blank'); reintroduce; overload;
procedure ClearContentAll;
procedure SetPage(const html: string);
destructor Destroy; override;
property Browser: TWVBrowser read FView;
property Busy: Boolean read GetBusy;
end;
{ TMainForm }
TMainForm = class(TForm, IWritingToolsUI)
About1: TMenuItem;
ActionAbortGeneration: TAction;
ActionHide: TAction;
ActionList1: TActionList;
ActivityIndicator: TProgressBar;
ButtonClear: TButton;
EditCustomPrompt: TEdit;
Exit1: TMenuItem;
LabelStatus: TLabel;
N1: TMenuItem;
PageRunning: TPanel;
PageActions: TPanel;
PageBrowser: TPanel;
Panel1: TPanel;
PopupMenu1: TPopupMenu;
Show1: TMenuItem;
TrayIcon1: TTrayIcon;
WVBrowser1: TWVBrowser;
WVWindowParent1: TWVWindowParent;
procedure About1Click(Sender: TObject);
procedure ActionAbortGenerationExecute(Sender: TObject);
procedure ActionHideExecute(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure PageActionsResize(Sender: TObject);
procedure Show1Click(Sender: TObject);
procedure WVBrowser1AcceleratorKeyPressed(Sender: TObject;
const aController: ICoreWebView2Controller;
const aArgs: ICoreWebView2AcceleratorKeyPressedEventArgs);
private
FWritingTools: TWritingTools;
FBrowserUpdater: TEdgeBrowserUpdater;
FActButtons: array of TButton;
FActivePageId: Integer;
procedure LayoutButtons;
procedure ActionButtonClick(Sender: TObject);
procedure WMHotKey(var Msg: TMessage); message WM_HOTKEY;
procedure ShowPage(Id: Integer);
procedure UpdateAIMemo(L: string);
procedure ShowStatus(Msg: string);
procedure LLMStateChanged(Sender: TObject; ABusy: Boolean);
procedure LLMLoaded;
procedure ShowChat;
public
end;
var
MainForm: TMainForm;
implementation
{$R *.lfm}
{ TEdgeBrowserUpdater }
procedure TEdgeBrowserUpdater.BrowserNavCompleted(Sender: TObject;
const aWebView: ICoreWebView2;
const aArgs: ICoreWebView2NavigationCompletedEventArgs);
begin
FView.OnNavigationCompleted := nil;
if FHtmlPending then
begin
FHtmlPending := False;
DoSetPage(FHtml);
Exit;
end;
end;
procedure TEdgeBrowserUpdater.BrowserAfterCreated(Sender: TObject);
begin
FViewWnd.UpdateSize;
if FHtmlPending then
begin
FHtmlPending := False;
DoSetPage(FHtml);
end;
end;
procedure TEdgeBrowserUpdater.TimerTimer(Sender: TObject);
begin
FTimer.Enabled := False;
InitWebView;
end;
function TEdgeBrowserUpdater.GetBusy: Boolean;
begin
Result := Assigned(FView.OnNavigationCompleted);
end;
procedure TEdgeBrowserUpdater.InitWebView;
begin
if GlobalWebView2Loader.Initialized then
begin
FView.CreateBrowser(FViewWnd.Handle);
end
else
FTimer.Enabled := True;
end;
procedure TEdgeBrowserUpdater.DoSetPage(const html: string);
begin
FView.OnNavigationCompleted := @BrowserNavCompleted;
FView.NavigateToString(UnicodeString(html));
end;
constructor TEdgeBrowserUpdater.Create(AOwner: TComponent; View: TWVBrowser;
ViewWnd: TWVWindowParent; InitialURL: string);
begin
inherited Create(AOwner);
FView := View;
FViewWnd := ViewWnd;
FTimer := TTimer.Create(Self);
FTimer.Enabled := False;
FTimer.Interval := 20;
FTimer.OnTimer := @TimerTimer;
FView.OnAfterCreated := @BrowserAfterCreated;
FHtml := InitialURL;
FHtmlPending := True;
InitWebView;
end;
procedure TEdgeBrowserUpdater.ClearContentAll;
begin
SetPage('<html></html>');
end;
procedure TEdgeBrowserUpdater.SetPage(const html: string);
begin
if Assigned(FView.OnNavigationCompleted) then
begin
FHtmlPending := True;
FHtml := html;
Exit;
end;
DoSetPage(html);
end;
destructor TEdgeBrowserUpdater.Destroy;
begin
inherited Destroy;
end;
{ TMainForm }
procedure TMainForm.ActionAbortGenerationExecute(Sender: TObject);
begin
FWritingTools.AbortGeneration;
end;
procedure TMainForm.ActionHideExecute(Sender: TObject);
begin
FBrowserUpdater.SetPage('<html></html>');
Hide;
end;
procedure TMainForm.Exit1Click(Sender: TObject);
begin
Close;
end;
procedure TMainForm.About1Click(Sender: TObject);
begin
FWritingTools.About;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
Enabled := False;
FActivePageId := -1;
if GlobalWebView2Loader.InitializationError then
begin
ShowMessage(UTF8Encode(GlobalWebView2Loader.ErrorMessage));
Application.Terminate;
end;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
FWritingTools.Free;
end;
procedure TMainForm.FormPaint(Sender: TObject);
Var
R: TRect;
begin
R := TRect.Create(0, 0, Width, Height);
Canvas.GradientFill(R,
FWritingTools.Profile.UIConfig.Background.Color1,
FWritingTools.Profile.UIConfig.Background.Color2, gdVertical);
end;
procedure TMainForm.FormShow(Sender: TObject);
var
Profile: TWritingProfile;
FN: string;
I: Integer;
function CreateButton(Id: Integer; A: TWritingAction): TButton;
var
C: string;
begin
Result := TButton.Create(PageActions);
Result.Parent := PageActions;
Result.Tag := Id;
C := A.Name;
if A.Shortcut <> Chr(0) then
C := C + Format(' (&%s)', [Profile.Action[I].Shortcut]);
Result.Caption := C;
Result.OnClick := @ActionButtonClick;
end;
begin
OnShow := nil;
FN := ExtractFilePath(Application.ExeName) + 'profile.json';
if ParamCount() > 1 then FN := ParamStr(1);
if not FileExists(FN) then
begin
MessageDlg(Format('profile.json not found: %s', [FN]), TMsgDlgType.mtError, [mbOK], -1);
Application.Terminate;
Exit;
end;
FBrowserUpdater := TEdgeBrowserUpdater.Create(Self, WVBrowser1, WVWindowParent1, '');
FWritingTools := TWritingTools.Create(Self, Self, FN);
FWritingTools.EditCustomPrompt := EditCustomPrompt;
EditCustomPrompt.OnKeyPress := @FWritingTools.EditCustomPromptKeyPress;
ButtonClear.OnClick := @FWritingTools.ButtonClearClick;
Profile := FWritingTools.Profile;
SetLength(FActButtons, Profile.ActionNumber);
for I := 0 to Profile.ActionNumber - 1 do
begin
FActButtons[I] := CreateButton(I, Profile.Action[I]);
end;
LabelStatus.Caption := 'loading LLMs';
FWritingTools.Startup;
ShowPage(CARD_MAIN);
end;
procedure TMainForm.PageActionsResize(Sender: TObject);
begin
LayoutButtons;
end;
procedure TMainForm.Show1Click(Sender: TObject);
begin
Show;
end;
procedure TMainForm.WVBrowser1AcceleratorKeyPressed(Sender: TObject;
const aController: ICoreWebView2Controller;
const aArgs: ICoreWebView2AcceleratorKeyPressedEventArgs);
var
Msg: TMessage;
Key: SYSUINT;
LParam: SYSINT;
KeyEventKind: COREWEBVIEW2_KEY_EVENT_KIND;
begin
if Failed(aArgs.Get_VirtualKey(Key))
or Failed(aArgs.Get_KeyEventLParam(LParam))
or Failed(aArgs.Get_KeyEventKind(KeyEventKind)) then Exit;
case KeyEventKind of
COREWEBVIEW2_KEY_EVENT_KIND_KEY_DOWN:
Msg.Msg := WM_KEYDOWN;
COREWEBVIEW2_KEY_EVENT_KIND_KEY_UP:
Msg.Msg := WM_KEYUP;
COREWEBVIEW2_KEY_EVENT_KIND_SYSTEM_KEY_DOWN:
Msg.Msg := WM_SYSKEYDOWN;
COREWEBVIEW2_KEY_EVENT_KIND_SYSTEM_KEY_UP:
Msg.Msg := WM_SYSKEYUP;
else
Msg.Msg := 0;
end;
Msg.WParam := Key;
Msg.LParam := LParam;
if IsShortcut(TLMKey(Msg)) then
aArgs.Set_Handled(Integer(LongBool(True)));
end;
procedure TMainForm.LayoutButtons;
const
GAP_V = 20;
GAP_H = 35;
var
RowsNum: Integer;
W, H: Integer;
I: Integer;
Row: Integer;
Col: Integer;
begin
RowsNum := (Length(FActButtons) + 1) div 2;
if RowsNum < 1 then Exit;
W := (PageActions.Width - 3 * GAP_H) div 2;
H := (PageActions.Height - (1 + RowsNum) * GAP_V) div RowsNum;
if (W < 10) or (H < 10) then Exit;
for I := 0 to High(FActButtons) do
begin
Row := I div 2;
Col := I mod 2;
with FActButtons[i] do
begin
Width := W;
Height := H;
Left := GAP_H + (GAP_H + W) * Col;
Top := GAP_V + (GAP_V + H) * Row;
end;
end;
end;
procedure TMainForm.ActionButtonClick(Sender: TObject);
begin
if not (Sender is TButton) then Exit;
FWritingTools.DoAction((Sender as TButton).Tag);
end;
procedure TMainForm.WMHotKey(var Msg: TMessage);
begin
FWritingTools.UserHotkey;
end;
procedure TMainForm.ShowPage(Id: Integer);
begin
if FActivePageId = Id then Exit;
FActivePageId := Id;
case Id of
CARD_MAIN: begin
PageActions.Visible := True;
PageRunning.Visible := False;
// PageBrowser.Visible := False;
PageActions.Align := alClient;
end;
CARD_RUNNING: begin
PageActions.Visible := False;
PageRunning.Visible := True;
PageBrowser.Visible := False;
PageRunning.Align := alClient;
end
else
PageActions.Visible := False;
PageRunning.Visible := False;
PageBrowser.Visible := True;
PageBrowser.Align := alClient;
WVWindowParent1.UpdateSize;
end;
ButtonClear.Visible := FActivePageId = CARD_CHAT;
end;
procedure TMainForm.UpdateAIMemo(L: string);
begin
FBrowserUpdater.SetPage(L);
end;
procedure TMainForm.ShowStatus(Msg: string);
begin
LabelStatus.Caption := Msg;
end;
procedure TMainForm.LLMStateChanged(Sender: TObject; ABusy: Boolean);
begin
ActivityIndicator.Visible := ABusy;
ButtonClear.Visible := (not ABusy) and (FActivePageId = CARD_CHAT);
end;
procedure TMainForm.LLMLoaded;
begin
Enabled := True;
ActivityIndicator.Visible := False;
end;
procedure TMainForm.ShowChat;
begin
FBrowserUpdater.SetPage(FWritingTools.RenderChat);
end;
initialization
GlobalWebView2Loader := TWVLoader.Create(nil);
GlobalWebView2Loader.UserDataFolder := UTF8Decode(GetTempDir + 'WritingToolsCache');
GlobalWebView2Loader.StartWebView2;
end.