[NUI]Refactor Components (#1152)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Scrollbar.cs
1 /*
2  * Copyright(c) 2019 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 using System;
18 using Tizen.NUI.BaseComponents;
19 using System.ComponentModel;
20 using Tizen.NUI.Binding;
21
22 namespace Tizen.NUI.Components
23 {
24     /// <summary>
25     /// The ScrollBar class of nui component. It allows users to recognize the direction and the range of lists/content. .
26     /// </summary>
27     /// <since_tizen> 6 </since_tizen>
28     public class ScrollBar : Control
29     {
30         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
31         [EditorBrowsable(EditorBrowsableState.Never)]
32         public static readonly BindableProperty DirectionProperty = BindableProperty.Create("Direction", typeof(DirectionType), typeof(ScrollBar), DirectionType.Horizontal, propertyChanged: (bindable, oldValue, newValue) =>
33         {
34             var instance = (ScrollBar)bindable;
35             if (newValue != null)
36             {
37                 instance.Style.Direction = (DirectionType?)newValue;
38                 instance.UpdateValue();
39             }
40         },
41         defaultValueCreator: (bindable) =>
42         {
43             var instance = (ScrollBar)bindable;
44             return instance.Style.Direction;
45         });
46         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
47         [EditorBrowsable(EditorBrowsableState.Never)]
48         public static readonly BindableProperty MaxValueProperty = BindableProperty.Create("MaxValue", typeof(int), typeof(ScrollBar), default(int), propertyChanged: (bindable, oldValue, newValue) =>
49         {
50             var instance = (ScrollBar)bindable;
51             if (newValue != null)
52             {
53                 if ((int)newValue >= 0)
54                 {
55                     instance.maxValue = (int)newValue;
56                     instance.UpdateValue();
57                 }
58             }
59         },
60         defaultValueCreator: (bindable) =>
61         {
62             var instance = (ScrollBar)bindable;
63             return instance.maxValue;
64         });
65         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
66         [EditorBrowsable(EditorBrowsableState.Never)]
67         public static readonly BindableProperty MinValueProperty = BindableProperty.Create("MinValue", typeof(int), typeof(ScrollBar), default(int), propertyChanged: (bindable, oldValue, newValue) =>
68         {
69             var instance = (ScrollBar)bindable;
70             if (newValue != null)
71             {
72                 if ((int)newValue >= 0)
73                 {
74                     instance.minValue = (int)newValue;
75                     instance.UpdateValue();
76                 }
77             }
78         },
79         defaultValueCreator: (bindable) =>
80         {
81             var instance = (ScrollBar)bindable;
82             return instance.minValue;
83         });
84         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
85         [EditorBrowsable(EditorBrowsableState.Never)]
86         public static readonly BindableProperty CurrentValueProperty = BindableProperty.Create("CurrentValue", typeof(int), typeof(ScrollBar), default(int), propertyChanged: (bindable, oldValue, newValue) =>
87         {
88             var instance = (ScrollBar)bindable;
89             if (newValue != null)
90             {
91                 if ((int)newValue >= 0)
92                 {
93                     instance.curValue = (int)newValue;
94                     instance.UpdateValue();
95                 }
96             }
97         },
98         defaultValueCreator: (bindable) =>
99         {
100             var instance = (ScrollBar)bindable;
101             return instance.curValue;
102         });
103         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
104         [EditorBrowsable(EditorBrowsableState.Never)]
105         public static readonly BindableProperty DurationProperty = BindableProperty.Create("Duration", typeof(uint), typeof(ScrollBar), default(uint), propertyChanged: (bindable, oldValue, newValue) =>
106         {
107             var instance = (ScrollBar)bindable;
108             if (newValue != null)
109             {
110                 instance.Style.Duration = (uint)newValue;
111                 if (instance.scrollAniPlayer != null)
112                 {
113                     instance.scrollAniPlayer.Duration = (int)newValue;
114                 }
115             }
116         },
117         defaultValueCreator: (bindable) =>
118         {
119             var instance = (ScrollBar)bindable;
120             return instance.Style.Duration;
121         });
122
123         private ImageView trackImage;
124         private ImageView thumbImage;
125         private Animation scrollAniPlayer = null;
126         private float thumbImagePosX;
127         private float thumbImagePosY;
128         private bool enableAni = false;
129         private int minValue;
130         private int maxValue;
131         private int curValue;
132
133         /// <summary>
134         /// The constructor of ScrollBar.
135         /// </summary>
136         /// <since_tizen> 6 </since_tizen>
137         public ScrollBar() : base()
138         {
139             Initialize();
140         }
141
142         /// <summary>
143         /// The constructor of ScrollBar with specific style.
144         /// </summary>
145         /// <param name="style">style name</param>
146         /// <since_tizen> 6 </since_tizen>
147         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
148         [EditorBrowsable(EditorBrowsableState.Never)]
149         public ScrollBar(string style) : base(style)
150         {
151             Initialize();
152         }
153
154         /// <summary>
155         /// The constructor of ScrollBar with specific style.
156         /// </summary>
157         /// <param name="style">The style object to initialize the ScrollBar.</param>
158         /// <since_tizen> 6 </since_tizen>
159         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
160         [EditorBrowsable(EditorBrowsableState.Never)]
161         public ScrollBar(ScrollBarStyle style) : base(style)
162         {
163             Initialize();
164         }
165
166         /// <summary>
167         /// The direction type of the Scroll.
168         /// </summary>
169         /// <since_tizen> 6 </since_tizen>
170         public enum DirectionType
171         {
172             /// <summary>
173             /// The Horizontal type.
174             /// </summary>
175             /// <since_tizen> 6 </since_tizen>
176             Horizontal,
177
178             /// <summary>
179             /// The Vertical type.
180             /// </summary>
181             /// <since_tizen> 6 </since_tizen>
182             Vertical
183         }
184
185         #region public property 
186         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
187         [EditorBrowsable(EditorBrowsableState.Never)]
188         public new ScrollBarStyle Style => ViewStyle as ScrollBarStyle;
189
190         /// <summary>
191         /// The property to get/set the direction of the ScrollBar.
192         /// </summary>
193         /// <since_tizen> 6 </since_tizen>
194         public DirectionType Direction
195         {
196             get
197             {
198                 return (DirectionType)GetValue(DirectionProperty);
199             }
200             set
201             {
202                 SetValue(DirectionProperty, value);
203             }
204         }
205
206         /// <summary>
207         /// The property to get/set the size of the thumb object.
208         /// </summary>
209         /// <exception cref="InvalidOperationException">Throw when ThumbSize is null.</exception>
210         /// <example>
211         /// <code>
212         /// ScrollBar scroll;
213         /// try
214         /// {
215         ///     scroll.ThumbSize = new Size(500, 10, 0);
216         /// }
217         /// catch(InvalidOperationException e)
218         /// {
219         ///     Tizen.Log.Error(LogTag, "Failed to set ThumbSize value : " + e.Message);
220         /// }
221         /// </code>
222         /// </example>
223         /// <since_tizen> 6 </since_tizen>
224         public Size ThumbSize
225         {
226             get
227             {
228                 if (Style.Thumb.Size == null)
229                 {
230                     Style.Thumb.Size = new Size();
231                 }
232                 return Style.Thumb.Size;
233             }
234             set
235             {
236                 if (Style.Thumb.Size == null)
237                 {
238                     Style.Thumb.Size = new Size();
239                 }
240                 if (thumbImage != null)
241                 {
242                     Style.Thumb.Size.Width = value.Width;
243                     Style.Thumb.Size.Height = value.Height;
244                     RelayoutRequest();
245                 }
246             }
247         }
248
249         /// <summary>
250         /// The property to get/set the image URL of the track object.
251         /// </summary>
252         /// <since_tizen> 6 </since_tizen>
253         public string TrackImageURL
254         {
255             get
256             {
257                 return Style.Track.ResourceUrl.All;
258             }
259             set
260             {
261                 if (trackImage != null)
262                 {
263                     if (Style.Track.ResourceUrl == null)
264                     {
265                         Style.Track.ResourceUrl = new StringSelector();
266                     }
267                     Style.Track.ResourceUrl.All = value;
268                 }
269                 RelayoutRequest();
270             }
271         }
272
273         /// <summary>
274         /// The property to get/set the color of the track object.
275         /// </summary>
276         /// <since_tizen> 6 </since_tizen>
277         public Color TrackColor
278         {
279             get
280             {
281                 return Style.Track.BackgroundColor?.All;
282             }
283             set
284             {
285                 if (Style.Track.BackgroundColor == null)
286                 {
287                     Style.Track.BackgroundColor = new ColorSelector { All = value };
288                 }
289                 else
290                 {
291                     Style.Track.BackgroundColor.All = value;
292                 }
293                 RelayoutRequest();
294             }
295         }
296
297         /// <summary>
298         /// The property to get/set the color of the thumb object.
299         /// </summary>
300         /// <since_tizen> 6 </since_tizen>
301         public Color ThumbColor
302         {
303             get
304             {
305                 return Style.Thumb.BackgroundColor?.All;
306             }
307             set
308             {
309                 if(Style.Thumb.BackgroundColor == null)
310                 {
311                     Style.Thumb.BackgroundColor = new ColorSelector { All = value };
312                 }
313                 else
314                 {
315                     Style.Thumb.BackgroundColor.All = value;
316                 }
317                 RelayoutRequest();
318             }
319         }
320
321         /// <summary>
322         /// The property to get/set the max value of the ScrollBar.
323         /// </summary>
324         /// <since_tizen> 6 </since_tizen>
325         public int MaxValue
326         {
327             get
328             {
329                 return (int)GetValue(MaxValueProperty);
330             }
331             set
332             {
333                 SetValue(MaxValueProperty, value);
334             }
335         }
336
337         /// <summary>
338         /// The property to get/set the min value of the ScrollBar.
339         /// </summary>
340         /// <since_tizen> 6 </since_tizen>
341         public int MinValue
342         {
343             get
344             {
345                 return (int)GetValue(MinValueProperty);
346             }
347             set
348             {
349                 SetValue(MinValueProperty, value);
350             }
351         }
352
353         /// <summary>
354         /// The property to get/set the current value of the ScrollBar.
355         /// </summary>
356         /// <exception cref="ArgumentOutOfRangeException">Throw when Current value is less than Min value, or greater than Max value.</exception>
357         /// <example>
358         /// <code>
359         /// ScrollBar scroll;
360         /// scroll.MaxValue = 100;
361         /// scroll.MinValue = 0;
362         /// try
363         /// {
364         ///     scroll.CurrentValue = 50;
365         /// }
366         /// catch(ArgumentOutOfRangeException e)
367         /// {
368         ///     Tizen.Log.Error(LogTag, "Failed to set Current value : " + e.Message);
369         /// }
370         /// </code>
371         /// </example>
372         /// <since_tizen> 6 </since_tizen>
373         public int CurrentValue
374         {
375             get
376             {
377                 return (int)GetValue(CurrentValueProperty);
378             }
379             set
380             {
381                 SetValue(CurrentValueProperty, value);
382             }
383         }
384
385         /// <summary>
386         /// Property to set/get animation duration.
387         /// </summary>
388         /// <since_tizen> 6 </since_tizen>
389         public uint Duration
390         {
391             get
392             {
393                 return (uint)GetValue(DurationProperty);
394             }
395             set
396             {
397                 SetValue(DurationProperty, value);
398             }
399         }
400         #endregion
401
402         /// <summary>
403         /// Method to set current value. The thumb object would move to the corresponding position with animation or not.
404         /// </summary>
405         /// <param name="currentValue">The special current value.</param>
406         /// <param name="EnableAnimation">Enable move with animation or not, the default value is true.</param>
407         /// <exception cref="ArgumentOutOfRangeException">Throw when current size is less than the min value, or greater than the max value.</exception>
408         /// <example>
409         /// <code>
410         /// ScrollBar scroll;
411         /// scroll.MinValue = 0;
412         /// scroll.MaxValue = 100;
413         /// try
414         /// {
415         ///     scroll.SetCurrentValue(50);
416         /// }
417         /// catch(ArgumentOutOfRangeException e)
418         /// {
419         ///     Tizen.Log.Error(LogTag, "Failed to set current value : " + e.Message);
420         /// }
421         /// </code>
422         /// </example>
423         /// <since_tizen> 6 </since_tizen>
424         public void SetCurrentValue(int currentValue, bool enableAnimation = true)
425         {
426             if (currentValue < minValue || currentValue > maxValue)
427             {
428                 //TNLog.E("Current value is less than the Min value, or greater than the Max value. currentValue = " + currentValue + ";");
429                 throw new ArgumentOutOfRangeException("Wrong Current value. It shoud be greater than the Min value, and less than the Max value!");
430             }
431
432             enableAni = enableAnimation;
433             CurrentValue = currentValue;
434         }
435
436         /// <summary>
437         /// Dispose ScrollBar.
438         /// </summary>
439         /// <param name="type">The DisposeTypes value.</param>
440         /// <since_tizen> 6 </since_tizen>
441         protected override void Dispose(DisposeTypes type)
442         {
443             if (disposed)
444             {
445                 return;
446             }
447
448             if (type == DisposeTypes.Explicit)
449             {
450                 //Called by User
451                 //Release your own managed resources here.
452                 //You should release all of your own disposable objects here.
453
454                 Utility.Dispose(trackImage);
455                 Utility.Dispose(thumbImage);
456
457                 if (scrollAniPlayer != null)
458                 {
459                     scrollAniPlayer.Stop();
460                     scrollAniPlayer.Clear();
461                     scrollAniPlayer.Dispose();
462                     scrollAniPlayer = null;
463                 }
464             }
465
466             //Release your own unmanaged resources here.
467             //You should not access any managed member here except static instance.
468             //because the execution order of Finalizes is non-deterministic.
469             //Unreference this from if a static instance refer to this. 
470
471             //You must call base.Dispose(type) just before exit.
472             base.Dispose(type);
473         }
474
475         /// <summary>
476         /// Get Scrollbar style.
477         /// </summary>
478         /// <since_tizen> 6 </since_tizen>
479         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
480         [EditorBrowsable(EditorBrowsableState.Never)]
481         protected override ViewStyle GetViewStyle()
482         {
483             return new ScrollBarStyle();
484         }
485
486         /// <summary>
487         /// Theme change callback when theme is changed, this callback will be trigger.
488         /// </summary>
489         /// <since_tizen> 6 </since_tizen>
490         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
491         [EditorBrowsable(EditorBrowsableState.Never)]
492         protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
493         {
494             ScrollBarStyle tempStyle = StyleManager.Instance.GetAttributes(style) as ScrollBarStyle;
495             if (tempStyle != null)
496             {
497                 Style.CopyFrom(tempStyle);
498                 UpdateValue();
499             }
500         }
501
502         private void Initialize()
503         {
504             this.Focusable = false;
505
506             trackImage = new ImageView
507             {
508                 Focusable = false,
509                 WidthResizePolicy = ResizePolicyType.FillToParent,
510                 HeightResizePolicy = ResizePolicyType.FillToParent,
511                 PositionUsesPivotPoint = true,
512                 ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
513                 PivotPoint = Tizen.NUI.PivotPoint.CenterLeft
514             };
515             this.Add(trackImage);
516             trackImage.ApplyStyle(Style.Track);
517
518             thumbImage = new ImageView
519             {
520                 Focusable = false,
521                 WidthResizePolicy = ResizePolicyType.Fixed,
522                 HeightResizePolicy = ResizePolicyType.Fixed,
523                 PositionUsesPivotPoint = true,
524                 ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
525                 PivotPoint = Tizen.NUI.PivotPoint.CenterLeft
526             };
527             this.Add(thumbImage);
528             thumbImage.ApplyStyle(Style.Thumb);
529
530             scrollAniPlayer = new Animation(334);
531             scrollAniPlayer.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
532
533             thumbImagePosX = 0;
534             thumbImagePosY = 0;
535             LayoutDirectionChanged += OnLayoutDirectionChanged;
536         }
537
538         private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
539         {
540             RelayoutRequest();
541         }
542
543         private void UpdateValue()
544         {
545             if (minValue >= maxValue || curValue < minValue || curValue > maxValue) return;
546
547             float width = (float)Size2D.Width;
548             float height = (float)Size2D.Height;
549             float thumbW = 0.0f;
550             float thumbH = 0.0f;
551             if (Style.Thumb.Size == null)
552             {
553                 return;
554             }
555             else
556             {
557                 thumbW = Style.Thumb.Size.Width;
558                 thumbH = Style.Thumb.Size.Height;
559             }
560             float ratio = (float)(curValue - minValue) / (float)(maxValue - minValue);
561
562             if (Style.Direction == DirectionType.Horizontal)
563             {
564                 if (LayoutDirection == ViewLayoutDirectionType.RTL)
565                 {
566                     ratio = 1.0f - ratio;
567                 }
568
569                 float posX = ratio * (width - thumbW);
570                 float posY = (height - thumbH) / 2.0f;
571
572                 thumbImagePosX = posX;
573                 if (null != scrollAniPlayer)
574                 {
575                     scrollAniPlayer.Stop();
576                 }
577
578                 if (!enableAni)
579                 {
580                     thumbImage.Position = new Position(posX, posY, 0);
581                 }
582                 else
583                 {
584                     if (null != scrollAniPlayer)
585                     {
586                         scrollAniPlayer.Clear();
587                         scrollAniPlayer.AnimateTo(thumbImage, "PositionX", posX);
588                         scrollAniPlayer.Play();
589                     }
590                 }
591             }
592             else
593             {
594                 float posX = (width - thumbW) / 2.0f;
595                 float posY = ratio * (height - thumbH);
596
597                 thumbImagePosY = posY;
598                 if (null != scrollAniPlayer)
599                 {
600                     scrollAniPlayer.Stop();
601                 }
602
603                 if (!enableAni)
604                 {
605                     thumbImage.Position = new Position(posX, posY, 0);
606                 }
607                 else
608                 {
609                     if (null != scrollAniPlayer)
610                     {
611                         scrollAniPlayer.Clear();
612                         scrollAniPlayer.AnimateTo(thumbImage, "PositionY", posY);
613                         scrollAniPlayer.Play();
614                     }
615                 }
616             }
617
618             if (enableAni) enableAni = false;
619         }
620
621         private DirectionType CurrentDirection()
622         {
623             DirectionType dir = DirectionType.Horizontal;
624             if (null != Style.Direction)
625             {
626                 dir = Style.Direction.Value;
627             }
628             return dir;
629         }
630
631         private int CalculateCurrentValue(float offset, DirectionType dir)
632         {
633             if (dir == DirectionType.Horizontal)
634             {
635                 thumbImagePosX += offset;
636                 if (thumbImagePosX < 0)
637                 {
638                     thumbImage.PositionX = 0;
639                     curValue = minValue;
640                 }
641                 else if (thumbImagePosX > Size2D.Width - thumbImage.Size2D.Width)
642                 {
643                     thumbImage.PositionX = Size2D.Width - thumbImage.Size2D.Width;
644                     curValue = maxValue;
645                 }
646                 else
647                 {
648                     thumbImage.PositionX = thumbImagePosX;
649                     curValue = (int)((thumbImagePosX / (float)(Size2D.Width - thumbImage.Size2D.Width)) * (float)(maxValue - minValue) + 0.5f);
650                 }
651             }
652             else
653             {
654                 thumbImagePosY += offset;
655                 if (thumbImagePosY < 0)
656                 {
657                     thumbImage.PositionY = 0;
658                     curValue = minValue;
659                 }
660                 else if (thumbImagePosY > Size2D.Height - thumbImage.Size2D.Height)
661                 {
662                     thumbImage.PositionY = Size2D.Height - thumbImage.Size2D.Height;
663                     curValue = maxValue;
664                 }
665                 else
666                 {
667                     thumbImage.PositionY = thumbImagePosY;
668                     curValue = (int)((thumbImagePosY / (float)(Size2D.Height - thumbImage.Size2D.Height)) * (float)(maxValue - minValue) + 0.5f);
669                 }
670             }
671
672             return curValue;
673         }
674     }
675 }