This repository has been archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDALLE.Main.pas
352 lines (309 loc) · 9.48 KB
/
DALLE.Main.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
unit DALLE.Main;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, OpenAI,
FMX.Objects, FMX.Layouts, FMX.ListBox, FMX.Controls.Presentation, FMX.StdCtrls,
System.ImageList, FMX.ImgList, FMX.SVGIconImageList, DALLE.FrameChat,
FMX.Memo.Types, FMX.ScrollBox, FMX.Memo, FMX.Effects, FMX.Filter.Effects,
FMX.Edit;
type
TFormMain = class(TForm)
LayoutChats: TLayout;
Rectangle1: TRectangle;
StyleBook: TStyleBook;
SVGIconImageList: TSVGIconImageList;
LayoutHead: TLayout;
ButtonMenu: TButton;
ButtonNewChatCompact: TButton;
LabelChatName: TLabel;
LayoutMenuContent: TLayout;
RectangleMenu: TRectangle;
LayoutMenu: TLayout;
ButtonNewChat: TButton;
ListBoxChatList: TListBox;
ListBoxItem1: TListBoxItem;
ListBoxItem3: TListBoxItem;
RectangleMenuBG: TRectangle;
ButtonCloseMenu: TButton;
LayoutMenuContainer: TLayout;
Layout1: TLayout;
ButtonDiscord: TButton;
Line1: TLine;
ButtonClear: TButton;
ButtonFAQ: TButton;
LayoutChatsBox: TLayout;
Layout2: TLayout;
ButtonClearConfirm: TButton;
ButtonClearCancel: TButton;
procedure ButtonNewChatClick(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure ButtonMenuClick(Sender: TObject);
procedure ButtonCloseMenuClick(Sender: TObject);
procedure ButtonNewChatCompactClick(Sender: TObject);
procedure FormConstrainedResize(Sender: TObject; var MinWidth, MinHeight, MaxWidth, MaxHeight: Single);
procedure FormVirtualKeyboardShown(Sender: TObject; KeyboardVisible: Boolean; const Bounds: TRect);
procedure FormVirtualKeyboardHidden(Sender: TObject; KeyboardVisible: Boolean; const Bounds: TRect);
procedure ButtonClearClick(Sender: TObject);
procedure ButtonClearConfirmClick(Sender: TObject);
procedure ButtonClearCancelClick(Sender: TObject);
procedure ButtonDiscordClick(Sender: TObject);
procedure ButtonFAQClick(Sender: TObject);
private
FOpenAI: TOpenAIComponent;
FMode: TWindowMode;
FChatIdCount: Integer;
procedure SetMode(const Value: TWindowMode);
procedure UpdateMode;
function NextChatId: Integer;
procedure SelectChat(const ChatId: string);
procedure FOnChatItemClick(Sender: TObject);
{$HINTS OFF}
procedure FOnChatItemTap(Sender: TObject; const Point: TPointF);
{$HINTS ON}
function CreateChat: string;
procedure CloseMenu;
procedure Clear;
procedure ShowClearConfirm;
procedure HideClearConfirm;
public
property OpenAI: TOpenAIComponent read FOpenAI;
constructor Create(AOwner: TComponent); override;
property Mode: TWindowMode read FMode write SetMode;
end;
const
API_TOKEN = {$include MY_TOKEN.txt};
URL_DISCORD = 'https://discord.com/invite/openai';
URL_FAQ = 'https://help.openai.com/en/collections/3742473-chatgpt';
var
FormMain: TFormMain;
const
AniInterpolation = TInterpolationType.Quadratic;
implementation
uses
{$IFDEF ANDROID}
Androidapi.Helpers, Androidapi.JNI.GraphicsContentViewText,
{$ENDIF}
{$IFDEF MSWINDOWS}
ShellAPI,
{$ENDIF}
FMX.Ani, System.Math;
{$R *.fmx}
{$IFDEF ANDROID}
procedure OpenUrl(const URL: string);
begin
TAndroidHelper.Context.startActivity(
TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW, StrToJURI(URL)));
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
procedure OpenUrl(const URL: string);
begin
ShellExecute(0, 'open', PChar(URL), nil, nil, 0);
end;
{$ENDIF}
{ TFormMain }
procedure TFormMain.ShowClearConfirm;
begin
ButtonClear.Text := 'Confirm clear';
ButtonClearConfirm.Visible := True;
ButtonClearCancel.Visible := True;
end;
procedure TFormMain.HideClearConfirm;
begin
ButtonClear.Text := 'Clear conversations';
ButtonClearConfirm.Visible := False;
ButtonClearCancel.Visible := False;
end;
procedure TFormMain.ButtonClearCancelClick(Sender: TObject);
begin
HideClearConfirm;
end;
procedure TFormMain.ButtonClearClick(Sender: TObject);
begin
ShowClearConfirm;
end;
procedure TFormMain.ButtonClearConfirmClick(Sender: TObject);
begin
Clear;
end;
procedure TFormMain.ButtonCloseMenuClick(Sender: TObject);
begin
CloseMenu;
end;
procedure TFormMain.ButtonDiscordClick(Sender: TObject);
begin
OpenUrl(URL_DISCORD);
end;
procedure TFormMain.ButtonFAQClick(Sender: TObject);
begin
OpenUrl(URL_FAQ);
end;
procedure TFormMain.ButtonMenuClick(Sender: TObject);
begin
RectangleMenuBG.Opacity := 0;
LayoutMenuContainer.Opacity := 0;
LayoutMenuContainer.Margins.Left := -(LayoutMenuContainer.Width - 45);
ButtonCloseMenu.Opacity := 0;
LayoutMenuContent.Visible := True;
TAnimator.AnimateFloat(RectangleMenuBG, 'Opacity', 1, 0.2, TAnimationType.InOut, AniInterpolation);
TAnimator.AnimateFloat(ButtonCloseMenu, 'Opacity', 1, 0.2, TAnimationType.InOut, AniInterpolation);
TAnimator.AnimateFloat(LayoutMenuContainer, 'Opacity', 1, 0.2, TAnimationType.InOut, AniInterpolation);
TAnimator.AnimateFloat(LayoutMenuContainer, 'Margins.Left', 0, 0.2, TAnimationType.InOut, AniInterpolation);
end;
function TFormMain.NextChatId: Integer;
begin
Inc(FChatIdCount);
Result := FChatIdCount;
end;
procedure TFormMain.CloseMenu;
begin
TAnimator.AnimateFloat(RectangleMenuBG, 'Opacity', 0, 0.2, TAnimationType.InOut, AniInterpolation);
TAnimator.AnimateFloat(ButtonCloseMenu, 'Opacity', 0, 0.2, TAnimationType.InOut, AniInterpolation);
TAnimator.AnimateFloat(LayoutMenuContainer, 'Opacity', 0, 0.2, TAnimationType.InOut, AniInterpolation);
TAnimator.AnimateFloatWait(LayoutMenuContainer, 'Margins.Left', -(LayoutMenuContainer.Width - 45), 0.2, TAnimationType.InOut, AniInterpolation);
LayoutMenuContent.Visible := False;
end;
function TFormMain.CreateChat: string;
begin
Result := TGUID.NewGuid.ToString;
var ChatTitle := 'New chat ' + NextChatId.ToString;
var Frame := TFrameChat.Create(LayoutChatsBox);
Frame.Align := TAlignLayout.Client;
Frame.Parent := LayoutChatsBox;
Frame.API := OpenAI;
Frame.ChatId := Result;
Frame.Title := ChatTitle;
Frame.Mode := Mode;
var ItemList := TListBoxItem.Create(ListBoxChatList);
ItemList.HitTest := True;
{$IFDEF ANDROID}
ItemList.OnTap := FOnChatItemTap;
{$ELSE}
ItemList.OnClick := FOnChatItemClick;
{$ENDIF}
ItemList.Margins.Bottom := 8;
ItemList.Text := ChatTitle;
ItemList.TagString := Result;
ItemList.ImageIndex := 1;
ItemList.DisableDisappear := True;
ListBoxChatList.AddObject(ItemList);
ItemList.ApplyStyleLookup;
end;
procedure TFormMain.ButtonNewChatClick(Sender: TObject);
begin
SelectChat(CreateChat);
if Mode = wmCompact then
CloseMenu;
end;
procedure TFormMain.ButtonNewChatCompactClick(Sender: TObject);
begin
SelectChat(CreateChat);
end;
procedure TFormMain.FOnChatItemClick(Sender: TObject);
var
Item: TListBoxItem absolute Sender;
begin
SelectChat(Item.TagString);
if Mode = wmCompact then
CloseMenu;
end;
procedure TFormMain.FOnChatItemTap(Sender: TObject; const Point: TPointF);
begin
FOnChatItemClick(Sender);
end;
procedure TFormMain.SelectChat(const ChatId: string);
begin
for var Control in LayoutChatsBox.Controls do
if Control is TFrameChat then
begin
var Frame := Control as TFrameChat;
Frame.Visible := Frame.ChatId = ChatId;
end;
for var i := 0 to Pred(ListBoxChatList.Count) do
if ListBoxChatList.ListItems[i].TagString = ChatId then
begin
ListBoxChatList.ListItems[i].IsSelected := True;
LabelChatName.Text := ListBoxChatList.ListItems[i].Text;
Exit;
end;
end;
constructor TFormMain.Create(AOwner: TComponent);
begin
inherited;
FChatIdCount := 0;
FOpenAI := TOpenAIComponent.Create(Self);
FOpenAI.Token := API_TOKEN;
ListBoxChatList.AniCalculations.Animation := True;
Clear;
FMode := wmFull;
UpdateMode;
end;
procedure TFormMain.Clear;
begin
FChatIdCount := 0;
HideClearConfirm;
ListBoxChatList.Clear;
while LayoutChatsBox.ControlsCount > 0 do
LayoutChatsBox.Controls[0].Free;
SelectChat(CreateChat);
end;
procedure TFormMain.FormConstrainedResize(Sender: TObject; var MinWidth, MinHeight, MaxWidth, MaxHeight: Single);
begin
FormResize(Sender);
end;
procedure TFormMain.FormResize(Sender: TObject);
begin
LayoutMenuContainer.Width := Min(320, ClientWidth - 45);
if ClientWidth < 768 then
Mode := wmCompact
else
Mode := wmFull;
end;
procedure TFormMain.FormVirtualKeyboardHidden(Sender: TObject; KeyboardVisible: Boolean; const Bounds: TRect);
begin
Padding.Bottom := 0;
end;
procedure TFormMain.FormVirtualKeyboardShown(Sender: TObject; KeyboardVisible: Boolean; const Bounds: TRect);
begin
Padding.Bottom := Bounds.Height;
end;
procedure TFormMain.UpdateMode;
begin
for var Control in LayoutChatsBox.Controls do
if Control is TFrameChat then
begin
var Frame := Control as TFrameChat;
Frame.Mode := FMode;
end;
case FMode of
wmCompact:
begin
RectangleMenu.Align := TAlignLayout.Client;
RectangleMenu.Parent := LayoutMenuContainer;
LayoutHead.Visible := True;
ButtonCloseMenu.Visible := True;
end;
wmFull:
begin
RectangleMenu.Align := TAlignLayout.Left;
RectangleMenu.Width := 260;
RectangleMenu.Parent := Self;
LayoutHead.Visible := False;
ButtonCloseMenu.Visible := False;
LayoutMenuContent.Visible := False;
end;
end;
end;
procedure TFormMain.SetMode(const Value: TWindowMode);
begin
if FMode = Value then
Exit;
FMode := Value;
UpdateMode;
end;
initialization
{$IFDEF DEBUG}
ReportMemoryLeaksOnShutdown := True;
{$ENDIF}
end.