[NUI]Refactor Components (#1152)
[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 (value != null)
93                 {
94                     if (Style.Track.ResourceUrl == null)
95                     {
96                         Style.Track.ResourceUrl = new StringSelector();
97                     }
98                     Style.Track.ResourceUrl.All = value;
99                 }
100             }
101         }
102
103         /// <summary>
104         /// Background image's resource url selector in Switch.
105         /// </summary>
106         /// <since_tizen> 6 </since_tizen>
107         public StringSelector SwitchBackgroundImageURLSelector
108         {
109             get
110             {
111                 return (StringSelector)Style.Track?.ResourceUrl;
112             }
113             set
114             {
115                 if (value != null)
116                 {
117                     Style.Track.ResourceUrl = value.Clone() as StringSelector;
118                 }
119             }
120         }
121
122         /// <summary>
123         /// Handler image's resource url in Switch.
124         /// </summary>
125         /// <since_tizen> 6 </since_tizen>
126         public string SwitchHandlerImageURL
127         {
128             get
129             {
130                 return Style.Thumb?.ResourceUrl?.All;
131             }
132             set
133             {
134                 if (value != null)
135                 {
136                     if (Style.Thumb.ResourceUrl == null)
137                     {
138                         Style.Thumb.ResourceUrl = new StringSelector();
139                     }
140                     Style.Thumb.ResourceUrl.All = value;
141                 }
142             }
143         }
144
145         /// <summary>
146         /// Handler image's resource url selector in Switch.
147         /// </summary>
148         /// <since_tizen> 6 </since_tizen>
149         public StringSelector SwitchHandlerImageURLSelector
150         {
151             get
152             {
153                 return (StringSelector)Style.Thumb?.ResourceUrl;
154             }
155             set
156             {
157                 if (value != null)
158                 {
159                     Style.Thumb.ResourceUrl = value.Clone() as StringSelector;
160                 }
161             }
162         }
163
164         /// <summary>
165         /// Handler image's size in Switch.
166         /// </summary>
167         /// <since_tizen> 6 </since_tizen>
168         public Size SwitchHandlerImageSize
169         {
170             get
171             {
172                 return Style.Thumb?.Size ?? new Size(0, 0);
173             }
174             set
175             {
176                 Style.Thumb.Size = value;
177             }
178         }
179
180         /// <summary>
181         /// Dispose Switch and all children on it.
182         /// </summary>
183         /// <param name="type">Dispose type.</param>
184         /// <since_tizen> 6 </since_tizen>
185         protected override void Dispose(DisposeTypes type)
186         {
187             if (disposed) return;
188
189             if (type == DisposeTypes.Explicit)
190             {
191                 if (null != handlerAni)
192                 {
193                     if (handlerAni.State == Animation.States.Playing)
194                     {
195                         handlerAni.Stop();
196                     }
197                     handlerAni.Dispose();
198                     handlerAni = null;
199                 }
200
201                 Utility.Dispose(thumbImage);
202                 Utility.Dispose(trackImage);
203             }
204
205             base.Dispose(type);
206         }
207
208         /// <summary>
209         /// Called after a key event is received by the view that has had its focus set.
210         /// </summary>
211         /// <param name="key">The key event.</param>
212         /// <returns>True if the key event should be consumed.</returns>
213         /// <since_tizen> 6 </since_tizen>
214         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
215         [EditorBrowsable(EditorBrowsableState.Never)]
216         public override bool OnKey(Key key)
217         {
218             if (!IsEnabled) return false;
219
220             bool ret = base.OnKey(key);
221             if (key.State == Key.StateType.Up)
222             {
223                 if (key.KeyPressedName == "Return")
224                 {
225                     OnSelect();
226                 }
227             }
228
229             return ret;
230         }
231
232         /// <summary>
233         /// Called after a touch event is received by the owning view.<br />
234         /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
235         /// </summary>
236         /// <param name="touch">The touch event.</param>
237         /// <returns>True if the event should be consumed.</returns>
238         /// <since_tizen> 6 </since_tizen>
239         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
240         [EditorBrowsable(EditorBrowsableState.Never)]
241         public override bool OnTouch(Touch touch)
242         {
243             if(!IsEnabled) return false;
244
245             PointStateType state = touch.GetState(0);
246             bool ret = base.OnTouch(touch);
247             switch (state)
248             {
249                 case PointStateType.Up:
250                     OnSelect();
251                     break;
252                 default:
253                     break;
254             }
255             return ret;
256         }
257
258         /// <summary>
259         /// Get Switch attribues.
260         /// </summary>
261         /// <since_tizen> 6 </since_tizen>
262         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
263         [EditorBrowsable(EditorBrowsableState.Never)]
264         protected override ViewStyle GetViewStyle()
265         {
266             return new SwitchStyle();
267         }
268
269         private void Initialize()
270         {
271             Style.IsSelectable = true;
272             handlerAni = new Animation(aniTime);
273             trackImage = new ImageView()
274             {
275                 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
276                 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
277                 PositionUsesPivotPoint = true,
278                 WidthResizePolicy = ResizePolicyType.FillToParent,
279                 HeightResizePolicy = ResizePolicyType.FillToParent,
280                 Name = "SwitchBackgroundImage",
281             };
282             Add(trackImage);
283             trackImage.ApplyStyle(Style.Track);
284
285             thumbImage = new ImageView()
286             {
287                 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
288                 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
289                 PositionUsesPivotPoint = true,
290                 Name = "SwitchHandlerImage",
291             };
292             trackImage.Add(thumbImage);
293             thumbImage.ApplyStyle(Style.Thumb);
294         }
295
296         /// <summary>
297         /// Theme change callback when theme is changed, this callback will be trigger.
298         /// </summary>
299         /// <since_tizen> 6 </since_tizen>
300         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
301         [EditorBrowsable(EditorBrowsableState.Never)]
302         protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
303         {
304             SwitchStyle tempAttributes = StyleManager.Instance.GetAttributes(style) as SwitchStyle;
305             if (null != tempAttributes)
306             {
307                 Style.CopyFrom(tempAttributes);
308             }
309         }
310
311         private void OnSelect()
312         {
313             if (handlerAni.State == Animation.States.Playing)
314             {
315                 handlerAni.Stop();
316             }
317             handlerAni.Clear();
318             handlerAni.AnimateTo(thumbImage, "PositionX", Size2D.Width - thumbImage.Size2D.Width - thumbImage.Position2D.X);
319             trackImage.Opacity = 0.5f; ///////need defined by UX
320             handlerAni.AnimateTo(trackImage, "Opacity", 1);
321             handlerAni.Play();
322
323             if (SelectedEvent != null)
324             {
325                 SelectEventArgs eventArgs = new SelectEventArgs();
326                 eventArgs.IsSelected = IsSelected;
327                 SelectedEvent(this, eventArgs);
328             }
329         }
330
331         /// <summary>
332         /// SelectEventArgs is a class to record item selected arguments which will sent to user.
333         /// </summary>
334         /// <since_tizen> 6 </since_tizen>
335         public class SelectEventArgs : EventArgs
336         {
337             /// <summary> Select state of Switch </summary>
338             /// <since_tizen> 6 </since_tizen>
339             public bool IsSelected;
340         }
341     }
342 }