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)(uint)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> 8 </since_tizen>
148 public ScrollBar(string style) : base(style)
154 /// The constructor of ScrollBar with specific style.
156 /// <param name="scrollBarStyle">The style object to initialize the ScrollBar.</param>
157 /// <since_tizen> 8 </since_tizen>
158 public ScrollBar(ScrollBarStyle scrollBarStyle) : base(scrollBarStyle)
164 /// The direction type of the Scroll.
166 /// <since_tizen> 6 </since_tizen>
167 public enum DirectionType
170 /// The Horizontal type.
172 /// <since_tizen> 6 </since_tizen>
176 /// The Vertical type.
178 /// <since_tizen> 6 </since_tizen>
182 #region public property
184 /// Get style of scrollbar.
186 /// <since_tizen> 8 </since_tizen>
187 public new ScrollBarStyle Style => ViewStyle as ScrollBarStyle;
190 /// The property to get/set the direction of the ScrollBar.
192 /// <since_tizen> 6 </since_tizen>
193 public DirectionType Direction
197 return (DirectionType)GetValue(DirectionProperty);
201 SetValue(DirectionProperty, value);
206 /// The property to get/set the size of the thumb object.
208 /// <exception cref="InvalidOperationException">Throw when ThumbSize is null.</exception>
211 /// ScrollBar scroll;
214 /// scroll.ThumbSize = new Size(500, 10, 0);
216 /// catch(InvalidOperationException e)
218 /// Tizen.Log.Error(LogTag, "Failed to set ThumbSize value : " + e.Message);
222 /// <since_tizen> 6 </since_tizen>
223 public Size ThumbSize
227 return Style?.Thumb?.Size;
231 if (null != Style?.Thumb)
233 Style.Thumb.Size = value;
240 /// The property to get/set the image URL of the track object.
242 /// <since_tizen> 6 </since_tizen>
243 public string TrackImageURL
247 return Style?.Track?.ResourceUrl?.All;
251 if (null != Style?.Track)
253 Style.Track.ResourceUrl = value;
260 /// The property to get/set the color of the track object.
262 /// <since_tizen> 6 </since_tizen>
263 public Color TrackColor
267 return Style?.Track?.BackgroundColor?.All;
271 if (null != Style?.Track)
273 Style.Track.BackgroundColor = value;
280 /// The property to get/set the color of the thumb object.
282 /// <since_tizen> 6 </since_tizen>
283 public Color ThumbColor
287 return Style?.Thumb?.BackgroundColor?.All;
291 if(null != Style?.Thumb)
293 Style.Thumb.BackgroundColor = value;
300 /// The property to get/set the max value of the ScrollBar.
302 /// <since_tizen> 6 </since_tizen>
307 return (int)GetValue(MaxValueProperty);
311 SetValue(MaxValueProperty, value);
316 /// The property to get/set the min value of the ScrollBar.
318 /// <since_tizen> 6 </since_tizen>
323 return (int)GetValue(MinValueProperty);
327 SetValue(MinValueProperty, value);
332 /// The property to get/set the current value of the ScrollBar.
334 /// <exception cref="ArgumentOutOfRangeException">Throw when Current value is less than Min value, or greater than Max value.</exception>
337 /// ScrollBar scroll;
338 /// scroll.MaxValue = 100;
339 /// scroll.MinValue = 0;
342 /// scroll.CurrentValue = 50;
344 /// catch(ArgumentOutOfRangeException e)
346 /// Tizen.Log.Error(LogTag, "Failed to set Current value : " + e.Message);
350 /// <since_tizen> 6 </since_tizen>
351 public int CurrentValue
355 return (int)GetValue(CurrentValueProperty);
359 SetValue(CurrentValueProperty, value);
364 /// Property to set/get animation duration.
366 /// <since_tizen> 6 </since_tizen>
371 return (uint)GetValue(DurationProperty);
375 SetValue(DurationProperty, value);
381 /// Method to set current value. The thumb object would move to the corresponding position with animation or not.
383 /// <param name="currentValue">The special current value.</param>
384 /// <param name="enableAnimation">Enable move with animation or not, the default value is true.</param>
385 /// <exception cref="ArgumentOutOfRangeException">Throw when current size is less than the min value, or greater than the max value.</exception>
388 /// ScrollBar scroll;
389 /// scroll.MinValue = 0;
390 /// scroll.MaxValue = 100;
393 /// scroll.SetCurrentValue(50);
395 /// catch(ArgumentOutOfRangeException e)
397 /// Tizen.Log.Error(LogTag, "Failed to set current value : " + e.Message);
401 /// <since_tizen> 6 </since_tizen>
402 public void SetCurrentValue(int currentValue, bool enableAnimation = true)
404 if (currentValue < minValue || currentValue > maxValue)
406 //TNLog.E("Current value is less than the Min value, or greater than the Max value. currentValue = " + currentValue + ";");
407 #pragma warning disable CA2208 // Instantiate argument exceptions correctly
408 throw new ArgumentOutOfRangeException("Wrong Current value. It shoud be greater than the Min value, and less than the Max value!");
409 #pragma warning restore CA2208 // Instantiate argument exceptions correctly
412 enableAni = enableAnimation;
413 CurrentValue = currentValue;
417 /// Dispose ScrollBar.
419 /// <param name="type">The DisposeTypes value.</param>
420 /// <since_tizen> 6 </since_tizen>
421 protected override void Dispose(DisposeTypes type)
428 if (type == DisposeTypes.Explicit)
431 //Release your own managed resources here.
432 //You should release all of your own disposable objects here.
434 Utility.Dispose(trackImage);
435 Utility.Dispose(thumbImage);
437 if (scrollAniPlayer != null)
439 scrollAniPlayer.Stop();
440 scrollAniPlayer.Clear();
441 scrollAniPlayer.Dispose();
442 scrollAniPlayer = null;
446 //Release your own unmanaged resources here.
447 //You should not access any managed member here except static instance.
448 //because the execution order of Finalizes is non-deterministic.
449 //Unreference this from if a static instance refer to this.
451 //You must call base.Dispose(type) just before exit.
456 /// Get Scrollbar style.
458 /// <returns>The default scrollbar style.</returns>
459 /// <since_tizen> 8 </since_tizen>
460 protected override ViewStyle GetViewStyle()
462 return new ScrollBarStyle();
466 /// Theme change callback when theme is changed, this callback will be trigger.
468 /// <param name="sender">The sender</param>
469 /// <param name="e">The event data</param>
470 /// <since_tizen> 8 </since_tizen>
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);