-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
ChatGPT.FrameCode.pas
281 lines (246 loc) · 7.88 KB
/
ChatGPT.FrameCode.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
unit ChatGPT.FrameCode;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
FMX.Objects, FMX.Memo.Types, FMX.Controls.Presentation, FMX.ScrollBox,
FMX.Edit.Style, FMX.Memo, FMX.Layouts, FMX.Memo.Style, ChatGPT.Classes,
FMX.TextLayout, ChatGPT.Code, FMX.Gestures, FMX.RichEdit.Style;
{$IF DEFINED(ANDROID) OR DEFINED(IOS) OR DEFINED(IOS64)}
{$DEFINE MOBILE}
{$ENDIF}
type
TMemo = class(FMX.Memo.TMemo)
protected
procedure MouseWheel(Shift: TShiftState; WheelDelta: Integer; var Handled: Boolean); override;
end;
TFrameCode = class(TFrame)
RectangleHead: TRectangle;
RectangleClient: TRectangle;
MemoCode: TMemo;
LabelLanguage: TLabel;
LayoutCopyCode: TLayout;
PathCopy: TPath;
LabelCopy: TLabel;
TimerMouseOver: TTimer;
procedure FrameResize(Sender: TObject);
procedure LayoutCopyCodeClick(Sender: TObject);
procedure MemoCodeMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; var Handled: Boolean);
procedure LabelCopyResize(Sender: TObject);
procedure MemoCodeMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Single);
procedure MemoCodeMouseLeave(Sender: TObject);
procedure MemoCodeClick(Sender: TObject);
procedure RectangleClientMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
procedure RectangleClientMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Single);
procedure RectangleClientMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
procedure TimerMouseOverTimer(Sender: TObject);
procedure MemoCodePresentationNameChoosing(Sender: TObject;
var PresenterName: string);
private
FOnWheel: TMouseWheelEvent;
FStyledMemo: TRichEditStyled;
FUnderMouse: TUnderMouse;
FMouseMemo: TPointF;
FUnderMouseAttr: TTextAttribute;
FMouseDown: TPointF;
procedure FOnStyleLookup(Sender: TObject);
procedure SetOnWheel(const Value: TMouseWheelEvent);
function IsJson(const Value: string): Boolean;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function GetContentHeight: Single;
procedure Fill(Data: TPart);
property OnWheel: TMouseWheelEvent read FOnWheel write SetOnWheel;
end;
implementation
uses
System.Math, FMX.Clipboard, System.JSON, FMX.Platform, ChatGPT.FrameUIMessage,
System.Net.URLClient;
{$R *.fmx}
{ TFrameCode }
constructor TFrameCode.Create(AOwner: TComponent);
begin
inherited;
Name := '';
MemoCode.DisableDisappear := True;
FStyledMemo := (MemoCode.Presentation as TRichEditStyled);
FUnderMouseAttr.Color := MemoCode.TextSettings.FontColor; // $FF006CE8;
FUnderMouseAttr.Font := TFont.Create;
FUnderMouseAttr.Font.Assign(MemoCode.TextSettings.Font);
FUnderMouseAttr.Font.Style := [TFontStyle.fsUnderline];
{$IFDEF MOBILE}
MemoCode.HitTest := False;
{$ENDIF}
MemoCode.OnApplyStyleLookup := FOnStyleLookup;
end;
destructor TFrameCode.Destroy;
begin
FUnderMouseAttr.Font.Free;
inherited;
end;
function TFrameCode.IsJson(const Value: string): Boolean;
begin
Result := False;
try
var JSON := TJSONObject.ParseJSONValue(Value);
if Assigned(JSON) then
begin
JSON.Free;
Result := True;
end;
except
Result := False;
end;
end;
procedure TFrameCode.Fill(Data: TPart);
begin
if Data.Language.IsEmpty then
if IsJson(Data.Content) then
Data.Language := 'json';
if not Data.Language.IsEmpty then
//FCodeSyntax := TCodeSyntax.FindSyntax(Data.Language, MemoCode.Font, MemoCode.FontColor);
FStyledMemo.SetCodeSyntaxName(Data.Language, MemoCode.Font, MemoCode.FontColor)
else
FStyledMemo.SetCodeSyntaxName('md', MemoCode.Font, MemoCode.FontColor);
MemoCode.Text := Data.Content;
if Data.Language.IsEmpty then
LabelLanguage.Text := ''
else
LabelLanguage.Text := Data.Language;
FrameResize(nil);
end;
procedure TFrameCode.FOnStyleLookup(Sender: TObject);
begin
FrameResize(nil);
end;
procedure TFrameCode.FrameResize(Sender: TObject);
begin
Height := GetContentHeight;
end;
function TFrameCode.GetContentHeight: Single;
begin
{$IFDEF NEW_MEMO}
//(MemoCode.Presentation as TFixedStyledMemo).InvalidateContentSize;
//(MemoCode.Presentation as TFixedStyledMemo).PrepareForPaint;
{$ELSE}
FStyledMemo.InvalidateContentSize;
FStyledMemo.PrepareForPaint;
{$ENDIF}
Result := Max(MemoCode.ContentBounds.Height + 20, 30) +
MemoCode.Margins.Top +
MemoCode.Margins.Bottom +
RectangleHead.Height;
end;
procedure TFrameCode.LabelCopyResize(Sender: TObject);
begin
LayoutCopyCode.Width := LabelCopy.Width + PathCopy.Width + 8;
end;
procedure TFrameCode.LayoutCopyCodeClick(Sender: TObject);
begin
var ClipBoard: IFMXClipboardService;
if TPlatformServices.Current.SupportsPlatformService(IFMXClipboardService, ClipBoard) then
begin
ClipBoard.SetClipboard(MemoCode.Text);
ShowUIMessage('Coppied');
end
else
ShowUIMessage('Clipboard error');
end;
procedure TFrameCode.MemoCodeClick(Sender: TObject);
begin
if (FUnderMouse.WordLine <> -1) and (not FUnderMouse.Text.IsEmpty) and (MemoCode.SelLength = 0) then
OpenUrl(FUnderMouse.Text);
end;
procedure TFrameCode.MemoCodeMouseLeave(Sender: TObject);
begin
{$IFDEF NEW_MEMO}
TimerMouseOver.Enabled := False;
FUnderMouse.WordLine := -1;
FStyledMemo.UpdateVisibleLayoutParams;
FStyledMemo.Repaint;
{$ENDIF}
end;
procedure TFrameCode.MemoCodeMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Single);
begin
{$IFDEF NEW_MEMO}
TimerMouseOver.Enabled := False;
TimerMouseOver.Enabled := True;
FMouseMemo := TPointF.Create(X, Y);
{$ENDIF}
end;
procedure TFrameCode.MemoCodeMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; var Handled: Boolean);
begin {
if ssShift in Shift then
begin
//MemoCode.DisableMouseWheel := False;
//MemoCode.Model
//Exit;
end;
if (MemoCode.SelLength > 0) and (Root.Captured = IControl(FStyledMemo)) then
begin
Handled := True;
if Assigned(FOnWheel) then
FOnWheel(Sender, Shift, WheelDelta, Handled);
end; }
end;
procedure TFrameCode.MemoCodePresentationNameChoosing(Sender: TObject;
var PresenterName: string);
begin
PresenterName := 'RichEditStyled';
end;
procedure TFrameCode.RectangleClientMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
FMouseDown := TPointF.Create(X, Y);
Root.Captured := RectangleClient;
end;
procedure TFrameCode.RectangleClientMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Single);
begin
if ssLeft in Shift then
begin
MemoCode.ViewportPosition := TPointF.Create(MemoCode.ViewportPosition.X + (FMouseDown.X - X), MemoCode.ViewportPosition.Y);
FMouseDown := TPointF.Create(X, Y);
end;
end;
procedure TFrameCode.RectangleClientMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
Root.Captured := nil;
end;
procedure TFrameCode.SetOnWheel(const Value: TMouseWheelEvent);
begin
FOnWheel := Value;
end;
procedure TFrameCode.TimerMouseOverTimer(Sender: TObject);
begin
TimerMouseOver.Enabled := False;
{$IFDEF NEW_MEMO} {
var BeginWord: Int64;
var Line: Int64;
var Str := FStyledMemo.GetWordAtPos(FMouseMemo.X, FMouseMemo.Y, BeginWord, Line);
if (not Str.IsEmpty) and (Str.ToLower.StartsWith('http')) then
try
TURI.Create(Str);
MemoCode.Cursor := crHandPoint;
except
MemoCode.Cursor := crDefault;
Line := -1;
end
else
begin
MemoCode.Cursor := crDefault;
Line := -1;
end;
FUnderMouse.WordStart := BeginWord;
FUnderMouse.WordLength := Str.Length;
FUnderMouse.WordLine := Line;
FUnderMouse.Text := Str;
FStyledMemo.UpdateVisibleLayoutParams;
FStyledMemo.Repaint; }
{$ENDIF}
end;
{ TMemo }
procedure TMemo.MouseWheel(Shift: TShiftState; WheelDelta: Integer; var Handled: Boolean);
begin
inherited;
end;
end.