Refine background property of control (#1183)
[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                 return Style?.Thumb?.Size;
229             }
230             set
231             {
232                 if (null != Style?.Thumb)
233                 {
234                     Style.Thumb.Size = value;
235                     RelayoutRequest();
236                 }
237             }
238         }
239
240         /// <summary>
241         /// The property to get/set the image URL of the track object.
242         /// </summary>
243         /// <since_tizen> 6 </since_tizen>
244         public string TrackImageURL
245         {
246             get
247             {
248                 return Style?.Track?.ResourceUrl?.All;
249             }
250             set
251             {
252                 if (null != Style?.Track)
253                 {
254                     Style.Track.ResourceUrl = value;
255                     RelayoutRequest();
256                 }
257             }
258         }
259
260         /// <summary>
261         /// The property to get/set the color of the track object.
262         /// </summary>
263         /// <since_tizen> 6 </since_tizen>
264         public Color TrackColor
265         {
266             get
267             {
268                 return Style?.Track?.BackgroundColor?.All;
269             }
270             set
271             {
272                 if (null != Style?.Track)
273                 {
274                     Style.Track.BackgroundColor = value;
275                     RelayoutRequest();
276                 }
277             }
278         }
279
280         /// <summary>
281         /// The property to get/set the color of the thumb object.
282         /// </summary>
283         /// <since_tizen> 6 </since_tizen>
284         public Color ThumbColor
285         {
286             get
287             {
288                 return Style?.Thumb?.BackgroundColor?.All;
289             }
290             set
291             {
292                 if(null != Style?.Thumb)
293                 {
294                     Style.Thumb.BackgroundColor = value;
295                     RelayoutRequest();
296                 }
297             }
298         }
299
300         /// <summary>
301         /// The property to get/set the max value of the ScrollBar.
302         /// </summary>
303         /// <since_tizen> 6 </since_tizen>
304         public int MaxValue
305         {
306             get
307             {
308                 return (int)GetValue(MaxValueProperty);
309             }
310             set
311             {
312                 SetValue(MaxValueProperty, value);
313             }
314         }
315
316         /// <summary>
317         /// The property to get/set the min value of the ScrollBar.
318         /// </summary>
319         /// <since_tizen> 6 </since_tizen>
320         public int MinValue
321         {
322             get
323             {
324                 return (int)GetValue(MinValueProperty);
325             }
326             set
327             {
328                 SetValue(MinValueProperty, value);
329             }
330         }
331
332         /// <summary>
333         /// The property to get/set the current value of the ScrollBar.
334         /// </summary>
335         /// <exception cref="ArgumentOutOfRangeException">Throw when Current value is less than Min value, or greater than Max value.</exception>
336         /// <example>
337         /// <code>
338         /// ScrollBar scroll;
339         /// scroll.MaxValue = 100;
340         /// scroll.MinValue = 0;
341         /// try
342         /// {
343         ///     scroll.CurrentValue = 50;
344         /// }
345         /// catch(ArgumentOutOfRangeException e)
346         /// {
347         ///     Tizen.Log.Error(LogTag, "Failed to set Current value : " + e.Message);
348         /// }
349         /// </code>
350         /// </example>
351         /// <since_tizen> 6 </since_tizen>
352         public int CurrentValue
353         {
354             get
355             {
356                 return (int)GetValue(CurrentValueProperty);
357             }
358             set
359             {
360                 SetValue(CurrentValueProperty, value);
361             }
362         }
363
364         /// <summary>
365         /// Property to set/get animation duration.
366         /// </summary>
367         /// <since_tizen> 6 </since_tizen>
368         public uint Duration
369         {
370             get
371             {
372                 return (uint)GetValue(DurationProperty);
373             }
374             set
375             {
376                 SetValue(DurationProperty, value);
377             }
378         }
379         #endregion
380
381         /// <summary>
382         /// Method to set current value. The thumb object would move to the corresponding position with animation or not.
383         /// </summary>
384         /// <param name="currentValue">The special current value.</param>
385         /// <param name="EnableAnimation">Enable move with animation or not, the default value is true.</param>
386         /// <exception cref="ArgumentOutOfRangeException">Throw when current size is less than the min value, or greater than the max value.</exception>
387         /// <example>
388         /// <code>
389         /// ScrollBar scroll;
390         /// scroll.MinValue = 0;
391         /// scroll.MaxValue = 100;
392         /// try
393         /// {
394         ///     scroll.SetCurrentValue(50);
395         /// }
396         /// catch(ArgumentOutOfRangeException e)
397         /// {
398         ///     Tizen.Log.Error(LogTag, "Failed to set current value : " + e.Message);
399         /// }
400         /// </code>
401         /// </example>
402         /// <since_tizen> 6 </since_tizen>
403         public void SetCurrentValue(int currentValue, bool enableAnimation = true)
404         {
405             if (currentValue < minValue || currentValue > maxValue)
406             {
407                 //TNLog.E("Current value is less than the Min value, or greater than the Max value. currentValue = " + currentValue + ";");
408                 throw new ArgumentOutOfRangeException("Wrong Current value. It shoud be greater than the Min value, and less than the Max value!");
409             }
410
411             enableAni = enableAnimation;
412             CurrentValue = currentValue;
413         }
414
415         /// <summary>
416         /// Dispose ScrollBar.
417         /// </summary>
418         /// <param name="type">The DisposeTypes value.</param>
419         /// <since_tizen> 6 </since_tizen>
420         protected override void Dispose(DisposeTypes type)
421         {
422             if (disposed)
423             {
424                 return;
425             }
426
427             if (type == DisposeTypes.Explicit)
428             {
429                 //Called by User
430                 //Release your own managed resources here.
431                 //You should release all of your own disposable objects here.
432
433                 Utility.Dispose(trackImage);
434                 Utility.Dispose(thumbImage);
435
436                 if (scrollAniPlayer != null)
437                 {
438                     scrollAniPlayer.Stop();
439                     scrollAniPlayer.Clear();
440                     scrollAniPlayer.Dispose();
441                     scrollAniPlayer = null;
442                 }
443             }
444
445             //Release your own unmanaged resources here.
446             //You should not access any managed member here except static instance.
447             //because the execution order of Finalizes is non-deterministic.
448             //Unreference this from if a static instance refer to this. 
449
450             //You must call base.Dispose(type) just before exit.
451             base.Dispose(type);
452         }
453
454         /// <summary>
455         /// Get Scrollbar style.
456         /// </summary>
457         /// <since_tizen> 6 </since_tizen>
458         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
459         [EditorBrowsable(EditorBrowsableState.Never)]
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         /// <since_tizen> 6 </since_tizen>
469         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
470         [EditorBrowsable(EditorBrowsableState.Never)]
471         protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
472         {
473             ScrollBarStyle tempStyle = StyleManager.Instance.GetAttributes(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 }