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;
132 static ScrollBar() { }
135 /// The constructor of ScrollBar.
137 /// <since_tizen> 6 </since_tizen>
138 public ScrollBar() : base()
144 /// The constructor of ScrollBar with specific style.
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)
156 /// The constructor of ScrollBar with specific style.
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)
168 /// The direction type of the Scroll.
170 /// <since_tizen> 6 </since_tizen>
171 public enum DirectionType
174 /// The Horizontal type.
176 /// <since_tizen> 6 </since_tizen>
180 /// The Vertical type.
182 /// <since_tizen> 6 </since_tizen>
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;
192 /// The property to get/set the direction of the ScrollBar.
194 /// <since_tizen> 6 </since_tizen>
195 public DirectionType Direction
199 return (DirectionType)GetValue(DirectionProperty);
203 SetValue(DirectionProperty, value);
208 /// The property to get/set the size of the thumb object.
210 /// <exception cref="InvalidOperationException">Throw when ThumbSize is null.</exception>
213 /// ScrollBar scroll;
216 /// scroll.ThumbSize = new Size(500, 10, 0);
218 /// catch(InvalidOperationException e)
220 /// Tizen.Log.Error(LogTag, "Failed to set ThumbSize value : " + e.Message);
224 /// <since_tizen> 6 </since_tizen>
225 public Size ThumbSize
229 return Style?.Thumb?.Size;
233 if (null != Style?.Thumb)
235 Style.Thumb.Size = value;
242 /// The property to get/set the image URL of the track object.
244 /// <since_tizen> 6 </since_tizen>
245 public string TrackImageURL
249 return Style?.Track?.ResourceUrl?.All;
253 if (null != Style?.Track)
255 Style.Track.ResourceUrl = value;
262 /// The property to get/set the color of the track object.
264 /// <since_tizen> 6 </since_tizen>
265 public Color TrackColor
269 return Style?.Track?.BackgroundColor?.All;
273 if (null != Style?.Track)
275 Style.Track.BackgroundColor = value;
282 /// The property to get/set the color of the thumb object.
284 /// <since_tizen> 6 </since_tizen>
285 public Color ThumbColor
289 return Style?.Thumb?.BackgroundColor?.All;
293 if(null != Style?.Thumb)
295 Style.Thumb.BackgroundColor = value;
302 /// The property to get/set the max value of the ScrollBar.
304 /// <since_tizen> 6 </since_tizen>
309 return (int)GetValue(MaxValueProperty);
313 SetValue(MaxValueProperty, value);
318 /// The property to get/set the min value of the ScrollBar.
320 /// <since_tizen> 6 </since_tizen>
325 return (int)GetValue(MinValueProperty);
329 SetValue(MinValueProperty, value);
334 /// The property to get/set the current value of the ScrollBar.
336 /// <exception cref="ArgumentOutOfRangeException">Throw when Current value is less than Min value, or greater than Max value.</exception>
339 /// ScrollBar scroll;
340 /// scroll.MaxValue = 100;
341 /// scroll.MinValue = 0;
344 /// scroll.CurrentValue = 50;
346 /// catch(ArgumentOutOfRangeException e)
348 /// Tizen.Log.Error(LogTag, "Failed to set Current value : " + e.Message);
352 /// <since_tizen> 6 </since_tizen>
353 public int CurrentValue
357 return (int)GetValue(CurrentValueProperty);
361 SetValue(CurrentValueProperty, value);
366 /// Property to set/get animation duration.
368 /// <since_tizen> 6 </since_tizen>
373 return (uint)GetValue(DurationProperty);
377 SetValue(DurationProperty, value);
383 /// Method to set current value. The thumb object would move to the corresponding position with animation or not.
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>
390 /// ScrollBar scroll;
391 /// scroll.MinValue = 0;
392 /// scroll.MaxValue = 100;
395 /// scroll.SetCurrentValue(50);
397 /// catch(ArgumentOutOfRangeException e)
399 /// Tizen.Log.Error(LogTag, "Failed to set current value : " + e.Message);
403 /// <since_tizen> 6 </since_tizen>
404 public void SetCurrentValue(int currentValue, bool enableAnimation = true)
406 if (currentValue < minValue || currentValue > maxValue)
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
414 enableAni = enableAnimation;
415 CurrentValue = currentValue;
419 /// Dispose ScrollBar.
421 /// <param name="type">The DisposeTypes value.</param>
422 /// <since_tizen> 6 </since_tizen>
423 protected override void Dispose(DisposeTypes type)
430 if (type == DisposeTypes.Explicit)
433 //Release your own managed resources here.
434 //You should release all of your own disposable objects here.
436 Utility.Dispose(trackImage);
437 Utility.Dispose(thumbImage);
439 if (scrollAniPlayer != null)
441 scrollAniPlayer.Stop();
442 scrollAniPlayer.Clear();
443 scrollAniPlayer.Dispose();
444 scrollAniPlayer = null;
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.
453 //You must call base.Dispose(type) just before exit.
458 /// Get Scrollbar style.
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()
465 return new ScrollBarStyle();
469 /// Theme change callback when theme is changed, this callback will be trigger.
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)
476 ScrollBarStyle tempStyle = StyleManager.Instance.GetViewStyle(style) as ScrollBarStyle;
477 if (tempStyle != null)
479 Style.CopyFrom(tempStyle);
484 private void Initialize()
486 this.Focusable = false;
488 trackImage = new ImageView
491 WidthResizePolicy = ResizePolicyType.FillToParent,
492 HeightResizePolicy = ResizePolicyType.FillToParent,
493 PositionUsesPivotPoint = true,
494 ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
495 PivotPoint = Tizen.NUI.PivotPoint.CenterLeft
497 this.Add(trackImage);
498 trackImage.ApplyStyle(Style.Track);
500 thumbImage = new ImageView
503 WidthResizePolicy = ResizePolicyType.Fixed,
504 HeightResizePolicy = ResizePolicyType.Fixed,
505 PositionUsesPivotPoint = true,
506 ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
507 PivotPoint = Tizen.NUI.PivotPoint.CenterLeft
509 this.Add(thumbImage);
510 thumbImage.ApplyStyle(Style.Thumb);
512 scrollAniPlayer = new Animation(334);
513 scrollAniPlayer.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
517 LayoutDirectionChanged += OnLayoutDirectionChanged;
520 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
525 private void UpdateValue()
527 if (minValue >= maxValue || curValue < minValue || curValue > maxValue) return;
529 float width = (float)Size2D.Width;
530 float height = (float)Size2D.Height;
533 if (Style.Thumb.Size == null)
539 thumbW = Style.Thumb.Size.Width;
540 thumbH = Style.Thumb.Size.Height;
542 float ratio = (float)(curValue - minValue) / (float)(maxValue - minValue);
544 if (Style.Direction == DirectionType.Horizontal)
546 if (LayoutDirection == ViewLayoutDirectionType.RTL)
548 ratio = 1.0f - ratio;
551 float posX = ratio * (width - thumbW);
552 float posY = (height - thumbH) / 2.0f;
554 thumbImagePosX = posX;
555 if (null != scrollAniPlayer)
557 scrollAniPlayer.Stop();
562 thumbImage.Position = new Position(posX, posY, 0);
566 if (null != scrollAniPlayer)
568 scrollAniPlayer.Clear();
569 scrollAniPlayer.AnimateTo(thumbImage, "PositionX", posX);
570 scrollAniPlayer.Play();
576 float posX = (width - thumbW) / 2.0f;
577 float posY = ratio * (height - thumbH);
579 thumbImagePosY = posY;
580 if (null != scrollAniPlayer)
582 scrollAniPlayer.Stop();
587 thumbImage.Position = new Position(posX, posY, 0);
591 if (null != scrollAniPlayer)
593 scrollAniPlayer.Clear();
594 scrollAniPlayer.AnimateTo(thumbImage, "PositionY", posY);
595 scrollAniPlayer.Play();
600 if (enableAni) enableAni = false;
603 private DirectionType CurrentDirection()
605 DirectionType dir = DirectionType.Horizontal;
606 if (null != Style.Direction)
608 dir = Style.Direction.Value;
613 private int CalculateCurrentValue(float offset, DirectionType dir)
615 if (dir == DirectionType.Horizontal)
617 thumbImagePosX += offset;
618 if (thumbImagePosX < 0)
620 thumbImage.PositionX = 0;
623 else if (thumbImagePosX > Size2D.Width - thumbImage.Size2D.Width)
625 thumbImage.PositionX = Size2D.Width - thumbImage.Size2D.Width;
630 thumbImage.PositionX = thumbImagePosX;
631 curValue = (int)((thumbImagePosX / (float)(Size2D.Width - thumbImage.Size2D.Width)) * (float)(maxValue - minValue) + 0.5f);
636 thumbImagePosY += offset;
637 if (thumbImagePosY < 0)
639 thumbImage.PositionY = 0;
642 else if (thumbImagePosY > Size2D.Height - thumbImage.Size2D.Height)
644 thumbImage.PositionY = Size2D.Height - thumbImage.Size2D.Height;
649 thumbImage.PositionY = thumbImagePosY;
650 curValue = (int)((thumbImagePosY / (float)(Size2D.Height - thumbImage.Size2D.Height)) * (float)(maxValue - minValue) + 0.5f);