2 * Copyright(c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using Tizen.NUI.BaseComponents;
19 using System.ComponentModel;
20 using Tizen.NUI.Binding;
22 namespace Tizen.NUI.Components
25 /// The ScrollBar class of nui component. It allows users to recognize the direction and the range of lists/content. .
27 /// <since_tizen> 6 </since_tizen>
28 public class ScrollBar : Control
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) =>
34 var instance = (ScrollBar)bindable;
37 instance.Style.Direction = (DirectionType?)newValue;
38 instance.UpdateValue();
41 defaultValueCreator: (bindable) =>
43 var instance = (ScrollBar)bindable;
44 return instance.Style.Direction;
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) =>
50 var instance = (ScrollBar)bindable;
53 if ((int)newValue >= 0)
55 instance.maxValue = (int)newValue;
56 instance.UpdateValue();
60 defaultValueCreator: (bindable) =>
62 var instance = (ScrollBar)bindable;
63 return instance.maxValue;
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) =>
69 var instance = (ScrollBar)bindable;
72 if ((int)newValue >= 0)
74 instance.minValue = (int)newValue;
75 instance.UpdateValue();
79 defaultValueCreator: (bindable) =>
81 var instance = (ScrollBar)bindable;
82 return instance.minValue;
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) =>
88 var instance = (ScrollBar)bindable;
91 if ((int)newValue >= 0)
93 instance.curValue = (int)newValue;
94 instance.UpdateValue();
98 defaultValueCreator: (bindable) =>
100 var instance = (ScrollBar)bindable;
101 return instance.curValue;
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) =>
107 var instance = (ScrollBar)bindable;
108 if (newValue != null)
110 instance.Style.Duration = (uint)newValue;
111 if (instance.scrollAniPlayer != null)
113 instance.scrollAniPlayer.Duration = (int)newValue;
117 defaultValueCreator: (bindable) =>
119 var instance = (ScrollBar)bindable;
120 return instance.Style.Duration;
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;
134 /// The constructor of ScrollBar.
136 /// <since_tizen> 6 </since_tizen>
137 public ScrollBar() : base()
143 /// The constructor of ScrollBar with specific style.
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)
155 /// The constructor of ScrollBar with specific style.
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)
167 /// The direction type of the Scroll.
169 /// <since_tizen> 6 </since_tizen>
170 public enum DirectionType
173 /// The Horizontal type.
175 /// <since_tizen> 6 </since_tizen>
179 /// The Vertical type.
181 /// <since_tizen> 6 </since_tizen>
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;
191 /// The property to get/set the direction of the ScrollBar.
193 /// <since_tizen> 6 </since_tizen>
194 public DirectionType Direction
198 return (DirectionType)GetValue(DirectionProperty);
202 SetValue(DirectionProperty, value);
207 /// The property to get/set the size of the thumb object.
209 /// <exception cref="InvalidOperationException">Throw when ThumbSize is null.</exception>
212 /// ScrollBar scroll;
215 /// scroll.ThumbSize = new Size(500, 10, 0);
217 /// catch(InvalidOperationException e)
219 /// Tizen.Log.Error(LogTag, "Failed to set ThumbSize value : " + e.Message);
223 /// <since_tizen> 6 </since_tizen>
224 public Size ThumbSize
228 if (Style.Thumb.Size == null)
230 Style.Thumb.Size = new Size();
232 return Style.Thumb.Size;
236 if (Style.Thumb.Size == null)
238 Style.Thumb.Size = new Size();
240 if (thumbImage != null)
242 Style.Thumb.Size.Width = value.Width;
243 Style.Thumb.Size.Height = value.Height;
250 /// The property to get/set the image URL of the track object.
252 /// <since_tizen> 6 </since_tizen>
253 public string TrackImageURL
257 return Style.Track.ResourceUrl.All;
261 if (trackImage != null)
263 if (Style.Track.ResourceUrl == null)
265 Style.Track.ResourceUrl = new StringSelector();
267 Style.Track.ResourceUrl.All = value;
274 /// The property to get/set the color of the track object.
276 /// <since_tizen> 6 </since_tizen>
277 public Color TrackColor
281 return Style.Track.BackgroundColor?.All;
285 if (Style.Track.BackgroundColor == null)
287 Style.Track.BackgroundColor = new ColorSelector { All = value };
291 Style.Track.BackgroundColor.All = value;
298 /// The property to get/set the color of the thumb object.
300 /// <since_tizen> 6 </since_tizen>
301 public Color ThumbColor
305 return Style.Thumb.BackgroundColor?.All;
309 if(Style.Thumb.BackgroundColor == null)
311 Style.Thumb.BackgroundColor = new ColorSelector { All = value };
315 Style.Thumb.BackgroundColor.All = value;
322 /// The property to get/set the max value of the ScrollBar.
324 /// <since_tizen> 6 </since_tizen>
329 return (int)GetValue(MaxValueProperty);
333 SetValue(MaxValueProperty, value);
338 /// The property to get/set the min value of the ScrollBar.
340 /// <since_tizen> 6 </since_tizen>
345 return (int)GetValue(MinValueProperty);
349 SetValue(MinValueProperty, value);
354 /// The property to get/set the current value of the ScrollBar.
356 /// <exception cref="ArgumentOutOfRangeException">Throw when Current value is less than Min value, or greater than Max value.</exception>
359 /// ScrollBar scroll;
360 /// scroll.MaxValue = 100;
361 /// scroll.MinValue = 0;
364 /// scroll.CurrentValue = 50;
366 /// catch(ArgumentOutOfRangeException e)
368 /// Tizen.Log.Error(LogTag, "Failed to set Current value : " + e.Message);
372 /// <since_tizen> 6 </since_tizen>
373 public int CurrentValue
377 return (int)GetValue(CurrentValueProperty);
381 SetValue(CurrentValueProperty, value);
386 /// Property to set/get animation duration.
388 /// <since_tizen> 6 </since_tizen>
393 return (uint)GetValue(DurationProperty);
397 SetValue(DurationProperty, value);
403 /// Method to set current value. The thumb object would move to the corresponding position with animation or not.
405 /// <param name="currentValue">The special current value.</param>
406 /// <param name="EnableAnimation">Enable move with animation or not, the default value is true.</param>
407 /// <exception cref="ArgumentOutOfRangeException">Throw when current size is less than the min value, or greater than the max value.</exception>
410 /// ScrollBar scroll;
411 /// scroll.MinValue = 0;
412 /// scroll.MaxValue = 100;
415 /// scroll.SetCurrentValue(50);
417 /// catch(ArgumentOutOfRangeException e)
419 /// Tizen.Log.Error(LogTag, "Failed to set current value : " + e.Message);
423 /// <since_tizen> 6 </since_tizen>
424 public void SetCurrentValue(int currentValue, bool enableAnimation = true)
426 if (currentValue < minValue || currentValue > maxValue)
428 //TNLog.E("Current value is less than the Min value, or greater than the Max value. currentValue = " + currentValue + ";");
429 throw new ArgumentOutOfRangeException("Wrong Current value. It shoud be greater than the Min value, and less than the Max value!");
432 enableAni = enableAnimation;
433 CurrentValue = currentValue;
437 /// Dispose ScrollBar.
439 /// <param name="type">The DisposeTypes value.</param>
440 /// <since_tizen> 6 </since_tizen>
441 protected override void Dispose(DisposeTypes type)
448 if (type == DisposeTypes.Explicit)
451 //Release your own managed resources here.
452 //You should release all of your own disposable objects here.
454 Utility.Dispose(trackImage);
455 Utility.Dispose(thumbImage);
457 if (scrollAniPlayer != null)
459 scrollAniPlayer.Stop();
460 scrollAniPlayer.Clear();
461 scrollAniPlayer.Dispose();
462 scrollAniPlayer = null;
466 //Release your own unmanaged resources here.
467 //You should not access any managed member here except static instance.
468 //because the execution order of Finalizes is non-deterministic.
469 //Unreference this from if a static instance refer to this.
471 //You must call base.Dispose(type) just before exit.
476 /// Get Scrollbar style.
478 /// <since_tizen> 6 </since_tizen>
479 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
480 [EditorBrowsable(EditorBrowsableState.Never)]
481 protected override ViewStyle GetViewStyle()
483 return new ScrollBarStyle();
487 /// Theme change callback when theme is changed, this callback will be trigger.
489 /// <since_tizen> 6 </since_tizen>
490 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
491 [EditorBrowsable(EditorBrowsableState.Never)]
492 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
494 ScrollBarStyle tempStyle = StyleManager.Instance.GetAttributes(style) as ScrollBarStyle;
495 if (tempStyle != null)
497 Style.CopyFrom(tempStyle);
502 private void Initialize()
504 this.Focusable = false;
506 trackImage = new ImageView
509 WidthResizePolicy = ResizePolicyType.FillToParent,
510 HeightResizePolicy = ResizePolicyType.FillToParent,
511 PositionUsesPivotPoint = true,
512 ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
513 PivotPoint = Tizen.NUI.PivotPoint.CenterLeft
515 this.Add(trackImage);
516 trackImage.ApplyStyle(Style.Track);
518 thumbImage = new ImageView
521 WidthResizePolicy = ResizePolicyType.Fixed,
522 HeightResizePolicy = ResizePolicyType.Fixed,
523 PositionUsesPivotPoint = true,
524 ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
525 PivotPoint = Tizen.NUI.PivotPoint.CenterLeft
527 this.Add(thumbImage);
528 thumbImage.ApplyStyle(Style.Thumb);
530 scrollAniPlayer = new Animation(334);
531 scrollAniPlayer.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
535 LayoutDirectionChanged += OnLayoutDirectionChanged;
538 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
543 private void UpdateValue()
545 if (minValue >= maxValue || curValue < minValue || curValue > maxValue) return;
547 float width = (float)Size2D.Width;
548 float height = (float)Size2D.Height;
551 if (Style.Thumb.Size == null)
557 thumbW = Style.Thumb.Size.Width;
558 thumbH = Style.Thumb.Size.Height;
560 float ratio = (float)(curValue - minValue) / (float)(maxValue - minValue);
562 if (Style.Direction == DirectionType.Horizontal)
564 if (LayoutDirection == ViewLayoutDirectionType.RTL)
566 ratio = 1.0f - ratio;
569 float posX = ratio * (width - thumbW);
570 float posY = (height - thumbH) / 2.0f;
572 thumbImagePosX = posX;
573 if (null != scrollAniPlayer)
575 scrollAniPlayer.Stop();
580 thumbImage.Position = new Position(posX, posY, 0);
584 if (null != scrollAniPlayer)
586 scrollAniPlayer.Clear();
587 scrollAniPlayer.AnimateTo(thumbImage, "PositionX", posX);
588 scrollAniPlayer.Play();
594 float posX = (width - thumbW) / 2.0f;
595 float posY = ratio * (height - thumbH);
597 thumbImagePosY = posY;
598 if (null != scrollAniPlayer)
600 scrollAniPlayer.Stop();
605 thumbImage.Position = new Position(posX, posY, 0);
609 if (null != scrollAniPlayer)
611 scrollAniPlayer.Clear();
612 scrollAniPlayer.AnimateTo(thumbImage, "PositionY", posY);
613 scrollAniPlayer.Play();
618 if (enableAni) enableAni = false;
621 private DirectionType CurrentDirection()
623 DirectionType dir = DirectionType.Horizontal;
624 if (null != Style.Direction)
626 dir = Style.Direction.Value;
631 private int CalculateCurrentValue(float offset, DirectionType dir)
633 if (dir == DirectionType.Horizontal)
635 thumbImagePosX += offset;
636 if (thumbImagePosX < 0)
638 thumbImage.PositionX = 0;
641 else if (thumbImagePosX > Size2D.Width - thumbImage.Size2D.Width)
643 thumbImage.PositionX = Size2D.Width - thumbImage.Size2D.Width;
648 thumbImage.PositionX = thumbImagePosX;
649 curValue = (int)((thumbImagePosX / (float)(Size2D.Width - thumbImage.Size2D.Width)) * (float)(maxValue - minValue) + 0.5f);
654 thumbImagePosY += offset;
655 if (thumbImagePosY < 0)
657 thumbImage.PositionY = 0;
660 else if (thumbImagePosY > Size2D.Height - thumbImage.Size2D.Height)
662 thumbImage.PositionY = Size2D.Height - thumbImage.Size2D.Height;
667 thumbImage.PositionY = thumbImagePosY;
668 curValue = (int)((thumbImagePosY / (float)(Size2D.Height - thumbImage.Size2D.Height)) * (float)(maxValue - minValue) + 0.5f);