[NUI.Components] Fix build warnings (#1233)
[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)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 #pragma warning disable CA2208 // Instantiate argument exceptions correctly
409                 throw new ArgumentOutOfRangeException("Wrong Current value. It shoud be greater than the Min value, and less than the Max value!");
410 #pragma warning restore CA2208 // Instantiate argument exceptions correctly
411             }
412
413             enableAni = enableAnimation;
414             CurrentValue = currentValue;
415         }
416
417         /// <summary>
418         /// Dispose ScrollBar.
419         /// </summary>
420         /// <param name="type">The DisposeTypes value.</param>
421         /// <since_tizen> 6 </since_tizen>
422         protected override void Dispose(DisposeTypes type)
423         {
424             if (disposed)
425             {
426                 return;
427             }
428
429             if (type == DisposeTypes.Explicit)
430             {
431                 //Called by User
432                 //Release your own managed resources here.
433                 //You should release all of your own disposable objects here.
434
435                 Utility.Dispose(trackImage);
436                 Utility.Dispose(thumbImage);
437
438                 if (scrollAniPlayer != null)
439                 {
440                     scrollAniPlayer.Stop();
441                     scrollAniPlayer.Clear();
442                     scrollAniPlayer.Dispose();
443                     scrollAniPlayer = null;
444                 }
445             }
446
447             //Release your own unmanaged resources here.
448             //You should not access any managed member here except static instance.
449             //because the execution order of Finalizes is non-deterministic.
450             //Unreference this from if a static instance refer to this. 
451
452             //You must call base.Dispose(type) just before exit.
453             base.Dispose(type);
454         }
455
456         /// <summary>
457         /// Get Scrollbar style.
458         /// </summary>
459         /// <since_tizen> 6 </since_tizen>
460         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
461         [EditorBrowsable(EditorBrowsableState.Never)]
462         protected override ViewStyle GetViewStyle()
463         {
464             return new ScrollBarStyle();
465         }
466
467         /// <summary>
468         /// Theme change callback when theme is changed, this callback will be trigger.
469         /// </summary>
470         /// <since_tizen> 6 </since_tizen>
471         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
472         [EditorBrowsable(EditorBrowsableState.Never)]
473         protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
474         {
475             ScrollBarStyle tempStyle = StyleManager.Instance.GetViewStyle(style) as ScrollBarStyle;
476             if (tempStyle != null)
477             {
478                 Style.CopyFrom(tempStyle);
479                 UpdateValue();
480             }
481         }
482
483         private void Initialize()
484         {
485             this.Focusable = false;
486
487             trackImage = new ImageView
488             {
489                 Focusable = false,
490                 WidthResizePolicy = ResizePolicyType.FillToParent,
491                 HeightResizePolicy = ResizePolicyType.FillToParent,
492                 PositionUsesPivotPoint = true,
493                 ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
494                 PivotPoint = Tizen.NUI.PivotPoint.CenterLeft
495             };
496             this.Add(trackImage);
497             trackImage.ApplyStyle(Style.Track);
498
499             thumbImage = new ImageView
500             {
501                 Focusable = false,
502                 WidthResizePolicy = ResizePolicyType.Fixed,
503                 HeightResizePolicy = ResizePolicyType.Fixed,
504                 PositionUsesPivotPoint = true,
505                 ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
506                 PivotPoint = Tizen.NUI.PivotPoint.CenterLeft
507             };
508             this.Add(thumbImage);
509             thumbImage.ApplyStyle(Style.Thumb);
510
511             scrollAniPlayer = new Animation(334);
512             scrollAniPlayer.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
513
514             thumbImagePosX = 0;
515             thumbImagePosY = 0;
516             LayoutDirectionChanged += OnLayoutDirectionChanged;
517         }
518
519         private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
520         {
521             RelayoutRequest();
522         }
523
524         private void UpdateValue()
525         {
526             if (minValue >= maxValue || curValue < minValue || curValue > maxValue) return;
527
528             float width = (float)Size2D.Width;
529             float height = (float)Size2D.Height;
530             float thumbW = 0.0f;
531             float thumbH = 0.0f;
532             if (Style.Thumb.Size == null)
533             {
534                 return;
535             }
536             else
537             {
538                 thumbW = Style.Thumb.Size.Width;
539                 thumbH = Style.Thumb.Size.Height;
540             }
541             float ratio = (float)(curValue - minValue) / (float)(maxValue - minValue);
542
543             if (Style.Direction == DirectionType.Horizontal)
544             {
545                 if (LayoutDirection == ViewLayoutDirectionType.RTL)
546                 {
547                     ratio = 1.0f - ratio;
548                 }
549
550                 float posX = ratio * (width - thumbW);
551                 float posY = (height - thumbH) / 2.0f;
552
553                 thumbImagePosX = posX;
554                 if (null != scrollAniPlayer)
555                 {
556                     scrollAniPlayer.Stop();
557                 }
558
559                 if (!enableAni)
560                 {
561                     thumbImage.Position = new Position(posX, posY, 0);
562                 }
563                 else
564                 {
565                     if (null != scrollAniPlayer)
566                     {
567                         scrollAniPlayer.Clear();
568                         scrollAniPlayer.AnimateTo(thumbImage, "PositionX", posX);
569                         scrollAniPlayer.Play();
570                     }
571                 }
572             }
573             else
574             {
575                 float posX = (width - thumbW) / 2.0f;
576                 float posY = ratio * (height - thumbH);
577
578                 thumbImagePosY = posY;
579                 if (null != scrollAniPlayer)
580                 {
581                     scrollAniPlayer.Stop();
582                 }
583
584                 if (!enableAni)
585                 {
586                     thumbImage.Position = new Position(posX, posY, 0);
587                 }
588                 else
589                 {
590                     if (null != scrollAniPlayer)
591                     {
592                         scrollAniPlayer.Clear();
593                         scrollAniPlayer.AnimateTo(thumbImage, "PositionY", posY);
594                         scrollAniPlayer.Play();
595                     }
596                 }
597             }
598
599             if (enableAni) enableAni = false;
600         }
601
602         private DirectionType CurrentDirection()
603         {
604             DirectionType dir = DirectionType.Horizontal;
605             if (null != Style.Direction)
606             {
607                 dir = Style.Direction.Value;
608             }
609             return dir;
610         }
611
612         private int CalculateCurrentValue(float offset, DirectionType dir)
613         {
614             if (dir == DirectionType.Horizontal)
615             {
616                 thumbImagePosX += offset;
617                 if (thumbImagePosX < 0)
618                 {
619                     thumbImage.PositionX = 0;
620                     curValue = minValue;
621                 }
622                 else if (thumbImagePosX > Size2D.Width - thumbImage.Size2D.Width)
623                 {
624                     thumbImage.PositionX = Size2D.Width - thumbImage.Size2D.Width;
625                     curValue = maxValue;
626                 }
627                 else
628                 {
629                     thumbImage.PositionX = thumbImagePosX;
630                     curValue = (int)((thumbImagePosX / (float)(Size2D.Width - thumbImage.Size2D.Width)) * (float)(maxValue - minValue) + 0.5f);
631                 }
632             }
633             else
634             {
635                 thumbImagePosY += offset;
636                 if (thumbImagePosY < 0)
637                 {
638                     thumbImage.PositionY = 0;
639                     curValue = minValue;
640                 }
641                 else if (thumbImagePosY > Size2D.Height - thumbImage.Size2D.Height)
642                 {
643                     thumbImage.PositionY = Size2D.Height - thumbImage.Size2D.Height;
644                     curValue = maxValue;
645                 }
646                 else
647                 {
648                     thumbImage.PositionY = thumbImagePosY;
649                     curValue = (int)((thumbImagePosY / (float)(Size2D.Height - thumbImage.Size2D.Height)) * (float)(maxValue - minValue) + 0.5f);
650                 }
651             }
652
653             return curValue;
654         }
655     }
656 }