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