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 /// Slider value changed event data.
27 /// <since_tizen> 8 </since_tizen>
28 public class SliderValueChangedEventArgs : EventArgs
31 /// Current Slider value
33 /// <since_tizen> 8 </since_tizen>
34 public float CurrentValue { get; set; }
38 /// Slider sliding finished event data.
40 /// <since_tizen> 8 </since_tizen>
41 public class SliderSlidingFinishedEventArgs : EventArgs
44 /// Current Slider value
46 /// <since_tizen> 8 </since_tizen>
47 public float CurrentValue { get; set; }
51 /// A slider lets users select a value from a continuous or discrete range of values by moving the slider thumb.
53 /// <since_tizen> 6 </since_tizen>
54 public partial class Slider : Control
56 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
57 [EditorBrowsable(EditorBrowsableState.Never)]
58 public static readonly BindableProperty IndicatorTypeProperty = BindableProperty.Create("IndicatorType", typeof(IndicatorType), typeof(Slider), IndicatorType.None, propertyChanged: (bindable, oldValue, newValue) =>
60 var instance = (Slider)bindable;
63 instance.privateIndicatorType = (IndicatorType)newValue;
66 defaultValueCreator: (bindable) =>
68 var instance = (Slider)bindable;
69 return instance.privateIndicatorType;
71 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
72 [EditorBrowsable(EditorBrowsableState.Never)]
73 public static readonly BindableProperty SpaceBetweenTrackAndIndicatorProperty = BindableProperty.Create(nameof(SpaceBetweenTrackAndIndicator), typeof(uint), typeof(Slider), (uint)0, propertyChanged: (bindable, oldValue, newValue) =>
75 var instance = (Slider)bindable;
78 instance.privateSpaceBetweenTrackAndIndicator = (uint)newValue;
81 defaultValueCreator: (bindable) =>
83 var instance = (Slider)bindable;
84 return instance.privateSpaceBetweenTrackAndIndicator;
86 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
87 [EditorBrowsable(EditorBrowsableState.Never)]
88 public static readonly BindableProperty TrackThicknessProperty = BindableProperty.Create(nameof(TrackThickness), typeof(uint), typeof(Slider), (uint)0, propertyChanged: (bindable, oldValue, newValue) =>
90 var instance = (Slider)bindable;
93 instance.privateTrackThickness = (uint)newValue;
96 defaultValueCreator: (bindable) =>
98 var instance = (Slider)bindable;
99 return instance.privateTrackThickness;
105 /// The constructor of the Slider class.
107 /// <since_tizen> 6 </since_tizen>
114 /// The constructor of the Slider class with specific style.
116 /// <param name="style">The string to initialize the Slider</param>
117 /// <since_tizen> 8 </since_tizen>
118 public Slider(string style) : base(style)
124 /// The constructor of the Slider class with specific style.
126 /// <param name="sliderStyle">The style object to initialize the Slider</param>
127 /// <since_tizen> 8 </since_tizen>
128 public Slider(SliderStyle sliderStyle) : base(sliderStyle)
134 /// The value changed event handler.
136 /// <since_tizen> 6 </since_tizen>
137 [Obsolete("Deprecated in API8; Will be removed in API10. Please use ValueChanged event instead.")]
138 public event EventHandler<ValueChangedArgs> ValueChangedEvent
142 valueChangedHandler += value;
146 valueChangedHandler -= value;
151 /// The sliding finished event handler.
153 /// <since_tizen> 6 </since_tizen>
154 [Obsolete("Deprecated in API8; Will be removed in API10. Please use SlidingFinished event instead.")]
155 public event EventHandler<SlidingFinishedArgs> SlidingFinishedEvent
159 slidingFinishedHandler += value;
163 slidingFinishedHandler -= value;
168 /// The value changed event handler.
170 /// <since_tizen> 8 </since_tizen>
171 public event EventHandler<SliderValueChangedEventArgs> ValueChanged
175 sliderValueChangedHandler += value;
179 sliderValueChangedHandler -= value;
184 /// The sliding finished event handler.
186 /// <since_tizen> 8 </since_tizen>
187 public event EventHandler<SliderSlidingFinishedEventArgs> SlidingFinished
191 sliderSlidingFinishedHandler += value;
195 sliderSlidingFinishedHandler -= value;
200 /// The state changed event handler.
202 /// <since_tizen> 6 </since_tizen>
203 [Obsolete("Deprecated in API8; Will be removed in API10. Please use View.ControlStateChangedEvent")]
204 public event EventHandler<StateChangedArgs> StateChangedEvent
208 stateChangedHandler += value;
212 stateChangedHandler -= value;
217 /// The direction type of slider.
219 /// <since_tizen> 6 </since_tizen>
220 public enum DirectionType
223 /// The Horizontal type.
225 /// <since_tizen> 6 </since_tizen>
229 /// The Vertical type.
231 /// <since_tizen> 6 </since_tizen>
236 /// The indicator type of slider.
238 /// <since_tizen> 6 </since_tizen>
239 public enum IndicatorType
241 /// <summary> Only contains slider bar.</summary>
242 /// <since_tizen> 6 </since_tizen>
245 /// <summary> Contains slider bar, IndicatorImage.</summary>
246 /// <since_tizen> 6 </since_tizen>
249 /// <summary> Contains slider bar, IndicatorText.</summary>
250 /// <since_tizen> 6 </since_tizen>
255 /// Return a copied Style instance of Slider
258 /// It returns copied Style instance and changing it does not effect to the Slider.
259 /// Style setting is possible by using constructor or the function of ApplyStyle(ViewStyle viewStyle)
261 /// <since_tizen> 8 </since_tizen>
262 public new SliderStyle Style
266 var result = new SliderStyle(sliderStyle);
267 result.CopyPropertiesFromView(this);
268 result.Track.CopyPropertiesFromView(bgTrackImage);
269 result.Progress.CopyPropertiesFromView(slidedTrackImage);
270 result.Thumb.CopyPropertiesFromView(thumbImage);
271 result.LowIndicatorImage.CopyPropertiesFromView(lowIndicatorImage);
272 result.HighIndicatorImage.CopyPropertiesFromView(highIndicatorImage);
273 result.LowIndicator.CopyPropertiesFromView(lowIndicatorText);
274 result.HighIndicator.CopyPropertiesFromView(highIndicatorText);
280 /// Return a copied Style instance of Slider
282 private SliderStyle sliderStyle => ViewStyle as SliderStyle;
285 /// Gets or sets the direction type of slider.
287 /// <since_tizen> 6 </since_tizen>
288 public DirectionType Direction
296 if (direction == value)
301 RelayoutBaseComponent(false);
303 UpdateBgTrackPosition();
309 /// Gets or sets the indicator type, arrow or sign.
311 /// <since_tizen> 6 </since_tizen>
312 public IndicatorType Indicator
316 return (IndicatorType)GetValue(IndicatorTypeProperty);
320 SetValue(IndicatorTypeProperty, value);
325 /// Gets or sets the minimum value of slider.
327 /// <since_tizen> 6 </since_tizen>
328 public float MinValue
342 /// Gets or sets the maximum value of slider.
344 /// <since_tizen> 6 </since_tizen>
345 public float MaxValue
359 /// Gets or sets the current value of slider.
361 /// <since_tizen> 6 </since_tizen>
362 public float CurrentValue
376 /// Gets or sets the size of the thumb image object.
378 /// <since_tizen> 6 </since_tizen>
379 public Size ThumbSize
383 return thumbImage?.Size;
387 if (null != thumbImage)
389 thumbImage.Size = value;
390 sliderStyle.Thumb.Size = value;
396 /// Gets or sets the resource url of the thumb image object.
398 /// <since_tizen> 6 </since_tizen>
399 public string ThumbImageURL
403 return thumbImage?.ResourceUrl;
407 if (null != thumbImage)
409 thumbImage.ResourceUrl = value;
410 sliderStyle.Thumb.ResourceUrl = value;
416 /// Gets or sets the resource url selector of the thumb image object.
417 /// Getter returns copied selector value if exist, null otherwise.
419 /// <since_tizen> 6 </since_tizen>
420 public StringSelector ThumbImageURLSelector
422 get => thumbImage == null ? null : new StringSelector((Selector<string>)thumbImage.GetValue(ImageView.ResourceUrlSelectorProperty));
423 set => thumbImage?.SetValue(ImageView.ResourceUrlSelectorProperty, value);
427 /// Gets or sets the color of the background track image object.
429 /// <since_tizen> 6 </since_tizen>
430 public Color BgTrackColor
434 return bgTrackImage?.BackgroundColor;
438 if (null != bgTrackImage)
440 bgTrackImage.BackgroundColor = value;
441 sliderStyle.Track.BackgroundColor = value;
447 /// Gets or sets the color of the slided track image object.
449 /// <since_tizen> 6 </since_tizen>
450 public Color SlidedTrackColor
454 return slidedTrackImage?.BackgroundColor;
458 if (null != slidedTrackImage)
460 slidedTrackImage.BackgroundColor = value;
461 sliderStyle.Progress.BackgroundColor = value;
467 /// Gets or sets the thickness value of the track.
469 /// <since_tizen> 6 </since_tizen>
470 public uint TrackThickness
474 return (uint)GetValue(TrackThicknessProperty);
478 SetValue(TrackThicknessProperty, value);
483 /// Gets or sets the resource url of the low indicator image object.
485 /// <since_tizen> 6 </since_tizen>
486 public string LowIndicatorImageURL
490 return lowIndicatorImage?.ResourceUrl;
494 if (null != lowIndicatorImage)
496 lowIndicatorImage.ResourceUrl = value;
497 sliderStyle.LowIndicatorImage.ResourceUrl = value;
503 /// Gets or sets the resource url of the high indicator image object.
505 /// <since_tizen> 6 </since_tizen>
506 public string HighIndicatorImageURL
510 return highIndicatorImage?.ResourceUrl;
514 if (null != highIndicatorImage)
516 highIndicatorImage.ResourceUrl = value;
517 sliderStyle.HighIndicatorImage.ResourceUrl = value;
523 /// Gets or sets the text content of the low indicator text object.
525 /// <since_tizen> 6 </since_tizen>
526 public string LowIndicatorTextContent
530 return lowIndicatorText?.Text;
534 if (null != lowIndicatorText)
536 lowIndicatorText.Text= value;
537 sliderStyle.LowIndicator.Text= value;
543 /// Gets or sets the text content of the high indicator text object.
545 /// <since_tizen> 6 </since_tizen>
546 public string HighIndicatorTextContent
550 return highIndicatorText?.Text;
554 if (null != highIndicatorText)
556 highIndicatorText.Text = value;
557 sliderStyle.HighIndicator.Text = value;
563 /// Gets or sets the size of the low indicator object(image or text).
565 /// <since_tizen> 6 </since_tizen>
566 public Size LowIndicatorSize
570 return lowIndicatorSize;
574 lowIndicatorSize = value;
575 UpdateLowIndicatorSize();
577 UpdateBgTrackPosition();
583 /// Gets or sets the size of the high indicator object(image or text).
585 /// <since_tizen> 6 </since_tizen>
586 public Size HighIndicatorSize
590 return highIndicatorText?.Size;
594 if (null != highIndicatorText)
596 highIndicatorText.Size = value;
597 sliderStyle.HighIndicator.Size = value;
603 /// Gets or sets the value of the space between track and indicator.
605 /// <since_tizen> 6 </since_tizen>
606 public uint SpaceBetweenTrackAndIndicator
610 return (uint)GetValue(SpaceBetweenTrackAndIndicatorProperty);
614 SetValue(SpaceBetweenTrackAndIndicatorProperty, value);
618 private Extents spaceBetweenTrackAndIndicator
622 if (null == _spaceBetweenTrackAndIndicator)
624 _spaceBetweenTrackAndIndicator = new Extents((ushort start, ushort end, ushort top, ushort bottom) =>
626 Extents extents = new Extents(start, end, top, bottom);
627 _spaceBetweenTrackAndIndicator.CopyFrom(extents);
631 return _spaceBetweenTrackAndIndicator;
635 private IndicatorType privateIndicatorType
639 return indicatorType;
643 if (indicatorType == value)
647 indicatorType = value;
648 RelayoutBaseComponent(false);
650 UpdateBgTrackPosition();
655 private uint privateTrackThickness
659 return trackThickness ?? 0;
663 trackThickness = value;
664 if (bgTrackImage != null)
666 if (direction == DirectionType.Horizontal)
668 bgTrackImage.SizeHeight = (float)trackThickness.Value;
670 else if (direction == DirectionType.Vertical)
672 bgTrackImage.SizeWidth = (float)trackThickness.Value;
675 if (slidedTrackImage != null)
677 if (direction == DirectionType.Horizontal)
679 slidedTrackImage.SizeHeight = (float)trackThickness.Value;
681 else if (direction == DirectionType.Vertical)
683 slidedTrackImage.SizeWidth = (float)trackThickness.Value;
689 private uint privateSpaceBetweenTrackAndIndicator
693 return privateTrackPadding.Start;
697 ushort val = (ushort)value;
698 privateTrackPadding = new Extents(val, val, val, val);
702 private Extents privateTrackPadding
706 return spaceBetweenTrackAndIndicator;
710 spaceBetweenTrackAndIndicator.CopyFrom(value);
711 UpdateComponentByIndicatorTypeChanged();
713 UpdateBgTrackPosition();
719 /// Focus gained callback.
721 /// <since_tizen> 8 </since_tizen>
722 public override void OnFocusGained()
724 //State = ControlStates.Focused;
725 UpdateState(true, isPressed);
726 base.OnFocusGained();
730 /// Focus Lost callback.
732 /// <since_tizen> 8 </since_tizen>
733 public override void OnFocusLost()
735 //State = ControlStates.Normal;
736 UpdateState(false, isPressed);
741 /// Apply style to scrollbar.
743 /// <param name="viewStyle">The style to apply.</param>
744 /// <since_tizen> 8 </since_tizen>
745 public override void ApplyStyle(ViewStyle viewStyle)
747 base.ApplyStyle(viewStyle);
749 SliderStyle sliderStyle = viewStyle as SliderStyle;
751 if (null != sliderStyle?.Progress)
753 CreateSlidedTrack().ApplyStyle(sliderStyle.Progress);
756 if (null != sliderStyle?.LowIndicator)
758 CreateLowIndicatorText().ApplyStyle(sliderStyle.LowIndicator);
761 if (null != sliderStyle?.HighIndicator)
763 CreateHighIndicatorText().ApplyStyle(sliderStyle.HighIndicator);
766 if (null != sliderStyle?.Track)
768 CreateBackgroundTrack().ApplyStyle(sliderStyle.Track);
771 if (null != sliderStyle?.Thumb)
773 CreateThumb().ApplyStyle(sliderStyle.Thumb);
776 EnableControlStatePropagation = true;
780 /// Get Slider style.
782 /// <returns>The default slider style.</returns>
783 /// <since_tizen> 8 </since_tizen>
784 protected override ViewStyle CreateViewStyle()
786 return new SliderStyle();
792 /// <param name="type">Dispose type.</param>
793 /// <since_tizen> 6 </since_tizen>
794 protected override void Dispose(DisposeTypes type)
801 if (type == DisposeTypes.Explicit)
803 if (null != panGestureDetector)
805 if (null != thumbImage)
807 panGestureDetector.Detach(thumbImage);
809 panGestureDetector.Detected -= OnPanGestureDetected;
810 panGestureDetector.Dispose();
811 panGestureDetector = null;
814 if (null != thumbImage)
816 thumbImage.TouchEvent -= OnTouchEventForThumb;
817 Utility.Dispose(thumbImage);
819 Utility.Dispose(slidedTrackImage);
820 if (null != bgTrackImage)
822 bgTrackImage.TouchEvent -= OnTouchEventForBgTrack;
823 Utility.Dispose(bgTrackImage);
825 Utility.Dispose(lowIndicatorImage);
826 Utility.Dispose(highIndicatorImage);
827 Utility.Dispose(lowIndicatorText);
828 Utility.Dispose(highIndicatorText);
835 /// Update Slider by style.
837 /// <since_tizen> 6 </since_tizen>
838 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
839 [EditorBrowsable(EditorBrowsableState.Never)]
840 protected override void OnUpdate()
842 RelayoutBaseComponent();
844 UpdateComponentByIndicatorTypeChanged();
846 UpdateBgTrackPosition();
847 UpdateLowIndicatorSize();
852 /// Theme change callback when theme is changed, this callback will be trigger.
854 /// <param name="sender">The sender</param>
855 /// <param name="e">The event data</param>
856 [EditorBrowsable(EditorBrowsableState.Never)]
857 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
859 SliderStyle sliderStyle = StyleManager.Instance.GetViewStyle(StyleName) as SliderStyle;
860 if (sliderStyle != null)
862 ApplyStyle(sliderStyle);
867 private void CalculateCurrentValueByGesture(float offset)
869 currentSlidedOffset += offset;
871 if (currentSlidedOffset <= 0)
875 else if (currentSlidedOffset >= BgTrackLength())
881 int bgTrackLength = BgTrackLength();
882 if (bgTrackLength != 0)
884 curValue = ((currentSlidedOffset / (float)bgTrackLength) * (float)(maxValue - minValue)) + minValue;
887 if (valueChangedHandler != null)
889 ValueChangedArgs args = new ValueChangedArgs();
890 args.CurrentValue = curValue;
891 valueChangedHandler(this, args);
894 if (sliderValueChangedHandler != null)
896 SliderValueChangedEventArgs args = new SliderValueChangedEventArgs();
897 args.CurrentValue = curValue;
898 sliderValueChangedHandler(this, args);
902 private bool OnTouchEventForBgTrack(object source, TouchEventArgs e)
904 PointStateType state = e.Touch.GetState(0);
905 if (state == PointStateType.Down)
907 Vector2 pos = e.Touch.GetLocalPosition(0);
908 CalculateCurrentValueByTouch(pos);
910 if (null != slidingFinishedHandler)
912 SlidingFinishedArgs args = new SlidingFinishedArgs();
913 args.CurrentValue = curValue;
914 slidingFinishedHandler(this, args);
917 if (null !=sliderSlidingFinishedHandler)
919 SliderSlidingFinishedEventArgs args = new SliderSlidingFinishedEventArgs();
920 args.CurrentValue = curValue;
921 sliderSlidingFinishedHandler(this, args);
927 private bool OnTouchEventForThumb(object source, TouchEventArgs e)
929 PointStateType state = e.Touch.GetState(0);
930 if (state == PointStateType.Down)
932 UpdateState(isFocused, true);
934 else if (state == PointStateType.Up)
936 UpdateState(isFocused, false);
941 private void CalculateCurrentValueByTouch(Vector2 pos)
943 int bgTrackLength = BgTrackLength();
944 if (direction == DirectionType.Horizontal)
946 currentSlidedOffset = pos.X;
948 else if (direction == DirectionType.Vertical)
950 currentSlidedOffset = bgTrackLength - pos.Y;
952 if (bgTrackLength != 0)
954 curValue = ((currentSlidedOffset / (float)bgTrackLength) * (maxValue - minValue)) + minValue;
955 if (null != valueChangedHandler)
957 ValueChangedArgs args = new ValueChangedArgs();
958 args.CurrentValue = curValue;
959 valueChangedHandler(this, args);
962 if (null != sliderValueChangedHandler)
964 SliderValueChangedEventArgs args = new SliderValueChangedEventArgs();
965 args.CurrentValue = curValue;
966 sliderValueChangedHandler(this, args);
971 private void UpdateState(bool isFocusedNew, bool isPressedNew)
973 if (isFocused == isFocusedNew && isPressed == isPressedNew)
977 if (thumbImage == null || Style == null)
981 isFocused = isFocusedNew;
982 isPressed = isPressedNew;
984 if (!isFocused && !isPressed)
986 ControlState = ControlState.Normal;
987 if (stateChangedHandler != null)
989 StateChangedArgs args = new StateChangedArgs();
990 args.CurrentState = (ControlStates)ControlStates.Normal;
991 stateChangedHandler(this, args);
996 ControlState = ControlState.Pressed;
998 if (stateChangedHandler != null)
1000 StateChangedArgs args = new StateChangedArgs();
1001 args.CurrentState = (ControlStates)ControlStates.Pressed;
1002 stateChangedHandler(this, args);
1005 else if (!isPressed && isFocused)
1007 ControlState = ControlState.Focused;
1009 if (stateChangedHandler != null)
1011 StateChangedArgs args = new StateChangedArgs();
1012 args.CurrentState = (ControlStates)ControlStates.Focused;
1013 stateChangedHandler(this, args);
1018 private void UpdateComponentByIndicatorTypeChanged()
1020 IndicatorType type = CurrentIndicatorType();
1021 if (type == IndicatorType.None)
1023 if (lowIndicatorImage != null)
1025 lowIndicatorImage.Hide();
1027 if (highIndicatorImage != null)
1029 highIndicatorImage.Hide();
1031 if (lowIndicatorText != null)
1033 lowIndicatorText.Hide();
1035 if (highIndicatorText != null)
1037 highIndicatorText.Hide();
1040 else if (type == IndicatorType.Image)
1042 if (lowIndicatorImage != null)
1044 lowIndicatorImage.Show();
1046 if (highIndicatorImage != null)
1048 highIndicatorImage.Show();
1050 if (lowIndicatorText != null)
1052 lowIndicatorText.Hide();
1054 if (highIndicatorText != null)
1056 highIndicatorText.Hide();
1059 else if (type == IndicatorType.Text)
1061 if (lowIndicatorText != null)
1063 lowIndicatorText.Show();
1065 if (highIndicatorText != null)
1067 highIndicatorText.Show();
1069 if (lowIndicatorImage != null)
1071 lowIndicatorImage.Hide();
1073 if (highIndicatorImage != null)
1075 highIndicatorImage.Hide();
1081 /// Value Changed event data.
1083 /// <since_tizen> 6 </since_tizen>
1084 [Obsolete("Deprecated in API8; Will be removed in API10. Please use SliderValueChangedEventArgs instead.")]
1085 public class ValueChangedArgs : EventArgs
1090 /// <since_tizen> 6 </since_tizen>
1091 [Obsolete("Deprecated in API8; Will be removed in API10. Please use SliderValueChangedEventArgs.CurrentValue instead.")]
1092 public float CurrentValue;
1096 /// Value Changed event data.
1098 /// <since_tizen> 6 </since_tizen>
1099 [Obsolete("Deprecated in API8; Will be removed in API10. Please use SliderSlidingFinishedEventArgs instead.")]
1100 public class SlidingFinishedArgs : EventArgs
1105 /// <since_tizen> 6 </since_tizen>
1106 [Obsolete("Deprecated in API8; Will be removed in API10. Please use SliderSlidingFinishedEventArgs.CurrentValue instead.")]
1107 public float CurrentValue;
1111 /// State Changed event data.
1113 /// <since_tizen> 6 </since_tizen>
1114 [Obsolete("Deprecated in API8; Will be removed in API10. Please use View.ControlStateChangedEventArgs")]
1115 public class StateChangedArgs : EventArgs
1120 /// <since_tizen> 6 </since_tizen>
1121 [Obsolete("Deprecated in API8; Will be removed in API10")]
1122 public ControlStates CurrentState;