[NUI] Remove bindings between Style and View (#1788)
[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 System.ComponentModel;
19 using Tizen.NUI.BaseComponents;
20 using Tizen.NUI.Components.Extension;
21
22 namespace Tizen.NUI.Components
23 {
24     /// <summary>
25     /// Switch is one kind of common component, it can be used as selector.
26     /// User can handle Navigation by adding/inserting/deleting NavigationItem.
27     /// </summary>
28     /// <since_tizen> 6 </since_tizen>
29     public class Switch : Button
30     {
31         private ImageView track = null;
32         private ImageView thumb = null;
33
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> 8 </since_tizen>
50         public Switch(string style) : base(style)
51         {
52             Initialize();
53         }
54
55         /// <summary>
56         /// Creates a new instance of a Switch with style.
57         /// </summary>
58         /// <param name="switchStyle">Create Switch by style customized by user.</param>
59         /// <since_tizen> 8 </since_tizen>
60         public Switch(SwitchStyle switchStyle) : base(switchStyle)
61         {
62             Initialize();
63         }
64
65         /// <summary>
66         /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
67         /// </summary>
68         /// <since_tizen> 6 </since_tizen>
69         public event EventHandler<SelectEventArgs> SelectedEvent;
70
71         /// <summary>
72         /// Return a copied Style instance of Switch
73         /// </summary>
74         /// <remarks>
75         /// It returns copied Style instance and changing it does not effect to the Switch.
76         /// Style setting is possible by using constructor or the function of ApplyStyle(ViewStyle viewStyle)
77         /// </remarks>
78         /// <since_tizen> 8 </since_tizen>
79         public new SwitchStyle Style
80         {
81             get
82             {
83                 var result = new SwitchStyle(ViewStyle as SwitchStyle);
84                 result.CopyPropertiesFromView(this);
85                 result.Track.CopyPropertiesFromView(Track);
86                 result.Thumb.CopyPropertiesFromView(Thumb);
87                 return result;
88             }
89         }
90
91         /// <summary>
92         /// Apply style to switch.
93         /// </summary>
94         /// <param name="viewStyle">The style to apply.</param>
95         [EditorBrowsable(EditorBrowsableState.Never)]
96         public override void ApplyStyle(ViewStyle viewStyle)
97         {
98             base.ApplyStyle(viewStyle);
99
100             SwitchStyle swStyle = viewStyle as SwitchStyle;
101
102             if (null != swStyle)
103             {
104                 if (swStyle.Track != null)
105                 {
106                     Track.ApplyStyle(swStyle.Track);
107                 }
108
109                 if (swStyle.Thumb != null)
110                 {
111                     Thumb.ApplyStyle(swStyle.Thumb);
112                 }
113             }
114         }
115
116         /// <summary>
117         /// Switch's track part.
118         /// </summary>
119         /// <since_tizen> 8 </since_tizen>
120         public ImageView Track
121         {
122             get
123             {
124                 if (track == null)
125                 {
126                     track = new ImageView()
127                     {
128                         PositionUsesPivotPoint = true,
129                         ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
130                         PivotPoint = Tizen.NUI.PivotPoint.CenterLeft,
131                         WidthResizePolicy = ResizePolicyType.FillToParent,
132                         HeightResizePolicy = ResizePolicyType.FillToParent
133                     };
134
135                     var extension = (SwitchExtension)Extension;
136                     if (extension != null)
137                     {
138                         track = extension.OnCreateTrack(this, track);
139                     }
140                     Add(track);
141                 }
142                 return track;
143             }
144             internal set
145             {
146                 track = value;
147             }
148         }
149
150         /// <summary>
151         /// Switch's thumb part.
152         /// </summary>
153         /// <since_tizen> 8 </since_tizen>
154         public ImageView Thumb
155         {
156             get
157             {
158                 if (thumb == null)
159                 {
160                     thumb = new ImageView()
161                     {
162                         PositionUsesPivotPoint = true,
163                         ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
164                         PivotPoint = Tizen.NUI.PivotPoint.CenterLeft,
165                         WidthResizePolicy = ResizePolicyType.Fixed,
166                         HeightResizePolicy = ResizePolicyType.Fixed
167                     };
168
169                     var extension = (SwitchExtension)Extension;
170                     if (extension != null)
171                     {
172                         thumb = extension.OnCreateThumb(this, thumb);
173                     }
174                     Add(thumb);
175                 }
176                 return thumb;
177             }
178             internal set
179             {
180                 thumb = value;
181             }
182         }
183
184         /// <summary>
185         /// Background image's resource url selector in Switch.
186         /// </summary>
187         /// <since_tizen> 6 </since_tizen>
188         public StringSelector SwitchBackgroundImageURLSelector
189         {
190             get => track == null ? null : new StringSelector((Selector<string>)thumb.GetValue(ImageView.ResourceUrlSelectorProperty));
191             set => track?.SetValue(ImageView.ResourceUrlSelectorProperty, value);
192         }
193
194         /// <summary>
195         /// Handler image's resource url in Switch.
196         /// </summary>
197         /// <since_tizen> 6 </since_tizen>
198         public string SwitchHandlerImageURL
199         {
200             get
201             {
202                 return Thumb.ResourceUrl;
203             }
204             set
205             {
206                 Thumb.ResourceUrl = value;
207             }
208         }
209
210         /// <summary>
211         /// Handler image's resource url selector in Switch.
212         /// Getter returns copied selector value if exist, null otherwise.
213         /// </summary>
214         /// <since_tizen> 6 </since_tizen>
215         public StringSelector SwitchHandlerImageURLSelector
216         {
217             get => thumb == null ? null : new StringSelector((Selector<string>)thumb.GetValue(ImageView.ResourceUrlSelectorProperty));
218             set => thumb?.SetValue(ImageView.ResourceUrlSelectorProperty, value);
219         }
220
221         /// <summary>
222         /// Handler image's size in Switch.
223         /// </summary>
224         /// <since_tizen> 6 </since_tizen>
225         public Size SwitchHandlerImageSize
226         {
227             get
228             {
229                 return Thumb.Size;
230             }
231             set
232             {
233                 Thumb.Size = value;
234             }
235         }
236
237         /// <summary>
238         /// Dispose Switch and all children on it.
239         /// </summary>
240         /// <param name="type">Dispose type.</param>
241         /// <since_tizen> 6 </since_tizen>
242         protected override void Dispose(DisposeTypes type)
243         {
244             if (disposed) return;
245
246             if (type == DisposeTypes.Explicit)
247             {
248                 Utility.Dispose(Thumb);
249                 Utility.Dispose(Track);
250             }
251
252             base.Dispose(type);
253         }
254
255         /// <summary>
256         /// Called after a key event is received by the view that has had its focus set.
257         /// </summary>
258         /// <param name="key">The key event.</param>
259         /// <returns>True if the key event should be consumed.</returns>
260         /// <since_tizen> 8 </since_tizen>
261         public override bool OnKey(Key key)
262         {
263             return base.OnKey(key);
264         }
265
266         /// <summary>
267         /// Called after a touch event is received by the owning view.<br />
268         /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
269         /// </summary>
270         /// <param name="touch">The touch event.</param>
271         /// <returns>True if the event should be consumed.</returns>
272         /// <since_tizen> 8 </since_tizen>
273         public override bool OnTouch(Touch touch)
274         {
275             return base.OnTouch(touch);
276         }
277
278         /// <summary>
279         /// Get Switch style.
280         /// </summary>
281         /// <returns>The default switch style.</returns>
282         /// <since_tizen> 8 </since_tizen>
283         protected override ViewStyle CreateViewStyle()
284         {
285             return new SwitchStyle();
286         }
287
288         /// <inheritdoc/>
289         [EditorBrowsable(EditorBrowsableState.Never)]
290         protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
291         {
292             base.OnControlStateChanged(controlStateChangedInfo);
293
294             if (!IsSelectable)
295             {
296                 return;
297             }
298
299             bool previousSelected = controlStateChangedInfo.PreviousState.Contains(ControlState.Selected);
300
301             if (previousSelected != IsSelected)
302             {
303                 OnSelect();
304             }
305         }
306
307         private void Initialize()
308         {
309             IsSelectable = true;
310         }
311
312         /// <summary>
313         /// Theme change callback when theme is changed, this callback will be trigger.
314         /// </summary>
315         /// <param name="sender">The sender</param>
316         /// <param name="e">The event data</param>
317         [EditorBrowsable(EditorBrowsableState.Never)]
318         protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
319         {
320             SwitchStyle switchStyle = StyleManager.Instance.GetViewStyle(StyleName) as SwitchStyle;
321             if (null != switchStyle)
322             {
323                 ApplyStyle(switchStyle);
324             }
325         }
326
327         private void OnSelect()
328         {
329             ((SwitchExtension)Extension)?.OnSelectedChanged(this);
330
331             if (SelectedEvent != null)
332             {
333                 SelectEventArgs eventArgs = new SelectEventArgs();
334                 eventArgs.IsSelected = IsSelected;
335                 SelectedEvent(this, eventArgs);
336             }
337         }
338
339         /// <summary>
340         /// SelectEventArgs is a class to record item selected arguments which will sent to user.
341         /// </summary>
342         /// <since_tizen> 6 </since_tizen>
343         public class SelectEventArgs : EventArgs
344         {
345             /// <summary> Select state of Switch </summary>
346             /// <since_tizen> 6 </since_tizen>
347             public bool IsSelected;
348         }
349     }
350 }