/
ScrollableBase.cs
executable file
·2208 lines (1972 loc) · 85.4 KB
/
ScrollableBase.cs
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
/* Copyright (c) 2021 Samsung Electronics Co., Ltd.
*
* 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.
*
*/
using System;
using Tizen.NUI.BaseComponents;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Tizen.NUI.Accessibility;
namespace Tizen.NUI.Components
{
/// <summary>
/// ScrollEventArgs is a class to record scroll event arguments which will sent to user.
/// </summary>
/// <since_tizen> 8 </since_tizen>
[global::System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1001: Types that own disposable fields should be disposable.", Justification = "Scroll event is temporarily used for notifying scroll position update, so position will not be disposed during the event processing.")]
public class ScrollEventArgs : EventArgs
{
// Position class is derived class of Disposable class and they will be implicitly disposed by DisposeQueue,
// so that there will be no memory leak.
private Position position;
private Position scrollPosition;
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="position">Current container position</param>
/// <since_tizen> 8 </since_tizen>
public ScrollEventArgs(Position position)
{
this.position = position;
this.scrollPosition = new Position(-position);
}
/// <summary>
/// Current position of ContentContainer.
/// </summary>
/// <since_tizen> 8 </since_tizen>
public Position Position
{
get
{
return position;
}
}
/// <summary>
/// Current scroll position of scrollableBase pan.
/// This is the position in the opposite direction to the current position of ContentContainer.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public Position ScrollPosition
{
get
{
return scrollPosition;
}
}
}
/// <summary>
/// ScrollOutofBoundEventArgs is to record scroll out-of-bound event arguments which will be sent to user.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public class ScrollOutOfBoundEventArgs : EventArgs
{
/// <summary>
/// The direction to be touched.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public enum Direction
{
/// <summary>
/// Upwards.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
Up,
/// <summary>
/// Downwards.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
Down,
/// <summary>
/// Left bound.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
Left,
/// <summary>
/// Right bound.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
Right,
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="direction">Current pan direction</param>
/// <param name="displacement">Current total displacement</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public ScrollOutOfBoundEventArgs(Direction direction, float displacement)
{
PanDirection = direction;
Displacement = displacement;
}
/// <summary>
/// Current pan direction of ContentContainer.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public Direction PanDirection
{
get;
}
/// <summary>
/// Current total displacement of ContentContainer.
/// if its value is greater than 0, it is at the top/left;
/// if less than 0, it is at the bottom/right.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public float Displacement
{
get;
}
}
/// <summary>
/// This class provides a View that can scroll a single View with a layout. This View can be a nest of Views.
/// </summary>
/// <since_tizen> 8 </since_tizen>
public partial class ScrollableBase : Control
{
static bool LayoutDebugScrollableBase = false; // Debug flag
static bool focusDebugScrollableBase = false; // Debug flag
private Direction mScrollingDirection = Direction.Vertical;
private bool mScrollEnabled = true;
private int mScrollDuration = 125;
private int mPageWidth = 0;
private float mPageFlickThreshold = 0.4f;
private float mScrollingEventThreshold = 0.001f;
private bool fadeScrollbar = true;
private class ScrollableBaseCustomLayout : AbsoluteLayout
{
protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
{
MeasuredSize.StateType childWidthState = MeasuredSize.StateType.MeasuredSizeOK;
MeasuredSize.StateType childHeightState = MeasuredSize.StateType.MeasuredSizeOK;
Direction scrollingDirection = Direction.Vertical;
ScrollableBase scrollableBase = this.Owner as ScrollableBase;
if (scrollableBase != null)
{
scrollingDirection = scrollableBase.ScrollingDirection;
}
float totalWidth = 0.0f;
float totalHeight = 0.0f;
// measure child, should be a single scrolling child
foreach (LayoutItem childLayout in LayoutChildren)
{
if (childLayout != null && childLayout.Owner.Name == "ContentContainer")
{
// Get size of child
// Use an Unspecified MeasureSpecification mode so scrolling child is not restricted to it's parents size in Height (for vertical scrolling)
// or Width for horizontal scrolling
if (scrollingDirection == Direction.Vertical)
{
MeasureSpecification unrestrictedMeasureSpec = new MeasureSpecification(heightMeasureSpec.Size, MeasureSpecification.ModeType.Unspecified);
MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), unrestrictedMeasureSpec, new LayoutLength(0)); // Height unrestricted by parent
}
else
{
MeasureSpecification unrestrictedMeasureSpec = new MeasureSpecification(widthMeasureSpec.Size, MeasureSpecification.ModeType.Unspecified);
MeasureChildWithMargins(childLayout, unrestrictedMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0)); // Width unrestricted by parent
}
totalWidth = (childLayout.MeasuredWidth.Size + (childLayout.Padding.Start + childLayout.Padding.End)).AsDecimal();
totalHeight = (childLayout.MeasuredHeight.Size + (childLayout.Padding.Top + childLayout.Padding.Bottom)).AsDecimal();
if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
{
childWidthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
}
if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
{
childHeightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
}
}
}
MeasuredSize widthSizeAndState = ResolveSizeAndState(new LayoutLength(totalWidth), widthMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
MeasuredSize heightSizeAndState = ResolveSizeAndState(new LayoutLength(totalHeight), heightMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
totalWidth = widthSizeAndState.Size.AsDecimal();
totalHeight = heightSizeAndState.Size.AsDecimal();
// Ensure layout respects it's given minimum size
totalWidth = Math.Max(totalWidth, SuggestedMinimumWidth.AsDecimal());
totalHeight = Math.Max(totalHeight, SuggestedMinimumHeight.AsDecimal());
widthSizeAndState.State = childWidthState;
heightSizeAndState.State = childHeightState;
SetMeasuredDimensions(ResolveSizeAndState(new LayoutLength(totalWidth), widthMeasureSpec, childWidthState),
ResolveSizeAndState(new LayoutLength(totalHeight), heightMeasureSpec, childHeightState));
// Size of ScrollableBase is changed. Change Page width too.
if (scrollableBase != null)
{
scrollableBase.mPageWidth = (int)MeasuredWidth.Size.AsRoundedValue();
scrollableBase.OnScrollingChildRelayout(null, null);
}
}
} // ScrollableBaseCustomLayout
/// <summary>
/// The direction axis to scroll.
/// </summary>
/// <since_tizen> 8 </since_tizen>
public enum Direction
{
/// <summary>
/// Horizontal axis.
/// </summary>
/// <since_tizen> 8 </since_tizen>
Horizontal,
/// <summary>
/// Vertical axis.
/// </summary>
/// <since_tizen> 8 </since_tizen>
Vertical
}
/// <summary>
/// Scrolling direction mode.
/// Default is Vertical scrolling.
/// </summary>
/// <since_tizen> 8 </since_tizen>
public Direction ScrollingDirection
{
get
{
return (Direction)GetValue(ScrollingDirectionProperty);
}
set
{
SetValue(ScrollingDirectionProperty, value);
NotifyPropertyChanged();
}
}
private Direction InternalScrollingDirection
{
get
{
return mScrollingDirection;
}
set
{
if (value != mScrollingDirection)
{
//Reset scroll position and stop scroll animation
ScrollTo(0, false);
mScrollingDirection = value;
mPanGestureDetector.ClearAngles();
mPanGestureDetector.AddDirection(value == Direction.Horizontal ?
PanGestureDetector.DirectionHorizontal : PanGestureDetector.DirectionVertical);
ContentContainer.WidthSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.MatchParent : LayoutParamPolicies.WrapContent;
ContentContainer.HeightSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.WrapContent : LayoutParamPolicies.MatchParent;
SetScrollbar();
}
}
}
/// <summary>
/// Enable or disable scrolling.
/// </summary>
/// <since_tizen> 8 </since_tizen>
public bool ScrollEnabled
{
get
{
return (bool)GetValue(ScrollEnabledProperty);
}
set
{
SetValue(ScrollEnabledProperty, value);
NotifyPropertyChanged();
}
}
private bool InternalScrollEnabled
{
get
{
return mScrollEnabled;
}
set
{
if (value != mScrollEnabled)
{
mScrollEnabled = value;
if (mScrollEnabled)
{
mPanGestureDetector.Detected += OnPanGestureDetected;
}
else
{
mPanGestureDetector.Detected -= OnPanGestureDetected;
}
}
}
}
/// <summary>
/// Gets scrollable status.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
protected override bool AccessibilityIsScrollable()
{
return true;
}
/// <summary>
/// Pages mode, enables moving to the next or return to current page depending on pan displacement.
/// Default is false.
/// </summary>
/// <since_tizen> 8 </since_tizen>
public bool SnapToPage
{
get
{
return (bool)GetValue(SnapToPageProperty);
}
set
{
SetValue(SnapToPageProperty, value);
NotifyPropertyChanged();
}
}
private bool InternalSnapToPage { set; get; } = false;
/// <summary>
/// Get current page.
/// Working property with SnapToPage property.
/// </summary>
/// <since_tizen> 8 </since_tizen>
public int CurrentPage { get; private set; } = 0;
/// <summary>
/// Duration of scroll animation.
/// Default value is 125ms.
/// </summary>
/// <since_tizen> 8 </since_tizen>
public int ScrollDuration
{
get
{
return (int)GetValue(ScrollDurationProperty);
}
set
{
SetValue(ScrollDurationProperty, value);
NotifyPropertyChanged();
}
}
private int InternalScrollDuration
{
set
{
mScrollDuration = value >= 0 ? value : mScrollDuration;
}
get
{
return mScrollDuration;
}
}
/// <summary>
/// Scroll Available area.
/// </summary>
/// <since_tizen> 8 </since_tizen>
public Vector2 ScrollAvailableArea
{
get
{
return GetValue(ScrollAvailableAreaProperty) as Vector2;
}
set
{
SetValue(ScrollAvailableAreaProperty, value);
NotifyPropertyChanged();
}
}
private Vector2 InternalScrollAvailableArea { set; get; }
/// <summary>
/// An event emitted when user starts dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
/// </summary>
/// <since_tizen> 8 </since_tizen>
public event EventHandler<ScrollEventArgs> ScrollDragStarted;
/// <summary>
/// An event emitted when user stops dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
/// </summary>
/// <since_tizen> 8 </since_tizen>
public event EventHandler<ScrollEventArgs> ScrollDragEnded;
/// <summary>
/// An event emitted when the scrolling slide animation starts, user can subscribe or unsubscribe to this event handler.<br />
/// </summary>
/// <since_tizen> 8 </since_tizen>
public event EventHandler<ScrollEventArgs> ScrollAnimationStarted;
/// <summary>
/// An event emitted when the scrolling slide animation ends, user can subscribe or unsubscribe to this event handler.<br />
/// </summary>
/// <since_tizen> 8 </since_tizen>
public event EventHandler<ScrollEventArgs> ScrollAnimationEnded;
/// <summary>
/// An event emitted when scrolling, user can subscribe or unsubscribe to this event handler.<br />
/// </summary>
/// <since_tizen> 8 </since_tizen>
public event EventHandler<ScrollEventArgs> Scrolling;
/// <summary>
/// An event emitted when scrolling out of bound, user can subscribe or unsubscribe to this event handler.<br />
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public event EventHandler<ScrollOutOfBoundEventArgs> ScrollOutOfBound;
/// <summary>
/// Scrollbar for ScrollableBase.
/// </summary>
/// <since_tizen> 8 </since_tizen>
public ScrollbarBase Scrollbar
{
get
{
return GetValue(ScrollbarProperty) as ScrollbarBase;
}
set
{
SetValue(ScrollbarProperty, value);
NotifyPropertyChanged();
}
}
private ScrollbarBase InternalScrollbar
{
get
{
return scrollBar;
}
set
{
if (scrollBar)
{
base.Remove(scrollBar);
}
scrollBar = value;
if (scrollBar != null)
{
scrollBar.Name = "ScrollBar";
base.Add(scrollBar);
if (hideScrollbar)
{
scrollBar.Hide();
}
else
{
scrollBar.Show();
}
SetScrollbar();
}
}
}
/// <summary>
/// Always hide Scrollbar.
/// </summary>
/// <since_tizen> 8 </since_tizen>
public bool HideScrollbar
{
get
{
return (bool)GetValue(HideScrollbarProperty);
}
set
{
SetValue(HideScrollbarProperty, value);
NotifyPropertyChanged();
}
}
private bool InternalHideScrollbar
{
get
{
return hideScrollbar;
}
set
{
hideScrollbar = value;
if (scrollBar)
{
if (value)
{
scrollBar.Hide();
}
else
{
scrollBar.Show();
if (fadeScrollbar)
{
scrollBar.Opacity = 1.0f;
scrollBar.FadeOut();
}
}
}
}
}
/// <summary>
/// The boolean flag for automatic fading Scrollbar.
/// Scrollbar will be faded out when scroll stay in certain position longer than the threshold.
/// Scrollbar will be faded in scroll position changes.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool FadeScrollbar
{
get => (bool)GetValue(FadeScrollbarProperty);
set => SetValue(FadeScrollbarProperty, value);
}
private bool InternalFadeScrollbar
{
get
{
return fadeScrollbar;
}
set
{
fadeScrollbar = value;
if (scrollBar != null && !hideScrollbar)
{
if (value)
{
scrollBar.FadeOut();
}
else
{
scrollBar.Opacity = 1.0f;
// Removing fadeout timer and animation.
scrollBar.FadeIn();
}
}
}
}
/// <summary>
/// Container which has content of ScrollableBase.
/// </summary>
/// <since_tizen> 8 </since_tizen>
public View ContentContainer { get; private set; }
/// <summary>
/// Set the layout on this View. Replaces any existing Layout.
/// </summary>
/// <since_tizen> 8 </since_tizen>
public new LayoutItem Layout
{
get
{
return GetValue(LayoutProperty) as LayoutItem;
}
set
{
SetValue(LayoutProperty, value);
NotifyPropertyChanged();
}
}
private LayoutItem InternalLayout
{
get
{
return ContentContainer.Layout;
}
set
{
ContentContainer.Layout = value;
}
}
/// <summary>
/// List of children of Container.
/// </summary>
/// <since_tizen> 8 </since_tizen>
public new List<View> Children
{
get
{
return ContentContainer.Children;
}
}
/// <summary>
/// Deceleration rate of scrolling by finger.
/// Rate should be bigger than 0 and smaller than 1.
/// Default value is 0.998f;
/// </summary>
/// <since_tizen> 8 </since_tizen>
public float DecelerationRate
{
get
{
return (float)GetValue(DecelerationRateProperty);
}
set
{
SetValue(DecelerationRateProperty, value);
NotifyPropertyChanged();
}
}
private float InternalDecelerationRate
{
get
{
return decelerationRate;
}
set
{
decelerationRate = (value < 1 && value > 0) ? value : decelerationRate;
logValueOfDeceleration = (float)Math.Log(value);
}
}
/// <summary>
/// Threshold not to go infinite at the end of scrolling animation.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public float DecelerationThreshold
{
get
{
return (float)GetValue(DecelerationThresholdProperty);
}
set
{
SetValue(DecelerationThresholdProperty, value);
NotifyPropertyChanged();
}
}
private float InternalDecelerationThreshold { get; set; } = 0.1f;
/// <summary>
/// Scrolling event will be thrown when this amount of scroll position is changed.
/// If this threshold becomes smaller, the tracking detail increases but the scrolling range that can be tracked becomes smaller.
/// If large sized ContentContainer is required, please use larger threshold value.
/// Default ScrollingEventThreshold value is 0.001f.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public float ScrollingEventThreshold
{
get
{
return (float)GetValue(ScrollingEventThresholdProperty);
}
set
{
SetValue(ScrollingEventThresholdProperty, value);
NotifyPropertyChanged();
}
}
private float InternalScrollingEventThreshold
{
get
{
return mScrollingEventThreshold;
}
set
{
if (mScrollingEventThreshold != value && value > 0)
{
ContentContainer.RemovePropertyNotification(propertyNotification);
propertyNotification = ContentContainer.AddPropertyNotification("position", PropertyCondition.Step(value));
propertyNotification.Notified += OnPropertyChanged;
mScrollingEventThreshold = value;
}
}
}
/// <summary>
/// Page will be changed when velocity of panning is over threshold.
/// The unit of threshold is pixel per millisecond.
/// </summary>
/// <since_tizen> 8 </since_tizen>
public float PageFlickThreshold
{
get
{
return (float)GetValue(PageFlickThresholdProperty);
}
set
{
SetValue(PageFlickThresholdProperty, value);
NotifyPropertyChanged();
}
}
private float InternalPageFlickThreshold
{
get
{
return mPageFlickThreshold;
}
set
{
mPageFlickThreshold = value >= 0f ? value : mPageFlickThreshold;
}
}
/// <summary>
/// Padding for the ScrollableBase
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public new Extents Padding
{
get
{
return GetValue(PaddingProperty) as Extents;
}
set
{
SetValue(PaddingProperty, value);
NotifyPropertyChanged();
}
}
private Extents InternalPadding
{
get
{
return ContentContainer.Padding;
}
set
{
ContentContainer.Padding = value;
}
}
/// <summary>
/// Alphafunction for scroll animation.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public AlphaFunction ScrollAlphaFunction
{
get
{
return GetValue(ScrollAlphaFunctionProperty) as AlphaFunction;
}
set
{
SetValue(ScrollAlphaFunctionProperty, value);
NotifyPropertyChanged();
}
}
private AlphaFunction InternalScrollAlphaFunction { get; set; } = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
private bool hideScrollbar = true;
private float maxScrollDistance;
private float childTargetPosition = 0.0f;
private PanGestureDetector mPanGestureDetector;
private ScrollbarBase scrollBar;
private bool scrolling = false;
private float ratioOfScreenWidthToCompleteScroll = 0.5f;
private float totalDisplacementForPan = 0.0f;
private Size previousContainerSize = new Size();
private Size previousSize = new Size();
private PropertyNotification propertyNotification;
private float noticeAnimationEndBeforePosition = 0.0f;
private bool readyToNotice = false;
/// <summary>
/// Notice before animation is finished.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
// Let's consider more whether this needs to be set as protected.
public float NoticeAnimationEndBeforePosition
{
get
{
return (float)GetValue(NoticeAnimationEndBeforePositionProperty);
}
set
{
SetValue(NoticeAnimationEndBeforePositionProperty, value);
NotifyPropertyChanged();
}
}
private float InternalNoticeAnimationEndBeforePosition
{
get => noticeAnimationEndBeforePosition;
set => noticeAnimationEndBeforePosition = value;
}
/// <summary>
/// Step scroll move distance.
/// Key focus originally moves focusable objects, but in ScrollableBase,
/// if focusable object is too far or un-exist and ScrollableBase is focusable,
/// it can scroll move itself by key input.
/// this value decide how long distance will it moves in one step.
/// if any value is not set, step will be moved quater size of ScrollableBase length.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public float StepScrollDistance
{
get
{
return (float)GetValue(StepScrollDistanceProperty);
}
set
{
SetValue(StepScrollDistanceProperty, value);
NotifyPropertyChanged();
}
}
private float stepScrollDistance = 0f;
/// <summary>
/// Wheel scroll move distance.
/// This value decide how long distance will it moves in wheel event.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public float WheelScrollDistance
{
get
{
return (float)GetValue(WheelScrollDistanceProperty);
}
set
{
SetValue(WheelScrollDistanceProperty, value);
NotifyPropertyChanged();
}
}
private float wheelScrollDistance = 50f;
// Let's consider more whether this needs to be set as protected.
private float finalTargetPosition;
private Animation scrollAnimation;
// Declare user alpha function delegate
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate float UserAlphaFunctionDelegate(float progress);
private UserAlphaFunctionDelegate customScrollAlphaFunction;
private float velocityOfLastPan = 0.0f;
private float panAnimationDuration = 0.0f;
private float panAnimationDelta = 0.0f;
private float logValueOfDeceleration = 0.0f;
private float decelerationRate = 0.0f;
private View topOverShootingShadowView;
private View bottomOverShootingShadowView;
private View leftOverShootingShadowView;
private View rightOverShootingShadowView;
private const int overShootingShadowScaleHeightLimit = 64 * 3;
private const int overShootingShadowAnimationDuration = 300;
private Animation overShootingShadowAnimation;
private bool isOverShootingShadowShown = false;
private float startShowShadowDisplacement;
private void Initialize()
{
DecelerationRate = 0.998f;
base.Layout = new ScrollableBaseCustomLayout();
mPanGestureDetector = new PanGestureDetector();
mPanGestureDetector.Attach(this);
mPanGestureDetector.AddDirection(PanGestureDetector.DirectionVertical);
if (mPanGestureDetector.GetMaximumTouchesRequired() < 2) mPanGestureDetector.SetMaximumTouchesRequired(2);
mPanGestureDetector.Detected += OnPanGestureDetected;
ClippingMode = ClippingModeType.ClipToBoundingBox;
//Default Scrolling child
ContentContainer = new View()
{
Name = "ContentContainer",
WidthSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.MatchParent : LayoutParamPolicies.WrapContent,
HeightSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.WrapContent : LayoutParamPolicies.MatchParent,
};
// Check if children's sizes change to update Scrollbar
ContentContainer.Relayout += OnScrollingChildRelayout;
propertyNotification = ContentContainer.AddPropertyNotification("position", PropertyCondition.Step(mScrollingEventThreshold));
propertyNotification.Notified += OnPropertyChanged;
base.Add(ContentContainer);
// Check if ScrollableBase's size changes to update Scrollbar
base.Relayout += OnScrollingChildRelayout;
Scrollbar = new Scrollbar();
//Show vertical shadow on the top (or bottom) of the scrollable when panning down (or up).
topOverShootingShadowView = new View
{
BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_top.png",
Opacity = 1.0f,
SizeHeight = 0.0f,
PositionUsesPivotPoint = true,
ParentOrigin = NUI.ParentOrigin.TopCenter,
PivotPoint = NUI.PivotPoint.TopCenter,
};
bottomOverShootingShadowView = new View
{
BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_bottom.png",
Opacity = 1.0f,
SizeHeight = 0.0f,
PositionUsesPivotPoint = true,
ParentOrigin = NUI.ParentOrigin.BottomCenter,
PivotPoint = NUI.PivotPoint.BottomCenter,
};
//Show horizontal shadow on the left (or right) of the scrollable when panning down (or up).
leftOverShootingShadowView = new View
{
BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_left.png",
Opacity = 1.0f,
SizeWidth = 0.0f,
PositionUsesPivotPoint = true,
ParentOrigin = NUI.ParentOrigin.CenterLeft,
PivotPoint = NUI.PivotPoint.CenterLeft,
};
rightOverShootingShadowView = new View
{
BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_right.png",
Opacity = 1.0f,
SizeWidth = 0.0f,
PositionUsesPivotPoint = true,
ParentOrigin = NUI.ParentOrigin.CenterRight,
PivotPoint = NUI.PivotPoint.CenterRight,
};
WheelEvent += OnWheelEvent;
AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Trait, "ScrollableBase");
SetKeyboardNavigationSupport(true);
}
/// <summary>
/// Default Constructor
/// </summary>
/// <since_tizen> 8 </since_tizen>
public ScrollableBase() : base()
{
Initialize();
}
/// <summary>
/// Creates a new instance of a ScrollableBase with style.
/// </summary>
/// <param name="style">Creates ScrollableBase by special style defined in UX.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public ScrollableBase(string style) : base(style)
{
Initialize();
}
/// <summary>
/// Creates a new instance of a ScrollableBase with style.
/// </summary>
/// <param name="style">A style applied to the newly created ScrollableBase.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public ScrollableBase(ControlStyle style) : base(style)
{
Initialize();
}
private bool OnInterruptTouchingChildTouched(object source, View.TouchEventArgs args)