[NUI] Fix Switch selection bug. (#1798)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Extension / SlidingSwitchExtension.cs
1 /*
2  * Copyright(c) 2020 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 using System;
18 using System.ComponentModel;
19 using Tizen.NUI.BaseComponents;
20
21 namespace Tizen.NUI.Components.Extension
22 {
23     /// <summary>
24     /// The SlidingSwitchExtension class makes attached Switch to animate thumb when selected/unselected.
25     /// <remark>
26     /// This extension must be used within a class implementing SwitchStyle.
27     /// </remark>
28     /// </summary>
29     [EditorBrowsable(EditorBrowsableState.Never)]
30     internal class SlidingSwitchExtension : SwitchExtension
31     {
32         private Animation slidingAnimation;
33
34         public SlidingSwitchExtension() : base()
35         {
36             slidingAnimation = new Animation(100);
37         }
38
39         /// <inheritdoc/>
40         public override void OnSelectedChanged(Switch switchButton)
41         {
42             var track = switchButton.Track;
43             var thumb = switchButton.Thumb;
44
45             if (track == null || thumb == null || null == slidingAnimation)
46             {
47                 return;
48             }
49
50             if (slidingAnimation.State == Animation.States.Playing)
51             {
52                 slidingAnimation.Stop();
53             }
54
55             slidingAnimation.Clear();
56             slidingAnimation.AnimateTo(thumb, "PositionX", track.Size.Width - thumb.Size.Width - thumb.Position.X);
57             slidingAnimation.Play();
58         }
59
60         public override void OnDispose(Button button)
61         {
62             if (slidingAnimation != null)
63             {
64                 if (slidingAnimation.State == Animation.States.Playing)
65                 {
66                     slidingAnimation.Stop();
67                 }
68                 slidingAnimation.Dispose();
69                 slidingAnimation = null;
70             }
71         }
72     }
73 }