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.
28 /// Please note that this class will be replaced with Scrollbar class in the near future.
30 /// <since_tizen> 6 </since_tizen>
31 /// This will be deprecated
32 [Obsolete("Deprecated in API8; Will be removed in API10")]
33 public class ScrollBar : Control
35 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
36 [EditorBrowsable(EditorBrowsableState.Never)]
37 public static readonly BindableProperty DirectionProperty = BindableProperty.Create(nameof(Direction), typeof(DirectionType), typeof(ScrollBar), DirectionType.Horizontal, propertyChanged: (bindable, oldValue, newValue) =>
39 var instance = (ScrollBar)bindable;
42 instance.Style.Direction = (DirectionType?)newValue;
43 instance.UpdateValue();
46 defaultValueCreator: (bindable) =>
48 var instance = (ScrollBar)bindable;
49 return instance.Style.Direction;
51 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
52 [EditorBrowsable(EditorBrowsableState.Never)]
53 public static readonly BindableProperty MaxValueProperty = BindableProperty.Create(nameof(MaxValue), typeof(int), typeof(ScrollBar), default(int), propertyChanged: (bindable, oldValue, newValue) =>
55 var instance = (ScrollBar)bindable;
58 if ((int)newValue >= 0)
60 instance.maxValue = (int)newValue;
61 instance.UpdateValue();
65 defaultValueCreator: (bindable) =>
67 var instance = (ScrollBar)bindable;
68 return instance.maxValue;
70 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
71 [EditorBrowsable(EditorBrowsableState.Never)]
72 public static readonly BindableProperty MinValueProperty = BindableProperty.Create(nameof(MinValue), typeof(int), typeof(ScrollBar), default(int), propertyChanged: (bindable, oldValue, newValue) =>
74 var instance = (ScrollBar)bindable;
77 if ((int)newValue >= 0)
79 instance.minValue = (int)newValue;
80 instance.UpdateValue();
84 defaultValueCreator: (bindable) =>
86 var instance = (ScrollBar)bindable;
87 return instance.minValue;
89 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
90 [EditorBrowsable(EditorBrowsableState.Never)]
91 public static readonly BindableProperty CurrentValueProperty = BindableProperty.Create(nameof(CurrentValue), typeof(int), typeof(ScrollBar), default(int), propertyChanged: (bindable, oldValue, newValue) =>
93 var instance = (ScrollBar)bindable;
96 if ((int)newValue >= 0)
98 instance.curValue = (int)newValue;
99 instance.UpdateValue();
103 defaultValueCreator: (bindable) =>
105 var instance = (ScrollBar)bindable;
106 return instance.curValue;
108 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
109 [EditorBrowsable(EditorBrowsableState.Never)]
110 public static readonly BindableProperty DurationProperty = BindableProperty.Create(nameof(Duration), typeof(uint), typeof(ScrollBar), default(uint), propertyChanged: (bindable, oldValue, newValue) =>
112 var instance = (ScrollBar)bindable;
113 if (newValue != null)
115 instance.Style.Duration = (uint)newValue;
116 if (instance.scrollAniPlayer != null)
118 instance.scrollAniPlayer.Duration = (int)(uint)newValue;
122 defaultValueCreator: (bindable) =>
124 var instance = (ScrollBar)bindable;
125 return instance.Style.Duration;
128 private ImageView trackImage;
129 private ImageView thumbImage;
130 private Animation scrollAniPlayer = null;
131 private float thumbImagePosX;
132 private float thumbImagePosY;
133 private bool enableAni = false;
134 private int minValue;
135 private int maxValue;
136 private int curValue;
137 static ScrollBar() { }
140 /// The constructor of ScrollBar.
142 /// <since_tizen> 6 </since_tizen>
143 /// This will be deprecated
144 [Obsolete("Deprecated in API8; Will be removed in API10")]
145 public ScrollBar() : base()
151 /// The constructor of ScrollBar with specific style.
153 /// <param name="style">style name</param>
154 [EditorBrowsable(EditorBrowsableState.Never)]
155 public ScrollBar(string style) : base(style)
161 /// The constructor of ScrollBar with specific style.
163 /// <param name="scrollBarStyle">The style object to initialize the ScrollBar.</param>
164 [EditorBrowsable(EditorBrowsableState.Never)]
165 public ScrollBar(ScrollBarStyle scrollBarStyle) : base(scrollBarStyle)
171 /// The direction type of the Scroll.
173 /// <since_tizen> 6 </since_tizen>
174 /// This will be deprecated
175 [Obsolete("Deprecated in API8; Will be removed in API10")]
176 public enum DirectionType
179 /// The Horizontal type.
181 /// <since_tizen> 6 </since_tizen>
182 /// This will be deprecated
183 [Obsolete("Deprecated in API8; Will be removed in API10")]
187 /// The Vertical type.
189 /// <since_tizen> 6 </since_tizen>
190 /// This will be deprecated
191 [Obsolete("Deprecated in API8; Will be removed in API10")]
195 #region public property
197 /// Get style of scrollbar.
199 [EditorBrowsable(EditorBrowsableState.Never)]
200 public new ScrollBarStyle Style => ViewStyle as ScrollBarStyle;
203 /// The property to get/set the direction of the ScrollBar.
205 /// <since_tizen> 6 </since_tizen>
206 /// This will be deprecated
207 [Obsolete("Deprecated in API8; Will be removed in API10")]
208 public DirectionType Direction
212 return (DirectionType)GetValue(DirectionProperty);
216 SetValue(DirectionProperty, value);
221 /// The property to get/set the size of the thumb object.
223 /// <exception cref="InvalidOperationException">Throw when ThumbSize is null.</exception>
226 /// ScrollBar scroll;
229 /// scroll.ThumbSize = new Size(500, 10, 0);
231 /// catch(InvalidOperationException e)
233 /// Tizen.Log.Error(LogTag, "Failed to set ThumbSize value : " + e.Message);
237 /// <since_tizen> 6 </since_tizen>
238 /// This will be deprecated
239 [Obsolete("Deprecated in API8; Will be removed in API10")]
240 public Size ThumbSize
244 return Style?.Thumb?.Size;
248 if (null != Style?.Thumb)
250 Style.Thumb.Size = value;
257 /// The property to get/set the image URL of the track object.
259 /// <since_tizen> 6 </since_tizen>
260 /// This will be deprecated
261 [Obsolete("Deprecated in API8; Will be removed in API10")]
262 public string TrackImageURL
266 return Style?.Track?.ResourceUrl?.All;
270 if (null != Style?.Track)
272 Style.Track.ResourceUrl = value;
279 /// The property to get/set the color of the track object.
281 /// <since_tizen> 6 </since_tizen>
282 /// This will be deprecated
283 [Obsolete("Deprecated in API8; Will be removed in API10")]
284 public Color TrackColor
288 return Style?.Track?.BackgroundColor?.All;
292 if (null != Style?.Track)
294 Style.Track.BackgroundColor = value;
301 /// The property to get/set the color of the thumb object.
303 /// <since_tizen> 6 </since_tizen>
304 /// This will be deprecated
305 [Obsolete("Deprecated in API8; Will be removed in API10")]
306 public Color ThumbColor
310 return Style?.Thumb?.BackgroundColor?.All;
314 if(null != Style?.Thumb)
316 Style.Thumb.BackgroundColor = value;
323 /// The property to get/set the max value of the ScrollBar.
325 /// <since_tizen> 6 </since_tizen>
326 /// This will be deprecated
327 [Obsolete("Deprecated in API8; Will be removed in API10")]
332 return (int)GetValue(MaxValueProperty);
336 SetValue(MaxValueProperty, value);
341 /// The property to get/set the min value of the ScrollBar.
343 /// <since_tizen> 6 </since_tizen>
344 /// This will be deprecated
345 [Obsolete("Deprecated in API8; Will be removed in API10")]
350 return (int)GetValue(MinValueProperty);
354 SetValue(MinValueProperty, value);
359 /// The property to get/set the current value of the ScrollBar.
361 /// <exception cref="ArgumentOutOfRangeException">Throw when Current value is less than Min value, or greater than Max value.</exception>
364 /// ScrollBar scroll;
365 /// scroll.MaxValue = 100;
366 /// scroll.MinValue = 0;
369 /// scroll.CurrentValue = 50;
371 /// catch(ArgumentOutOfRangeException e)
373 /// Tizen.Log.Error(LogTag, "Failed to set Current value : " + e.Message);
377 /// <since_tizen> 6 </since_tizen>
378 /// This will be deprecated
379 [Obsolete("Deprecated in API8; Will be removed in API10")]
380 public int CurrentValue
384 return (int)GetValue(CurrentValueProperty);
388 SetValue(CurrentValueProperty, value);
393 /// Property to set/get animation duration.
395 /// <since_tizen> 6 </since_tizen>
396 /// This will be deprecated
397 [Obsolete("Deprecated in API8; Will be removed in API10")]
402 return (uint)GetValue(DurationProperty);
406 SetValue(DurationProperty, value);
412 /// Method to set current value. The thumb object would move to the corresponding position with animation or not.
414 /// <param name="currentValue">The special current value.</param>
415 /// <param name="enableAnimation">Enable move with animation or not, the default value is true.</param>
416 /// <exception cref="ArgumentOutOfRangeException">Throw when current size is less than the min value, or greater than the max value.</exception>
419 /// ScrollBar scroll;
420 /// scroll.MinValue = 0;
421 /// scroll.MaxValue = 100;
424 /// scroll.SetCurrentValue(50);
426 /// catch(ArgumentOutOfRangeException e)
428 /// Tizen.Log.Error(LogTag, "Failed to set current value : " + e.Message);
432 /// <since_tizen> 6 </since_tizen>
433 /// This will be deprecated
434 [Obsolete("Deprecated in API8; Will be removed in API10")]
435 public void SetCurrentValue(int currentValue, bool enableAnimation = true)
437 if (currentValue < minValue || currentValue > maxValue)
439 //TNLog.E("Current value is less than the Min value, or greater than the Max value. currentValue = " + currentValue + ";");
440 #pragma warning disable CA2208 // Instantiate argument exceptions correctly
441 throw new ArgumentOutOfRangeException("Wrong Current value. It should be greater than the Min value, and less than the Max value!");
442 #pragma warning restore CA2208 // Instantiate argument exceptions correctly
445 enableAni = enableAnimation;
446 CurrentValue = currentValue;
450 /// Dispose ScrollBar.
452 /// <param name="type">The DisposeTypes value.</param>
453 /// <since_tizen> 6 </since_tizen>
454 /// This will be deprecated
455 [Obsolete("Deprecated in API8; Will be removed in API10")]
456 protected override void Dispose(DisposeTypes type)
463 if (type == DisposeTypes.Explicit)
466 //Release your own managed resources here.
467 //You should release all of your own disposable objects here.
469 Utility.Dispose(trackImage);
470 Utility.Dispose(thumbImage);
472 if (scrollAniPlayer != null)
474 scrollAniPlayer.Stop();
475 scrollAniPlayer.Clear();
476 scrollAniPlayer.Dispose();
477 scrollAniPlayer = null;
481 //Release your own unmanaged resources here.
482 //You should not access any managed member here except static instance.
483 //because the execution order of Finalizes is non-deterministic.
484 //Unreference this from if a static instance refer to this.
486 //You must call base.Dispose(type) just before exit.
491 /// Get Scrollbar style.
493 /// <returns>The default scrollbar style.</returns>
494 [EditorBrowsable(EditorBrowsableState.Never)]
495 protected override ViewStyle CreateViewStyle()
497 return new ScrollBarStyle();
501 /// Theme change callback when theme is changed, this callback will be trigger.
503 /// <param name="sender">The sender</param>
504 /// <param name="e">The event data</param>
505 [EditorBrowsable(EditorBrowsableState.Never)]
506 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
508 ScrollBarStyle tempStyle = StyleManager.Instance.GetViewStyle(StyleName) as ScrollBarStyle;
509 if (tempStyle != null)
511 Style.CopyFrom(tempStyle);
516 private void Initialize()
518 this.Focusable = false;
520 trackImage = new ImageView
523 WidthResizePolicy = ResizePolicyType.FillToParent,
524 HeightResizePolicy = ResizePolicyType.FillToParent,
525 PositionUsesPivotPoint = true,
526 ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
527 PivotPoint = Tizen.NUI.PivotPoint.CenterLeft
529 this.Add(trackImage);
530 trackImage.ApplyStyle(Style.Track);
532 thumbImage = new ImageView
535 WidthResizePolicy = ResizePolicyType.Fixed,
536 HeightResizePolicy = ResizePolicyType.Fixed,
537 PositionUsesPivotPoint = true,
538 ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
539 PivotPoint = Tizen.NUI.PivotPoint.CenterLeft
541 this.Add(thumbImage);
542 thumbImage.ApplyStyle(Style.Thumb);
544 scrollAniPlayer = new Animation(334);
545 scrollAniPlayer.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
549 LayoutDirectionChanged += OnLayoutDirectionChanged;
552 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
557 private void UpdateValue()
559 if (minValue >= maxValue || curValue < minValue || curValue > maxValue) return;
561 float width = (float)Size2D.Width;
562 float height = (float)Size2D.Height;
565 if (Style.Thumb.Size == null)
571 thumbW = Style.Thumb.Size.Width;
572 thumbH = Style.Thumb.Size.Height;
574 float ratio = (float)(curValue - minValue) / (float)(maxValue - minValue);
576 if (Style.Direction == DirectionType.Horizontal)
578 if (LayoutDirection == ViewLayoutDirectionType.RTL)
580 ratio = 1.0f - ratio;
583 float posX = ratio * (width - thumbW);
584 float posY = (height - thumbH) / 2.0f;
586 thumbImagePosX = posX;
587 if (null != scrollAniPlayer)
589 scrollAniPlayer.Stop();
594 thumbImage.Position = new Position(posX, posY, 0);
598 if (null != scrollAniPlayer)
600 scrollAniPlayer.Clear();
601 scrollAniPlayer.AnimateTo(thumbImage, "PositionX", posX);
602 scrollAniPlayer.Play();
608 float posX = (width - thumbW) / 2.0f;
609 float posY = ratio * (height - thumbH);
611 thumbImagePosY = posY;
612 if (null != scrollAniPlayer)
614 scrollAniPlayer.Stop();
619 thumbImage.Position = new Position(posX, posY, 0);
623 if (null != scrollAniPlayer)
625 scrollAniPlayer.Clear();
626 scrollAniPlayer.AnimateTo(thumbImage, "PositionY", posY);
627 scrollAniPlayer.Play();
632 if (enableAni) enableAni = false;
635 private DirectionType CurrentDirection()
637 DirectionType dir = DirectionType.Horizontal;
638 if (null != Style.Direction)
640 dir = Style.Direction.Value;
645 private int CalculateCurrentValue(float offset, DirectionType dir)
647 if (dir == DirectionType.Horizontal)
649 thumbImagePosX += offset;
650 if (thumbImagePosX < 0)
652 thumbImage.PositionX = 0;
655 else if (thumbImagePosX > Size2D.Width - thumbImage.Size2D.Width)
657 thumbImage.PositionX = Size2D.Width - thumbImage.Size2D.Width;
662 thumbImage.PositionX = thumbImagePosX;
663 curValue = (int)((thumbImagePosX / (float)(Size2D.Width - thumbImage.Size2D.Width)) * (float)(maxValue - minValue) + 0.5f);
668 thumbImagePosY += offset;
669 if (thumbImagePosY < 0)
671 thumbImage.PositionY = 0;
674 else if (thumbImagePosY > Size2D.Height - thumbImage.Size2D.Height)
676 thumbImage.PositionY = Size2D.Height - thumbImage.Size2D.Height;
681 thumbImage.PositionY = thumbImagePosY;
682 curValue = (int)((thumbImagePosY / (float)(Size2D.Height - thumbImage.Size2D.Height)) * (float)(maxValue - minValue) + 0.5f);