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