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 return Style?.Thumb?.Size;
232 if (null != Style?.Thumb)
234 Style.Thumb.Size = value;
241 /// The property to get/set the image URL of the track object.
243 /// <since_tizen> 6 </since_tizen>
244 public string TrackImageURL
248 return Style?.Track?.ResourceUrl?.All;
252 if (null != Style?.Track)
254 Style.Track.ResourceUrl = value;
261 /// The property to get/set the color of the track object.
263 /// <since_tizen> 6 </since_tizen>
264 public Color TrackColor
268 return Style?.Track?.BackgroundColor?.All;
272 if (null != Style?.Track)
274 Style.Track.BackgroundColor = value;
281 /// The property to get/set the color of the thumb object.
283 /// <since_tizen> 6 </since_tizen>
284 public Color ThumbColor
288 return Style?.Thumb?.BackgroundColor?.All;
292 if(null != Style?.Thumb)
294 Style.Thumb.BackgroundColor = value;
301 /// The property to get/set the max value of the ScrollBar.
303 /// <since_tizen> 6 </since_tizen>
308 return (int)GetValue(MaxValueProperty);
312 SetValue(MaxValueProperty, value);
317 /// The property to get/set the min value of the ScrollBar.
319 /// <since_tizen> 6 </since_tizen>
324 return (int)GetValue(MinValueProperty);
328 SetValue(MinValueProperty, value);
333 /// The property to get/set the current value of the ScrollBar.
335 /// <exception cref="ArgumentOutOfRangeException">Throw when Current value is less than Min value, or greater than Max value.</exception>
338 /// ScrollBar scroll;
339 /// scroll.MaxValue = 100;
340 /// scroll.MinValue = 0;
343 /// scroll.CurrentValue = 50;
345 /// catch(ArgumentOutOfRangeException e)
347 /// Tizen.Log.Error(LogTag, "Failed to set Current value : " + e.Message);
351 /// <since_tizen> 6 </since_tizen>
352 public int CurrentValue
356 return (int)GetValue(CurrentValueProperty);
360 SetValue(CurrentValueProperty, value);
365 /// Property to set/get animation duration.
367 /// <since_tizen> 6 </since_tizen>
372 return (uint)GetValue(DurationProperty);
376 SetValue(DurationProperty, value);
382 /// Method to set current value. The thumb object would move to the corresponding position with animation or not.
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>
389 /// ScrollBar scroll;
390 /// scroll.MinValue = 0;
391 /// scroll.MaxValue = 100;
394 /// scroll.SetCurrentValue(50);
396 /// catch(ArgumentOutOfRangeException e)
398 /// Tizen.Log.Error(LogTag, "Failed to set current value : " + e.Message);
402 /// <since_tizen> 6 </since_tizen>
403 public void SetCurrentValue(int currentValue, bool enableAnimation = true)
405 if (currentValue < minValue || currentValue > maxValue)
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!");
411 enableAni = enableAnimation;
412 CurrentValue = currentValue;
416 /// Dispose ScrollBar.
418 /// <param name="type">The DisposeTypes value.</param>
419 /// <since_tizen> 6 </since_tizen>
420 protected override void Dispose(DisposeTypes type)
427 if (type == DisposeTypes.Explicit)
430 //Release your own managed resources here.
431 //You should release all of your own disposable objects here.
433 Utility.Dispose(trackImage);
434 Utility.Dispose(thumbImage);
436 if (scrollAniPlayer != null)
438 scrollAniPlayer.Stop();
439 scrollAniPlayer.Clear();
440 scrollAniPlayer.Dispose();
441 scrollAniPlayer = null;
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.
450 //You must call base.Dispose(type) just before exit.
455 /// Get Scrollbar style.
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()
462 return new ScrollBarStyle();
466 /// Theme change callback when theme is changed, this callback will be trigger.
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)
473 ScrollBarStyle tempStyle = StyleManager.Instance.GetViewStyle(style) as ScrollBarStyle;
474 if (tempStyle != null)
476 Style.CopyFrom(tempStyle);
481 private void Initialize()
483 this.Focusable = false;
485 trackImage = new ImageView
488 WidthResizePolicy = ResizePolicyType.FillToParent,
489 HeightResizePolicy = ResizePolicyType.FillToParent,
490 PositionUsesPivotPoint = true,
491 ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
492 PivotPoint = Tizen.NUI.PivotPoint.CenterLeft
494 this.Add(trackImage);
495 trackImage.ApplyStyle(Style.Track);
497 thumbImage = new ImageView
500 WidthResizePolicy = ResizePolicyType.Fixed,
501 HeightResizePolicy = ResizePolicyType.Fixed,
502 PositionUsesPivotPoint = true,
503 ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
504 PivotPoint = Tizen.NUI.PivotPoint.CenterLeft
506 this.Add(thumbImage);
507 thumbImage.ApplyStyle(Style.Thumb);
509 scrollAniPlayer = new Animation(334);
510 scrollAniPlayer.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
514 LayoutDirectionChanged += OnLayoutDirectionChanged;
517 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
522 private void UpdateValue()
524 if (minValue >= maxValue || curValue < minValue || curValue > maxValue) return;
526 float width = (float)Size2D.Width;
527 float height = (float)Size2D.Height;
530 if (Style.Thumb.Size == null)
536 thumbW = Style.Thumb.Size.Width;
537 thumbH = Style.Thumb.Size.Height;
539 float ratio = (float)(curValue - minValue) / (float)(maxValue - minValue);
541 if (Style.Direction == DirectionType.Horizontal)
543 if (LayoutDirection == ViewLayoutDirectionType.RTL)
545 ratio = 1.0f - ratio;
548 float posX = ratio * (width - thumbW);
549 float posY = (height - thumbH) / 2.0f;
551 thumbImagePosX = posX;
552 if (null != scrollAniPlayer)
554 scrollAniPlayer.Stop();
559 thumbImage.Position = new Position(posX, posY, 0);
563 if (null != scrollAniPlayer)
565 scrollAniPlayer.Clear();
566 scrollAniPlayer.AnimateTo(thumbImage, "PositionX", posX);
567 scrollAniPlayer.Play();
573 float posX = (width - thumbW) / 2.0f;
574 float posY = ratio * (height - thumbH);
576 thumbImagePosY = posY;
577 if (null != scrollAniPlayer)
579 scrollAniPlayer.Stop();
584 thumbImage.Position = new Position(posX, posY, 0);
588 if (null != scrollAniPlayer)
590 scrollAniPlayer.Clear();
591 scrollAniPlayer.AnimateTo(thumbImage, "PositionY", posY);
592 scrollAniPlayer.Play();
597 if (enableAni) enableAni = false;
600 private DirectionType CurrentDirection()
602 DirectionType dir = DirectionType.Horizontal;
603 if (null != Style.Direction)
605 dir = Style.Direction.Value;
610 private int CalculateCurrentValue(float offset, DirectionType dir)
612 if (dir == DirectionType.Horizontal)
614 thumbImagePosX += offset;
615 if (thumbImagePosX < 0)
617 thumbImage.PositionX = 0;
620 else if (thumbImagePosX > Size2D.Width - thumbImage.Size2D.Width)
622 thumbImage.PositionX = Size2D.Width - thumbImage.Size2D.Width;
627 thumbImage.PositionX = thumbImagePosX;
628 curValue = (int)((thumbImagePosX / (float)(Size2D.Width - thumbImage.Size2D.Width)) * (float)(maxValue - minValue) + 0.5f);
633 thumbImagePosY += offset;
634 if (thumbImagePosY < 0)
636 thumbImage.PositionY = 0;
639 else if (thumbImagePosY > Size2D.Height - thumbImage.Size2D.Height)
641 thumbImage.PositionY = Size2D.Height - thumbImage.Size2D.Height;
646 thumbImage.PositionY = thumbImagePosY;
647 curValue = (int)((thumbImagePosY / (float)(Size2D.Height - thumbImage.Size2D.Height)) * (float)(maxValue - minValue) + 0.5f);