forked from zsdwxm/KernowSoftwareFMX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ksVirtualListView.pas
3330 lines (2910 loc) · 89.7 KB
/
ksVirtualListView.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
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ *******************************************************************************
* *
* TksVirtualListView *
* *
* https://github.com/gmurt/KernowSoftwareFMX *
* *
* Copyright 2015 Graham Murt *
* *
* email: [email protected] *
* *
* 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 forthe specific language governing permissions and *
* limitations under the License. *
* *
*******************************************************************************}
unit ksVirtualListView;
interface
{$I ksComponents.inc}
uses System.Classes, System.Types, ksTypes, FMX.Graphics, System.UITypes,
System.Generics.Collections, FMX.Types, FMX.InertialMovement, System.UIConsts,
FMX.StdCtrls, FMX.Controls, FMX.Platform, FMX.Objects;
const
C_VLIST_ITEM_DEFAULT_HEIGHT = 44;
C_VLIST_HEADER_DEFAULT_HEIGHT = 38;
C_VLIST_DEFAULT_SELECTED_COLOR = $FFE9E9E9;
C_VLIST_CACHE_COUNT = 100;
C_LONG_TAP_DURATION = 400;
{$IFDEF MSWINDOWS}
C_VLIST_SCROLLBAR_WIDTH = 16;
{$ELSE}
C_VLIST_SCROLLBAR_WIDTH = 8;
{$ENDIF}
C_ACCESSORY_WIDTH = 12;
type
TksVListItem = class;
TksVListItemList = class;
TksVirtualListView = class;
TksVListActionButton = class;
TksVListActionButtons = class;
TksSelectionType = (ksSingleSelect, ksMultiSelect);
TksVListCheckBoxAlign = (ksCbLeftAlign, ksCbRightAlign);
TksVListActionButtonAlign = (ksAbLeftAlign, ksAbRightAlign);
TksVListSwipeDirection = (ksSwipeFromLeft, ksSwipeFromRight);
TksVListItemPurpose = (None, Header);
TksVListItemState = (Normal, Deleting, Deleted, Sliding);
TksVListItemClickEvent = procedure(Sender: TObject; AItem: TksVListItem) of object;
TksVListItemLongTapEvent = procedure(Sender: TObject; AItem: TksVListItem) of object;
TksVListItemSwipeEvent = procedure(Sender: TObject; ARow: TksVListItem; ASwipeDirection: TksVListSwipeDirection; AButtons: TksVListActionButtons) of object;
TksVListDeletingItemEvent = procedure(Sender: TObject; AItem: TksVListItem; var ACanDelete: Boolean) of object;
TksItemActionButtonClickEvent = procedure(Sender: TObject; ARow: TksVListItem; AButton: TksVListActionButton) of object;
TksVirtualListViewAppearence = class(TPersistent)
private
[weak]FListView: TksVirtualListView;
FBackground: TAlphaColor;
FItemBackground: TAlphaColor;
FSeparatorColor: TAlphaColor;
FHeaderColor: TAlphaColor;
FHeaderFontColor: TAlphaColor;
FSelectedColor: TAlphaColor;
FSelectedFontColor: TAlphaColor;
procedure SetBackground(const Value: TAlphaColor);
procedure SetItemBackground(const Value: TAlphaColor);
procedure SetSeparatorBackground(const Value: TAlphaColor);
procedure SetHeaderColor(const Value: TAlphaColor);
procedure SetSelectedColor(const Value: TAlphaColor);
procedure SetSelectedFontColor(const Value: TAlphaColor);
procedure SetHeaderFontColor(const Value: TAlphaColor);
public
constructor Create(AListView: TksVirtualListView);
destructor Destroy; override;
procedure Assign(ASource: TPersistent); override;
published
property Background: TAlphaColor read FBackground write SetBackground default claWhite;
property HeaderColor: TAlphaColor read FHeaderColor write SetHeaderColor default claNull;
property HeaderFontColor: TAlphaColor read FHeaderFontColor write SetHeaderFontColor default claNull;
property SeparatorColor: TAlphaColor read FSeparatorColor write SetSeparatorBackground default claDarkgray;
property ItemBackground: TAlphaColor read FItemBackground write SetItemBackground default claWhite;
property SelectedColor: TAlphaColor read FSelectedColor write SetSelectedColor default C_VLIST_DEFAULT_SELECTED_COLOR;
property SelectedFontColor: TAlphaColor read FSelectedFontColor write SetSelectedFontColor default claNull;
end;
TksVListActionButton = class
strict private
FWidth: integer;
FIcon: TBitmap;
FTextColor: TAlphaColor;
FColor: TAlphaColor;
FText: string;
FIsDeleteButton: Boolean;
FAccessory: TksAccessoryType;
private
FButtonRect: TRectF;
procedure SetAccessory(const Value: TksAccessoryType);
private
procedure SetTextColor(const Value: TAlphaColor);
public
constructor Create(AIsDelete: Boolean);
destructor Destroy; override;
procedure DrawToCanvas(ACanvas: TCanvas; ARect: TRectF);
property Accessory: TksAccessoryType read FAccessory write SetAccessory;
property Text: string read FText write FText;
property TextColor: TAlphaColor read FTextColor write SetTextColor default claWhite;
property Color: TAlphaColor read FColor write FColor;
property Width: integer read FWidth write FWidth default 80;
property IsDeleteButton: Boolean read FIsDeleteButton write FIsDeleteButton;
end;
TksVListActionButtons = class(TObjectList<TksVListActionButton>)
strict private
FAlignment: TksVListActionButtonAlign;
private
function GetTotalWidth: integer;
public
constructor Create(AOwner: TksVListItem);
procedure DrawToCanvas(ACanvas: TCanvas; ARect: TRectF);
function AddButton(AText: string; AColor, ATextColor: TAlphaColor;
const AIcon: TksAccessoryType = atNone; const AWidth: integer = 60): TksVListActionButton;
function ButtonAtXY(x, y: single): TksVListActionButton;
property Alignment: TksVListActionButtonAlign read FAlignment
write FAlignment;
property TotalWidth: integer read GetTotalWidth;
end;
TksVListCheckBoxOptions = class(TPersistent)
private
[weak]
FOwner: TksVirtualListView;
FVisible: Boolean;
FMode: TksSelectionType;
FAlignment: TksVListCheckBoxAlign;
procedure SetAlignment(const Value: TksVListCheckBoxAlign);
procedure SetMode(const Value: TksSelectionType);
procedure SetVisible(const Value: Boolean);
procedure Changed;
public
constructor Create(AOwner: TksVirtualListView); virtual;
published
property Visible: Boolean read FVisible write SetVisible default False;
property Mode: TksSelectionType read FMode write SetMode
default ksSingleSelect;
property Alignment: TksVListCheckBoxAlign read FAlignment write SetAlignment
default ksCbRightAlign;
end;
TksVListSelectionOptions = class(TPersistent)
private
[weak]
FOwner: TksVirtualListView;
FKeepSelection: Boolean;
FSelectionType: TksSelectionType;
procedure SetKeepSelection(const Value: Boolean);
procedure SetSelectionType(const Value: TksSelectionType);
procedure Changed;
public
constructor Create(AOwner: TksVirtualListView); virtual;
published
property KeepSelection: Boolean read FKeepSelection write SetKeepSelection
default False;
property SelectionType: TksSelectionType read FSelectionType
write SetSelectionType default ksSingleSelect;
end;
TksVListItemBaseObject = class(TPersistent)
private
[weak]
FOwner: TksVListItem;
FVertAlign: TVerticalAlignment;
FHorzAlign: TAlignment;
FLeft: single;
FTop: single;
FWidth: single;
FHeight: single;
FVisible: Boolean;
FUsePercentForXPos: Boolean;
FOnChange: TNotifyEvent;
procedure SetVertAlign(const Value: TVerticalAlignment);
procedure SetHeight(const Value: single);
procedure SetLeft(const Value: single);
procedure SetTop(const Value: single);
procedure SetWidth(const Value: single);
procedure SetHorzAlign(const Value: TAlignment);
procedure SetVisible(const Value: Boolean);
procedure SetUsePercentForXPos(const Value: Boolean);
protected
function CalcObjectRect(AItemRect: TRectF): TRectF; virtual;
procedure Changed; virtual;
procedure DrawToCanvas(ACanvas: TCanvas; AItemRect: TRectF); virtual;
public
constructor Create(AItem: TksVListItem); virtual;
property Left: single read FLeft write SetLeft;
property Top: single read FTop write SetTop;
property Width: single read FWidth write SetWidth;
property Height: single read FHeight write SetHeight;
property HorzAlign: TAlignment read FHorzAlign write SetHorzAlign;
property VertAlign: TVerticalAlignment read FVertAlign write SetVertAlign default TVerticalAlignment.taVerticalCenter;
property UsePercentForXPos: Boolean read FUsePercentForXPos write SetUsePercentForXPos default False;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
published
property Visible: Boolean read FVisible write SetVisible default True;
end;
TksVListItemTextObject = class(TksVListItemBaseObject)
private
FCached: TBitmap;
FCachedSize: TRectF;
FTextSettings: TTextSettings;
FText: string;
FMaxWidth: integer;
procedure SetText(const Value: string);
function GetFont: TFont;
procedure SetFont(const Value: TFont);
procedure SetMaxWidth(const Value: integer);
protected
procedure Changed; override;
function ActualTextWidth: single;
function CalculateSize: TRectF;
procedure BeforeRenderText(ACanvas: TCanvas; ARect: TRectF); virtual;
procedure CacheTextToBmp(ASelected: Boolean); virtual;
public
constructor Create(AItem: TksVListItem); override;
destructor Destroy; override;
procedure ClearCache;
procedure DrawToCanvas(ACanvas: TCanvas; AItemRect: TRectF); override;
property Text: string read FText write SetText;
property TextSettings: TTextSettings read FTextSettings;
property Font: TFont read GetFont write SetFont;
property MaxWidth: integer read FMaxWidth write SetMaxWidth default 0;
end;
TksVListItemShapeObject = class(TksVListItemBaseObject)
private
FStroke: TStrokeBrush;
FFill: TBrush;
FCornerRadius: single;
public
constructor Create(AItem: TksVListItem); override;
destructor Destroy; override;
procedure DrawToCanvas(ACanvas: TCanvas; AItemRect: TRectF); override;
property CornerRadius: single read FCornerRadius write FCornerRadius;
end;
TksVListItemBubbleObject = class(TksVListItemTextObject)
private
FColor: TAlphaColor;
FTextColor: TAlphaColor;
FSender: string;
procedure SetColor(const Value: TAlphaColor);
procedure SetTextColor(const Value: TAlphaColor);
protected
procedure BeforeRenderText(ACanvas: TCanvas; ARect: TRectF); override;
procedure CacheTextToBmp(ASelected: Boolean); override;
public
constructor Create(AItem: TksVListItem); override;
property Color: TAlphaColor read FColor write SetColor;
property TextColor: TAlphaColor read FTextColor write SetTextColor;
property Sender: string read FSender write FSender;
end;
TksVListItemImageObject = class(TksVListItemBaseObject)
private
FBitmap: TBitmap;
FOwnsImage: Boolean;
FBackground: TAlphaColor;
FOpacity: single;
function GetIsEmpty: Boolean;
procedure SetBackground(const Value: TAlphaColor);
procedure SetOpacity(const Value: single);
protected
procedure SetBitmap(const Value: TBitmap); virtual;
public
constructor Create(AItem: TksVListItem); override;
destructor Destroy; override;
procedure DrawToCanvas(ACanvas: TCanvas; AItemRect: TRectF); override;
procedure SetProperties(ABitmap: TBitmap; AOpaqueColor, ABackgroundColor: TAlphaColor);
procedure SetOpaqueColor(AColor: TAlphaColor);
property IsEmpty: Boolean read GetIsEmpty;
property Bitmap: TBitmap read FBitmap write SetBitmap;
property Background: TAlphaColor read FBackground write SetBackground;
property Opacity: single read FOpacity write SetOpacity;
end;
TksVListItemAccessoryObject = class(TksVListItemImageObject)
private
FAccessoryType: TksAccessoryType;
FColor: TAlphaColor;
procedure RedrawAccessory;
procedure SetAccessoryType(const Value: TksAccessoryType);
procedure SetColor(const Value: TAlphaColor);
protected
procedure SetBitmap(const Value: TBitmap); override;
public
constructor Create(AItem: TksVListItem); override;
procedure DrawToCanvas(ACanvas: TCanvas; AItemRect: TRectF); override;
property AccessoryType: TksAccessoryType read FAccessoryType write SetAccessoryType default atNone;
property Color: TAlphaColor read FColor write SetColor default claNull;
end;
TksVListItem = class // (TFmxObject)
private
[weak]FOwner: TksVListItemList;
// FCachedRow: TBitmap;
FBackground: TAlphaColor;
FCanSelect: Boolean;
FChecked: Boolean;
FHeight: integer;
FSelected: Boolean;
FItemRect: TRectF;
FImage: TksVListItemImageObject;
FTitle: TksVListItemTextObject;
FSubTitle: TksVListItemTextObject;
FDetail: TksVListItemTextObject;
FAccessory: TksVListItemAccessoryObject;
FActionButtons: TksVListActionButtons;
FOffset: integer;
FChanged: Boolean;
FUpdateCount: integer;
FAbsoluteIndex: integer;
FIndex: integer;
FSwipeCalc: TAniCalculations;
FDeleteCalc: TAniCalculations;
FTagInt: integer;
FPurpose: TksVListItemPurpose;
FState: TksVListItemState;
FIconSize: integer;
// events
FOnClick: TNotifyEvent;
FObjects: TObjectList<TksVListItemBaseObject>;
FTagStr: string;
FCheckBoxVisible: Boolean;
// procedure CacheRowToBitmap;
procedure Changed;
procedure SetAccessory(const Value: TksVListItemAccessoryObject);
procedure SetDetail(const Value: TksVListItemTextObject);
procedure SetImage(const Value: TksVListItemImageObject);
procedure SetTitle(const Value: TksVListItemTextObject);
procedure SetHeight(const Value: integer);
procedure UpdateStandardObjectPositions;
procedure SetSelected(const Value: Boolean);
procedure SelectItem(ADeselectAfter: integer);
procedure SetChecked(const Value: Boolean);
procedure SetSubTitle(const Value: TksVListItemTextObject);
procedure SetBackground(const Value: TAlphaColor);
// procedure SetOffset(const Value: integer);
procedure DeleteItem;
procedure SlideOut(ADirection: TksVListSwipeDirection);
procedure SlideIn;
procedure SwipeCalcChange(Sender: TObject);
procedure DeleteCalcChange(Sender: TObject);
function CreateAniCalc(AOnChange: TNotifyEvent): TAniCalculations;
procedure DoClicked;
procedure SetPurpose(const Value: TksVListItemPurpose);
procedure SetOffset(const Value: integer);
procedure SetCanSelect(const Value: Boolean);
procedure SetIconSize(const Value: integer);
public
constructor Create(Owner: TksVListItemList); virtual;
destructor Destroy; override;
function IsItemVisible(AViewPort: TRectF): Boolean;
function AddText(x, y: single; AText: string): TksVListItemTextObject; overload;
function AddText(x, y, AWidth: single; AText: string): TksVListItemTextObject; overload;
function AddImage(x, y, AWidth, AHeight: single; ABitmap: TBitmap): TksVListItemImageObject;
function DrawRect(x, y, AWidth, AHeight, ACornerRadius: single; AStroke, AFill: TAlphaColor): TksVListItemShapeObject;
function AddChatBubble(AText, ASender: string; ALeftAlign: Boolean): TksVListItemBubbleObject;
//procedure BeginUpdate;
///procedure EndUpdate;
procedure CacheItem;
procedure ClearCache;
// procedure ShowActionButtons(AAlign: TksVListActionButtonAlign);
// procedure HideActionButtons;
procedure DrawToCanvas(ACanvas: TCanvas; AScrollPos: single; ADrawToCache: Boolean);
property AbsoluteIndex: integer read FAbsoluteIndex;
property Index: integer read FIndex;
property Background: TAlphaColor read FBackground write SetBackground default claNull;
property CanSelect: Boolean read FCanSelect write SetCanSelect default True;
property Checked: Boolean read FChecked write SetChecked default False;
property Height: integer read FHeight write SetHeight;
property Image: TksVListItemImageObject read FImage write SetImage;
property Title: TksVListItemTextObject read FTitle write SetTitle;
property SubTitle: TksVListItemTextObject read FSubTitle write SetSubTitle;
property Detail: TksVListItemTextObject read FDetail write SetDetail;
property Accessory: TksVListItemAccessoryObject read FAccessory
write SetAccessory;
property ItemRect: TRectF read FItemRect;
property Selected: Boolean read FSelected write SetSelected;
property Purpose: TksVListItemPurpose read FPurpose write SetPurpose
default None;
property TagInt: integer read FTagInt write FTagInt default 0;
property TagStr: string read FTagStr write FTagStr;
property State: TksVListItemState read FState write FState default Normal;
property Offset: integer read FOffset write SetOffset default 0;
property IconSize: integer read FIconSize write SetIconSize default 28;
// events...
property OnClick: TNotifyEvent read FOnClick write FOnClick;
property CheckBoxVisible: Boolean read FCheckBoxVisible write FCheckBoxVisible default True;
end;
TksVListItemList = class
private
[weak]FOwner: TksVirtualListView;
FItems: TObjectList<TksVListItem>;
procedure UpdateItemRects;
procedure Changed;
function GetItem(index: integer): TksVListItem;
function GetCount: integer;
//procedure UpdateItemHeaders;
public
constructor Create(AOwner: TksVirtualListView); virtual;
destructor Destroy; override;
function Add: TksVListItem; overload;
function Add(ATitle, ASubTitle, ADetail: string; const AAccessory: TksAccessoryType = atNone): TksVListItem; overload;
function AddHeader(AText: string): TksVListItem;
function AddChatBubble(AText, ASender: string; AColor, ATextColor: TAlphaColor; ALeftAlign: Boolean): TksVListItem;
function Insert(AIndex: integer; ATitle, ASubTitle, ADetail: string; const AAccessory: TksAccessoryType = atNone): TksVListItem;
function ItemAtPos(x, y: single): TksVListItem;
procedure ClearCachesBeforeIndex(AIndex: integer);
procedure ClearCachesAfterIndex(AIndex: integer);
procedure Clear;
procedure Delete(AIndex: integer; AAnimate: Boolean);
property Count: integer read GetCount;
property Items[index: integer]: TksVListItem read GetItem; default;
end;
TksVListPullToRefreshOptions = class(TPersistent)
private
FPullText: string;
FReleaseText: string;
FEnabled: Boolean;
public
constructor Create; virtual;
published
property PullText: string read FPullText write FPullText;
property ReleaseText: string read FReleaseText write FReleaseText;
property Enabled: Boolean read FEnabled write FEnabled default False;
end;
TksVListDeleteButton = class(TPersistent)
private
FEnabled: Boolean;
FText: string;
FColor: TAlphaColor;
FTextColor: TAlphaColor;
FWidth: integer;
FShowImage: Boolean;
public
constructor Create; virtual;
published
property Color: TAlphaColor read FColor write FColor default claRed;
property TextColor: TAlphaColor read FTextColor write FTextColor default claWhite;
property Enabled: Boolean read FEnabled write FEnabled default False;
property Text: string read FText write FText;
property ShowImage: Boolean read FShowImage write FShowImage default True;
property Width: integer read FWidth write FWidth default 60;
end;
TksNoItemsText = class(TPersistent)
private
[weak]FOwner: TksVirtualListView;
FEnabled: Boolean;
FTextColor: TAlphaColor;
FFont: TFont;
FText: string;
procedure Changed;
procedure SetEnabled(const Value: Boolean);
procedure SetFont(const Value: TFont);
procedure SetText(const Value: string);
public
constructor Create(AListView: TksVirtualListView); virtual;
destructor Destroy; override;
procedure DrawToCanvas(ACanvas: TCanvas; ARect: TRectF);
published
property Enabled: Boolean read FEnabled write SetEnabled default False;
property Text: string read FText write SetText;
property Font: TFont read FFont write SetFont;
property TextColor: TAlphaColor read FTextColor write FTextColor default claSilver;
end;
TksScrollBar = class(TScrollBar)
public
constructor Create(AOwner: TComponent); override;
end;
[ComponentPlatformsAttribute(pidWin32 or pidWin64 or
{$IFDEF XE8_OR_NEWER} pidiOSDevice32 or pidiOSDevice64
{$ELSE} pidiOSDevice {$ENDIF} or pidiOSSimulator or pidAndroid)]
TksVirtualListView = class(TksControl)
private
FAniCalc: TksAniCalc;
FAppearence: TksVirtualListViewAppearence;
FCheckBoxOptions: TksVListCheckBoxOptions;
FItems: TksVListItemList;
FScrollPos: integer;
FScrollBar: TksScrollBar;
FOnScroll: TNotifyEvent;
FTotalItemHeight: single;
FMouseDownPos: TPointF;
FMaxScrollPos: integer;
FUpdateCount: integer;
FMouseDownTime: TDateTime;
FSelectionOptions: TksVListSelectionOptions;
FPendingRefresh: Boolean;
FOnPullRefresh: TNotifyEvent;
FTimerService: IFMXTimerService;
FLongTapTimer: TFmxHandle;
FDeletedItemCleanup: TFmxHandle;
FMousePt: TPointF;
FMouseDownItem: TksVListItem;
FEventService: IFMXApplicationEventService;
// FActionButtons: TksVListActionButtons;
FPullToRefresh: TksVListPullToRefreshOptions;
FDeleteButton: TksVListDeleteButton;
FItemHeight: integer;
FItemIndex: integer;
// events...
FOnItemClick: TksVListItemClickEvent;
FOnItemLongTap: TksVListItemLongTapEvent;
FOnItemSwipe: TksVListItemSwipeEvent;
FOnItemDeleted: TNotifyEvent;
FCanDeleteItem: TksVListDeletingItemEvent;
FHeaderHeight: integer;
FNoItemsText: TksNoItemsText;
FOnActionButtonClick: TksItemActionButtonClickEvent;
//FFont: TFont;
procedure SetScrollPos(const Value: integer);
procedure AniCalcChange(Sender: TObject);
procedure AniCalcStart(Sender: TObject);
procedure AniCalcStop(Sender: TObject);
function GetViewport: TRectF;
procedure CacheItems(AStartIndex, AEndIndex: integer);
// procedure UpdateOverlayPosition;
procedure SetCheckBoxOptions(const Value: TksVListCheckBoxOptions);
procedure ScrollBarChanged(Sender: TObject);
function CreateTimer(AInterval: integer; AProc: TTimerProc): TFmxHandle;
procedure DoItemLongTap(AItem: TksVListItem);
procedure LongTapTimerProc;
procedure KillTimer(var ATimer: TFmxHandle);
procedure DoItemClicked(AItem: TksVListItem; ACallClickEvent: Boolean);
function HandleAppEvent(AAppEvent: TApplicationEvent;
AContext: TObject): Boolean;
procedure ResetItemOffsets(AIgnore: TksVListItem);
procedure SetItemHeight(const Value: integer);
procedure SetHeaderHeight(const Value: integer);
procedure SetAppearence(const Value: TksVirtualListViewAppearence);
procedure SetItemIndex(const Value: integer);
procedure SetNoItemsText(const Value: TksNoItemsText);
//procedure SetFont(const Value: TFont);
protected
procedure Paint; override;
procedure DrawPullToRefresh;
procedure Resize; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; x, y: single); override;
procedure MouseMove(Shift: TShiftState; x, y: single); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
x, y: single); override;
procedure MouseWheel(Shift: TShiftState; WheelDelta: integer;
var Handled: Boolean); override;
procedure DoMouseLeave; override;
procedure DoItemDeleted;
// procedure Tap(const Point:TPointF); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure DoCleanupDeletedItems;
procedure DoItemSwiped(AItem: TksVListItem;
ASwipeDirection: TksVListSwipeDirection);
procedure BeginUpdate; override;
procedure EndUpdate; override;
procedure ClearItems;
procedure Invalidate;
procedure DeselectAll;
procedure ScrollTo(const Value: integer);
procedure ScrollToBottom(AAnimated: Boolean);
procedure UpdateScrollLimmits;
procedure UncheckAll;
procedure SwipeItem(AItem: TksVListItem; ASwipeDirection: TksVListSwipeDirection);
procedure SelectItem(AItem: TksVListItem);
property Items: TksVListItemList read FItems;
property ScrollPos: integer read FScrollPos write SetScrollPos;
property Viewport: TRectF read GetViewport;
property ItemIndex: integer read FItemIndex write SetItemIndex;
published
property Align;
property Appearence: TksVirtualListViewAppearence read FAppearence write SetAppearence;
property CheckBoxes: TksVListCheckBoxOptions read FCheckBoxOptions write SetCheckBoxOptions;
property DeleteButton: TksVListDeleteButton read FDeleteButton write FDeleteButton;
//property Font: TFont read FFont write SetFont;
property Height;
property ItemHeight: integer read FItemHeight write SetItemHeight default C_VLIST_ITEM_DEFAULT_HEIGHT;
property HeaderHeight: integer read FHeaderHeight write SetHeaderHeight default C_VLIST_HEADER_DEFAULT_HEIGHT;
property NoItemsText: TksNoItemsText read FNoItemsText write SetNoItemsText;
property Position;
property PullToRefresh: TksVListPullToRefreshOptions read FPullToRefresh write FPullToRefresh;
property SelectionOptions: TksVListSelectionOptions read FSelectionOptions write FSelectionOptions;
property Size;
property Width;
property CanDeleteItem: TksVListDeletingItemEvent read FCanDeleteItem write FCanDeleteItem;
property OnItemLongTap: TksVListItemLongTapEvent read FOnItemLongTap write FOnItemLongTap;
property OnItemClick: TksVListItemClickEvent read FOnItemClick write FOnItemClick;
property OnItemSwipe: TksVListItemSwipeEvent read FOnItemSwipe write FOnItemSwipe;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnScroll: TNotifyEvent read FOnScroll write FOnScroll;
property OnPullRefresh: TNotifyEvent read FOnPullRefresh write FOnPullRefresh;
property OnItemDeleted: TNotifyEvent read FOnItemDeleted write FOnItemDeleted;
property OnActionButtonClick: TksItemActionButtonClickEvent read FOnActionButtonClick write FOnActionButtonClick;
end;
// {$R *.dcr}
procedure Register;
implementation
uses SysUtils, Math, System.Math.Vectors, ksCommon,
DateUtils, FMX.Forms, FMX.Ani, FMX.Dialogs;
var
AAccessories: TksTableViewAccessoryImageList;
procedure Register;
begin
RegisterComponents('Kernow Software FMX', [TksVirtualListView]);
end;
{ TksVListItem }
function TksVListItem.AddText(x, y: single; AText: string): TksVListItemTextObject;
begin
Result := AddText(x, y, 0, AText);
end;
function TksVListItem.AddText(x, y, AWidth: single; AText: string): TksVListItemTextObject;
begin
Result := TksVListItemTextObject.Create(Self);
Result.FLeft := x;
Result.FTop := y;
Result.Width := AWidth;
Result.Text := AText;
FObjects.Add(Result);
end;
function TksVListItem.AddImage(x, y, AWidth, AHeight: single; ABitmap: TBitmap): TksVListItemImageObject;
begin
Result := TksVListItemImageObject.Create(Self);
Result.FLeft := x;
Result.FTop := y;
Result.Width := AWidth;
Result.Height := AHeight;
Result.Bitmap := ABitmap;
FObjects.Add(Result);
end;
{
procedure TksVListItem.BeginUpdate;
begin
Inc(FUpdateCount);
end; }
procedure TksVListItem.CacheItem;
begin
UpdateStandardObjectPositions;
FTitle.CacheTextToBmp(FSelected);
FSubTitle.CacheTextToBmp(FSelected);
FDetail.CacheTextToBmp(FSelected);
{case FSelected of
True: FAccessory.TintColor := FOwner.FOwner.Appearence.SelectedFontColor;
False: FAccessory.TintColor := claNull;
end; }
end;
procedure TksVListItem.ClearCache;
begin
FTitle.ClearCache;
FSubTitle.ClearCache;
FDetail.ClearCache;
end;
procedure TksVListItem.Changed;
begin
if FState <> Normal then
Exit;
FChanged := True;
FOwner.Changed;
end;
constructor TksVListItem.Create(Owner: TksVListItemList);
begin
inherited Create; // (nil);
FActionButtons := TksVListActionButtons.Create(Self);
FObjects := TObjectList<TksVListItemBaseObject>.Create(True);
FCheckBoxVisible := True;
FOffset := 0;
FUpdateCount := 0;
FAbsoluteIndex := 0;
FIndex := 0;
FTagInt := 0;
FTagStr := '';
FIconSize := 28;
FOwner := Owner;
FChecked := False;
FState := Normal;
FCanSelect := True;
FBackground := claNull;
//BeginUpdate;
try
FHeight := FOwner.FOwner.FItemHeight;
FImage := TksVListItemImageObject.Create(Self);
FImage.Width := 32;
FImage.Height := 32;
FImage.VertAlign := TVerticalAlignment.taVerticalCenter;
FTitle := TksVListItemTextObject.Create(Self);
FTitle.VertAlign := TVerticalAlignment.taVerticalCenter;
FSubTitle := TksVListItemTextObject.Create(Self);
FSubTitle.VertAlign := TVerticalAlignment.taVerticalCenter;
FSubTitle.TextSettings.FontColor := claDimgray;
FSubTitle.Font.Size := 14;
FDetail := TksVListItemTextObject.Create(Self);
FDetail.VertAlign := TVerticalAlignment.taVerticalCenter;
FDetail.HorzAlign := TAlignment.taRightJustify;
//FDetail.TextSettings.HorzAlign := TTextAlign.Trailing;
{$IFDEF IOS}
FDetail.TextSettings.FontColor := claDodgerblue;
{$ELSE}
FDetail.TextSettings.FontColor := claGray;
{$ENDIF}
FDetail.Font.Size := 14;
FAccessory := TksVListItemAccessoryObject.Create(Self);
FAccessory.VertAlign := TVerticalAlignment.taVerticalCenter;
FAccessory.HorzAlign := TAlignment.taRightJustify;
finally
//EndUpdate;
end;
FChanged := True;
end;
function TksVListItem.CreateAniCalc(AOnChange: TNotifyEvent): TAniCalculations;
begin
Result := TAniCalculations.Create(nil);
Result.ViewportPositionF := PointF(FOffset, 0);
Result.Animation := True;
Result.Averaging := True;
Result.Interval := 8;
Result.OnChanged := AOnChange;
end;
procedure TksVListItem.DeleteItem;
var
Targets: array of TAniCalculations.TTarget;
begin
FState := Deleting;
FreeAndNil(FDeleteCalc);
FDeleteCalc := CreateAniCalc(DeleteCalcChange);
FDeleteCalc.ViewportPositionF := PointF(0, FHeight);
SetLength(Targets, 1);
Targets[0].TargetType := TAniCalculations.TTargetType.Other;
Targets[0].Point := TPointD.Create(0, 0);
FDeleteCalc.SetTargets(Targets);
end;
destructor TksVListItem.Destroy;
begin
FreeAndNil(FSwipeCalc);
FreeAndNil(FObjects);
FreeAndNil(FTitle);
FreeAndNil(FSubTitle);
FreeAndNil(FDetail);
FreeAndNil(FImage);
FreeAndNil(FAccessory);
FreeAndNil(FActionButtons);
inherited;
end;
procedure TksVListItem.DoClicked;
begin
if Assigned(FOnClick) then
FOnClick(Self);
end;
procedure TksVListItem.SelectItem(ADeselectAfter: integer);
var
AStart: TDateTime;
begin
if (FPurpose <> None) or (FCanSelect = False) then
Exit;
//Selected := True;
FOwner.FOwner.ItemIndex := FIndex;
Application.ProcessMessages;
//FOwner.FOwner.Repaint;
if ADeselectAfter > 0 then
begin
AStart := Now;
while MilliSecondsBetween(AStart, Now) < ADeselectAfter do
begin
Sleep(10);
//Application.ProcessMessages;
end;
Selected := False;
end;
end;
procedure TksVListItem.SetAccessory(const Value: TksVListItemAccessoryObject);
begin
FAccessory.Assign(Value);
Changed;
end;
procedure TksVListItem.SetBackground(const Value: TAlphaColor);
begin
if FBackground <> Value then
begin
FBackground := Value;
Changed;
end;
end;
procedure TksVListItem.SetCanSelect(const Value: Boolean);
begin
if FCanSelect <> Value then
begin
FCanSelect := Value;
Changed;
end;
end;
procedure TksVListItem.SetChecked(const Value: Boolean);
begin
if FChecked <> Value then
begin
FChecked := Value;
Changed;
end;
end;
procedure TksVListItem.SetDetail(const Value: TksVListItemTextObject);
begin
FDetail.Assign(Value);
Changed;
end;
procedure TksVListItem.SetHeight(const Value: integer);
begin
if FHeight <> Value then
begin
FHeight := Value;
Changed;
if (FHeight = 0) and (FState = Deleting) then
FState := Deleted;
end;
end;
procedure TksVListItem.SetIconSize(const Value: integer);
begin
if FIconSize <> Value then
begin
FIconSize := Value;
Changed;
end;
end;
procedure TksVListItem.SetImage(const Value: TksVListItemImageObject);
begin
FImage.Assign(Value);
// FImage := Value.CreateThumbnail(Round(32 * GetScreenScale), Round(32 * GetScreenScale));
Changed;
end;
procedure TksVListItem.SetOffset(const Value: integer);
begin
if FOffset <> Value then
begin
FOffset := Value;
if (FOffset = FActionButtons.TotalWidth) or (FOffset = 0) then
FState := Normal;
end;
end;
procedure TksVListItem.SetPurpose(const Value: TksVListItemPurpose);
begin
if FPurpose <> Value then
begin
FPurpose := Value;
if FPurpose = Header then
FHeight := FOwner.FOwner.HeaderHeight
else
FHeight := FOwner.FOwner.ItemHeight;
Changed;
end;
end;
procedure TksVListItem.SetSelected(const Value: Boolean);
begin
if FSelected <> Value then
begin
if FCanSelect = False then
FSelected := False
else
FSelected := Value;
ClearCache;
CacheItem;
Changed;
end;
end;
procedure TksVListItem.SetSubTitle(const Value: TksVListItemTextObject);
begin
FSubTitle.Assign(Value);
Changed;
end;
procedure TksVListItem.SetTitle(const Value: TksVListItemTextObject);
begin
FTitle.Assign(Value);
Changed;
end;
procedure TksVListItem.SlideOut(ADirection: TksVListSwipeDirection);
var
Targets: array of TAniCalculations.TTarget;
begin
FState := Sliding;
FSelected := False;
FreeAndNil(FSwipeCalc);
FSwipeCalc := CreateAniCalc(SwipeCalcChange);
SetLength(Targets, 1);
Targets[0].TargetType := TAniCalculations.TTargetType.Other;
if ADirection = ksSwipeFromLeft then
Targets[0].Point := TPointD.Create(Min(FOffset + FActionButtons.TotalWidth,
FActionButtons.TotalWidth), 0)
else
Targets[0].Point := TPointD.Create(Max(FOffset - FActionButtons.TotalWidth,
0 - FActionButtons.TotalWidth), 0);
FSwipeCalc.SetTargets(Targets);
end;
procedure TksVListItem.SlideIn;
var
Targets: array of TAniCalculations.TTarget;
begin
FState := Sliding;
FreeAndNil(FSwipeCalc);
FSwipeCalc := CreateAniCalc(SwipeCalcChange);
SetLength(Targets, 1);
Targets[0].TargetType := TAniCalculations.TTargetType.Other;
Targets[0].Point := TPointD.Create(0, 0);
FSwipeCalc.SetTargets(Targets);
end;
procedure TksVListItem.SwipeCalcChange(Sender: TObject);
begin
Offset := Round(FSwipeCalc.ViewportPosition.x);
FOwner.FOwner.Invalidate;
end;
procedure TksVListItem.DeleteCalcChange(Sender: TObject);