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(nameof(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(nameof(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(nameof(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(nameof(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(nameof(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 #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
413 enableAni = enableAnimation;
414 CurrentValue = currentValue;
418 /// Dispose ScrollBar.
420 /// <param name="type">The DisposeTypes value.</param>
421 /// <since_tizen> 6 </since_tizen>
422 protected override void Dispose(DisposeTypes type)
429 if (type == DisposeTypes.Explicit)
432 //Release your own managed resources here.
433 //You should release all of your own disposable objects here.
435 Utility.Dispose(trackImage);
436 Utility.Dispose(thumbImage);
438 if (scrollAniPlayer != null)
440 scrollAniPlayer.Stop();
441 scrollAniPlayer.Clear();
442 scrollAniPlayer.Dispose();
443 scrollAniPlayer = null;
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.
452 //You must call base.Dispose(type) just before exit.
457 /// Get Scrollbar style.
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()
464 return new ScrollBarStyle();
468 /// Theme change callback when theme is changed, this callback will be trigger.
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)
475 ScrollBarStyle tempStyle = StyleManager.Instance.GetViewStyle(style) as ScrollBarStyle;
476 if (tempStyle != null)
478 Style.CopyFrom(tempStyle);
483 private void Initialize()
485 this.Focusable = false;
487 trackImage = new ImageView
490 WidthResizePolicy = ResizePolicyType.FillToParent,
491 HeightResizePolicy = ResizePolicyType.FillToParent,
492 PositionUsesPivotPoint = true,
493 ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
494 PivotPoint = Tizen.NUI.PivotPoint.CenterLeft
496 this.Add(trackImage);
497 trackImage.ApplyStyle(Style.Track);
499 thumbImage = new ImageView
502 WidthResizePolicy = ResizePolicyType.Fixed,
503 HeightResizePolicy = ResizePolicyType.Fixed,
504 PositionUsesPivotPoint = true,
505 ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
506 PivotPoint = Tizen.NUI.PivotPoint.CenterLeft
508 this.Add(thumbImage);
509 thumbImage.ApplyStyle(Style.Thumb);
511 scrollAniPlayer = new Animation(334);
512 scrollAniPlayer.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
516 LayoutDirectionChanged += OnLayoutDirectionChanged;
519 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
524 private void UpdateValue()
526 if (minValue >= maxValue || curValue < minValue || curValue > maxValue) return;
528 float width = (float)Size2D.Width;
529 float height = (float)Size2D.Height;
532 if (Style.Thumb.Size == null)
538 thumbW = Style.Thumb.Size.Width;
539 thumbH = Style.Thumb.Size.Height;
541 float ratio = (float)(curValue - minValue) / (float)(maxValue - minValue);
543 if (Style.Direction == DirectionType.Horizontal)
545 if (LayoutDirection == ViewLayoutDirectionType.RTL)
547 ratio = 1.0f - ratio;
550 float posX = ratio * (width - thumbW);
551 float posY = (height - thumbH) / 2.0f;
553 thumbImagePosX = posX;
554 if (null != scrollAniPlayer)
556 scrollAniPlayer.Stop();
561 thumbImage.Position = new Position(posX, posY, 0);
565 if (null != scrollAniPlayer)
567 scrollAniPlayer.Clear();
568 scrollAniPlayer.AnimateTo(thumbImage, "PositionX", posX);
569 scrollAniPlayer.Play();
575 float posX = (width - thumbW) / 2.0f;
576 float posY = ratio * (height - thumbH);
578 thumbImagePosY = posY;
579 if (null != scrollAniPlayer)
581 scrollAniPlayer.Stop();
586 thumbImage.Position = new Position(posX, posY, 0);
590 if (null != scrollAniPlayer)
592 scrollAniPlayer.Clear();
593 scrollAniPlayer.AnimateTo(thumbImage, "PositionY", posY);
594 scrollAniPlayer.Play();
599 if (enableAni) enableAni = false;
602 private DirectionType CurrentDirection()
604 DirectionType dir = DirectionType.Horizontal;
605 if (null != Style.Direction)
607 dir = Style.Direction.Value;
612 private int CalculateCurrentValue(float offset, DirectionType dir)
614 if (dir == DirectionType.Horizontal)
616 thumbImagePosX += offset;
617 if (thumbImagePosX < 0)
619 thumbImage.PositionX = 0;
622 else if (thumbImagePosX > Size2D.Width - thumbImage.Size2D.Width)
624 thumbImage.PositionX = Size2D.Width - thumbImage.Size2D.Width;
629 thumbImage.PositionX = thumbImagePosX;
630 curValue = (int)((thumbImagePosX / (float)(Size2D.Width - thumbImage.Size2D.Width)) * (float)(maxValue - minValue) + 0.5f);
635 thumbImagePosY += offset;
636 if (thumbImagePosY < 0)
638 thumbImage.PositionY = 0;
641 else if (thumbImagePosY > Size2D.Height - thumbImage.Size2D.Height)
643 thumbImage.PositionY = Size2D.Height - thumbImage.Size2D.Height;
648 thumbImage.PositionY = thumbImagePosY;
649 curValue = (int)((thumbImagePosY / (float)(Size2D.Height - thumbImage.Size2D.Height)) * (float)(maxValue - minValue) + 0.5f);