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