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 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
28 [EditorBrowsable(EditorBrowsableState.Never)]
29 public class ScrollBar : Control
31 private ScrollBarAttributes scrollBarAttrs;
32 private ImageView trackImage;
33 private ImageView thumbImage;
34 private Animation scrollAniPlayer = null;
35 private float thumbImagePosX;
36 private float thumbImagePosY;
37 private bool enableAni = false;
43 /// The constructor of ScrollBar
45 /// <since_tizen> 6 </since_tizen>
46 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
47 [EditorBrowsable(EditorBrowsableState.Never)]
48 public ScrollBar() : base()
54 /// The constructor of ScrollBar with specific style.
56 /// <param name="style">style name</param>
57 /// <since_tizen> 6 </since_tizen>
58 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
59 [EditorBrowsable(EditorBrowsableState.Never)]
60 public ScrollBar(string style) : base(style)
66 /// The constructor of ScrollBar with specific Attributes.
68 /// <param name="attributes">The Attributes object to initialize the ScrollBar.</param>
69 /// <since_tizen> 6 </since_tizen>
70 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
71 [EditorBrowsable(EditorBrowsableState.Never)]
72 public ScrollBar(ScrollBarAttributes attributes) : base(attributes)
78 /// The direction type of the Scroll.
80 /// <since_tizen> 6 </since_tizen>
81 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
82 [EditorBrowsable(EditorBrowsableState.Never)]
83 public enum DirectionType
86 /// The Horizontal type.
88 /// <since_tizen> 6 </since_tizen>
89 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
90 [EditorBrowsable(EditorBrowsableState.Never)]
94 /// The Vertical type.
96 /// <since_tizen> 6 </since_tizen>
97 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
98 [EditorBrowsable(EditorBrowsableState.Never)]
102 #region public property
105 /// The property to get/set the direction of the ScrollBar.
107 /// <since_tizen> 6 </since_tizen>
108 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
109 [EditorBrowsable(EditorBrowsableState.Never)]
110 public DirectionType Direction
114 return scrollBarAttrs.Direction.Value;
118 scrollBarAttrs.Direction = value;
124 /// The property to get/set the size of the thumb object.
126 /// <exception cref="InvalidOperationException">Throw when ThumbSize is null.</exception>
129 /// ScrollBar scroll;
132 /// scroll.ThumbSize = new Size(500, 10, 0);
134 /// catch(InvalidOperationException e)
136 /// Tizen.Log.Error(LogTag, "Failed to set ThumbSize value : " + e.Message);
140 /// <since_tizen> 6 </since_tizen>
141 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
142 [EditorBrowsable(EditorBrowsableState.Never)]
143 public Size2D ThumbSize
147 if (scrollBarAttrs.ThumbImageAttributes.Size2D == null)
149 scrollBarAttrs.ThumbImageAttributes.Size2D = new Size2D();
151 return scrollBarAttrs.ThumbImageAttributes.Size2D;
155 if (scrollBarAttrs.ThumbImageAttributes.Size2D == null)
157 scrollBarAttrs.ThumbImageAttributes.Size2D = new Size2D();
159 if (thumbImage != null)
161 scrollBarAttrs.ThumbImageAttributes.Size2D.Width = value.Width;
162 scrollBarAttrs.ThumbImageAttributes.Size2D.Height = value.Height;
169 /// The property to get/set the image URL of the track object.
171 /// <since_tizen> 6 </since_tizen>
172 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
173 [EditorBrowsable(EditorBrowsableState.Never)]
174 public string TrackImageURL
178 return scrollBarAttrs.TrackImageAttributes.ResourceURL.All;
182 if (trackImage != null)
184 if (scrollBarAttrs.TrackImageAttributes.ResourceURL == null)
186 scrollBarAttrs.TrackImageAttributes.ResourceURL = new StringSelector();
188 scrollBarAttrs.TrackImageAttributes.ResourceURL.All = value;
195 /// The property to get/set the color of the track object.
197 /// <since_tizen> 6 </since_tizen>
198 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
199 [EditorBrowsable(EditorBrowsableState.Never)]
200 public Color TrackColor
204 return scrollBarAttrs.TrackImageAttributes.BackgroundColor?.All;
208 if (scrollBarAttrs.TrackImageAttributes.BackgroundColor == null)
210 scrollBarAttrs.TrackImageAttributes.BackgroundColor = new ColorSelector { All = value };
214 scrollBarAttrs.TrackImageAttributes.BackgroundColor.All = value;
221 /// The property to get/set the color of the thumb object.
223 /// <since_tizen> 6 </since_tizen>
224 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
225 [EditorBrowsable(EditorBrowsableState.Never)]
226 public Color ThumbColor
230 return scrollBarAttrs.ThumbImageAttributes.BackgroundColor?.All;
234 if(scrollBarAttrs.ThumbImageAttributes.BackgroundColor == null)
236 scrollBarAttrs.ThumbImageAttributes.BackgroundColor = new ColorSelector { All = value };
240 scrollBarAttrs.ThumbImageAttributes.BackgroundColor.All = value;
247 /// The property to get/set the max value of the ScrollBar.
249 /// <since_tizen> 6 </since_tizen>
250 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
251 [EditorBrowsable(EditorBrowsableState.Never)]
269 /// The property to get/set the min value of the ScrollBar.
271 /// <since_tizen> 6 </since_tizen>
272 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
273 [EditorBrowsable(EditorBrowsableState.Never)]
291 /// The property to get/set the current value of the ScrollBar.
293 /// <exception cref="ArgumentOutOfRangeException">Throw when Current value is less than Min value, or greater than Max value.</exception>
296 /// ScrollBar scroll;
297 /// scroll.MaxValue = 100;
298 /// scroll.MinValue = 0;
301 /// scroll.CurrentValue = 50;
303 /// catch(ArgumentOutOfRangeException e)
305 /// Tizen.Log.Error(LogTag, "Failed to set Current value : " + e.Message);
309 /// <since_tizen> 6 </since_tizen>
310 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
311 [EditorBrowsable(EditorBrowsableState.Never)]
312 public int CurrentValue
329 /// Property to set/get animation duration.
331 /// <since_tizen> 6 </since_tizen>
332 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
333 [EditorBrowsable(EditorBrowsableState.Never)]
338 return scrollBarAttrs.Duration;
342 scrollBarAttrs.Duration = value;
343 if (scrollAniPlayer != null)
345 scrollAniPlayer.Duration = (int)value;
352 /// Method to set current value. The thumb object would move to the corresponding position with animation or not.
354 /// <param name="currentValue">The special current value.</param>
355 /// <param name="isEnableAni">Enable move with animation or not, the default value is true.</param>
356 /// <exception cref="ArgumentOutOfRangeException">Throw when current size is less than the min value, or greater than the max value.</exception>
359 /// ScrollBar scroll;
360 /// scroll.MinValue = 0;
361 /// scroll.MaxValue = 100;
364 /// scroll.SetCurrentValue(50);
366 /// catch(ArgumentOutOfRangeException e)
368 /// Tizen.Log.Error(LogTag, "Failed to set current value : " + e.Message);
372 /// <since_tizen> 6 </since_tizen>
373 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
374 [EditorBrowsable(EditorBrowsableState.Never)]
375 public void SetCurrentValue(int currentValue, bool isEnableAni = true)
377 if (currentValue < minValue || currentValue > maxValue)
379 //TNLog.E("Current value is less than the Min value, or greater than the Max value. currentValue = " + currentValue + ";");
380 throw new ArgumentOutOfRangeException("Wrong Current value. It shoud be greater than the Min value, and less than the Max value!");
383 enableAni = isEnableAni;
384 CurrentValue = currentValue;
388 /// Dispose ScrollBar.
390 /// <param name="type">The DisposeTypes value.</param>
391 /// <since_tizen> 6 </since_tizen>
392 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
393 [EditorBrowsable(EditorBrowsableState.Never)]
394 protected override void Dispose(DisposeTypes type)
401 if (type == DisposeTypes.Explicit)
404 //Release your own managed resources here.
405 //You should release all of your own disposable objects here.
407 if (trackImage != null)
409 Utility.Dispose(trackImage);
412 if (thumbImage != null)
414 Utility.Dispose(thumbImage);
417 if (scrollAniPlayer != null)
419 scrollAniPlayer.Stop();
420 scrollAniPlayer.Clear();
421 scrollAniPlayer.Dispose();
422 scrollAniPlayer = null;
424 // UIDirectionChangedEvent -= OnUIDirectionChangedEvent;
427 //Release your own unmanaged resources here.
428 //You should not access any managed member here except static instance.
429 //because the execution order of Finalizes is non-deterministic.
430 //Unreference this from if a static instance refer to this.
432 //You must call base.Dispose(type) just before exit.
437 /// The method to update Attributes.
439 /// <since_tizen> 6 </since_tizen>
440 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
441 [EditorBrowsable(EditorBrowsableState.Never)]
442 protected override void OnUpdate()
444 ApplyAttributes(this, scrollBarAttrs);
445 ApplyAttributes(trackImage, scrollBarAttrs.TrackImageAttributes);
446 ApplyAttributes(thumbImage, scrollBarAttrs.ThumbImageAttributes);
459 /// Get Scrollbar attribues.
461 /// <since_tizen> 6 </since_tizen>
462 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
463 [EditorBrowsable(EditorBrowsableState.Never)]
464 protected override Attributes GetAttributes()
466 return new ScrollBarAttributes()
468 ThumbImageAttributes = new ImageAttributes
470 WidthResizePolicy = ResizePolicyType.Fixed,
471 HeightResizePolicy = ResizePolicyType.Fixed,
472 PositionUsesPivotPoint = true,
473 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
474 PivotPoint = Tizen.NUI.PivotPoint.TopLeft
476 TrackImageAttributes = new ImageAttributes
478 WidthResizePolicy = ResizePolicyType.FillToParent,
479 HeightResizePolicy = ResizePolicyType.FillToParent,
480 PositionUsesPivotPoint = true,
481 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
482 PivotPoint = Tizen.NUI.PivotPoint.TopLeft
489 /// Theme change callback when theme is changed, this callback will be trigger.
491 /// <since_tizen> 6 </since_tizen>
492 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
493 [EditorBrowsable(EditorBrowsableState.Never)]
494 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
496 ScrollBarAttributes tempAttributes = StyleManager.Instance.GetAttributes(style) as ScrollBarAttributes;
497 if (tempAttributes != null)
499 attributes = scrollBarAttrs = tempAttributes;
504 private void Initialize()
506 scrollBarAttrs = this.attributes as ScrollBarAttributes;
507 if(scrollBarAttrs == null)
509 throw new Exception("ScrollBar attribute parse error.");
512 this.Focusable = false;
514 trackImage = new ImageView
517 WidthResizePolicy = ResizePolicyType.FillToParent,
518 HeightResizePolicy = ResizePolicyType.FillToParent,
519 PositionUsesPivotPoint = true,
520 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
521 PivotPoint = Tizen.NUI.PivotPoint.TopLeft
524 thumbImage = new ImageView
527 WidthResizePolicy = ResizePolicyType.Fixed,
528 HeightResizePolicy = ResizePolicyType.Fixed,
529 PositionUsesPivotPoint = true,
530 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
531 PivotPoint = Tizen.NUI.PivotPoint.TopLeft
537 scrollAniPlayer = new Animation(334);
538 scrollAniPlayer.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
542 LayoutDirectionChanged += OnLayoutDirectionChanged;
545 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
550 private void UpdateValue(bool enableAni = false)
552 if (trackImage == null || thumbImage == null || scrollBarAttrs.Direction == null)
557 if (minValue >= maxValue || curValue < minValue || curValue > maxValue)
561 float width = (float)Size2D.Width;
562 float height = (float)Size2D.Height;
565 if (scrollBarAttrs.ThumbImageAttributes.Size2D == null)
571 thumbW = scrollBarAttrs.ThumbImageAttributes.Size2D.Width;
572 thumbH = scrollBarAttrs.ThumbImageAttributes.Size2D.Height;
574 float ratio = (float)(curValue - minValue) / (float)(maxValue - minValue);
576 if (scrollBarAttrs.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 (scrollAniPlayer != null)
589 scrollAniPlayer.Stop();
594 thumbImage.Position = new Position(posX, posY, 0);
598 if (scrollAniPlayer != null)
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 (scrollAniPlayer != null)
614 scrollAniPlayer.Stop();
619 thumbImage.Position = new Position(posX, posY, 0);
623 if (scrollAniPlayer != null)
625 scrollAniPlayer.Clear();
626 scrollAniPlayer.AnimateTo(thumbImage, "PositionY", posY);
627 scrollAniPlayer.Play();
633 private DirectionType CurrentDirection()
635 DirectionType dir = DirectionType.Horizontal;
636 if (scrollBarAttrs != null)
638 if (scrollBarAttrs.Direction != null)
640 dir = scrollBarAttrs.Direction.Value;
646 private int CalculateCurrentValue(float offset, DirectionType dir)
648 if (dir == DirectionType.Horizontal)
650 thumbImagePosX += offset;
651 if (thumbImagePosX < 0)
653 thumbImage.PositionX = 0;
656 else if (thumbImagePosX > Size2D.Width - thumbImage.Size2D.Width)
658 thumbImage.PositionX = Size2D.Width - thumbImage.Size2D.Width;
663 thumbImage.PositionX = thumbImagePosX;
664 curValue = (int)((thumbImagePosX / (float)(Size2D.Width - thumbImage.Size2D.Width)) * (float)(maxValue - minValue) + 0.5f);
669 thumbImagePosY += offset;
670 if (thumbImagePosY < 0)
672 thumbImage.PositionY = 0;
675 else if (thumbImagePosY > Size2D.Height - thumbImage.Size2D.Height)
677 thumbImage.PositionY = Size2D.Height - thumbImage.Size2D.Height;
682 thumbImage.PositionY = thumbImagePosY;
683 curValue = (int)((thumbImagePosY / (float)(Size2D.Height - thumbImage.Size2D.Height)) * (float)(maxValue - minValue) + 0.5f);
687 return (int)curValue;