2 * Copyright(c) 2020 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 System.ComponentModel;
19 using Tizen.NUI.BaseComponents;
21 namespace Tizen.NUI.Components.Extension
24 /// The SlidingSwitchExtension class makes attached Switch to animate thumb when selected/unselected.
26 /// This extension must be used within a class implementing SwitchStyle.
29 [EditorBrowsable(EditorBrowsableState.Never)]
30 internal class SlidingSwitchExtension : SwitchExtension
32 private Animation slidingAnimation;
34 public SlidingSwitchExtension() : base()
36 slidingAnimation = new Animation(100);
39 public override void OnClick(Button button, Button.ClickEventArgs eventArgs)
41 if (button as Switch == null)
43 throw new Exception("SlidingSwitchExtension must be used within a SwitchStyle or derived class.");
46 var switchButton = (Switch)button;
47 var track = switchButton.GetCurrentTrack(this);
48 var thumb = switchButton.GetCurrentThumb(this);
50 if (track == null || thumb == null || null == slidingAnimation)
55 if (slidingAnimation.State == Animation.States.Playing)
57 slidingAnimation.Stop();
60 slidingAnimation.Clear();
61 slidingAnimation.AnimateTo(thumb, "PositionX", track.Size.Width - thumb.Size.Width - thumb.Position.X);
62 slidingAnimation.Play();
65 public override void OnDispose(Button button)
67 if (slidingAnimation != null)
69 if (slidingAnimation.State == Animation.States.Playing)
71 slidingAnimation.Stop();
73 slidingAnimation.Dispose();
74 slidingAnimation = null;