[NUI] Add Scrollbar and CircularScrollbar (#1628)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Scrollbar.cs
1 /*
2  * Copyright(c) 2020 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 System.ComponentModel;
19 using Tizen.NUI.BaseComponents;
20 using Tizen.NUI.Binding;
21
22 namespace Tizen.NUI.Components
23 {
24     /// <summary>
25     /// The Scrollbar is a component that contains track and thumb to indicate the current scrolled position of a scrollable object.
26     /// </summary>
27     [EditorBrowsable(EditorBrowsableState.Never)]
28     public class Scrollbar : ScrollbarBase
29     {
30         #region Fields
31
32         /// <summary>Bindable property of TrackThickness</summary>
33         [EditorBrowsable(EditorBrowsableState.Never)]
34         public static readonly BindableProperty TrackThicknessProperty = BindableProperty.Create(nameof(TrackThickness), typeof(float), typeof(Scrollbar), default(float), propertyChanged: (bindable, oldValue, newValue) =>
35         {
36             var instance = ((Scrollbar)bindable);
37             var thickness = (float?)newValue;
38
39             ((ScrollbarStyle)instance.viewStyle).TrackThickness = thickness;
40             instance.UpdateTrackThickness(thickness ?? 0);
41         },
42         defaultValueCreator: (bindable) =>
43         {
44             return ((ScrollbarStyle)((Scrollbar)bindable).viewStyle)?.TrackThickness ?? 0;
45         });
46
47         /// <summary>Bindable property of ThumbThickness</summary>
48         [EditorBrowsable(EditorBrowsableState.Never)]
49         public static readonly BindableProperty ThumbThicknessProperty = BindableProperty.Create(nameof(ThumbThickness), typeof(float), typeof(Scrollbar), default(float), propertyChanged: (bindable, oldValue, newValue) =>
50         {
51             var instance = ((Scrollbar)bindable);
52             var thickness = (float?)newValue;
53
54             ((ScrollbarStyle)instance.viewStyle).ThumbThickness = thickness;
55             instance.UpdateThumbThickness(thickness ?? 0);
56         },
57         defaultValueCreator: (bindable) =>
58         {
59             return ((ScrollbarStyle)((Scrollbar)bindable).viewStyle)?.ThumbThickness ?? 0;
60         });
61
62         /// <summary>Bindable property of TrackColor</summary>
63         [EditorBrowsable(EditorBrowsableState.Never)]
64         public static readonly BindableProperty TrackColorProperty = BindableProperty.Create(nameof(TrackColor), typeof(Color), typeof(Scrollbar), null, propertyChanged: (bindable, oldValue, newValue) =>
65         {
66             var instance = ((Scrollbar)bindable);
67             var color = (Color)newValue;
68
69             ((ScrollbarStyle)instance.viewStyle).TrackColor = color;
70             instance.UpdateTrackColor(color);
71         },
72         defaultValueCreator: (bindable) =>
73         {
74             return ((ScrollbarStyle)((Scrollbar)bindable).viewStyle)?.TrackColor;
75         });
76
77         /// <summary>Bindable property of ThumbColor</summary>
78         [EditorBrowsable(EditorBrowsableState.Never)]
79         public static readonly BindableProperty ThumbColorProperty = BindableProperty.Create(nameof(ThumbColor), typeof(Color), typeof(Scrollbar), null, propertyChanged: (bindable, oldValue, newValue) =>
80         {
81             var instance = ((Scrollbar)bindable);
82             var color = (Color)newValue;
83
84             ((ScrollbarStyle)instance.viewStyle).ThumbColor = color;
85             instance.UpdateThumbColor(color);
86         },
87         defaultValueCreator: (bindable) =>
88         {
89             return ((ScrollbarStyle)((Scrollbar)bindable).viewStyle)?.ThumbColor;
90         });
91
92         /// <summary>Bindable property of TrackPadding</summary>
93         [EditorBrowsable(EditorBrowsableState.Never)]
94         public static readonly BindableProperty TrackPaddingProperty = BindableProperty.Create(nameof(TrackPadding), typeof(Extents), typeof(Scrollbar), null, propertyChanged: (bindable, oldValue, newValue) =>
95         {
96             var instance = ((Scrollbar)bindable);
97             var trackPadding = (Extents)newValue;
98
99             ((ScrollbarStyle)instance.viewStyle).TrackPadding = trackPadding;
100             instance.UpdateTrackPadding(trackPadding);
101         },
102         defaultValueCreator: (bindable) =>
103         {
104             return ((ScrollbarStyle)((Scrollbar)bindable).viewStyle)?.TrackPadding;
105         });
106
107         private static readonly string TrackVisualName = "Track";
108         private static readonly string ThumbVisualName = "Thumb";
109         private ColorVisual trackVisual;
110         private ColorVisual thumbVisual;
111         private Animation thumbPositionAnimation;
112         private Animation thumbSizeAnimation;
113         private Calculator calculator;
114
115         #endregion Fields
116
117
118         #region Constructors
119
120         /// <summary>
121         /// Create an empty Scrollbar.
122         /// </summary>
123         public Scrollbar() : base(new ScrollbarStyle())
124         {
125         }
126
127         /// <summary>
128         /// Create a Scrollbar and initialize with properties.
129         /// </summary>
130         /// <param name="contentLength">The total length of the content.</param>
131         /// <param name="viewSize">The size of View that contains the content to scroll.</param>
132         /// <param name="currentPosition">The start position of the View in content length. This is the View's top position if the scroller is vertical, otherwise, View's left position.</param>
133         /// <param name="isHorizontal">Whether the direction of scrolling is horizontal or not. It is vertical if the value is false.</param>
134         [EditorBrowsable(EditorBrowsableState.Never)]
135         public Scrollbar(float contentLength, Size viewSize, float currentPosition, bool isHorizontal = false) : base(new ScrollbarStyle())
136         {
137             Initialize(contentLength, viewSize, currentPosition, isHorizontal);
138         }
139
140         /// <summary>
141         /// Create an empty Scrollbar with a ScrollbarStyle instance to set style properties.
142         /// </summary>
143         [EditorBrowsable(EditorBrowsableState.Never)]
144         public Scrollbar(ScrollbarStyle style) : base(style)
145         {
146         }
147
148         /// <summary>
149         /// Static constructor to initialize bindable properties when loading.
150         /// </summary>
151         static Scrollbar()
152         {
153         }
154
155         #endregion Constructors
156
157
158         #region Properties
159
160         /// <summary>
161         /// The thickness of the track.
162         /// </summary>
163         [EditorBrowsable(EditorBrowsableState.Never)]
164         public float TrackThickness
165         {
166             get => (float)GetValue(TrackThicknessProperty);
167             set => SetValue(TrackThicknessProperty, value);
168         }
169
170         /// <summary>
171         /// The thickness of the thumb.
172         /// </summary>
173         [EditorBrowsable(EditorBrowsableState.Never)]
174         public float ThumbThickness
175         {
176             get => (float)GetValue(ThumbThicknessProperty);
177             set => SetValue(ThumbThicknessProperty, value);
178         }
179
180         /// <summary>
181         /// The color of the track part.
182         /// </summary>
183         [EditorBrowsable(EditorBrowsableState.Never)]
184         public Color TrackColor
185         {
186             get => (Color)GetValue(TrackColorProperty);
187             set => SetValue(TrackColorProperty, value);
188         }
189
190         /// <summary>
191         /// The color of the thumb part.
192         /// </summary>
193         [EditorBrowsable(EditorBrowsableState.Never)]
194         public Color ThumbColor
195         {
196             get => (Color)GetValue(ThumbColorProperty);
197             set => SetValue(ThumbColorProperty, value);
198         }
199
200         /// <summary>
201         /// The padding value of the track.
202         /// </summary>
203         [EditorBrowsable(EditorBrowsableState.Never)]
204         public Extents TrackPadding
205         {
206             get => (Extents)GetValue(TrackPaddingProperty);
207             set => SetValue(TrackPaddingProperty, value);
208         }
209
210         #endregion Properties
211
212
213         #region Methods
214
215         /// <inheritdoc/>
216         [EditorBrowsable(EditorBrowsableState.Never)]
217         public override void Initialize(float contentLength, Size viewSize, float currentPosition, bool isHorizontal = false)
218         {
219             if (isHorizontal)
220             {
221                 calculator = new HorizontalCalculator(contentLength > 0.0f ? contentLength : 0.0f, viewSize.Width);
222             }
223             else
224             {
225                 calculator = new VerticalCalculator(contentLength > 0.0f ? contentLength : 0.0f, viewSize.Height);
226             }
227
228             Size = viewSize;
229
230             thumbPositionAnimation?.Stop();
231             thumbPositionAnimation = null;
232
233             thumbSizeAnimation?.Stop();
234             thumbSizeAnimation = null;
235
236             CreateTrackVisual();
237             CreateThumbVisual(currentPosition);
238
239             AddVisual(TrackVisualName, trackVisual);
240             AddVisual(ThumbVisualName, thumbVisual);
241         }
242
243         /// <inheritdoc/>
244         /// <remarks>Please note that, for now, only alpha functions created with BuiltinFunctions are valid when animating. Otherwise, it will be treated as a linear alpha function. </remarks>
245         [EditorBrowsable(EditorBrowsableState.Never)]
246         public override void Update(float contentLength, float position, uint durationMs = 0, AlphaFunction alphaFunction = null)
247         {
248             if (calculator == null)
249             {
250                 return;
251             }
252
253             calculator.Update(contentLength > 0.0f ? contentLength : 0.0f);
254
255             thumbVisual.Size = calculator.CalculateThumbSize(ThumbThickness, trackVisual.Size);
256             thumbVisual.Position = calculator.CalculateThumbScrollPosition(position, trackVisual.Size, thumbVisual.Position, TrackPadding);
257             thumbVisual.Opacity = calculator.CalculateThumbVisibility() ? 1.0f : 0.0f;
258
259             if (durationMs == 0)
260             {
261                 thumbVisual.UpdateVisual(true);
262
263                 return;
264             }
265
266             // TODO Support non built-in alpha function for visual trainsition in DALi.
267             AlphaFunction.BuiltinFunctions builtinAlphaFunction = alphaFunction?.GetBuiltinFunction() ?? AlphaFunction.BuiltinFunctions.Default;
268
269             thumbPositionAnimation?.Stop();
270             thumbPositionAnimation = AnimateVisual(thumbVisual, "position", thumbVisual.Position, 0, (int)durationMs, builtinAlphaFunction);
271             thumbPositionAnimation.Play();
272
273             thumbSizeAnimation?.Stop();
274             thumbSizeAnimation = AnimateVisual(thumbVisual, "size", thumbVisual.Size, 0, (int)durationMs, builtinAlphaFunction);
275             thumbSizeAnimation.Play();
276         }
277
278         /// <inheritdoc/>
279         /// <remarks>Please note that, for now, only alpha functions created with BuiltinFunctions are valid when animating. Otherwise, it will be treated as a linear alpha function. </remarks>
280         [EditorBrowsable(EditorBrowsableState.Never)]
281         public override void ScrollTo(float position, uint durationMs = 0, AlphaFunction alphaFunction = null)
282         {
283             if (thumbVisual == null)
284             {
285                 return;
286             }
287
288             thumbVisual.Position = calculator.CalculateThumbScrollPosition(position, trackVisual.Size, thumbVisual.Position, TrackPadding);
289
290             if (durationMs == 0)
291             {
292                 thumbVisual.UpdateVisual(true);
293
294                 return;
295             }
296
297             // TODO Support non built-in alpha function for visual trainsition in DALi.
298             AlphaFunction.BuiltinFunctions builtinAlphaFunction = alphaFunction?.GetBuiltinFunction() ?? AlphaFunction.BuiltinFunctions.Default;
299
300             thumbPositionAnimation?.Stop();
301             thumbPositionAnimation = AnimateVisual(thumbVisual, "position", thumbVisual.Position, 0, (int)durationMs, builtinAlphaFunction);
302             thumbPositionAnimation.Play();
303         }
304
305         /// <inheritdoc/>
306         [EditorBrowsable(EditorBrowsableState.Never)]
307         protected override ViewStyle GetViewStyle()
308         {
309             return new ScrollbarStyle();
310         }
311
312         /// <summary>
313         /// Create a track visual.
314         /// </summary>
315         [EditorBrowsable(EditorBrowsableState.Never)]
316         protected virtual void CreateTrackVisual()
317         {
318             trackVisual = new ColorVisual
319             {
320                 SuppressUpdateVisual = true,
321                 Color = Color.Black,
322                 SizePolicy = VisualTransformPolicyType.Absolute,
323                 Origin = calculator.CalculatorTrackAlign(),
324                 AnchorPoint = calculator.CalculatorTrackAlign(),
325                 Size = calculator.CalculateTrackSize(TrackThickness, TrackPadding),
326                 Position = calculator.CalculateTrackPosition(TrackPadding),
327             };
328         }
329
330         /// <summary>
331         /// Create a thumb visual.
332         /// </summary>
333         [EditorBrowsable(EditorBrowsableState.Never)]
334         protected virtual void CreateThumbVisual(float currentPosition)
335         {
336             Size thumbSize = calculator.CalculateThumbSize(ThumbThickness, trackVisual.Size);
337
338             thumbVisual = new ColorVisual
339             {
340                 SuppressUpdateVisual = true,
341                 MixColor = ThumbColor,
342                 SizePolicy = VisualTransformPolicyType.Absolute,
343                 Origin = calculator.CalculatorThumbAlign(),
344                 AnchorPoint = calculator.CalculatorThumbAlign(),
345                 Size = thumbSize,
346                 Position = calculator.CalculateThumbPosition(currentPosition, trackVisual.Size, thumbSize, TrackPadding),
347                 Opacity = (calculator.CalculateThumbVisibility() ? 1.0f : 0.0f),
348             };
349         }
350
351         /// <summary>
352         /// Update TrackThickness property of the scrollbar.
353         /// </summary>
354         /// <param name="thickness">The width of the track.</param>
355         [EditorBrowsable(EditorBrowsableState.Never)]
356         protected virtual void UpdateTrackThickness(float thickness)
357         {
358             if (trackVisual == null)
359             {
360                 return;
361             }
362
363             trackVisual.Size = calculator.CalculateTrackSize(thickness, TrackPadding);
364             trackVisual.UpdateVisual(true);
365         }
366
367         /// <summary>
368         /// Update ThumbThickness property of the scrollbar.
369         /// </summary>
370         /// <param name="thickness">The width of the track.</param>
371         [EditorBrowsable(EditorBrowsableState.Never)]
372         protected virtual void UpdateThumbThickness(float thickness)
373         {
374             if (thumbVisual == null)
375             {
376                 return;
377             }
378
379             thumbVisual.Size = calculator.CalculateThumbSize(thickness, trackVisual.Size);
380             thumbVisual.UpdateVisual(true);
381         }
382
383         /// <summary>
384         /// Update TrackColor property of the scrollbar.
385         /// </summary>
386         /// <param name="color">The color of the track.</param>
387         [EditorBrowsable(EditorBrowsableState.Never)]
388         protected virtual void UpdateTrackColor(Color color)
389         {
390             if (trackVisual == null)
391             {
392                 return;
393             }
394
395             trackVisual.MixColor = color;
396             trackVisual.UpdateVisual(true);
397         }
398
399         /// <summary>
400         /// Update ThumbColor property of the scrollbar.
401         /// </summary>
402         /// <param name="color">The color of the thumb.</param>
403         [EditorBrowsable(EditorBrowsableState.Never)]
404         protected virtual void UpdateThumbColor(Color color)
405         {
406             if (thumbVisual == null)
407             {
408                 return;
409             }
410
411             thumbVisual.MixColor = color;
412             thumbVisual.UpdateVisual(true);
413         }
414
415         /// <summary>
416         /// Update TrackPadding property of the scrollbar.
417         /// </summary>
418         /// <param name="trackPadding">The padding of the track.</param>
419         protected virtual void UpdateTrackPadding(Extents trackPadding)
420         {
421             if (trackVisual == null || thumbVisual == null)
422             {
423                 return;
424             }
425
426             trackVisual.Size = calculator.CalculateTrackSize(TrackThickness, trackPadding); 
427             trackVisual.Position = calculator.CalculateTrackPosition(trackPadding);
428             thumbVisual.Size = calculator.CalculateThumbSize(ThumbThickness, trackVisual.Size);
429             thumbVisual.Position = calculator.CalculateThumbPaddingPosition(trackVisual.Size, thumbVisual.Size, thumbVisual.Position, trackPadding);
430
431             trackVisual.UpdateVisual(true);
432             thumbVisual.UpdateVisual(true);
433         }
434
435         #endregion Methods
436
437
438         #region Classes
439
440         private abstract class Calculator
441         {
442             protected float contentLength;
443             protected float visibleLength;
444
445             public Calculator(float contentLength, float visibleLength)
446             {
447                 this.contentLength = contentLength;
448                 this.visibleLength = visibleLength;
449             }
450
451             public void Update(float contentLength)
452             {
453                 this.contentLength = contentLength;
454             }
455
456             public bool CalculateThumbVisibility()
457             {
458                 return contentLength > visibleLength;
459             }
460
461             public abstract Visual.AlignType CalculatorTrackAlign();
462             public abstract Visual.AlignType CalculatorThumbAlign();
463             public abstract Size CalculateTrackSize(float thickness, Extents trackPadding);
464             public abstract Vector2 CalculateTrackPosition(Extents trackPadding);
465             public abstract Size CalculateThumbSize(float thickness, Size trackSize);
466             public abstract Vector2 CalculateThumbPosition(float position, Size trackSize, Size thumbSize, Extents trackPadding);
467             public abstract Vector2 CalculateThumbPaddingPosition(Size trackSize, Size thumbSize, Vector2 thumbCurrentPosition, Extents trackPadding);
468             public abstract Vector2 CalculateThumbScrollPosition(float position, Size trackSize, Vector2 thumbCurrentPosition, Extents trackPadding);
469         }
470
471         private class HorizontalCalculator : Calculator
472         {
473             public HorizontalCalculator(float contentLength, float visibleLength) : base(contentLength, visibleLength)
474             {
475             }
476
477             public override Visual.AlignType CalculatorTrackAlign()
478             {
479                 return Visual.AlignType.BottomCenter;
480             }
481
482             public override Visual.AlignType CalculatorThumbAlign()
483             {
484                 return Visual.AlignType.BottomBegin;
485             }
486
487             public override Size CalculateTrackSize(float thickness, Extents trackPadding)
488             {
489                 return new Size(visibleLength - trackPadding.Start - trackPadding.End, thickness);
490             }
491
492             public override Vector2 CalculateTrackPosition(Extents trackPadding)
493             {
494                 return new Vector2(0, -trackPadding.Bottom);
495             }
496
497             public override Size CalculateThumbSize(float thickness, Size trackSize)
498             {
499                 return new Size(trackSize.Width * visibleLength / contentLength, thickness);
500             }
501
502             public override Vector2 CalculateThumbPosition(float position, Size trackSize, Size thumbSize, Extents trackPadding)
503             {
504                 float padding = ((trackSize.Height - thumbSize.Height) / 2.0f) + trackPadding.Bottom;
505                 float pos = Math.Min(Math.Max(position, 0.0f), contentLength - visibleLength);
506                 return new Vector2(trackPadding.Start + trackSize.Width * pos / contentLength, -padding);
507             }
508
509             public override Vector2 CalculateThumbPaddingPosition(Size trackSize, Size thumbSize, Vector2 thumbCurrentPosition, Extents trackPadding)
510             {
511                 float padding = ((trackSize.Height - thumbSize.Height) / 2.0f) + trackPadding.Bottom;
512                 return new Vector2(thumbCurrentPosition.X, -padding);
513             }
514
515             public override Vector2 CalculateThumbScrollPosition(float position, Size trackSize, Vector2 thumbCurrentPosition, Extents trackPadding)
516             {
517                 float pos = Math.Min(Math.Max(position, 0.0f), contentLength - visibleLength);
518                 return new Vector2(trackPadding.Start + trackSize.Width * pos / contentLength, thumbCurrentPosition.Y);
519             }
520         }
521
522         private class VerticalCalculator : Calculator
523         {
524             public VerticalCalculator(float contentLength, float visibleLength) : base(contentLength, visibleLength)
525             {
526             }
527
528             public override Visual.AlignType CalculatorTrackAlign()
529             {
530                 return Visual.AlignType.CenterEnd;
531             }
532
533             public override Visual.AlignType CalculatorThumbAlign()
534             {
535                 return Visual.AlignType.TopEnd;
536             }
537
538             public override Size CalculateTrackSize(float thickness, Extents trackPadding)
539             {
540                 return new Size(thickness, visibleLength - trackPadding.Top - trackPadding.Bottom);
541             }
542
543             public override Vector2 CalculateTrackPosition(Extents trackPadding)
544             {
545                 return new Vector2(-trackPadding.End, 0);
546             }
547
548             public override Size CalculateThumbSize(float thickness, Size trackSize)
549             {
550                 return new Size(thickness, trackSize.Height * visibleLength / contentLength);
551             }
552
553             public override Vector2 CalculateThumbPosition(float position, Size trackSize, Size thumbSize, Extents trackPadding)
554             {
555                 float padding = ((trackSize.Width - thumbSize.Width) / 2.0f) + trackPadding.End;
556                 float pos = Math.Min(Math.Max(position, 0.0f), contentLength - visibleLength);
557                 return new Vector2(-padding, trackPadding.Top + trackSize.Height * pos / contentLength);
558             }
559
560             public override Vector2 CalculateThumbPaddingPosition(Size trackSize, Size thumbSize, Vector2 thumbCurrentPosition, Extents trackPadding)
561             {
562                 float padding = ((trackSize.Width - thumbSize.Width) / 2.0f) + trackPadding.End;
563                 return new Vector2(-padding, thumbCurrentPosition.Y);
564             }
565
566             public override Vector2 CalculateThumbScrollPosition(float position, Size trackSize, Vector2 thumbPosition, Extents trackPadding)
567             {
568                 float pos = Math.Min(Math.Max(position, 0.0f), contentLength - visibleLength);
569                 return new Vector2(thumbPosition.X, trackPadding.Top + trackSize.Height * pos / contentLength);
570             }
571         }
572
573         #endregion Classes
574     }
575 }