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;
21 namespace Tizen.NUI.Components
24 /// The ScrollBar class of nui component. It allows users to recognize the direction and the range of lists/content. .
26 /// <since_tizen> 6 </since_tizen>
27 public class ScrollBar : Control
29 private ScrollBarAttributes scrollBarAttrs;
30 private ImageView trackImage;
31 private ImageView thumbImage;
32 private Animation scrollAniPlayer = null;
33 private float thumbImagePosX;
34 private float thumbImagePosY;
35 private bool enableAni = false;
41 /// The constructor of ScrollBar
43 /// <since_tizen> 6 </since_tizen>
44 public ScrollBar() : base()
50 /// The constructor of ScrollBar with specific style.
52 /// <param name="style">style name</param>
53 /// <since_tizen> 6 </since_tizen>
54 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
55 [EditorBrowsable(EditorBrowsableState.Never)]
56 public ScrollBar(string style) : base(style)
62 /// The constructor of ScrollBar with specific Attributes.
64 /// <param name="attributes">The Attributes object to initialize the ScrollBar.</param>
65 /// <since_tizen> 6 </since_tizen>
66 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
67 [EditorBrowsable(EditorBrowsableState.Never)]
68 public ScrollBar(ScrollBarAttributes attributes) : base(attributes)
74 /// The direction type of the Scroll.
76 /// <since_tizen> 6 </since_tizen>
77 public enum DirectionType
80 /// The Horizontal type.
82 /// <since_tizen> 6 </since_tizen>
86 /// The Vertical type.
88 /// <since_tizen> 6 </since_tizen>
92 #region public property
95 /// The property to get/set the direction of the ScrollBar.
97 /// <since_tizen> 6 </since_tizen>
98 public DirectionType Direction
102 return scrollBarAttrs.Direction.Value;
106 scrollBarAttrs.Direction = value;
112 /// The property to get/set the size of the thumb object.
114 /// <exception cref="InvalidOperationException">Throw when ThumbSize is null.</exception>
117 /// ScrollBar scroll;
120 /// scroll.ThumbSize = new Size(500, 10, 0);
122 /// catch(InvalidOperationException e)
124 /// Tizen.Log.Error(LogTag, "Failed to set ThumbSize value : " + e.Message);
128 /// <since_tizen> 6 </since_tizen>
129 public Size ThumbSize
133 if (scrollBarAttrs.ThumbImageAttributes.Size == null)
135 scrollBarAttrs.ThumbImageAttributes.Size = new Size();
137 return scrollBarAttrs.ThumbImageAttributes.Size;
141 if (scrollBarAttrs.ThumbImageAttributes.Size == null)
143 scrollBarAttrs.ThumbImageAttributes.Size = new Size();
145 if (thumbImage != null)
147 scrollBarAttrs.ThumbImageAttributes.Size.Width = value.Width;
148 scrollBarAttrs.ThumbImageAttributes.Size.Height = value.Height;
155 /// The property to get/set the image URL of the track object.
157 /// <since_tizen> 6 </since_tizen>
158 public string TrackImageURL
162 return scrollBarAttrs.TrackImageAttributes.ResourceURL.All;
166 if (trackImage != null)
168 if (scrollBarAttrs.TrackImageAttributes.ResourceURL == null)
170 scrollBarAttrs.TrackImageAttributes.ResourceURL = new StringSelector();
172 scrollBarAttrs.TrackImageAttributes.ResourceURL.All = value;
179 /// The property to get/set the color of the track object.
181 /// <since_tizen> 6 </since_tizen>
182 public Color TrackColor
186 return scrollBarAttrs.TrackImageAttributes.BackgroundColor?.All;
190 if (scrollBarAttrs.TrackImageAttributes.BackgroundColor == null)
192 scrollBarAttrs.TrackImageAttributes.BackgroundColor = new ColorSelector { All = value };
196 scrollBarAttrs.TrackImageAttributes.BackgroundColor.All = value;
203 /// The property to get/set the color of the thumb object.
205 /// <since_tizen> 6 </since_tizen>
206 public Color ThumbColor
210 return scrollBarAttrs.ThumbImageAttributes.BackgroundColor?.All;
214 if(scrollBarAttrs.ThumbImageAttributes.BackgroundColor == null)
216 scrollBarAttrs.ThumbImageAttributes.BackgroundColor = new ColorSelector { All = value };
220 scrollBarAttrs.ThumbImageAttributes.BackgroundColor.All = value;
227 /// The property to get/set the max value of the ScrollBar.
229 /// <since_tizen> 6 </since_tizen>
247 /// The property to get/set the min value of the ScrollBar.
249 /// <since_tizen> 6 </since_tizen>
267 /// The property to get/set the current value of the ScrollBar.
269 /// <exception cref="ArgumentOutOfRangeException">Throw when Current value is less than Min value, or greater than Max value.</exception>
272 /// ScrollBar scroll;
273 /// scroll.MaxValue = 100;
274 /// scroll.MinValue = 0;
277 /// scroll.CurrentValue = 50;
279 /// catch(ArgumentOutOfRangeException e)
281 /// Tizen.Log.Error(LogTag, "Failed to set Current value : " + e.Message);
285 /// <since_tizen> 6 </since_tizen>
286 public int CurrentValue
303 /// Property to set/get animation duration.
305 /// <since_tizen> 6 </since_tizen>
310 return scrollBarAttrs.Duration;
314 scrollBarAttrs.Duration = value;
315 if (scrollAniPlayer != null)
317 scrollAniPlayer.Duration = (int)value;
324 /// Method to set current value. The thumb object would move to the corresponding position with animation or not.
326 /// <param name="currentValue">The special current value.</param>
327 /// <param name="isEnableAni">Enable move with animation or not, the default value is true.</param>
328 /// <exception cref="ArgumentOutOfRangeException">Throw when current size is less than the min value, or greater than the max value.</exception>
331 /// ScrollBar scroll;
332 /// scroll.MinValue = 0;
333 /// scroll.MaxValue = 100;
336 /// scroll.SetCurrentValue(50);
338 /// catch(ArgumentOutOfRangeException e)
340 /// Tizen.Log.Error(LogTag, "Failed to set current value : " + e.Message);
344 /// <since_tizen> 6 </since_tizen>
345 public void SetCurrentValue(int currentValue, bool EnableAnimation = true)
347 if (currentValue < minValue || currentValue > maxValue)
349 //TNLog.E("Current value is less than the Min value, or greater than the Max value. currentValue = " + currentValue + ";");
350 throw new ArgumentOutOfRangeException("Wrong Current value. It shoud be greater than the Min value, and less than the Max value!");
353 enableAni = EnableAnimation;
354 CurrentValue = currentValue;
358 /// Dispose ScrollBar.
360 /// <param name="type">The DisposeTypes value.</param>
361 /// <since_tizen> 6 </since_tizen>
362 protected override void Dispose(DisposeTypes type)
369 if (type == DisposeTypes.Explicit)
372 //Release your own managed resources here.
373 //You should release all of your own disposable objects here.
375 if (trackImage != null)
377 Utility.Dispose(trackImage);
380 if (thumbImage != null)
382 Utility.Dispose(thumbImage);
385 if (scrollAniPlayer != null)
387 scrollAniPlayer.Stop();
388 scrollAniPlayer.Clear();
389 scrollAniPlayer.Dispose();
390 scrollAniPlayer = null;
392 // UIDirectionChangedEvent -= OnUIDirectionChangedEvent;
395 //Release your own unmanaged resources here.
396 //You should not access any managed member here except static instance.
397 //because the execution order of Finalizes is non-deterministic.
398 //Unreference this from if a static instance refer to this.
400 //You must call base.Dispose(type) just before exit.
405 /// The method to update Attributes.
407 /// <since_tizen> 6 </since_tizen>
408 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
409 [EditorBrowsable(EditorBrowsableState.Never)]
410 protected override void OnUpdate()
412 ApplyAttributes(this, scrollBarAttrs);
413 ApplyAttributes(trackImage, scrollBarAttrs.TrackImageAttributes);
414 ApplyAttributes(thumbImage, scrollBarAttrs.ThumbImageAttributes);
427 /// Get Scrollbar attribues.
429 /// <since_tizen> 6 </since_tizen>
430 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
431 [EditorBrowsable(EditorBrowsableState.Never)]
432 protected override Attributes GetAttributes()
434 return new ScrollBarAttributes()
436 ThumbImageAttributes = new ImageAttributes
438 WidthResizePolicy = ResizePolicyType.Fixed,
439 HeightResizePolicy = ResizePolicyType.Fixed,
440 PositionUsesPivotPoint = true,
441 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
442 PivotPoint = Tizen.NUI.PivotPoint.TopLeft
444 TrackImageAttributes = new ImageAttributes
446 WidthResizePolicy = ResizePolicyType.FillToParent,
447 HeightResizePolicy = ResizePolicyType.FillToParent,
448 PositionUsesPivotPoint = true,
449 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
450 PivotPoint = Tizen.NUI.PivotPoint.TopLeft
457 /// Theme change callback when theme is changed, this callback will be trigger.
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 void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
464 ScrollBarAttributes tempAttributes = StyleManager.Instance.GetAttributes(style) as ScrollBarAttributes;
465 if (tempAttributes != null)
467 attributes = scrollBarAttrs = tempAttributes;
472 private void Initialize()
474 scrollBarAttrs = this.attributes as ScrollBarAttributes;
475 if(scrollBarAttrs == null)
477 throw new Exception("ScrollBar attribute parse error.");
480 this.Focusable = false;
482 trackImage = new ImageView
485 WidthResizePolicy = ResizePolicyType.FillToParent,
486 HeightResizePolicy = ResizePolicyType.FillToParent,
487 PositionUsesPivotPoint = true,
488 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
489 PivotPoint = Tizen.NUI.PivotPoint.TopLeft
492 thumbImage = new ImageView
495 WidthResizePolicy = ResizePolicyType.Fixed,
496 HeightResizePolicy = ResizePolicyType.Fixed,
497 PositionUsesPivotPoint = true,
498 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
499 PivotPoint = Tizen.NUI.PivotPoint.TopLeft
505 scrollAniPlayer = new Animation(334);
506 scrollAniPlayer.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
510 LayoutDirectionChanged += OnLayoutDirectionChanged;
513 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
518 private void UpdateValue(bool enableAni = false)
520 if (trackImage == null || thumbImage == null || scrollBarAttrs.Direction == null)
525 if (minValue >= maxValue || curValue < minValue || curValue > maxValue)
529 float width = (float)Size2D.Width;
530 float height = (float)Size2D.Height;
533 if (scrollBarAttrs.ThumbImageAttributes.Size == null)
539 thumbW = scrollBarAttrs.ThumbImageAttributes.Size.Width;
540 thumbH = scrollBarAttrs.ThumbImageAttributes.Size.Height;
542 float ratio = (float)(curValue - minValue) / (float)(maxValue - minValue);
544 if (scrollBarAttrs.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 (scrollAniPlayer != null)
557 scrollAniPlayer.Stop();
562 thumbImage.Position = new Position(posX, posY, 0);
566 if (scrollAniPlayer != null)
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 (scrollAniPlayer != null)
582 scrollAniPlayer.Stop();
587 thumbImage.Position = new Position(posX, posY, 0);
591 if (scrollAniPlayer != null)
593 scrollAniPlayer.Clear();
594 scrollAniPlayer.AnimateTo(thumbImage, "PositionY", posY);
595 scrollAniPlayer.Play();
601 private DirectionType CurrentDirection()
603 DirectionType dir = DirectionType.Horizontal;
604 if (scrollBarAttrs != null)
606 if (scrollBarAttrs.Direction != null)
608 dir = scrollBarAttrs.Direction.Value;
614 private int CalculateCurrentValue(float offset, DirectionType dir)
616 if (dir == DirectionType.Horizontal)
618 thumbImagePosX += offset;
619 if (thumbImagePosX < 0)
621 thumbImage.PositionX = 0;
624 else if (thumbImagePosX > Size2D.Width - thumbImage.Size2D.Width)
626 thumbImage.PositionX = Size2D.Width - thumbImage.Size2D.Width;
631 thumbImage.PositionX = thumbImagePosX;
632 curValue = (int)((thumbImagePosX / (float)(Size2D.Width - thumbImage.Size2D.Width)) * (float)(maxValue - minValue) + 0.5f);
637 thumbImagePosY += offset;
638 if (thumbImagePosY < 0)
640 thumbImage.PositionY = 0;
643 else if (thumbImagePosY > Size2D.Height - thumbImage.Size2D.Height)
645 thumbImage.PositionY = Size2D.Height - thumbImage.Size2D.Height;
650 thumbImage.PositionY = thumbImagePosY;
651 curValue = (int)((thumbImagePosY / (float)(Size2D.Height - thumbImage.Size2D.Height)) * (float)(maxValue - minValue) + 0.5f);
655 return (int)curValue;