[NUI] Fix switch animation bug
[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, IDisposable
31     {
32         private bool disposed = false;
33         private Animation slidingAnimation;
34
35         public SlidingSwitchExtension() : base()
36         {
37             slidingAnimation = new Animation(100);
38         }
39
40         /// <inheritdoc/>
41         public override void OnSelectedChanged(Switch switchButton)
42         {
43             var track = switchButton.Track;
44             var thumb = switchButton.Thumb;
45
46             if (track == null || thumb == null || null == slidingAnimation)
47             {
48                 return;
49             }
50
51             if (slidingAnimation.State == Animation.States.Playing)
52             {
53                 slidingAnimation.EndAction = Animation.EndActions.StopFinal;
54                 slidingAnimation.Stop();
55             }
56
57             float destinationPosX = switchButton.IsSelected ? track.Size.Width - thumb.Size.Width : 0;
58
59             if (switchButton.IsOnWindow)
60             {
61                 slidingAnimation.Clear();
62                 slidingAnimation.AnimateTo(thumb, "PositionX", destinationPosX);
63                 slidingAnimation.EndAction = Animation.EndActions.StopFinal;
64                 slidingAnimation.Play();
65             }
66             else
67             {
68                 thumb.PositionX = destinationPosX;
69             }
70         }
71
72         public override void OnDispose(Button button)
73         {
74             if (slidingAnimation != null)
75             {
76                 if (slidingAnimation.State == Animation.States.Playing)
77                 {
78                     slidingAnimation.Stop();
79                 }
80                 slidingAnimation.Dispose();
81                 slidingAnimation = null;
82             }
83         }
84
85         /// <inheritdoc/>
86         [EditorBrowsable(EditorBrowsableState.Never)]
87         public override ImageView OnCreateTrack(Switch switchButton, ImageView track)
88         {
89             base.OnCreateTrack(switchButton, track);
90             track.Relayout += (s, e) => {
91                 if (slidingAnimation.State == Animation.States.Playing) return;
92                 switchButton.Thumb.PositionX = switchButton.IsSelected ? switchButton.Track.Size.Width - switchButton.Thumb.Size.Width : 0;
93             };
94             return track;
95         }
96
97         /// <inheritdoc/>
98         public override ImageView OnCreateThumb(Switch switchButton, ImageView thumb)
99         {
100             base.OnCreateThumb(switchButton, thumb);
101             thumb.Relayout += (s, e) => {
102                 if (slidingAnimation.State == Animation.States.Playing) return;
103                 thumb.PositionX = switchButton.IsSelected ? switchButton.Track.Size.Width - thumb.Size.Width : 0;
104             };
105             return thumb;
106         }
107
108         [EditorBrowsable(EditorBrowsableState.Never)]
109         protected virtual void Dispose(bool disposing)
110         {
111             if (disposed)
112             {
113                 return;
114             }
115
116             if (disposing)
117             {
118                 slidingAnimation?.Dispose();
119             }
120             disposed = true;
121         }
122
123         [EditorBrowsable(EditorBrowsableState.Never)]
124         public void Dispose()
125         {
126             Dispose(true);
127             global::System.GC.SuppressFinalize(this);
128         }
129     }
130 }