-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathksFormTransition.pas
More file actions
568 lines (459 loc) · 14.6 KB
/
Copy pathksFormTransition.pas
File metadata and controls
568 lines (459 loc) · 14.6 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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
{*******************************************************************************
* *
* TksFormTransition - Push/Pop Form Queue Component *
* *
* https://bitbucket.org/gmurt/kscomponents *
* *
* Copyright 2017 Graham Murt *
* *
* email: graham@kernow-software.co.uk *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
*******************************************************************************}
unit ksFormTransition;
interface
{$I ksComponents.inc}
uses System.UITypes, FMX.Controls, FMX.Layouts, FMX.Objects, System.Classes,
FMX.Types, FMX.Graphics, System.UIConsts, FMX.Effects,
FMX.StdCtrls, System.Types, FMX.Forms, ksTypes, System.Generics.Collections;
const
{$IFDEF ANDROID}
C_TRANSITION_DURATION = 0.2;
{$ELSE}
C_TRANSITION_DURATION = 0.2;
{$ENDIF}
C_FADE_OPACITY = 0.5;
type
TksTransitionMethod = (ksTmPush, ksTmPop);
IksFormTransition = interface
['{34A12E50-B52C-4A49-B081-9CB67CA5FD6E}']
procedure BeforeTransition(AType: TksTransitionMethod);
end;
IksPostFormTransition = interface
['{CD11BABA-8659-4F42-A6BC-5E03B74690EE}']
procedure AfterTransition(AType: TksTransitionMethod);
end;
TksTransitionType = (ksFtSlideInFromLeft,
ksFtSlideInFromTop,
ksFtSlideInFromRight,
ksFtSlideInFromBottom,
ksFtSlideOutToLeft,
ksFtSlideOutToTop,
ksFtSlideOutToRight,
ksFtSlideOutToBottom,
ksFtNoTransition);
TksFormTransitionItem = class
[weak]FFromForm: TCommonCustomForm;
[weak]FToForm: TCommonCustomForm;
FTransition: TksTransitionType;
public
property FromForm: TCommonCustomForm read FFromForm;
property ToForm: TCommonCustomForm read FToForm;
property Transition: TksTransitionType read FTransition write FTransition;
end;
TksFormTransitionList = class(TObjectList<TksFormTransitionItem>)
public
end;
[ComponentPlatformsAttribute(
pidWin32 or
pidWin64 or
{$IFDEF XE8_OR_NEWER} pidiOSDevice32 or pidiOSDevice64 {$ELSE} pidiOSDevice {$ENDIF} or
{$IFDEF XE10_3_OR_NEWER} pidiOSSimulator32 or pidiOSSimulator64 {$ELSE} pidiOSSimulator {$ENDIF} or
{$IFDEF XE10_3_OR_NEWER} pidAndroid32Arm or pidAndroid64Arm {$ELSE} pidAndroid {$ENDIF}
)]
TksFormTransition = class(TksComponent)
private
FInitalizedForms: TList<TCommonCustomForm>;
function GetTransitionList: TksFormTransitionList;
function TransitionExists(AFrom, ATo: TCommonCustomForm): Boolean;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function GetFormDepth(AForm: TCommonCustomForm): integer;
procedure Push(AForm: TCommonCustomForm;
const ATransition: TksTransitionType = ksFtSlideInFromRight;
const ARecordPush: Boolean = True);
procedure Pop;
procedure PopTo(AFormClass: string);
procedure PopAllForms;
property TransitionList: TksFormTransitionList read GetTransitionList;
end;
//{$R *.dcr}
procedure Push(AForm: TCommonCustomForm; const ATransition: TksTransitionType = ksFtSlideInFromRight; const ARecordPush: Boolean = True);
procedure Pop;
procedure PopTo(AFormClass: string);
procedure PopAllForms;
procedure ClearFormTransitionStack;
procedure ClearLastFormTransition;
procedure Register;
var
ShowLoadingIndicatorOnTransition: Boolean;
DisableTransitions: Boolean;
implementation
uses FMX.Ani, SysUtils, ksCommon, DateUtils, ksFormTransitionUI, ksToolbar,
ksPickers, ksLoadingIndicator
{$IFDEF IOS}
, IOSApi.UIKit
{$ENDIF}
;
var
_InternalTransitionList: TksFormTransitionList;
_InTransition: Boolean;
procedure Register;
begin
RegisterComponents('Kernow Software FMX', [TksFormTransition]);
end;
function IsIPad: Boolean;
begin
{$IFDEF IOS}
Result := TUIDevice.Wrap( TUIDevice.OCClass.currentDevice ).userInterfaceIdiom = UIUserInterfaceIdiomPad;
{$ELSE}
Result := False;
{$ENDIF}
end;
procedure Push(AForm: TCommonCustomForm; const ATransition: TksTransitionType = ksFtSlideInFromRight; const ARecordPush: Boolean = True);
var
ATran: TksFormTransition;
begin
ATran := TksFormTransition.Create(nil);
try
// prevent animation on ipad...
if IsIPad then
ATran.Push(AForm, ksFtNoTransition, ARecordPush)
else
{$IFDEF ANDROID}
ATran.Push(AForm, ksFtNoTransition, ARecordPush)
{$ELSE}
ATran.Push(AForm, ATransition, ARecordPush);
{$ENDIF}
finally
FreeAndNil(ATran);
end;
HideLoadingIndicator(AForm);
end;
procedure Pop;
var
ATran: TksFormTransition;
begin
ATran := TksFormTransition.Create(nil);
try
ATran.Pop;
finally
FreeAndNil(ATran);
end;
end;
procedure PopTo(AFormClass: string);
var
ATran: TksFormTransition;
begin
ATran := TksFormTransition.Create(nil);
try
ATran.PopTo(AFormClass);
finally
FreeAndNil(ATran);
end;
end;
procedure PopAllForms;
var
ATran: TksFormTransition;
begin
ATran := TksFormTransition.Create(nil);
try
ATran.PopAllForms;
finally
FreeAndNil(ATran);
end;
end;
procedure ClearFormTransitionStack;
begin
_InternalTransitionList.Clear;
end;
procedure ClearLastFormTransition;
begin
_InternalTransitionList.Delete(_InternalTransitionList.Count-1);
end;
{ TksFormTransition }
constructor TksFormTransition.Create(AOwner: TComponent);
begin
inherited;
FInitalizedForms := TList<TCommonCustomForm>.Create;
end;
destructor TksFormTransition.Destroy;
begin
FreeAndNil(FInitalizedForms);
inherited;
end;
function TksFormTransition.GetFormDepth(AForm: TCommonCustomForm): integer;
var
ICount: integer;
begin
Result := 0;
for ICount := 0 to TransitionList.Count-1 do
begin
if TransitionList[ICount].ToForm = AForm then
begin
Result := ICount+1;
end;
end;
end;
function TksFormTransition.GetTransitionList: TksFormTransitionList;
begin
Result := _InternalTransitionList;
end;
procedure TksFormTransition.Pop;
begin
PopTo('');
end;
procedure TksFormTransition.PopAllForms;
var
AInfo: TksFormTransitionItem;
AFrom, ATo: TCommonCustomForm;
AAnimateForm: TfrmFormTransitionUI;
AFormIntf: IksFormTransition;
begin
if _InternalTransitionList.Count = 0 then
Exit;
if _InTransition then
Exit;
_InTransition := True;
try
AAnimateForm := TfrmFormTransitionUI.Create(nil);
try
AInfo := _InternalTransitionList.Last;
AFrom := AInfo.FToForm;
ATo := _InternalTransitionList.First.FFromForm;
{$IFDEF XE10_2_OR_NEWER}
AAnimateForm.SystemStatusBar.Assign(AFrom.SystemStatusBar);
{$ENDIF}
{if Supports(ATo, IksFormTransition, AFormIntf) then
AFormIntf.BeforeTransition(ksTmPop); }
// moved to here...
if Supports(ATo, IksFormTransition, AFormIntf) then
AFormIntf.BeforeTransition(ksTmPop);
AAnimateForm.Initialise(AFrom, ATo);
AAnimateForm.Visible := True;
AAnimateForm.Animate(AInfo.FTransition, True);
ATo.Visible := True;
AAnimateForm.Visible := False;
// moved to here...
//if Supports(ATo, IksFormTransition, AFormIntf) then
// AFormIntf.BeforeTransition(ksTmPop);
AFrom.Visible := False;
ATo.Activate;
{$IFDEF MSWINDOWS}
ATo.SetBounds(AFrom.Left, AFrom.Top, AFrom.Width, AFrom.Height);
{$ENDIF}
_InternalTransitionList.Clear;//(_InternalTransitionList.Count-1);
finally
AAnimateForm.DisposeOf;
end;
finally
_InTransition := False;
end;
end;
procedure TksFormTransition.PopTo(AFormClass: string);
var
AInfo: TksFormTransitionItem;
AFrom, ATo: TCommonCustomForm;
AAnimateForm: TfrmFormTransitionUI;
AFormIntf: IksFormTransition;
ALevels: Integer;
ICount: integer;
ALastTransition: TksTransitionType;
begin
Screen.ActiveForm.Focused := nil;
ALevels := 1;
if _InternalTransitionList.Count < 1 then
Exit;
if _InTransition then
Exit;
_InTransition := True;
AInfo := _InternalTransitionList.Last;
AFrom := AInfo.FToForm;
ATo := AInfo.FFromForm;
ALastTransition := _InternalTransitionList.Last.FTransition;
if AFormClass <> '' then
begin
while ATo.ClassName <> AFormClass do
begin
AInfo := _InternalTransitionList.Items[_InternalTransitionList.IndexOf(AInfo)-1];
ATo := AInfo.FFromForm;
Inc(ALevels);
end;
end;
AInfo.Transition := ALastTransition;
if ShowLoadingIndicatorOnTransition then
ShowLoadingIndicator(AFrom);
try
if Supports(ATo, IksFormTransition, AFormIntf) then
AFormIntf.BeforeTransition(ksTmPop);
{$IFNDEF ANDROID}
AAnimateForm := TfrmFormTransitionUI.Create(nil);
try
{$IFDEF XE10_2_OR_NEWER}
AAnimateForm.SystemStatusBar.Assign(AFrom.SystemStatusBar);
{$ENDIF}
// moved to here...
AAnimateForm.Initialise(AFrom, ATo);
AAnimateForm.Visible := True;
AAnimateForm.Animate(AInfo.FTransition, True);
ATo.Visible := True;
AAnimateForm.Visible := False;
AFrom.Visible := False;
ATo.Activate;
{$IFDEF MSWINDOWS}
ATo.SetBounds(AFrom.Left, AFrom.Top, AFrom.Width, AFrom.Height);
{$ENDIF}
for ICount := ALevels downto 1 do
_InternalTransitionList.Delete(_InternalTransitionList.Count-1);
finally
AAnimateForm.DisposeOf;
end;
{$ELSE}
ATo.Visible := True;
AFrom.Visible := False;
ATo.Activate;
for ICount := ALevels downto 1 do
_InternalTransitionList.Delete(_InternalTransitionList.Count-1);
{$ENDIF}
finally
if ShowLoadingIndicatorOnTransition then
HideLoadingIndicator(AFrom);
_InTransition := False;
end;
end;
procedure TksFormTransition.Push(AForm: TCommonCustomForm;
const ATransition: TksTransitionType = ksFtSlideInFromRight;
const ARecordPush: Boolean = True);
var
AInfo: TksFormTransitionItem;
AFrom, ATo: TCommonCustomForm;
AAnimateForm: TfrmFormTransitionUI;
AFormIntf: IksFormTransition;
APostFormIntf: IksPostFormTransition;
ATran: TksTransitionType;
begin
if _InTransition then
Exit;
ATran := ATransition;
if DisableTransitions then
ATran := ksFtNoTransition;
if (Screen.ActiveForm = nil) then // can happen when main form calls push in onShow in Android
Exit;
AFrom := Screen.ActiveForm;
//(AFrom);
ATo := AForm;
{$IFDEF MSWINDOWS}
{$IFDEF XE10_OR_NEWER}
if AFrom <> nil then
ATo.Bounds := AFrom.Bounds;
{$ENDIF}
{$ENDIF}
_InTransition := True;
if (ShowLoadingIndicatorOnTransition) and (AFrom <> nil) then
ShowLoadingIndicator(AFrom);
try
PickerService.HidePickers;
{$IFDEF ANDROID}
// fix for Android initial form size
if FInitalizedForms.IndexOf(AFrom) = -1 then
FInitalizedForms.Add(AFrom);
if FInitalizedForms.IndexOf(ATo) = -1 then
begin
FInitalizedForms.Add(ATo);
end;
{$ENDIF}
if (AFrom = ATo) then
Exit;
if TransitionExists(AFrom, ATo) then
Exit;
AInfo := TksFormTransitionItem.Create;
AInfo.FFromForm := AFrom;
AInfo.FToForm := ATo;
AInfo.FTransition := ATran;
if ARecordPush then
_InternalTransitionList.Add(AInfo);
if (ATran <> TksTransitionType.ksFtNoTransition) and (DisableTransitions = False) then
begin
AAnimateForm := TfrmFormTransitionUI.Create(nil);
try
{$IFDEF XE10_2_OR_NEWER}
if AFrom.SystemStatusBar <> nil then
AAnimateForm.SystemStatusBar.Assign(AFrom.SystemStatusBar);
{$ENDIF}
AAnimateForm.Initialise(AFrom, ATo);
AAnimateForm.Visible := True;
AAnimateForm.Animate(AInfo.FTransition, False);
// moved to here...
if Supports(ATo, IksFormTransition, AFormIntf) then
AFormIntf.BeforeTransition(ksTmPush);
AForm.Visible := True;
AAnimateForm.Visible := False;
AFrom.Visible := False;
AForm.Activate;
{$IFDEF MSWINDOWS}
AForm.SetBounds(AFrom.Left, AFrom.Top, AFrom.Width, AFrom.Height);
{$ENDIF}
finally
AAnimateForm.DisposeOf;
end;
end
else
begin
// no animation...
if Supports(ATo, IksFormTransition, AFormIntf) then
AFormIntf.BeforeTransition(ksTmPush);
AForm.Visible := True;
if AFrom <> nil then
AFrom.Visible := False;
end;
if Supports(ATo, IksPostFormTransition, APostFormIntf) then
APostFormIntf.AfterTransition(ksTmPush);
finally
if not ARecordPush then
FreeAndNil(AInfo);
_InTransition := False;
if (ShowLoadingIndicatorOnTransition) and (AFrom <> nil) then
HideLoadingIndicator(AFrom);
end;
end;
function TksFormTransition.TransitionExists(AFrom,
ATo: TCommonCustomForm): Boolean;
var
ICount: integer;
AItem: TksFormTransitionItem;
begin
Result := False;
for ICount := 0 to GetTransitionList.Count-1 do
begin
AItem := GetTransitionList.Items[ICount];
if (AItem.FromForm = AFrom) and (AItem.ToForm = ATo) then
begin
Result := True;
Exit;
end;
end;
end;
initialization
{$IFDEF IOS}
ShowLoadingIndicatorOnTransition := True;
{$ELSE}
ShowLoadingIndicatorOnTransition := True;
{$ENDIF}
_InternalTransitionList := TksFormTransitionList.Create(True);
_InTransition := False;
DisableTransitions := False;
finalization
FreeAndNil(_InternalTransitionList);
end.