Fix Loading sizeproperty not work (#1797)
[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         public override void OnClick(Button button, Button.ClickEventArgs eventArgs)
40         {
41             if (button as Switch == null)
42             {
43                 throw new Exception("SlidingSwitchExtension must be used within a SwitchStyle or derived class.");
44             }
45
46             var switchButton = (Switch)button;
47             var track = switchButton.GetCurrentTrack(this);
48             var thumb = switchButton.GetCurrentThumb(this);
49
50             if (track == null || thumb == null || null == slidingAnimation)
51             {
52                 return;
53             }
54
55             if (slidingAnimation.State == Animation.States.Playing)
56             {
57                 slidingAnimation.Stop();
58             }
59
60             slidingAnimation.Clear();
61             slidingAnimation.AnimateTo(thumb, "PositionX", track.Size.Width - thumb.Size.Width - thumb.Position.X);
62             slidingAnimation.Play();
63         }
64
65         public override void OnDispose(Button button)
66         {
67             if (slidingAnimation != null)
68             {
69                 if (slidingAnimation.State == Animation.States.Playing)
70                 {
71                     slidingAnimation.Stop();
72                 }
73                 slidingAnimation.Dispose();
74                 slidingAnimation = null;
75             }
76         }
77     }
78 }