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