-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFMX.UIToast.pas
More file actions
519 lines (445 loc) · 15.2 KB
/
FMX.UIToast.pas
File metadata and controls
519 lines (445 loc) · 15.2 KB
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
unit FMX.UIToast;
{
* ===========================================================================
* FMX.UIToast - Displaying Customizable Toast Messages for Delphi/FMX
* Copyright (c) 2025 - MEStackCodes
* https://github.com/MEStackCodes
* ---------------------------------------------------------------------------
* Distributed under the MIT software license.
* ===========================================================================
}
interface
uses
FMX.Forms,
System.Classes, System.Types, System.UITypes, FMX.Types, FMX.Controls, FMX.Controls.Presentation,
FMX.Presentation.Messages, FMX.Presentation.Factory, FMX.Presentation.Style, FMX.Objects, FMX.Ani,
FMX.Graphics, FMX.Layouts;
type
TToastColorRec = record
Color: TAlphaColor;
Text: TAlphaColor;
end;
TToastType = record
const
Success: TToastColorRec = (Color: TAlphaColor($FF16A715); Text: TAlphaColor($FFFFFFFF));
Warning: TToastColorRec = (Color: TAlphaColor($FFFF8F00); Text: TAlphaColor($FF000000));
Danger: TToastColorRec = (Color: TAlphaColor($FFFF0505); Text: TAlphaColor($FFFFFFFF));
Dark: TToastColorRec = (Color: TAlphaColor($FF0C0C0C); Text: TAlphaColor($FFFFFFFF));
Light: TToastColorRec = (Color: TAlphaColor($FFF7F7F7); Text: TAlphaColor($FF000000));
end;
TToastAnimation = (taFade, taSlide, taNone);
TToastPosition = (tpTop, tpBottom, tpCenter);
TUIToast = class(TComponent)
private
FText: string;
FFontColor: TAlphaColor;
FBackColor: TAlphaColor;
FToastColorType: TToastColorRec;
FFontSize: Single;
FOpacity: Single;
FCornerRadius: Single;
FBorderColor: TAlphaColor;
FBorderWidth: Single;
FAnimation: TToastAnimation;
FDuration: Single;
FTransition: Single;
FPosition: TToastPosition;
FMargin: TBounds;
FHeight: Single;
FWidth: Single;
FTextAlign: TTextAlign;
FControlType: TControlType;
FToastParentControl: TFMXObject;
procedure SetControlType(const Value: TControlType);
procedure SetText(const Value: string);
procedure SetFontColor(const Value: TAlphaColor);
procedure SetBackColor(const Value: TAlphaColor);
procedure SetFontSize(const Value: Single);
procedure SetOpacity(const Value: Single);
procedure SetCornerRadius(const Value: Single);
procedure SetBorderColor(const Value: TAlphaColor);
procedure SetBorderWidth(const Value: Single);
procedure SetAnimation(const Value: TToastAnimation);
procedure SetDuration(const Value: Single);
procedure SetTransition(const Value: Single);
procedure SetPosition(const Value: TToastPosition);
procedure SetMargin(const Value: TBounds);
procedure SetHeight(const Value: Single);
procedure SetTextAlign(const Value: TTextAlign);
procedure SetToastColorType(const Value: TToastColorRec);
procedure CreateToastVisual;
procedure DestroyToastVisual(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Show(AParent: TFMXObject);
property ControlType: TControlType read FControlType write SetControlType;
property Text: string read FText write SetText;
property FontColor: TAlphaColor read FFontColor write SetFontColor default TAlphaColors.White;
property BackColor: TAlphaColor read FBackColor write SetBackColor default TAlphaColors.Black;
property FontSize: Single read FFontSize write SetFontSize;
property Opacity: Single read FOpacity write SetOpacity;
property CornerRadius: Single read FCornerRadius write SetCornerRadius;
property BorderColor: TAlphaColor read FBorderColor write SetBorderColor default TAlphaColors.Null;
property BorderWidth: Single read FBorderWidth write SetBorderWidth;
property Animation: TToastAnimation read FAnimation write SetAnimation default taFade;
property Duration: Single read FDuration write SetDuration;
property Transition: Single read FTransition write SetTransition;
property Position: TToastPosition read FPosition write SetPosition default tpBottom;
property Margin: TBounds read FMargin write SetMargin;
property ToastHeight: Single read FHeight write SetHeight;
property TextAlign: TTextAlign read FTextAlign write SetTextAlign default TTextAlign.Center;
property ToastColorType: TToastColorRec read FToastColorType write SetToastColorType;
end;
implementation
uses
SysUtils;
type
TVisualToastAnimation = class(TFloatAnimation)
private
FOnFinishCallback: TProc<TObject>;
protected
procedure DoFinish; override;
public
property OnFinishCallback: TProc<TObject> read FOnFinishCallback write FOnFinishCallback;
end;
TVisualToast = class(TLayout)
private
FToastControl: TPresentedControl;
FToastRect: TRectangle;
FToastText: TText;
FToastBlockContainer: TLayout;
FAnimator: TVisualToastAnimation;
FUIToastControl: TUIToast;
public
constructor Create(AOwner: TComponent); override;
procedure StartToastAnimation;
property UIToastControl: TUIToast read FUIToastControl write FUIToastControl;
property ToastBlockContainer: TLayout read FToastBlockContainer write FToastBlockContainer;
property ToastControl: TPresentedControl read FToastControl write FToastControl;
property ToastRect: TRectangle read FToastRect write FToastRect;
property ToastText: TText read FToastText write FToastText;
end;
procedure TVisualToastAnimation.DoFinish;
begin
inherited;
if (Assigned(FOnFinishCallback)) then
begin
FOnFinishCallback(Self);
end;
end;
constructor TVisualToast.Create(AOwner: TComponent);
begin
inherited;
Self.Align := TAlignLayout.Contents;
Self.Visible := True;
Self.HitTest := False;
FToastBlockContainer := TLayout.Create(Self);
FToastBlockContainer.Align := TAlignLayout.None;
FToastBlockContainer.Parent := Self;
FToastBlockContainer.Visible := True;
FToastControl := TPresentedControl.Create(FToastBlockContainer);
FToastControl.Align := TAlignLayout.Client;
FToastControl.Parent := FToastBlockContainer;
FToastControl.Visible := True;
FToastRect := TRectangle.Create(FToastControl);
FToastRect.Parent := FToastControl;
FToastRect.Padding := TBounds.Create(RectF(8, 2, 8, 2));
FToastRect.Align := TAlignLayout.Contents;
FToastRect.Visible := True;
FToastText := TText.Create(FToastRect);
FToastText.Parent := FToastRect;
FToastText.Align := TAlignLayout.Client;
FToastText.Visible := True;
FUIToastControl := nil;
end;
procedure TVisualToast.StartToastAnimation;
var
StartVerticalPosition: Single;
StopVerticalPosition: Single;
begin
StartVerticalPosition := 0;
StopVerticalPosition := 0;
case FUIToastControl.Animation of
taFade:
begin
Self.Opacity := 0;
FAnimator := TVisualToastAnimation.Create(Self);
FAnimator.Parent := Self;
FAnimator.Inverse := False;
FAnimator.Delay := 0;
FAnimator.PropertyName := 'Opacity';
FAnimator.StartValue := 0;
FAnimator.StopValue := 1;
FAnimator.Duration := FUIToastControl.Transition;
FAnimator.OnFinishCallback := procedure(Sender: TObject)
begin
if (not TVisualToastAnimation(Sender).Inverse) then
begin
TVisualToastAnimation(Sender).Inverse := True;
TVisualToastAnimation(Sender).Delay := FUIToastControl.Duration;
TVisualToastAnimation(Sender).Start;
end
else
begin
FUIToastControl.DestroyToastVisual(Sender);
end;
end;
FAnimator.Start;
end;
taSlide:
begin
case FUIToastControl.Position of
tpTop:
begin
FToastBlockContainer.Position.Y := (Self.Top - FToastBlockContainer.Height);
StartVerticalPosition := (Self.Top - FToastBlockContainer.Height);
StopVerticalPosition := Self.Top + FUIToastControl.Margin.Top;
end;
tpBottom:
begin
FToastBlockContainer.Position.Y := Self.Height;
StartVerticalPosition := Self.Height;
StopVerticalPosition := (Self.Height - FToastBlockContainer.Height) - FUIToastControl.Margin.Bottom;
end;
tpCenter:
begin
FToastControl.Position.Y := FToastBlockContainer.Position.Y + FToastBlockContainer.Height;
StartVerticalPosition := FToastBlockContainer.Position.Y + FToastBlockContainer.Height;
StopVerticalPosition := FToastBlockContainer.Position.Y - FToastBlockContainer.Height;
end;
end;
FAnimator := TVisualToastAnimation.Create(FToastBlockContainer);
FAnimator.Parent := FToastBlockContainer;
FAnimator.Inverse := False;
FAnimator.Delay := 0;
FAnimator.PropertyName := 'Position.Y';
FAnimator.StartValue := StartVerticalPosition;
FAnimator.StopValue := StopVerticalPosition;
FAnimator.Duration := FUIToastControl.Transition;
FAnimator.OnFinishCallback := procedure(Sender: TObject)
begin
if (not TVisualToastAnimation(Sender).Inverse) then
begin
TVisualToastAnimation(Sender).Inverse := True;
TVisualToastAnimation(Sender).Delay := FUIToastControl.Duration;
TVisualToastAnimation(Sender).Start;
end
else
begin
FUIToastControl.DestroyToastVisual(Sender);
end;
end;
FAnimator.Start;
end;
taNone:
begin
Self.Opacity := 0;
FAnimator := TVisualToastAnimation.Create(Self);
FAnimator.Parent := Self;
FAnimator.Inverse := False;
FAnimator.Delay := 0;
FAnimator.PropertyName := 'Opacity';
FAnimator.StartValue := 0;
FAnimator.StopValue := 1;
FAnimator.Duration := 0.1;
FAnimator.OnFinishCallback := procedure(Sender: TObject)
begin
if (not TVisualToastAnimation(Sender).Inverse) then
begin
TVisualToastAnimation(Sender).Inverse := True;
TVisualToastAnimation(Sender).Delay := FUIToastControl.Duration;
TVisualToastAnimation(Sender).Start;
end
else
begin
FUIToastControl.DestroyToastVisual(Sender);
end;
end;
FAnimator.Start;
end;
end;
end;
{ TUIToast }
constructor TUIToast.Create(AOwner: TComponent);
begin
inherited;
FText := 'Hello World! with UI Toast...';
FFontColor := TAlphaColors.White;
FBackColor := TAlphaColors.Black;
FFontSize := 16;
FOpacity := 1;
FCornerRadius := 12;
FBorderColor := TAlphaColors.Null;
FBorderWidth := 0;
FAnimation := taFade;
FDuration := 2.5;
FTransition := 0.2;
FPosition := tpBottom;
FMargin := TBounds.Create(RectF(16, 16, 16, 16));
FHeight := 48;
FWidth := 240;
FTextAlign := TTextAlign.Center;
FControlType := TControlType.Styled;
end;
destructor TUIToast.Destroy;
begin
FMargin.Free;
inherited;
end;
procedure TUIToast.SetControlType(const Value: TControlType);
begin
FControlType := Value;
end;
procedure TUIToast.SetText(const Value: string);
begin
FText := Value;
end;
procedure TUIToast.SetFontColor(const Value: TAlphaColor);
begin
FFontColor := Value;
end;
procedure TUIToast.SetBackColor(const Value: TAlphaColor);
begin
FBackColor := Value;
end;
procedure TUIToast.SetToastColorType(const Value: TToastColorRec);
begin
FToastColorType := Value;
FBackColor := Value.Color;
FFontColor := Value.Text;
end;
procedure TUIToast.SetFontSize(const Value: Single);
begin
FFontSize := Value;
end;
procedure TUIToast.SetOpacity(const Value: Single);
begin
FOpacity := Value;
end;
procedure TUIToast.SetCornerRadius(const Value: Single);
begin
FCornerRadius := Value;
end;
procedure TUIToast.SetBorderColor(const Value: TAlphaColor);
begin
FBorderColor := Value;
end;
procedure TUIToast.SetBorderWidth(const Value: Single);
begin
FBorderWidth := Value;
end;
procedure TUIToast.SetAnimation(const Value: TToastAnimation);
begin
FAnimation := Value;
end;
procedure TUIToast.SetDuration(const Value: Single);
begin
FDuration := Value;
end;
procedure TUIToast.SetTransition(const Value: Single);
begin
FTransition := Value;
end;
procedure TUIToast.SetPosition(const Value: TToastPosition);
begin
FPosition := Value;
end;
procedure TUIToast.SetMargin(const Value: TBounds);
begin
FMargin.Assign(Value);
end;
procedure TUIToast.SetHeight(const Value: Single);
begin
FHeight := Value;
end;
procedure TUIToast.SetTextAlign(const Value: TTextAlign);
begin
FTextAlign := Value;
end;
procedure TUIToast.CreateToastVisual;
var
ParentWidth, ParentHeight: Single;
IsLandscape: Boolean;
ToastWidth, MarginPercent, MarginValue: Single;
VisualToast: TVisualToast;
begin
if (FToastParentControl.ClassType.InheritsFrom(TCustomForm)) then
begin
ParentWidth := TCustomForm(FToastParentControl).Width;
ParentHeight := TCustomForm(FToastParentControl).Height;
end
else if (FToastParentControl.ClassType.InheritsFrom(TControl)) then
begin
ParentWidth := TControl(FToastParentControl).Width;
ParentHeight := TControl(FToastParentControl).Height;
end
else
begin
exit;
end;
IsLandscape := ParentWidth > ParentHeight;
if IsLandscape then
begin
ToastWidth := ParentWidth * 0.75;
MarginPercent := 0.125;
end
else
begin
ToastWidth := ParentWidth * 0.9;
MarginPercent := 0.05;
end;
MarginValue := ParentWidth * MarginPercent;
VisualToast := TVisualToast.Create(nil);
VisualToast.ToastBlockContainer.Height := FHeight;
VisualToast.ToastBlockContainer.Width := ToastWidth;
VisualToast.ToastBlockContainer.Margins.Left := MarginValue;
VisualToast.ToastBlockContainer.Margins.Right := MarginValue;
VisualToast.ToastBlockContainer.Position.X := MarginValue;
case FPosition of
tpTop:
VisualToast.ToastBlockContainer.Position.Y := FMargin.Top;
tpBottom:
VisualToast.ToastBlockContainer.Position.Y := (ParentHeight - FHeight) - FMargin.Bottom;
tpCenter:
VisualToast.ToastBlockContainer.Position.Y := (ParentHeight - FHeight) / 2;
end;
VisualToast.ToastRect.Fill.Color := FBackColor;
VisualToast.ToastRect.Opacity := FOpacity;
VisualToast.ToastRect.XRadius := FCornerRadius;
VisualToast.ToastRect.YRadius := FCornerRadius;
VisualToast.ToastRect.Stroke.Color := FBorderColor;
VisualToast.ToastRect.Stroke.Thickness := FBorderWidth;
VisualToast.ToastText.Text := FText;
VisualToast.ToastText.TextSettings.Font.Size := FFontSize;
VisualToast.ToastText.TextSettings.FontColor := FFontColor;
VisualToast.ToastText.TextSettings.HorzAlign := FTextAlign;
VisualToast.ToastText.TextSettings.WordWrap := True;
FToastParentControl.AddObject(VisualToast);
VisualToast.ToastControl.ControlType := FControlType;
// Blend Mode
if (VisualToast.ToastControl.ControlType = TControlType.Platform) then
begin
VisualToast.ToastControl.Opacity := 0.9;
end;
VisualToast.UIToastControl := Self;
VisualToast.StartToastAnimation;
end;
procedure TUIToast.DestroyToastVisual(Sender: TObject);
begin
TThread.Queue(nil,
procedure
begin
TVisualToast(Sender).Free;
end);
end;
procedure TUIToast.Show(AParent: TFMXObject);
begin
if (not Assigned(AParent)) then
begin
exit;
end;
FToastParentControl := AParent;
CreateToastVisual;
end;
end.