[NUI] Implement switch's Old background API (#1289)
[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         static Switch() { }
35
36         /// <summary>
37         /// Creates a new instance of a Switch.
38         /// </summary>
39         /// <since_tizen> 6 </since_tizen>
40         public Switch() : base()
41         {
42             Initialize();
43         }
44
45         /// <summary>
46         /// Creates a new instance of a Switch with style.
47         /// </summary>
48         /// <param name="style">Create Switch by special style defined in UX.</param>
49         /// <since_tizen> 6 </since_tizen>
50         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
51         [EditorBrowsable(EditorBrowsableState.Never)]
52         public Switch(string style) : base(style)
53         {
54             Initialize();
55         }
56
57         /// <summary>
58         /// Creates a new instance of a Switch with style.
59         /// </summary>
60         /// <param name="style">Create Switch by style customized by user.</param>
61         /// <since_tizen> 6 </since_tizen>
62         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
63         [EditorBrowsable(EditorBrowsableState.Never)]
64         public Switch(SwitchStyle style) : base(style)
65         {
66             Initialize();
67         }
68
69         /// <summary>
70         /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
71         /// </summary>
72         /// <since_tizen> 6 </since_tizen>
73         public event EventHandler<SelectEventArgs> SelectedEvent;
74
75         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
76         [EditorBrowsable(EditorBrowsableState.Never)]
77         public new SwitchStyle Style => ViewStyle as SwitchStyle;
78
79         /// <summary>
80         /// Background image's resource url in Switch.
81         /// </summary>
82         /// <since_tizen> 6 </since_tizen>
83         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
84         [EditorBrowsable(EditorBrowsableState.Never)]
85         public string SwitchBackgroundImageURL
86         {
87             get
88             {
89                 return Style?.Track?.ResourceUrl?.All;
90             }
91             set
92             {
93                 if (null != value && null != Style?.Track)
94                 {
95                     Style.Track.ResourceUrl = value;
96                 }
97             }
98         }
99
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                 StringSelector strSl = new StringSelector();
109                 strSl.Clone(Style?.Track?.ResourceUrl);
110                 return strSl;
111             }
112             set
113             {
114                 if (null != value && null != Style?.Track)
115                 {
116                     Style.Track.ResourceUrl = value;
117                 }
118             }
119         }
120
121         /// <summary>
122         /// Handler image's resource url in Switch.
123         /// </summary>
124         /// <since_tizen> 6 </since_tizen>
125         public string SwitchHandlerImageURL
126         {
127             get
128             {
129                 return Style?.Thumb?.ResourceUrl?.All;
130             }
131             set
132             {
133                 if (null != value && null != Style?.Thumb)
134                 {
135                     Style.Thumb.ResourceUrl = value;
136                 }
137             }
138         }
139
140         /// <summary>
141         /// Handler image's resource url selector in Switch.
142         /// </summary>
143         /// <since_tizen> 6 </since_tizen>
144         public StringSelector SwitchHandlerImageURLSelector
145         {
146             get
147             {
148                 StringSelector strSl = new StringSelector();
149                 strSl.Clone(Style?.Thumb?.ResourceUrl);
150                 return strSl;
151             }
152             set
153             {
154                 if (null != value && null != Style?.Thumb)
155                 {
156                     Style.Thumb.ResourceUrl = value;
157                 }
158             }
159         }
160
161         /// <summary>
162         /// Handler image's size in Switch.
163         /// </summary>
164         /// <since_tizen> 6 </since_tizen>
165         public Size SwitchHandlerImageSize
166         {
167             get
168             {
169                 return Style?.Thumb?.Size;
170             }
171             set
172             {
173                 if (null != Style?.Thumb)
174                 {
175                     Style.Thumb.Size = value;
176                 }
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 || null == key) 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 || null == touch) 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.GetViewStyle(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 }