f6a20ac4602a19de3d4759bb7ae42db29b94737d
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Slider.Internal.cs
1 /*
2  * Copyright(c) 2022 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 using System;
19 using System.ComponentModel;
20 using Tizen.NUI.BaseComponents;
21
22 namespace Tizen.NUI.Components
23 {
24     public partial class Slider
25     {
26         // the background track image object
27         private ImageView bgTrackImage = null;
28         // the slided track image object
29         private ImageView slidedTrackImage = null;
30         // the warning track image object
31         private ImageView warningTrackImage = null;
32         // the warning slided track image object
33         private ImageView warningSlidedTrackImage = null;
34         // the thumb image object
35         private ImageView thumbImage = null;
36         // the low indicator image object
37         private ImageView lowIndicatorImage = null;
38         // the high indicator image object
39         private ImageView highIndicatorImage = null;
40         // the low indicator text object
41         private TextLabel lowIndicatorText = null;
42         // the high indicator text object
43         private TextLabel highIndicatorText = null;
44         // the direction type
45         private DirectionType direction = DirectionType.Horizontal;
46         // the indicator type
47         private IndicatorType indicatorType = IndicatorType.None;
48         private const float round = 0.5f;
49         // the minimum value
50         private float minValue = 0;
51         // the maximum value
52         private float maxValue = 100;
53         // the current value
54         private float curValue = 0;
55         // the warning start value
56         private float warningStartValue = 100;
57         // the size of the low indicator
58         private Size lowIndicatorSize = null;
59         // the size of the high indicator
60         private Size highIndicatorSize = null;
61         // the track thickness value
62         private uint? trackThickness = null;
63         // the value of the space between track and indicator object
64         private Extents spaceTrackIndicator = null;
65         // Whether the value indicator is shown or not
66         private bool isValueShown = false;
67         // the value indicator text
68         private TextLabel valueIndicatorText = null;
69         // the value indicator image object
70         private ImageView valueIndicatorImage = null;
71
72         // To store the thumb size of normal state
73         private Size thumbSize = null;
74         // To store the thumb image url of normal state
75         private string thumbImageUrl = null;
76         // To store the thumb color of normal state
77         private Color thumbColor = Color.White;
78         // To store the thumb image url of warning state
79         private string warningThumbImageUrl = null;
80         // To store the thumb image url selector of warning state
81         private Selector<string> warningThumbImageUrlSelector = null;
82         // To store the thumb color of warning state
83         private Color warningThumbColor = null;
84         // the discrete value
85         private float discreteValue = 0;
86
87         private PanGestureDetector panGestureDetector = null;
88         private float currentSlidedOffset;
89         private EventHandler<SliderValueChangedEventArgs> sliderValueChangedHandler;
90         private EventHandler<SliderSlidingStartedEventArgs> sliderSlidingStartedHandler;
91         private EventHandler<SliderSlidingFinishedEventArgs> sliderSlidingFinishedHandler;
92
93         bool isFocused = false;
94         bool isPressed = false;
95
96         private void Initialize()
97         {
98             AccessibilityHighlightable = true;
99
100             currentSlidedOffset = 0;
101             isFocused = false;
102             isPressed = false;
103             LayoutDirectionChanged += OnLayoutDirectionChanged;
104
105             this.TouchEvent += OnTouchEventForTrack;
106
107             panGestureDetector = new PanGestureDetector();
108             panGestureDetector.Attach(this);
109             panGestureDetector.Detected += OnPanGestureDetected;
110
111             this.Layout = new LinearLayout()
112             {
113                 LinearOrientation = LinearLayout.Orientation.Horizontal,
114             };
115         }
116
117         private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
118         {
119             RelayoutRequest();
120         }
121
122         private ImageView CreateSlidedTrack()
123         {
124             if (null == slidedTrackImage)
125             {
126                 slidedTrackImage = new ImageView()
127                 {
128                     WidthResizePolicy = ResizePolicyType.Fixed,
129                     HeightResizePolicy = ResizePolicyType.Fixed,
130                     AccessibilityHidden = true,
131                 };
132
133                 if (bgTrackImage != null)
134                 {
135                     bgTrackImage.Add(slidedTrackImage);
136                 }
137             }
138
139             return slidedTrackImage;
140         }
141
142         private ImageView CreateWarningTrack()
143         {
144             if (null == warningTrackImage)
145             {
146                 warningTrackImage = new ImageView()
147                 {
148                     WidthResizePolicy = ResizePolicyType.Fixed,
149                     HeightResizePolicy = ResizePolicyType.Fixed,
150                     AccessibilityHidden = true,
151                 };
152
153                 if (bgTrackImage != null)
154                 {
155                     bgTrackImage.Add(warningTrackImage);
156                 }
157
158                 if (warningSlidedTrackImage != null)
159                 {
160                     warningTrackImage.Add(warningSlidedTrackImage);
161                 }
162
163                 if (slidedTrackImage != null)
164                 {
165                     warningTrackImage.RaiseAbove(slidedTrackImage);
166                 }
167             }
168
169             return warningTrackImage;
170         }
171
172         private ImageView CreateWarningSlidedTrack()
173         {
174             if (null == warningSlidedTrackImage)
175             {
176                 warningSlidedTrackImage = new ImageView()
177                 {
178                     WidthResizePolicy = ResizePolicyType.Fixed,
179                     HeightResizePolicy = ResizePolicyType.Fixed,
180                     AccessibilityHidden = true,
181                 };
182
183                 if (warningTrackImage != null)
184                 {
185                     warningTrackImage.Add(warningSlidedTrackImage);
186                 }
187             }
188
189             return warningSlidedTrackImage;
190         }
191
192         private TextLabel CreateLowIndicatorText()
193         {
194             if (null == lowIndicatorText)
195             {
196                 lowIndicatorText = new TextLabel()
197                 {
198                     WidthResizePolicy = ResizePolicyType.Fixed,
199                     HeightResizePolicy = ResizePolicyType.Fixed,
200                     AccessibilityHidden = true,
201                 };
202                 this.Add(lowIndicatorText);
203             }
204
205             return lowIndicatorText;
206         }
207
208         private TextLabel CreateHighIndicatorText()
209         {
210             if (null == highIndicatorText)
211             {
212                 highIndicatorText = new TextLabel()
213                 {
214                     WidthResizePolicy = ResizePolicyType.Fixed,
215                     HeightResizePolicy = ResizePolicyType.Fixed,
216                     AccessibilityHidden = true,
217                 };
218                 this.Add(highIndicatorText);
219             }
220
221             return highIndicatorText;
222         }
223
224         private ImageView CreateBackgroundTrack()
225         {
226             if (null == bgTrackImage)
227             {
228                 bgTrackImage = new ImageView()
229                 {
230                     WidthResizePolicy = ResizePolicyType.Fixed,
231                     HeightResizePolicy = ResizePolicyType.Fixed,
232                     ParentOrigin = Tizen.NUI.ParentOrigin.Center,
233                     PivotPoint = Tizen.NUI.PivotPoint.Center,
234                     PositionUsesPivotPoint = true,
235                     AccessibilityHidden = true,
236                 };
237                 this.Add(bgTrackImage);
238
239                 if (null != slidedTrackImage)
240                 {
241                     bgTrackImage.Add(slidedTrackImage);
242                 }
243                 if (null != warningTrackImage)
244                 {
245                     bgTrackImage.Add(warningTrackImage);
246                 }
247                 if (null != thumbImage)
248                 {
249                     bgTrackImage.Add(thumbImage);
250                     thumbImage.RaiseToTop();
251                 }
252             }
253
254             return bgTrackImage;
255         }
256
257         private ImageView CreateThumb()
258         {
259             if (null == thumbImage)
260             {
261                 thumbImage = new ImageView()
262                 {
263                     WidthResizePolicy = ResizePolicyType.Fixed,
264                     HeightResizePolicy = ResizePolicyType.Fixed,
265                     ParentOrigin = NUI.ParentOrigin.Center,
266                     PivotPoint = NUI.PivotPoint.Center,
267                     PositionUsesPivotPoint = true,
268                     EnableControlState = true,
269                     AccessibilityHidden = true,
270                 };
271                 if (bgTrackImage != null)
272                 {
273                     bgTrackImage.Add(thumbImage);
274                 }
275                 thumbImage.RaiseToTop();
276                 thumbImage.TouchEvent += OnTouchEventForThumb;
277             }
278
279             return thumbImage;
280         }
281
282         private ImageView CreateValueIndicator()
283         {
284             if (valueIndicatorImage == null)
285             {
286                 valueIndicatorImage = new ImageView()
287                 {
288                     PositionUsesPivotPoint = true,
289                     ParentOrigin = Tizen.NUI.ParentOrigin.Center,
290                     PivotPoint = Tizen.NUI.PivotPoint.Center,
291                     WidthResizePolicy = ResizePolicyType.Fixed,
292                     HeightResizePolicy = ResizePolicyType.Fixed,
293                     AccessibilityHidden = true,
294                 };
295                 if (valueIndicatorText != null)
296                 {
297                     valueIndicatorImage.Add(valueIndicatorText);
298                 }
299                 // TODO : ValueIndicator can be a child of Thumb
300                 this.Add(valueIndicatorImage);
301             }
302
303             valueIndicatorImage.Hide();
304             return valueIndicatorImage;
305         }
306
307         private TextLabel CreateValueIndicatorText()
308         {
309             if (null == valueIndicatorText)
310             {
311                 valueIndicatorText = new TextLabel()
312                 {
313                     WidthResizePolicy = ResizePolicyType.Fixed,
314                     HeightResizePolicy = ResizePolicyType.Fixed,
315                     AccessibilityHidden = true,
316                 };
317                 if (valueIndicatorImage != null)
318                 {
319                     valueIndicatorImage.Add(valueIndicatorText);
320                 }
321             }
322
323             return valueIndicatorText;
324         }
325
326         private void OnPanGestureDetected(object source, PanGestureDetector.DetectedEventArgs e)
327         {
328             if (e.PanGesture.State == Gesture.StateType.Started)
329             {
330                 if (direction == DirectionType.Horizontal)
331                 {
332                     currentSlidedOffset = slidedTrackImage.SizeWidth;
333                 }
334                 else if (direction == DirectionType.Vertical)
335                 {
336                     currentSlidedOffset = slidedTrackImage.SizeHeight;
337                 }
338
339                 if (isValueShown)
340                 {
341                     valueIndicatorImage.Show();
342                 }
343
344                 if (null != sliderSlidingStartedHandler)
345                 {
346                     SliderSlidingStartedEventArgs args = new SliderSlidingStartedEventArgs();
347                     args.CurrentValue = curValue;
348                     sliderSlidingStartedHandler(this, args);
349                 }
350                 UpdateState(isFocused, true);
351             }
352
353             if (e.PanGesture.State == Gesture.StateType.Continuing || e.PanGesture.State == Gesture.StateType.Started)
354             {
355                 if (direction == DirectionType.Horizontal)
356                 {
357                     CalculateCurrentValueByGesture(e.PanGesture.Displacement.X);
358                 }
359                 else if (direction == DirectionType.Vertical)
360                 {
361                     CalculateCurrentValueByGesture(-e.PanGesture.Displacement.Y);
362                 }
363                 UpdateValue();
364             }
365
366             if (e.PanGesture.State == Gesture.StateType.Finished)
367             {
368                 if (isValueShown)
369                 {
370                     valueIndicatorImage.Hide();
371                 }
372
373                 if (null != sliderSlidingFinishedHandler)
374                 {
375                     SliderSlidingFinishedEventArgs args = new SliderSlidingFinishedEventArgs();
376                     args.CurrentValue = curValue;
377                     sliderSlidingFinishedHandler(this, args);
378                 }
379
380                 UpdateState(isFocused, false);
381             }
382         }
383
384         // Relayout basic component: track, thumb and indicator
385         private void RelayoutBaseComponent(bool isInitial = true)
386         {
387             if (direction == DirectionType.Horizontal)
388             {
389                 if (slidedTrackImage != null)
390                 {
391                     slidedTrackImage.ParentOrigin = NUI.ParentOrigin.CenterLeft;
392                     slidedTrackImage.PivotPoint = NUI.PivotPoint.CenterLeft;
393                     slidedTrackImage.PositionUsesPivotPoint = true;
394                 }
395                 if (warningTrackImage != null)
396                 {
397                     warningTrackImage.ParentOrigin = NUI.ParentOrigin.CenterRight;
398                     warningTrackImage.PivotPoint = NUI.PivotPoint.CenterRight;
399                     warningTrackImage.PositionUsesPivotPoint = true;
400                 }
401                 if (warningSlidedTrackImage != null)
402                 {
403                     warningSlidedTrackImage.ParentOrigin = NUI.ParentOrigin.CenterLeft;
404                     warningSlidedTrackImage.PivotPoint = NUI.PivotPoint.CenterLeft;
405                     warningSlidedTrackImage.PositionUsesPivotPoint = true;
406                 }
407                 if (thumbImage != null)
408                 {
409                     thumbImage.ParentOrigin = NUI.ParentOrigin.CenterLeft;
410                     thumbImage.PivotPoint = NUI.PivotPoint.Center;
411                     thumbImage.PositionUsesPivotPoint = true;
412                 }
413                 if (lowIndicatorImage != null)
414                 {
415                     lowIndicatorImage.ParentOrigin = NUI.ParentOrigin.CenterLeft;
416                     lowIndicatorImage.PivotPoint = NUI.PivotPoint.CenterLeft;
417                     lowIndicatorImage.PositionUsesPivotPoint = true;
418                 }
419                 if (highIndicatorImage != null)
420                 {
421                     highIndicatorImage.ParentOrigin = NUI.ParentOrigin.CenterRight;
422                     highIndicatorImage.PivotPoint = NUI.PivotPoint.CenterRight;
423                     highIndicatorImage.PositionUsesPivotPoint = true;
424                 }
425                 if (lowIndicatorText != null)
426                 {
427                     lowIndicatorText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
428                     lowIndicatorText.PivotPoint = NUI.PivotPoint.CenterLeft;
429                     lowIndicatorText.PositionUsesPivotPoint = true;
430                 }
431                 if (highIndicatorText != null)
432                 {
433                     highIndicatorText.ParentOrigin = NUI.ParentOrigin.CenterRight;
434                     highIndicatorText.PivotPoint = NUI.PivotPoint.CenterRight;
435                     highIndicatorText.PositionUsesPivotPoint = true;
436                 }
437                 if (valueIndicatorImage != null)
438                 {
439                     valueIndicatorImage.ParentOrigin = NUI.ParentOrigin.TopLeft;
440                     valueIndicatorImage.PivotPoint = NUI.PivotPoint.BottomCenter;
441                     valueIndicatorImage.PositionUsesPivotPoint = true;
442                 }
443                 if (valueIndicatorText != null)
444                 {
445                     valueIndicatorText.ParentOrigin = NUI.ParentOrigin.TopCenter;
446                     valueIndicatorText.PivotPoint = NUI.PivotPoint.TopCenter;
447                     valueIndicatorText.PositionUsesPivotPoint = true;
448                     valueIndicatorText.Padding = new Extents(0, 0, 5, 0); // TODO : How to set the text as center
449                 }
450                 if (panGestureDetector != null)
451                 {
452                     if (!isInitial)
453                     {
454                         panGestureDetector.RemoveDirection(PanGestureDetector.DirectionVertical);
455                     }
456                     panGestureDetector.AddDirection(PanGestureDetector.DirectionHorizontal);
457                 }
458             }
459             else if (direction == DirectionType.Vertical)
460             {
461                 if (slidedTrackImage != null)
462                 {
463                     slidedTrackImage.ParentOrigin = NUI.ParentOrigin.BottomCenter;
464                     slidedTrackImage.PivotPoint = NUI.PivotPoint.BottomCenter;
465                     slidedTrackImage.PositionUsesPivotPoint = true;
466                 }
467                 if (warningTrackImage != null)
468                 {
469                     warningTrackImage.ParentOrigin = NUI.ParentOrigin.TopCenter;
470                     warningTrackImage.PivotPoint = NUI.PivotPoint.TopCenter;
471                     warningTrackImage.PositionUsesPivotPoint = true;
472                 }
473                 if (warningSlidedTrackImage != null)
474                 {
475                     warningSlidedTrackImage.ParentOrigin = NUI.ParentOrigin.BottomCenter;
476                     warningSlidedTrackImage.PivotPoint = NUI.PivotPoint.BottomCenter;
477                     warningSlidedTrackImage.PositionUsesPivotPoint = true;
478                 }
479                 if (thumbImage != null)
480                 {
481                     thumbImage.ParentOrigin = NUI.ParentOrigin.BottomCenter;
482                     thumbImage.PivotPoint = NUI.PivotPoint.Center;
483                     thumbImage.PositionUsesPivotPoint = true;
484                 }
485                 if (lowIndicatorImage != null)
486                 {
487                     lowIndicatorImage.ParentOrigin = NUI.ParentOrigin.BottomCenter;
488                     lowIndicatorImage.PivotPoint = NUI.PivotPoint.BottomCenter;
489                     lowIndicatorImage.PositionUsesPivotPoint = true;
490                 }
491                 if (highIndicatorImage != null)
492                 {
493                     highIndicatorImage.ParentOrigin = NUI.ParentOrigin.TopCenter;
494                     highIndicatorImage.PivotPoint = NUI.PivotPoint.TopCenter;
495                     highIndicatorImage.PositionUsesPivotPoint = true;
496                 }
497                 if (lowIndicatorText != null)
498                 {
499                     lowIndicatorText.ParentOrigin = NUI.ParentOrigin.BottomCenter;
500                     lowIndicatorText.PivotPoint = NUI.PivotPoint.BottomCenter;
501                     lowIndicatorText.PositionUsesPivotPoint = true;
502                 }
503                 if (highIndicatorText != null)
504                 {
505                     highIndicatorText.ParentOrigin = NUI.ParentOrigin.TopCenter;
506                     highIndicatorText.PivotPoint = NUI.PivotPoint.TopCenter;
507                     highIndicatorText.PositionUsesPivotPoint = true;
508                 }
509                 if (valueIndicatorImage != null)
510                 {
511                     valueIndicatorImage.ParentOrigin = NUI.ParentOrigin.BottomCenter;
512                     valueIndicatorImage.PivotPoint = NUI.PivotPoint.BottomCenter;
513                     valueIndicatorImage.PositionUsesPivotPoint = true;
514                 }
515                 if (valueIndicatorText != null)
516                 {
517                     valueIndicatorText.ParentOrigin = NUI.ParentOrigin.TopCenter;
518                     valueIndicatorText.PivotPoint = NUI.PivotPoint.TopCenter;
519                     valueIndicatorText.PositionUsesPivotPoint = true;
520                     valueIndicatorText.Padding = new Extents(0, 0, 5, 0); // TODO : How to set the text as center
521                 }
522                 if (panGestureDetector != null)
523                 {
524                     if (!isInitial)
525                     {
526                         panGestureDetector.RemoveDirection(PanGestureDetector.DirectionHorizontal);
527                     }
528                     panGestureDetector.AddDirection(PanGestureDetector.DirectionVertical);
529                 }
530             }
531         }
532
533         private int BgTrackLength()
534         {
535             int bgTrackLength = 0;
536             IndicatorType type = CurrentIndicatorType();
537
538             if (type == IndicatorType.None)
539             {
540                 if (direction == DirectionType.Horizontal)
541                 {
542                     bgTrackLength = this.Size2D.Width - thumbImage.Size2D.Width;
543                 }
544                 else if (direction == DirectionType.Vertical)
545                 {
546                     bgTrackLength = this.Size2D.Height - thumbImage.Size2D.Height;
547                 }
548             }
549             else if (type == IndicatorType.Image)
550             {// <lowIndicatorImage> <spaceBetweenTrackAndIndicator> <bgTrack> <spaceBetweenTrackAndIndicator> <highIndicatorImage>
551                 Size lowIndicatorImageSize = LowIndicatorImageSize();
552                 Size highIndicatorImageSize = HighIndicatorImageSize();
553                 int curSpace = (int)CurrentSpaceBetweenTrackAndIndicator();
554                 if (direction == DirectionType.Horizontal)
555                 {
556                     int lowIndicatorSpace = ((lowIndicatorImageSize.Width == 0) ? (0) : ((int)(curSpace + lowIndicatorImageSize.Width)));
557                     int highIndicatorSpace = ((highIndicatorImageSize.Width == 0) ? (0) : ((int)(curSpace + highIndicatorImageSize.Width)));
558                     bgTrackLength = this.Size2D.Width - lowIndicatorSpace - highIndicatorSpace;
559                 }
560                 else if (direction == DirectionType.Vertical)
561                 {
562                     int lowIndicatorSpace = ((lowIndicatorImageSize.Height == 0) ? (0) : ((int)(curSpace + lowIndicatorImageSize.Height)));
563                     int highIndicatorSpace = ((highIndicatorImageSize.Height == 0) ? (0) : ((int)(curSpace + highIndicatorImageSize.Height)));
564                     bgTrackLength = this.Size2D.Height - lowIndicatorSpace - highIndicatorSpace;
565                 }
566             }
567             else if (type == IndicatorType.Text)
568             {// <lowIndicatorText> <spaceBetweenTrackAndIndicator> <bgTrack> <spaceBetweenTrackAndIndicator> <highIndicatorText>
569                 Size lowIndicatorTextSize = LowIndicatorTextSize();
570                 Size highIndicatorTextSize = HighIndicatorTextSize();
571                 int curSpace = (int)CurrentSpaceBetweenTrackAndIndicator();
572                 if (direction == DirectionType.Horizontal)
573                 {
574                     int lowIndicatorSpace = ((lowIndicatorTextSize.Width == 0) ? (0) : ((int)(curSpace + lowIndicatorTextSize.Width)));
575                     int highIndicatorSpace = ((highIndicatorTextSize.Width == 0) ? (0) : ((int)(curSpace + highIndicatorTextSize.Width)));
576                     bgTrackLength = this.Size2D.Width - lowIndicatorSpace - highIndicatorSpace;
577                 }
578                 else if (direction == DirectionType.Vertical)
579                 {
580                     int lowIndicatorSpace = ((lowIndicatorTextSize.Height == 0) ? (0) : ((int)(curSpace + lowIndicatorTextSize.Height)));
581                     int highIndicatorSpace = ((highIndicatorTextSize.Height == 0) ? (0) : ((int)(curSpace + highIndicatorTextSize.Height)));
582                     bgTrackLength = this.Size2D.Height - lowIndicatorSpace - highIndicatorSpace;
583                 }
584             }
585             return bgTrackLength;
586         }
587
588         private void UpdateLowIndicatorSize()
589         {
590             if (lowIndicatorSize != null)
591             {
592                 if (lowIndicatorImage != null)
593                 {
594                     lowIndicatorImage.Size = lowIndicatorSize;
595                 }
596                 if (lowIndicatorText != null)
597                 {
598                     lowIndicatorText.Size = lowIndicatorSize;
599                 }
600             }
601             else
602             {
603                 if (lowIndicatorImage != null && lowIndicatorImage != null && lowIndicatorImage.Size != null)
604                 {
605                     lowIndicatorImage.Size = lowIndicatorSize ?? (ViewStyle as SliderStyle)?.LowIndicatorImage.Size;
606                 }
607                 if (lowIndicatorText != null && lowIndicatorText != null && lowIndicatorText.Size != null)
608                 {
609                     lowIndicatorText.Size = lowIndicatorSize ?? (ViewStyle as SliderStyle)?.LowIndicator.Size;
610                 }
611             }
612         }
613
614         private void UpdateBgTrackSize()
615         {
616             if (bgTrackImage == null)
617             {
618                 return;
619             }
620             int curTrackThickness = (int)CurrentTrackThickness();
621             int bgTrackLength = BgTrackLength();
622             if (direction == DirectionType.Horizontal)
623             {
624                 bgTrackImage.Size2D = new Size2D(bgTrackLength, curTrackThickness);
625             }
626             else if (direction == DirectionType.Vertical)
627             {
628                 bgTrackImage.Size2D = new Size2D(curTrackThickness, bgTrackLength);
629             }
630         }
631
632         private void UpdateBgTrackPosition()
633         {
634             if (bgTrackImage == null)
635             {
636                 return;
637             }
638             IndicatorType type = CurrentIndicatorType();
639
640             if (type == IndicatorType.None)
641             {
642                 bgTrackImage.Position2D = new Position2D(0, 0);
643             }
644             else if (type == IndicatorType.Image)
645             {
646                 Size lowIndicatorImageSize = LowIndicatorImageSize();
647                 Size highIndicatorImageSize = HighIndicatorImageSize();
648                 int curSpace = (int)CurrentSpaceBetweenTrackAndIndicator();
649                 if (direction == DirectionType.Horizontal)
650                 {
651                     int lowIndicatorSpace = ((lowIndicatorImageSize.Width == 0) ? (0) : ((int)(curSpace + lowIndicatorImageSize.Width)));
652                     int highIndicatorSpace = ((highIndicatorImageSize.Width == 0) ? (0) : ((int)(curSpace + highIndicatorImageSize.Width)));
653                     bgTrackImage.Position2D = new Position2D(lowIndicatorSpace - (lowIndicatorSpace + highIndicatorSpace) / 2, 0);
654                 }
655                 else if (direction == DirectionType.Vertical)
656                 {
657                     int lowIndicatorSpace = ((lowIndicatorImageSize.Height == 0) ? (0) : ((int)(curSpace + lowIndicatorImageSize.Height)));
658                     int highIndicatorSpace = ((highIndicatorImageSize.Height == 0) ? (0) : ((int)(curSpace + highIndicatorImageSize.Height)));
659                     bgTrackImage.Position2D = new Position2D(0, lowIndicatorSpace - (lowIndicatorSpace + highIndicatorSpace) / 2);
660                 }
661             }
662             else if (type == IndicatorType.Text)
663             {
664                 Size lowIndicatorTextSize = LowIndicatorTextSize();
665                 Size highIndicatorTextSize = HighIndicatorTextSize();
666                 int curSpace = (int)CurrentSpaceBetweenTrackAndIndicator();
667                 if (direction == DirectionType.Horizontal)
668                 {
669                     int lowIndicatorSpace = ((lowIndicatorTextSize.Width == 0) ? (0) : ((int)(curSpace + lowIndicatorTextSize.Width)));
670                     int highIndicatorSpace = ((highIndicatorTextSize.Width == 0) ? (0) : ((int)(curSpace + highIndicatorTextSize.Width)));
671                     bgTrackImage.Position2D = new Position2D(lowIndicatorSpace - (lowIndicatorSpace + highIndicatorSpace) / 2, 0);
672                 }
673                 else if (direction == DirectionType.Vertical)
674                 {
675                     int lowIndicatorSpace = ((lowIndicatorTextSize.Height == 0) ? (0) : ((int)(curSpace + lowIndicatorTextSize.Height)));
676                     int highIndicatorSpace = ((highIndicatorTextSize.Height == 0) ? (0) : ((int)(curSpace + highIndicatorTextSize.Height)));
677                     bgTrackImage.Position2D = new Position2D(0, -(lowIndicatorSpace - (lowIndicatorSpace + highIndicatorSpace) / 2));
678                 }
679             }
680         }
681
682         private void UpdateValue()
683         {
684             if (slidedTrackImage == null)
685             {
686                 return;
687             }
688             if (curValue < minValue || curValue > maxValue || minValue >= maxValue)
689             {
690                 return;
691             }
692
693             float ratio = 0;
694             ratio = (float)(curValue - minValue) / (float)(maxValue - minValue);
695
696             uint curTrackThickness = CurrentTrackThickness();
697
698             if (direction == DirectionType.Horizontal)
699             {
700                 if (LayoutDirection == ViewLayoutDirectionType.RTL)
701                 {
702                     ratio = 1.0f - ratio;
703                 }
704                 float slidedTrackLength = (float)BgTrackLength() * ratio;
705                 slidedTrackImage.Size2D = new Size2D((int)(slidedTrackLength + round), (int)curTrackThickness); //Add const round to reach Math.Round function.
706                 thumbImage.Position = new Position(slidedTrackImage.Size2D.Width, 0);
707                 thumbImage.RaiseToTop();
708
709                 if (isValueShown)
710                 {
711                     valueIndicatorImage.Position = new Position(slidedTrackImage.Size2D.Width, 0);
712                 }
713             }
714             else if (direction == DirectionType.Vertical)
715             {
716                 float slidedTrackLength = (float)BgTrackLength() * ratio;
717                 slidedTrackImage.Size2D = new Size2D((int)curTrackThickness, (int)(slidedTrackLength + round)); //Add const round to reach Math.Round function.
718                 thumbImage.Position = new Position(0, -slidedTrackImage.Size2D.Height);
719                 thumbImage.RaiseToTop();
720
721                 if (isValueShown)
722                 {
723                     valueIndicatorImage.Position = new Position(0, -(slidedTrackImage.Size2D.Height + thumbImage.Size.Height / 2));
724                 }
725             }
726
727             // Update the track and thumb when the value is over warning value.
728             if (curValue >= warningStartValue)
729             {
730                 if (direction == DirectionType.Horizontal)
731                 {
732                     warningSlidedTrackImage.Size2D = new Size2D((int)(((curValue - warningStartValue) / 100) * this.Size2D.Width), (int)curTrackThickness);
733                 }
734                 else if (direction == DirectionType.Vertical)
735                 {
736                     warningSlidedTrackImage.Size2D = new Size2D((int)curTrackThickness, (int)(((curValue - warningStartValue) / 100) * this.Size2D.Height));
737                 }
738
739                 if (warningThumbColor != null && thumbImage.Color.NotEqualTo(warningThumbColor))
740                 {
741                     thumbImage.Color = warningThumbColor;
742                 }
743                 if (warningThumbImageUrl != null && !thumbImage.ResourceUrl.Equals(warningThumbImageUrl))
744                 {
745                     thumbImage.ResourceUrl = warningThumbImageUrl;
746                 }
747             }
748             else
749             {
750                 warningSlidedTrackImage.Size2D = new Size2D(0, 0);
751                 if (warningThumbColor != null && thumbImage.Color.EqualTo(warningThumbColor))
752                 {
753                     thumbImage.Color = thumbColor;
754                 }
755                 if (warningThumbImageUrl != null && thumbImage.ResourceUrl.Equals(warningThumbImageUrl))
756                 {
757                     thumbImage.ResourceUrl = thumbImageUrl;
758                 }
759             }
760         }
761
762         private uint CurrentTrackThickness()
763         {
764             uint curTrackThickness = 0;
765             if (trackThickness != null)
766             {
767                 curTrackThickness = trackThickness.Value;
768             }
769             else
770             {
771                 if (ViewStyle is SliderStyle sliderStyle && sliderStyle.TrackThickness != null)
772                 {
773                     curTrackThickness = sliderStyle.TrackThickness.Value;
774                 }
775             }
776             return curTrackThickness;
777         }
778
779         private uint CurrentSpaceBetweenTrackAndIndicator()
780         {
781             uint curSpace = 0;
782             if (spaceBetweenTrackAndIndicator != null)
783             {
784                 curSpace = spaceBetweenTrackAndIndicator.Start;
785             }
786             else
787             {
788                 if (ViewStyle is SliderStyle sliderStyle && sliderStyle.TrackPadding != null)
789                 {
790                     curSpace = sliderStyle.TrackPadding.Start;
791                 }
792             }
793             return curSpace;
794         }
795
796         private void UpdateWarningTrackSize()
797         {
798             if (warningTrackImage == null)
799             {
800                 return;
801             }
802
803             int curTrackThickness = (int)CurrentTrackThickness();
804             float warningTrackLength = maxValue - warningStartValue;
805             if (direction == DirectionType.Horizontal)
806             {
807                 warningTrackLength = (warningTrackLength / 100) * this.Size2D.Width;
808                 warningTrackImage.Size2D = new Size2D((int)warningTrackLength, curTrackThickness);
809             }
810             else if (direction == DirectionType.Vertical)
811             {
812                 warningTrackLength = (warningTrackLength / 100) * this.Size2D.Height;
813                 warningTrackImage.Size2D = new Size2D(curTrackThickness, (int)warningTrackLength);
814             }
815         }
816
817         private IndicatorType CurrentIndicatorType()
818         {
819             IndicatorType type = IndicatorType.None;
820             if (ViewStyle is SliderStyle sliderStyle)
821             {
822                 type = (IndicatorType)sliderStyle.IndicatorType;
823             }
824             return type;
825         }
826
827         private Size LowIndicatorImageSize()
828         {
829             Size size = new Size(0, 0);
830             if (lowIndicatorSize != null)
831             {
832                 size = lowIndicatorSize;
833             }
834             else
835             {
836                 if (ViewStyle is SliderStyle sliderStyle && sliderStyle.LowIndicatorImage != null && sliderStyle.LowIndicatorImage.Size != null)
837                 {
838                     size = sliderStyle.LowIndicatorImage.Size;
839                 }
840             }
841             return size;
842         }
843
844         private Size HighIndicatorImageSize()
845         {
846             Size size = new Size(0, 0);
847             if (highIndicatorSize != null)
848             {
849                 size = highIndicatorSize;
850             }
851             else
852             {
853                 if (ViewStyle is SliderStyle sliderStyle && sliderStyle.HighIndicatorImage != null && sliderStyle.HighIndicatorImage.Size != null)
854                 {
855                     size = sliderStyle.HighIndicatorImage.Size;
856                 }
857             }
858             return size;
859         }
860
861         private Size LowIndicatorTextSize()
862         {
863             Size size = new Size(0, 0);
864             if (lowIndicatorSize != null)
865             {
866                 size = lowIndicatorSize;
867             }
868             else
869             {
870                 if (ViewStyle is SliderStyle sliderStyle && sliderStyle.LowIndicator != null && sliderStyle.LowIndicator.Size != null)
871                 {
872                     size = sliderStyle.LowIndicator.Size;
873                 }
874             }
875             return size;
876         }
877
878         private Size HighIndicatorTextSize()
879         {
880             Size size = new Size(0, 0);
881             if (highIndicatorSize != null)
882             {
883                 size = highIndicatorSize;
884             }
885             else
886             {
887                 if (ViewStyle is SliderStyle sliderStyle && sliderStyle.HighIndicator != null && sliderStyle.HighIndicator.Size != null)
888                 {
889                     size = sliderStyle.HighIndicator.Size;
890                 }
891             }
892             return size;
893         }
894     }
895 }