-
Notifications
You must be signed in to change notification settings - Fork 7
/
ChatFMX.Frame.Attachment.Document.pas
337 lines (308 loc) · 9.01 KB
/
ChatFMX.Frame.Attachment.Document.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
unit ChatFMX.Frame.Attachment.Document;
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.Controls.Presentation, FMX.Objects, FMX.Layouts, VK.API, VK.Entity.Doc,
VK.Types, System.Messaging, Skia, Skia.FMX, ChatFMX.Frame.Attachment;
type
TFrameAttachmentDocument = class(TFrameAttachment)
LabelSize: TLabel;
ImageIcon: TImage;
LayoutContent: TLayout;
LabelTitle: TLabel;
LayoutGeneral: TLayout;
LayoutPreview: TLayout;
ImagePreview: TImage;
LayoutInfoB: TLayout;
RectangleSize: TRectangle;
LabelTypeSize: TLabel;
RectangleOver: TRectangle;
AnimatedImageGIF: TSkAnimatedImage;
AniIndicatorGif: TAniIndicator;
procedure LabelTitleMouseLeave(Sender: TObject);
procedure LabelTitleMouseEnter(Sender: TObject);
procedure LabelTypeSizeResize(Sender: TObject);
procedure ImagePreviewClick(Sender: TObject);
procedure AnimatedImageGIFClick(Sender: TObject);
private
FTitle: string;
FDocSize: string;
FDocType: TVkDocumentType;
FModePreview: Boolean;
FImageUrl: string;
FImageFile: string;
FImageUrlGif: string;
FImageFileGif: string;
FExt: string;
FPreviewSize: TSizeF;
FUrl: string;
procedure FOnReadyImage(const Sender: TObject; const M: TMessage);
procedure FOnReadyImageGif(const Sender: TObject; const M: TMessage);
procedure SetTitle(const Value: string);
procedure SetDocSize(const Value: string);
procedure SetDocType(const Value: TVkDocumentType);
procedure SetModePreview(const Value: Boolean);
procedure SetExt(const Value: string);
procedure UpdatePreviewSize;
procedure SetPreviewSize(const Value: TSizeF);
procedure SetUrl(const Value: string);
public
procedure SetVisibility(const Value: Boolean); override;
constructor Create(AOwner: TComponent; AVK: TCustomVK); override;
destructor Destroy; override;
procedure Fill(Document: TVkDocument; AsPreview: Boolean);
property Title: string read FTitle write SetTitle;
property DocSize: string read FDocSize write SetDocSize;
property DocType: TVkDocumentType read FDocType write SetDocType;
property ModePreview: Boolean read FModePreview write SetModePreview;
property Ext: string read FExt write SetExt;
property PreviewSize: TSizeF read FPreviewSize write SetPreviewSize;
property Url: string read FUrl write SetUrl;
end;
var
IconDoc: TRectF = (
Top: 0; Bottom: -180
);
IconArchive: TRectF = (
Top: -30; Bottom: -150
);
IconImage: TRectF = (
Top: -60; Bottom: -120
);
IconAudio: TRectF = (
Top: -90; Bottom: -90
);
IconVideo: TRectF = (
Top: -120; Bottom: -60
);
IconBook: TRectF = (
Top: -150; Bottom: -30
);
IconFile: TRectF = (
Top: -180; Bottom: 0
);
implementation
uses
ChatFMX.PreviewManager, HGM.FMX.Image, VK.Entity.Common,
ChatFMX.Frame.Window.Photo;
{$R *.fmx}
procedure TFrameAttachmentDocument.SetVisibility(const Value: Boolean);
begin
inherited;
if Value then
begin
AnimatedImageGIF.Visible := False;
ImagePreview.Visible := True;
AnimatedImageGIF.Source.Data := [];
if (not FImageUrl.IsEmpty) and (ImagePreview.Bitmap.IsEmpty) then
TPreview.Instance.Subscribe(FImageUrl, FOnReadyImage);
end
else
begin
AnimatedImageGIF.Source.Data := [];
ImagePreview.Bitmap := nil;
end;
end;
procedure TFrameAttachmentDocument.AnimatedImageGIFClick(Sender: TObject);
begin
AnimatedImageGIF.Visible := False;
ImagePreview.Visible := True;
end;
constructor TFrameAttachmentDocument.Create(AOwner: TComponent; AVK: TCustomVK);
begin
inherited;
ImageIcon.Bitmap.LoadFromResource('doc_icons');
ImageIcon.BitmapMargins.Rect := IconAudio;
ModePreview := False;
end;
destructor TFrameAttachmentDocument.Destroy;
begin
TPreview.Instance.Unsubscribe(FOnReadyImageGif);
TPreview.Instance.Unsubscribe(FOnReadyImage);
inherited;
end;
procedure TFrameAttachmentDocument.Fill(Document: TVkDocument; AsPreview: Boolean);
begin
Height := 32;
ModePreview := AsPreview;
FUrl := Document.Url;
FImageUrl := '';
DocSize := Document.SizeStr;
if Document.Ext.ToLower = 'gif' then
FImageUrlGif := Document.Url;
if AsPreview then
begin
Ext := Document.Ext;
if Assigned(Document.Preview) and Assigned(Document.Preview.Photo) then
begin
var Size := Document.Preview.Photo.Sizes.GetSizeFromHeight(100000);
if Assigned(Size) then
begin
Height := 100;
Width := Height * (Size.Width / Size.Height);
FImageUrl := Size.Src;
//if not FImageUrl.IsEmpty then
// TPreview.Instance.Subscribe(FImageUrl, FOnReadyImage);
end;
end;
end
else
begin
Title := Document.Title;
DocType := Document.&Type;
end;
PreviewSize := TSizeF.Create(Width, Height);
end;
procedure TFrameAttachmentDocument.FOnReadyImage(const Sender: TObject; const M: TMessage);
var
Data: TMessagePreview absolute M;
begin
if Data.Value.Url <> FImageUrl then
Exit;
TPreview.Instance.Unsubscribe(FOnReadyImage);
FImageFile := Data.Value.FileName;
if FImageFile.IsEmpty then
begin
ImagePreview.Bitmap := nil;
end
else
try
ImagePreview.Bitmap.LoadFromFile(FImageFile);
except
ImagePreview.Bitmap := nil;
end;
end;
procedure TFrameAttachmentDocument.FOnReadyImageGif(const Sender: TObject; const M: TMessage);
var
Data: TMessagePreview absolute M;
begin
if Data.Value.Url <> FImageUrlGif then
Exit;
TPreview.Instance.Unsubscribe(FOnReadyImageGif);
FImageFileGif := Data.Value.FileName;
if FImageFileGif.IsEmpty then
begin
AnimatedImageGIF.Visible := False;
ImagePreview.Visible := True;
end
else
try
AnimatedImageGIF.LoadFromFile(FImageFileGif);
ImagePreview.Visible := False;
except
AnimatedImageGIF.Visible := False;
ImagePreview.Visible := True;
end;
AniIndicatorGif.Visible := False;
end;
procedure TFrameAttachmentDocument.ImagePreviewClick(Sender: TObject);
begin
if Ext.ToLower = 'gif' then
begin
if not FImageUrlGif.IsEmpty then
if AnimatedImageGIF.Visible then
begin
AnimatedImageGIF.Visible := False;
ImagePreview.Visible := True;
end
else
begin
ImagePreview.Visible := True;
AnimatedImageGIF.Visible := True;
AniIndicatorGif.Visible := True;
TPreview.Instance.Subscribe(FImageUrlGif, FOnReadyImageGif);
end;
end
else if (Ext.ToLower = 'jpg') or (Ext.ToLower = 'png') or (Ext.ToLower = 'jpeg') then
begin
var Form := Application.MainForm;
with TFrameWindowPhoto.Create(Form, VK) do
begin
Parent := Form;
Align := TAlignLayout.Contents;
FillUrl(Url);
ShowFrame;
end;
end;
end;
procedure TFrameAttachmentDocument.UpdatePreviewSize;
begin
LabelTypeSize.Text := Ext.ToUpper + ' · ' + FDocSize;
end;
procedure TFrameAttachmentDocument.LabelTitleMouseEnter(Sender: TObject);
var
Control: TLabel absolute Sender;
begin
Control.Font.Style := Control.Font.Style + [TFontStyle.fsUnderline];
end;
procedure TFrameAttachmentDocument.LabelTitleMouseLeave(Sender: TObject);
var
Control: TLabel absolute Sender;
begin
Control.Font.Style := Control.Font.Style - [TFontStyle.fsUnderline];
end;
procedure TFrameAttachmentDocument.LabelTypeSizeResize(Sender: TObject);
begin
RectangleSize.Width := LabelTypeSize.Width + RectangleSize.Padding.Left + RectangleSize.Padding.Right;
end;
procedure TFrameAttachmentDocument.SetDocSize(const Value: string);
begin
FDocSize := Value;
LabelSize.Text := FDocSize;
UpdatePreviewSize;
end;
procedure TFrameAttachmentDocument.SetDocType(const Value: TVkDocumentType);
begin
FDocType := Value;
case FDocType of
TVkDocumentType.None, TVkDocumentType.Unknown:
ImageIcon.BitmapMargins.Rect := IconFile;
TVkDocumentType.Text:
ImageIcon.BitmapMargins.Rect := IconDoc;
TVkDocumentType.Archive:
ImageIcon.BitmapMargins.Rect := IconArchive;
TVkDocumentType.GIF, TVkDocumentType.Picture:
ImageIcon.BitmapMargins.Rect := IconImage;
TVkDocumentType.Audio:
ImageIcon.BitmapMargins.Rect := IconAudio;
TVkDocumentType.Video:
ImageIcon.BitmapMargins.Rect := IconVideo;
TVkDocumentType.Book:
ImageIcon.BitmapMargins.Rect := IconBook;
end;
end;
procedure TFrameAttachmentDocument.SetExt(const Value: string);
begin
FExt := Value;
UpdatePreviewSize;
end;
procedure TFrameAttachmentDocument.SetModePreview(const Value: Boolean);
begin
FModePreview := Value;
LayoutGeneral.Visible := not FModePreview;
LayoutPreview.Visible := FModePreview;
if FModePreview then
begin
Padding.Top := 0;
end
else
begin
Padding.Top := 4;
Height := 32;
end;
end;
procedure TFrameAttachmentDocument.SetPreviewSize(const Value: TSizeF);
begin
FPreviewSize := Value;
end;
procedure TFrameAttachmentDocument.SetTitle(const Value: string);
begin
FTitle := Value;
LabelTitle.Text := FTitle;
end;
procedure TFrameAttachmentDocument.SetUrl(const Value: string);
begin
FUrl := Value;
end;
end.