[NUI] Fixed TCT failed issues (#1176)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Switch.cs
1 /*
2  * Copyright(c) 2019 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 Tizen.NUI.BaseComponents;
19 using System.ComponentModel;
20
21 namespace Tizen.NUI.Components
22 {
23     /// <summary>
24     /// Switch is one kind of common component, it can be used as selector.
25     /// User can handle Navigation by adding/inserting/deleting NavigationItem.
26     /// </summary>
27     /// <since_tizen> 6 </since_tizen>
28     public class Switch : Button
29     {
30         private const int aniTime = 100; // will be defined in const file later
31         private ImageView trackImage;
32         private ImageView thumbImage;
33         private Animation handlerAni = null;
34
35         /// <summary>
36         /// Creates a new instance of a Switch.
37         /// </summary>
38         /// <since_tizen> 6 </since_tizen>
39         public Switch() : base()
40         {
41             Initialize();
42         }
43
44         /// <summary>
45         /// Creates a new instance of a Switch with style.
46         /// </summary>
47         /// <param name="style">Create Switch by special style defined in UX.</param>
48         /// <since_tizen> 6 </since_tizen>
49         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
50         [EditorBrowsable(EditorBrowsableState.Never)]
51         public Switch(string style) : base(style)
52         {
53             Initialize();
54         }
55
56         /// <summary>
57         /// Creates a new instance of a Switch with style.
58         /// </summary>
59         /// <param name="style">Create Switch by style customized by user.</param>
60         /// <since_tizen> 6 </since_tizen>
61         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
62         [EditorBrowsable(EditorBrowsableState.Never)]
63         public Switch(SwitchStyle style) : base(style)
64         {
65             Initialize();
66         }
67
68         /// <summary>
69         /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
70         /// </summary>
71         /// <since_tizen> 6 </since_tizen>
72         public event EventHandler<SelectEventArgs> SelectedEvent;
73
74         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
75         [EditorBrowsable(EditorBrowsableState.Never)]
76         public new SwitchStyle Style => ViewStyle as SwitchStyle;
77
78         /// <summary>
79         /// Background image's resource url in Switch.
80         /// </summary>
81         /// <since_tizen> 6 </since_tizen>
82         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
83         [EditorBrowsable(EditorBrowsableState.Never)]
84         public string SwitchBackgroundImageURL
85         {
86             get
87             {
88                 return Style?.Track?.ResourceUrl?.All;
89             }
90             set
91             {
92                 if (null != value && null != Style?.Track)
93                 {
94                     Style.Track.ResourceUrl = value;
95                 }
96             }
97         }
98
99         private StringSelector switchBackgroundImageURLSelector = new StringSelector();
100         /// <summary>
101         /// Background image's resource url selector in Switch.
102         /// </summary>
103         /// <since_tizen> 6 </since_tizen>
104         public StringSelector SwitchBackgroundImageURLSelector
105         {
106             get
107             {
108                 return switchBackgroundImageURLSelector;
109             }
110             set
111             {
112                 switchBackgroundImageURLSelector.Clone(value);
113             }
114         }
115
116         /// <summary>
117         /// Handler image's resource url in Switch.
118         /// </summary>
119         /// <since_tizen> 6 </since_tizen>
120         public string SwitchHandlerImageURL
121         {
122             get
123             {
124                 return Style?.Thumb?.ResourceUrl?.All;
125             }
126             set
127             {
128                 if (null != value && null != Style?.Thumb)
129                 {
130                     Style.Thumb.ResourceUrl = value;
131                 }
132             }
133         }
134
135         private StringSelector switchHandlerImageURLSelector = new StringSelector();
136         /// <summary>
137         /// Handler image's resource url selector in Switch.
138         /// </summary>
139         /// <since_tizen> 6 </since_tizen>
140         public StringSelector SwitchHandlerImageURLSelector
141         {
142             get
143             {
144                 return switchHandlerImageURLSelector;
145             }
146             set
147             {
148                 switchHandlerImageURLSelector.Clone(value);
149             }
150         }
151
152         /// <summary>
153         /// Handler image's size in Switch.
154         /// </summary>
155         /// <since_tizen> 6 </since_tizen>
156         public Size SwitchHandlerImageSize
157         {
158             get
159             {
160                 return Style?.Thumb?.Size;
161             }
162             set
163             {
164                 if (null != Style?.Thumb)
165                 {
166                     Style.Thumb.Size = value;
167                 }
168             }
169         }
170
171         /// <summary>
172         /// Dispose Switch and all children on it.
173         /// </summary>
174         /// <param name="type">Dispose type.</param>
175         /// <since_tizen> 6 </since_tizen>
176         protected override void Dispose(DisposeTypes type)
177         {
178             if (disposed) return;
179
180             if (type == DisposeTypes.Explicit)
181             {
182                 if (null != handlerAni)
183                 {
184                     if (handlerAni.State == Animation.States.Playing)
185                     {
186                         handlerAni.Stop();
187                     }
188                     handlerAni.Dispose();
189                     handlerAni = null;
190                 }
191
192                 Utility.Dispose(thumbImage);
193                 Utility.Dispose(trackImage);
194             }
195
196             base.Dispose(type);
197         }
198
199         /// <summary>
200         /// Called after a key event is received by the view that has had its focus set.
201         /// </summary>
202         /// <param name="key">The key event.</param>
203         /// <returns>True if the key event should be consumed.</returns>
204         /// <since_tizen> 6 </since_tizen>
205         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
206         [EditorBrowsable(EditorBrowsableState.Never)]
207         public override bool OnKey(Key key)
208         {
209             if (!IsEnabled) return false;
210
211             bool ret = base.OnKey(key);
212             if (key.State == Key.StateType.Up)
213             {
214                 if (key.KeyPressedName == "Return")
215                 {
216                     OnSelect();
217                 }
218             }
219
220             return ret;
221         }
222
223         /// <summary>
224         /// Called after a touch event is received by the owning view.<br />
225         /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
226         /// </summary>
227         /// <param name="touch">The touch event.</param>
228         /// <returns>True if the event should be consumed.</returns>
229         /// <since_tizen> 6 </since_tizen>
230         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
231         [EditorBrowsable(EditorBrowsableState.Never)]
232         public override bool OnTouch(Touch touch)
233         {
234             if(!IsEnabled) return false;
235
236             PointStateType state = touch.GetState(0);
237             bool ret = base.OnTouch(touch);
238             switch (state)
239             {
240                 case PointStateType.Up:
241                     OnSelect();
242                     break;
243                 default:
244                     break;
245             }
246             return ret;
247         }
248
249         /// <summary>
250         /// Get Switch attribues.
251         /// </summary>
252         /// <since_tizen> 6 </since_tizen>
253         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
254         [EditorBrowsable(EditorBrowsableState.Never)]
255         protected override ViewStyle GetViewStyle()
256         {
257             return new SwitchStyle();
258         }
259
260         private void Initialize()
261         {
262             Style.IsSelectable = true;
263             handlerAni = new Animation(aniTime);
264             trackImage = new ImageView()
265             {
266                 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
267                 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
268                 PositionUsesPivotPoint = true,
269                 WidthResizePolicy = ResizePolicyType.FillToParent,
270                 HeightResizePolicy = ResizePolicyType.FillToParent,
271                 Name = "SwitchBackgroundImage",
272             };
273             Add(trackImage);
274             trackImage.ApplyStyle(Style.Track);
275
276             thumbImage = new ImageView()
277             {
278                 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
279                 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
280                 PositionUsesPivotPoint = true,
281                 Name = "SwitchHandlerImage",
282             };
283             trackImage.Add(thumbImage);
284             thumbImage.ApplyStyle(Style.Thumb);
285         }
286
287         /// <summary>
288         /// Theme change callback when theme is changed, this callback will be trigger.
289         /// </summary>
290         /// <since_tizen> 6 </since_tizen>
291         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
292         [EditorBrowsable(EditorBrowsableState.Never)]
293         protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
294         {
295             SwitchStyle tempAttributes = StyleManager.Instance.GetAttributes(style) as SwitchStyle;
296             if (null != tempAttributes)
297             {
298                 Style.CopyFrom(tempAttributes);
299             }
300         }
301
302         private void OnSelect()
303         {
304             if (handlerAni.State == Animation.States.Playing)
305             {
306                 handlerAni.Stop();
307             }
308             handlerAni.Clear();
309             handlerAni.AnimateTo(thumbImage, "PositionX", Size2D.Width - thumbImage.Size2D.Width - thumbImage.Position2D.X);
310             trackImage.Opacity = 0.5f; ///////need defined by UX
311             handlerAni.AnimateTo(trackImage, "Opacity", 1);
312             handlerAni.Play();
313
314             if (SelectedEvent != null)
315             {
316                 SelectEventArgs eventArgs = new SelectEventArgs();
317                 eventArgs.IsSelected = IsSelected;
318                 SelectedEvent(this, eventArgs);
319             }
320         }
321
322         /// <summary>
323         /// SelectEventArgs is a class to record item selected arguments which will sent to user.
324         /// </summary>
325         /// <since_tizen> 6 </since_tizen>
326         public class SelectEventArgs : EventArgs
327         {
328             /// <summary> Select state of Switch </summary>
329             /// <since_tizen> 6 </since_tizen>
330             public bool IsSelected;
331         }
332     }
333 }