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 public class ScrollBar : Control
33 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
34 [EditorBrowsable(EditorBrowsableState.Never)]
35 public static readonly BindableProperty DirectionProperty = BindableProperty.Create(nameof(Direction), typeof(DirectionType), typeof(ScrollBar), DirectionType.Horizontal, propertyChanged: (bindable, oldValue, newValue) =>
37 var instance = (ScrollBar)bindable;
40 instance.Style.Direction = (DirectionType?)newValue;
41 instance.UpdateValue();
44 defaultValueCreator: (bindable) =>
46 var instance = (ScrollBar)bindable;
47 return instance.Style.Direction;
49 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
50 [EditorBrowsable(EditorBrowsableState.Never)]
51 public static readonly BindableProperty MaxValueProperty = BindableProperty.Create(nameof(MaxValue), typeof(int), typeof(ScrollBar), default(int), propertyChanged: (bindable, oldValue, newValue) =>
53 var instance = (ScrollBar)bindable;
56 if ((int)newValue >= 0)
58 instance.maxValue = (int)newValue;
59 instance.UpdateValue();
63 defaultValueCreator: (bindable) =>
65 var instance = (ScrollBar)bindable;
66 return instance.maxValue;
68 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
69 [EditorBrowsable(EditorBrowsableState.Never)]
70 public static readonly BindableProperty MinValueProperty = BindableProperty.Create(nameof(MinValue), typeof(int), typeof(ScrollBar), default(int), propertyChanged: (bindable, oldValue, newValue) =>
72 var instance = (ScrollBar)bindable;
75 if ((int)newValue >= 0)
77 instance.minValue = (int)newValue;
78 instance.UpdateValue();
82 defaultValueCreator: (bindable) =>
84 var instance = (ScrollBar)bindable;
85 return instance.minValue;
87 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
88 [EditorBrowsable(EditorBrowsableState.Never)]
89 public static readonly BindableProperty CurrentValueProperty = BindableProperty.Create(nameof(CurrentValue), typeof(int), typeof(ScrollBar), default(int), propertyChanged: (bindable, oldValue, newValue) =>
91 var instance = (ScrollBar)bindable;
94 if ((int)newValue >= 0)
96 instance.curValue = (int)newValue;
97 instance.UpdateValue();
101 defaultValueCreator: (bindable) =>
103 var instance = (ScrollBar)bindable;
104 return instance.curValue;
106 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
107 [EditorBrowsable(EditorBrowsableState.Never)]
108 public static readonly BindableProperty DurationProperty = BindableProperty.Create(nameof(Duration), typeof(uint), typeof(ScrollBar), default(uint), propertyChanged: (bindable, oldValue, newValue) =>
110 var instance = (ScrollBar)bindable;
111 if (newValue != null)
113 instance.Style.Duration = (uint)newValue;
114 if (instance.scrollAniPlayer != null)
116 instance.scrollAniPlayer.Duration = (int)(uint)newValue;
120 defaultValueCreator: (bindable) =>
122 var instance = (ScrollBar)bindable;
123 return instance.Style.Duration;
126 private ImageView trackImage;
127 private ImageView thumbImage;
128 private Animation scrollAniPlayer = null;
129 private float thumbImagePosX;
130 private float thumbImagePosY;
131 private bool enableAni = false;
132 private int minValue;
133 private int maxValue;
134 private int curValue;
135 static ScrollBar() { }
138 /// The constructor of ScrollBar.
140 /// <since_tizen> 6 </since_tizen>
141 public ScrollBar() : base()
147 /// The constructor of ScrollBar with specific style.
149 /// <param name="style">style name</param>
150 /// <since_tizen> 8 </since_tizen>
151 public ScrollBar(string style) : base(style)
157 /// The constructor of ScrollBar with specific style.
159 /// <param name="scrollBarStyle">The style object to initialize the ScrollBar.</param>
160 /// <since_tizen> 8 </since_tizen>
161 public ScrollBar(ScrollBarStyle scrollBarStyle) : base(scrollBarStyle)
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
187 /// Get style of scrollbar.
189 /// <since_tizen> 8 </since_tizen>
190 public new ScrollBarStyle Style => ViewStyle as ScrollBarStyle;
193 /// The property to get/set the direction of the ScrollBar.
195 /// <since_tizen> 6 </since_tizen>
196 public DirectionType Direction
200 return (DirectionType)GetValue(DirectionProperty);
204 SetValue(DirectionProperty, value);
209 /// The property to get/set the size of the thumb object.
211 /// <exception cref="InvalidOperationException">Throw when ThumbSize is null.</exception>
214 /// ScrollBar scroll;
217 /// scroll.ThumbSize = new Size(500, 10, 0);
219 /// catch(InvalidOperationException e)
221 /// Tizen.Log.Error(LogTag, "Failed to set ThumbSize value : " + e.Message);
225 /// <since_tizen> 6 </since_tizen>
226 public Size ThumbSize
230 return Style?.Thumb?.Size;
234 if (null != Style?.Thumb)
236 Style.Thumb.Size = value;
243 /// The property to get/set the image URL of the track object.
245 /// <since_tizen> 6 </since_tizen>
246 public string TrackImageURL
250 return Style?.Track?.ResourceUrl?.All;
254 if (null != Style?.Track)
256 Style.Track.ResourceUrl = value;
263 /// The property to get/set the color of the track object.
265 /// <since_tizen> 6 </since_tizen>
266 public Color TrackColor
270 return Style?.Track?.BackgroundColor?.All;
274 if (null != Style?.Track)
276 Style.Track.BackgroundColor = value;
283 /// The property to get/set the color of the thumb object.
285 /// <since_tizen> 6 </since_tizen>
286 public Color ThumbColor
290 return Style?.Thumb?.BackgroundColor?.All;
294 if(null != Style?.Thumb)
296 Style.Thumb.BackgroundColor = value;
303 /// The property to get/set the max value of the ScrollBar.
305 /// <since_tizen> 6 </since_tizen>
310 return (int)GetValue(MaxValueProperty);
314 SetValue(MaxValueProperty, value);
319 /// The property to get/set the min value of the ScrollBar.
321 /// <since_tizen> 6 </since_tizen>
326 return (int)GetValue(MinValueProperty);
330 SetValue(MinValueProperty, value);
335 /// The property to get/set the current value of the ScrollBar.
337 /// <exception cref="ArgumentOutOfRangeException">Throw when Current value is less than Min value, or greater than Max value.</exception>
340 /// ScrollBar scroll;
341 /// scroll.MaxValue = 100;
342 /// scroll.MinValue = 0;
345 /// scroll.CurrentValue = 50;
347 /// catch(ArgumentOutOfRangeException e)
349 /// Tizen.Log.Error(LogTag, "Failed to set Current value : " + e.Message);
353 /// <since_tizen> 6 </since_tizen>
354 public int CurrentValue
358 return (int)GetValue(CurrentValueProperty);
362 SetValue(CurrentValueProperty, value);
367 /// Property to set/get animation duration.
369 /// <since_tizen> 6 </since_tizen>
374 return (uint)GetValue(DurationProperty);
378 SetValue(DurationProperty, value);
384 /// Method to set current value. The thumb object would move to the corresponding position with animation or not.
386 /// <param name="currentValue">The special current value.</param>
387 /// <param name="enableAnimation">Enable move with animation or not, the default value is true.</param>
388 /// <exception cref="ArgumentOutOfRangeException">Throw when current size is less than the min value, or greater than the max value.</exception>
391 /// ScrollBar scroll;
392 /// scroll.MinValue = 0;
393 /// scroll.MaxValue = 100;
396 /// scroll.SetCurrentValue(50);
398 /// catch(ArgumentOutOfRangeException e)
400 /// Tizen.Log.Error(LogTag, "Failed to set current value : " + e.Message);
404 /// <since_tizen> 6 </since_tizen>
405 public void SetCurrentValue(int currentValue, bool enableAnimation = true)
407 if (currentValue < minValue || currentValue > maxValue)
409 //TNLog.E("Current value is less than the Min value, or greater than the Max value. currentValue = " + currentValue + ";");
410 #pragma warning disable CA2208 // Instantiate argument exceptions correctly
411 throw new ArgumentOutOfRangeException("Wrong Current value. It shoud be greater than the Min value, and less than the Max value!");
412 #pragma warning restore CA2208 // Instantiate argument exceptions correctly
415 enableAni = enableAnimation;
416 CurrentValue = currentValue;
420 /// Dispose ScrollBar.
422 /// <param name="type">The DisposeTypes value.</param>
423 /// <since_tizen> 6 </since_tizen>
424 protected override void Dispose(DisposeTypes type)
431 if (type == DisposeTypes.Explicit)
434 //Release your own managed resources here.
435 //You should release all of your own disposable objects here.
437 Utility.Dispose(trackImage);
438 Utility.Dispose(thumbImage);
440 if (scrollAniPlayer != null)
442 scrollAniPlayer.Stop();
443 scrollAniPlayer.Clear();
444 scrollAniPlayer.Dispose();
445 scrollAniPlayer = null;
449 //Release your own unmanaged resources here.
450 //You should not access any managed member here except static instance.
451 //because the execution order of Finalizes is non-deterministic.
452 //Unreference this from if a static instance refer to this.
454 //You must call base.Dispose(type) just before exit.
459 /// Get Scrollbar style.
461 /// <returns>The default scrollbar style.</returns>
462 /// <since_tizen> 8 </since_tizen>
463 protected override ViewStyle CreateViewStyle()
465 return new ScrollBarStyle();
469 /// Theme change callback when theme is changed, this callback will be trigger.
471 /// <param name="sender">The sender</param>
472 /// <param name="e">The event data</param>
473 /// <since_tizen> 8 </since_tizen>
474 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
476 ScrollBarStyle tempStyle = StyleManager.Instance.GetViewStyle(StyleName) 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);