[NUI] Fix Picker value changed not invoked when align animation ended
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Picker.cs
1 /* Copyright (c) 2021 Samsung Electronics Co., Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  */
16 using System;
17 using Tizen.NUI;
18 using Tizen.NUI.BaseComponents;
19 using System.Collections.Generic;
20 using System.Collections.ObjectModel;
21 using System.ComponentModel;
22 using System.Diagnostics.CodeAnalysis;
23
24 namespace Tizen.NUI.Components
25 {
26     /// <summary>
27     /// ValueChangedEventArgs is a class to notify changed Picker value argument which will sent to user.
28     /// </summary>
29     [EditorBrowsable(EditorBrowsableState.Never)]
30     public class ValueChangedEventArgs : EventArgs
31     {
32         /// <summary>
33         /// ValueChangedEventArgs default constructor.
34         /// <param name="value">value of Picker.</param>
35         /// </summary>
36         [EditorBrowsable(EditorBrowsableState.Never)]   
37         public ValueChangedEventArgs(int value)
38         {
39             Value = value;
40         }
41
42         /// <summary>
43         /// ValueChangedEventArgs default constructor.
44         /// <returns>The current value of Picker.</returns>
45         /// </summary>
46         [EditorBrowsable(EditorBrowsableState.Never)]   
47         public int Value { get; }
48         
49     }
50
51     /// <summary>
52     /// Picker is a class which provides a function that allows the user to select 
53     /// a value through a scrolling motion by expressing the specified value as a list.
54     /// </summary>
55     [EditorBrowsable(EditorBrowsableState.Never)]
56     public class Picker : Control
57     {
58         //Tizen 6.5 base components Picker guide visible scroll item is 5.
59         private const int scrollVisibleItems = 5;
60         //Dummy item count for loop feature. Max value of scrolling distance in 
61         //RPI target is bigger than 20 items height. it can adjust depends on the internal logic and device env.
62         private const int dummyItemsForLoop = 20;             
63         private int startScrollOffset;
64         private int itemHeight;
65         private int startScrollY;
66         private int startY;
67         private int pageSize;
68         private int currentValue;
69         private int maxValue;
70         private int minValue;
71         private int lastScrollPosion;
72         private bool onAnimation; //Scroller on animation check.
73         private bool onAlignAnimation;
74         private bool displayedValuesUpdate; //User sets displayed value check.
75         private bool needItemUpdate; //min, max or display value updated check.
76         private bool loopEnabled;
77         private ReadOnlyCollection<string> displayedValues;
78         private PickerScroller pickerScroller;
79         private View upLine;
80         private View downLine;
81         private IList<TextLabel> itemList;
82         private PickerStyle pickerStyle => ViewStyle as PickerStyle;
83
84         /// <summary>
85         /// Creates a new instance of Picker.
86         /// </summary>
87         [EditorBrowsable(EditorBrowsableState.Never)]
88         public Picker()
89         {
90             Initialize();
91         }
92
93         /// <summary>
94         /// Creates a new instance of Picker.
95         /// </summary>
96         /// <param name="style">Creates Picker by special style defined in UX.</param>
97         [EditorBrowsable(EditorBrowsableState.Never)]
98         public Picker(string style) : base(style)
99         {
100             Initialize();
101         }
102
103         /// <summary>
104         /// Creates a new instance of Picker.
105         /// </summary>
106         /// <param name="pickerStyle">Creates Picker by style customized by user.</param>
107         [EditorBrowsable(EditorBrowsableState.Never)]
108         public Picker(PickerStyle pickerStyle) : base(pickerStyle)
109         {
110             Initialize();
111         }
112
113         /// <summary>
114         /// Dispose Picker and all children on it.
115         /// </summary>
116         /// <param name="type">Dispose type.</param>
117         [EditorBrowsable(EditorBrowsableState.Never)]
118         protected override void Dispose(DisposeTypes type)
119         {
120             if (disposed)
121             {
122                 return;
123             }
124
125             if (type == DisposeTypes.Explicit)
126             {
127                 if (itemList != null)
128                 {
129                     foreach (TextLabel textLabel in itemList)
130                     {
131                         if (pickerScroller) pickerScroller.Remove(textLabel);
132                         Utility.Dispose(textLabel);
133                     }
134
135                     itemList = null;
136                 }
137
138                 if (pickerScroller != null)
139                 {
140                     Remove(pickerScroller);
141                     Utility.Dispose(pickerScroller);
142                     pickerScroller = null;
143                 }
144
145                 Remove(upLine);
146                 Utility.Dispose(upLine);
147                 Remove(downLine);
148                 Utility.Dispose(downLine);
149             }
150
151             base.Dispose(type);
152         }
153
154         /// <summary>
155         /// An event emitted when Picker value changed, user can subscribe or unsubscribe to this event handler.
156         /// </summary>
157         [EditorBrowsable(EditorBrowsableState.Never)]
158         public event EventHandler<ValueChangedEventArgs> ValueChanged;
159
160         //TODO Fomatter here
161
162         /// <summary>
163         /// The values to be displayed instead of numbers.
164         /// </summary>
165         [EditorBrowsable(EditorBrowsableState.Never)]
166         public ReadOnlyCollection<String> DisplayedValues
167         {
168             get
169             {
170                 return displayedValues;
171             }
172             set
173             {
174                 displayedValues = value;
175
176                 needItemUpdate = true;
177                 displayedValuesUpdate = true;
178
179                 UpdateValueList();
180             }
181         }
182         
183         /// <summary>
184         /// The Current value of Picker.
185         /// </summary>
186         [EditorBrowsable(EditorBrowsableState.Never)]
187         public int CurrentValue
188         {
189             get
190             {
191                 return currentValue;
192             }
193             set
194             {
195                 if (currentValue == value) return;
196
197                 if (currentValue < minValue) currentValue = minValue;
198                 else if (currentValue > maxValue) currentValue = maxValue;
199
200                 currentValue = value;
201
202                 UpdateCurrentValue();
203             }
204         }
205
206         /// <summary>
207         /// The max value of Picker.
208         /// </summary>
209         [EditorBrowsable(EditorBrowsableState.Never)]
210         public int MaxValue
211         {
212             get
213             {
214                 return maxValue;
215             }
216             set
217             {
218                 if (maxValue == value) return;
219                 if (currentValue > value) currentValue = value;
220                 
221                 maxValue = value;
222                 needItemUpdate = true;
223
224                 UpdateValueList();
225             }
226         }
227
228         /// <summary>
229         /// The min value of Picker.
230         /// </summary>
231         [EditorBrowsable(EditorBrowsableState.Never)]
232         public int MinValue
233         {
234             get
235             {
236                 return minValue;
237             }
238             set
239             {
240                 if (minValue == value) return;
241                 if (currentValue < value) currentValue = value;
242                 
243                 minValue = value;
244                 needItemUpdate = true;
245
246                 UpdateValueList();
247             }
248         }
249
250         /// <inheritdoc/>
251         [EditorBrowsable(EditorBrowsableState.Never)]
252         public override void OnInitialize()
253         {
254             base.OnInitialize();
255             SetAccessibilityConstructor(Role.List, AccessibilityInterface.Value);
256         }
257
258         /// <summary>
259         /// Applies style to Picker.
260         /// </summary>
261         /// <param name="viewStyle">The style to apply.</param>
262         [EditorBrowsable(EditorBrowsableState.Never)]
263         public override void ApplyStyle(ViewStyle viewStyle)
264         {
265             base.ApplyStyle(viewStyle);
266
267             //Apply StartScrollOffset style.
268             if (pickerStyle?.StartScrollOffset != null)
269                 startScrollOffset = (int)pickerStyle.StartScrollOffset.Height;
270
271             //Apply ItemTextLabel style.
272             if (pickerStyle?.ItemTextLabel != null)
273             {
274                 itemHeight = (int)pickerStyle.ItemTextLabel.Size.Height;
275
276                 if (itemList != null)
277                     foreach (TextLabel textLabel in itemList)
278                         textLabel.ApplyStyle(pickerStyle.ItemTextLabel);
279             }
280
281             //Apply PickerCenterLine style.
282             if (pickerStyle?.Divider != null && upLine != null && downLine != null)
283             {
284                 upLine.ApplyStyle(pickerStyle.Divider);
285                 downLine.ApplyStyle(pickerStyle.Divider);
286                 downLine.PositionY = (int)pickerStyle.Divider.PositionY + itemHeight;
287             }
288         }
289                 
290         private void Initialize()
291         {
292             AccessibilityHighlightable = true;
293             HeightSpecification = LayoutParamPolicies.MatchParent;
294
295             //Picker Using scroller internally. actually it is a kind of scroller which has infinity loop,
296             //and item center align features.
297             pickerScroller = new PickerScroller(pickerStyle)
298             {
299                 Size = new Size(-1, pickerStyle.Size.Height),
300                 ScrollingDirection = ScrollableBase.Direction.Vertical,
301                 Layout = new LinearLayout()
302                 {
303                     LinearOrientation = LinearLayout.Orientation.Vertical,
304                 },
305                 //FIXME: Need to expand as many as possible;
306                 //       When user want to start list middle of the list item. currently confused how to create list before render.
307                 ScrollAvailableArea = new Vector2(0, 10000),
308                 Name = "pickerScroller",
309             };
310             pickerScroller.Scrolling += OnScroll;
311             pickerScroller.ScrollAnimationEnded += OnScrollAnimationEnded;
312             pickerScroller.ScrollAnimationStarted += OnScrollAnimationStarted;
313
314             itemList = new List<TextLabel>();
315             
316             minValue = maxValue = currentValue = 0;
317             displayedValues = null;
318             //Those many flags for min, max, value method calling sequence dependency.
319             needItemUpdate = true;
320             displayedValuesUpdate = false;
321             onAnimation = false;
322             loopEnabled = false;
323
324             startScrollOffset = (int)pickerStyle.StartScrollOffset.Height;
325             itemHeight = (int)pickerStyle.ItemTextLabel.Size.Height;
326             startScrollY = (itemHeight * dummyItemsForLoop) + startScrollOffset;
327             startY = startScrollOffset;
328
329             Add(pickerScroller);
330             AddLine();
331         }
332
333         private void OnValueChanged()
334         { 
335             ValueChangedEventArgs eventArgs =
336                 new ValueChangedEventArgs(displayedValuesUpdate ? Int32.Parse(itemList[currentValue].Name) : Int32.Parse(itemList[currentValue].Text));
337             ValueChanged?.Invoke(this, eventArgs);
338         }
339
340         private void PageAdjust(float positionY)
341         {
342             //Check the scroll is going out to the dummys if so, bring it back to page.
343             if (positionY > -(startScrollY - (itemHeight * 2)))
344                 pickerScroller.ScrollTo(-positionY + pageSize, false);
345             else if (positionY < -(startScrollY + pageSize - (itemHeight * 2)))
346                 pickerScroller.ScrollTo(-positionY - pageSize, false);
347         }
348
349         private void OnScroll(object sender, ScrollEventArgs e)
350         {
351             if (!loopEnabled || onAnimation || onAlignAnimation) return;
352             
353             PageAdjust(e.Position.Y);
354         }
355
356         private void OnScrollAnimationStarted(object sender, ScrollEventArgs e)
357         {
358             onAnimation = true;
359         }
360
361         private void OnScrollAnimationEnded(object sender, ScrollEventArgs e)
362         {
363             //Ignore if the scroll position was not changed. (called it from this function)
364             if (lastScrollPosion == (int)e.Position.Y) return;
365
366             //Calc offset from closest item.
367             int offset = (int)(e.Position.Y + startScrollOffset) % itemHeight;
368             if (offset < -(itemHeight / 2)) offset += itemHeight;
369
370             lastScrollPosion = (int)(-e.Position.Y + offset);
371
372             onAnimation = false;
373             if (onAlignAnimation) {
374                 onAlignAnimation = false;
375                 PageAdjust(e.Position.Y);
376                 if (currentValue != ((int)(-e.Position.Y / itemHeight) + 2))
377                 {
378                     currentValue = ((int)(-e.Position.Y / itemHeight) + 2);
379                     OnValueChanged();
380                 }
381
382                 return;
383             }
384
385             //Item center align with animation, otherwise changed event emit.
386             if (offset != 0) {
387                 onAlignAnimation = true;
388                 pickerScroller.ScrollTo(-e.Position.Y + offset, true);
389             }
390             else {
391                 if (currentValue != ((int)(-e.Position.Y / itemHeight) + 2))
392                 {
393                     currentValue = ((int)(-e.Position.Y / itemHeight) + 2);
394                     OnValueChanged();
395                 }
396             }
397         }
398
399         //This is UI requirement. It helps where exactly center item is.
400         private void AddLine()
401         {
402             upLine = new View(pickerStyle.Divider);
403             downLine = new View(pickerStyle.Divider)
404             {
405                 Position = new Position(0, (int)pickerStyle.Divider.PositionY + itemHeight),
406             };
407
408             Add(upLine);
409             Add(downLine);
410         }
411
412         private String GetItemText(bool loopEnabled, int idx)
413         {
414             if (!loopEnabled) return " ";
415             else {
416                 if (displayedValuesUpdate) {
417                     idx = idx - MinValue;
418                     if (idx <= displayedValues.Count) {
419                         return displayedValues[idx];
420                     }
421                     return " ";
422                 }
423
424                 return idx.ToString();
425             }
426         }
427
428         //FIXME: If textVisual can add in scroller please change it to textVisual for performance
429         [SuppressMessage("Microsoft.Reliability",
430                          "CA2000:DisposeObjectsBeforeLosingScope",
431                          Justification = "The items are added to itemList and are disposed in Picker.Dispose().")]
432         private void AddPickerItem(bool loopEnabled, int idx)
433         {
434             TextLabel temp = new TextLabel(pickerStyle.ItemTextLabel)
435             {
436                 WidthSpecification = LayoutParamPolicies.MatchParent,
437                 Text = GetItemText(loopEnabled, idx),
438                 Name = idx.ToString(),
439             };
440
441             itemList.Add(temp);
442             pickerScroller.Add(temp);
443         }
444
445         private void UpdateCurrentValue()
446         {
447             // -2 for center align
448             int startItemIdx = (currentValue == 0) ? -2 : currentValue - minValue - 2;
449
450             if (loopEnabled) startY = ((dummyItemsForLoop + startItemIdx) * itemHeight) + startScrollOffset;
451             // + 2 for non loop picker center align
452             else startY = ((2 + startItemIdx) * itemHeight) + startScrollOffset;
453             pickerScroller.ScrollTo(startY, false);
454         }
455
456         private void UpdateValueList()
457         {
458             if (!needItemUpdate) return;
459             if (minValue > maxValue) return;
460
461             //FIXME: This is wrong.
462             //       But scroller can't update item property after added please fix me.
463             if (itemList.Count > 0) {
464                 itemList.Clear();
465                 pickerScroller.RemoveAllChildren();
466             }
467
468             if (maxValue - minValue + 1 >= scrollVisibleItems)
469             {
470                 loopEnabled = true;
471                 //Current scroller can't add at specific index.
472                 //So need below calc.
473                 int dummyStartIdx = 0;
474                 if (maxValue - minValue >= dummyItemsForLoop)
475                   dummyStartIdx = maxValue - dummyItemsForLoop + 1;
476                 else
477                   dummyStartIdx = maxValue - (dummyItemsForLoop % (maxValue - minValue + 1)) + 1;
478
479                 //Start add items in scroller. first dummys for scroll anim.
480                 for (int i = 0; i < dummyItemsForLoop; i++)
481                 {
482                     if (dummyStartIdx > maxValue) dummyStartIdx = minValue;
483                     AddPickerItem(loopEnabled, dummyStartIdx++);
484                 }
485                 //Second real items.
486                 for (int i = minValue; i <= maxValue; i++)
487                 {
488                     AddPickerItem(loopEnabled, i);
489                 }
490                 //Last dummys for scroll anim.
491                 dummyStartIdx = minValue;
492                 for (int i = 0; i < dummyItemsForLoop; i++)
493                 {
494                     if (dummyStartIdx > maxValue) dummyStartIdx = minValue;
495                     AddPickerItem(loopEnabled, dummyStartIdx++);
496                 }
497             }
498             else
499             {
500                 loopEnabled = false;
501
502                 for (int i = 0; i < 2; i++)
503                     AddPickerItem(loopEnabled, 0);
504                 for (int i = minValue; i <= maxValue; i++)
505                     AddPickerItem(!loopEnabled, i);
506                 for (int i = 0; i < 2; i++)
507                     AddPickerItem(loopEnabled, 0);
508
509             }
510             pageSize = itemHeight * (maxValue - minValue + 1);
511
512             UpdateCurrentValue();
513
514             //Give a correct scroll area.
515             pickerScroller.ScrollAvailableArea = new Vector2(0, (itemList.Count * itemHeight) - pickerStyle.Size.Height);
516
517             needItemUpdate = false;
518         }
519
520         internal class PickerScroller : ScrollableBase
521         {
522             private int itemHeight;
523             private int startScrollOffset;
524             private float velocityOfLastPan = 0.0f;
525             private float panAnimationDuration = 0.0f;
526             private float panAnimationDelta = 0.0f;
527             private float decelerationRate = 0.0f;
528             private float logValueOfDeceleration = 0.0f;
529             private delegate float UserAlphaFunctionDelegate(float progress);
530             private UserAlphaFunctionDelegate customScrollAlphaFunction;
531
532             public PickerScroller(PickerStyle pickerStyle) : base()
533             {
534                 //Default rate is 0.998. this is for reduce scroll animation length.
535                 decelerationRate = 0.991f;
536                 startScrollOffset = (int)pickerStyle.StartScrollOffset.Height;
537                 itemHeight = (int)pickerStyle.ItemTextLabel.Size.Height;
538                 logValueOfDeceleration = (float)Math.Log(decelerationRate);
539             }
540
541             private float CustomScrollAlphaFunction(float progress)
542             {
543                 if (panAnimationDelta == 0)
544                 {
545                     return 1.0f;
546                 }
547                 else
548                 {
549                     // Parameter "progress" is normalized value. We need to multiply target duration to calculate distance.
550                     // Can get real distance using equation of deceleration (check Decelerating function)
551                     // After get real distance, normalize it
552                     float realDuration = progress * panAnimationDuration;
553                     float realDistance = velocityOfLastPan * ((float)Math.Pow(decelerationRate, realDuration) - 1) / logValueOfDeceleration;
554                     float result = Math.Min(realDistance / Math.Abs(panAnimationDelta), 1.0f);
555
556                     return result;
557                 }
558             }
559
560             //Override Decelerating for Picker feature.
561             protected override void Decelerating(float velocity, Animation animation)
562             {
563                 //Reduce Scroll animation speed.
564                 //The picker is to select items in the scroll area, it is not correct to animate
565                 //the scroll with very high speed.
566                 velocity *= 0.5f;
567                 velocityOfLastPan = Math.Abs(velocity);
568
569                 float currentScrollPosition = -ContentContainer.PositionY;
570                 panAnimationDelta = (velocityOfLastPan * decelerationRate) / (1 - decelerationRate);
571                 panAnimationDelta = velocity > 0 ? -panAnimationDelta : panAnimationDelta;
572
573                 float destination = -(panAnimationDelta + currentScrollPosition);
574                 //Animation destination has to center of the item.
575                 float align = destination % itemHeight;
576                 destination -= align;
577                 destination -= startScrollOffset;
578
579                 float adjustDestination = AdjustTargetPositionOfScrollAnimation(destination);
580
581                 float maxPosition = ScrollAvailableArea != null ? ScrollAvailableArea.Y : 0;
582                 float minPosition = ScrollAvailableArea != null ? ScrollAvailableArea.X : 0;
583
584                 if (destination < -maxPosition || destination > minPosition)
585                 {
586                     panAnimationDelta = velocity > 0 ? (currentScrollPosition - minPosition) : (maxPosition - currentScrollPosition);
587                     destination = velocity > 0 ? minPosition : -maxPosition;
588                     destination = -maxPosition + itemHeight;
589
590                     if (panAnimationDelta == 0)
591                     {
592                         panAnimationDuration = 0.0f;
593                     }
594                     else
595                     {
596                         panAnimationDuration = (float)Math.Log((panAnimationDelta * logValueOfDeceleration / velocityOfLastPan + 1), decelerationRate);
597                     }
598                 }
599                 else
600                 {
601                     panAnimationDuration = (float)Math.Log(-DecelerationThreshold * logValueOfDeceleration / velocityOfLastPan) / logValueOfDeceleration;
602
603                     if (adjustDestination != destination)
604                     {
605                         destination = adjustDestination;
606                         panAnimationDelta = destination + currentScrollPosition;
607                         velocityOfLastPan = Math.Abs(panAnimationDelta * logValueOfDeceleration / ((float)Math.Pow(decelerationRate, panAnimationDuration) - 1));
608                         panAnimationDuration = (float)Math.Log(-DecelerationThreshold * logValueOfDeceleration / velocityOfLastPan) / logValueOfDeceleration;
609                     }
610                 }
611
612                 customScrollAlphaFunction = new UserAlphaFunctionDelegate(CustomScrollAlphaFunction);
613                 animation.DefaultAlphaFunction = new AlphaFunction(customScrollAlphaFunction);
614                 animation.Duration = (int)panAnimationDuration;
615                 animation.AnimateTo(ContentContainer, "PositionY", (int)destination);
616                 animation.Play();
617             }
618         }
619     }
620 }