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, IDisposable
32 private bool disposed = false;
33 private Animation slidingAnimation;
35 public SlidingSwitchExtension() : base()
37 slidingAnimation = new Animation(100);
41 public override void OnSelectedChanged(Switch switchButton)
43 var track = switchButton.Track;
44 var thumb = switchButton.Thumb;
46 if (track == null || thumb == null || null == slidingAnimation)
51 if (slidingAnimation.State == Animation.States.Playing)
53 slidingAnimation.EndAction = Animation.EndActions.StopFinal;
54 slidingAnimation.Stop();
57 float destinationPosX = switchButton.IsSelected ? track.Size.Width - thumb.Size.Width : 0;
59 if (switchButton.IsOnWindow)
61 slidingAnimation.Clear();
62 slidingAnimation.AnimateTo(thumb, "PositionX", destinationPosX);
63 slidingAnimation.EndAction = Animation.EndActions.StopFinal;
64 slidingAnimation.Play();
68 thumb.PositionX = destinationPosX;
72 public override void OnDispose(Button button)
74 if (slidingAnimation != null)
76 if (slidingAnimation.State == Animation.States.Playing)
78 slidingAnimation.Stop();
80 slidingAnimation.Dispose();
81 slidingAnimation = null;
86 [EditorBrowsable(EditorBrowsableState.Never)]
87 public override ImageView OnCreateTrack(Switch switchButton, ImageView track)
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;
98 public override ImageView OnCreateThumb(Switch switchButton, ImageView thumb)
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;
108 [EditorBrowsable(EditorBrowsableState.Never)]
109 protected virtual void Dispose(bool disposing)
118 slidingAnimation?.Dispose();
123 [EditorBrowsable(EditorBrowsableState.Never)]
124 public void Dispose()
127 global::System.GC.SuppressFinalize(this);