2 * Copyright(c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.ComponentModel;
19 using Tizen.NUI.BaseComponents;
20 using Tizen.NUI.Components.Extension;
22 namespace Tizen.NUI.Components
25 /// Switch is one kind of common component, it can be used as selector.
26 /// User can handle Navigation by adding/inserting/deleting NavigationItem.
28 /// <since_tizen> 6 </since_tizen>
29 public class Switch : Button
31 private ImageView track = null;
32 private ImageView thumb = null;
37 /// Creates a new instance of a Switch.
39 /// <since_tizen> 6 </since_tizen>
40 public Switch() : base()
46 /// Creates a new instance of a Switch with style.
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)
56 /// Creates a new instance of a Switch with style.
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)
66 /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
68 /// <since_tizen> 6 </since_tizen>
69 [Obsolete("Deprecated in API8; Will be removed in API10. Please use SelectedChanged event instead.")]
70 public event EventHandler<SelectEventArgs> SelectedEvent;
73 /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.
75 /// <since_tizen> 8 </since_tizen>
76 public event EventHandler<SelectedChangedEventArgs> SelectedChanged;
79 /// Return a copied Style instance of Switch
82 /// It returns copied Style instance and changing it does not effect to the Switch.
83 /// Style setting is possible by using constructor or the function of ApplyStyle(ViewStyle viewStyle)
85 /// <since_tizen> 8 </since_tizen>
86 public new SwitchStyle Style
90 var result = new SwitchStyle(ViewStyle as SwitchStyle);
91 result.CopyPropertiesFromView(this);
92 result.Track.CopyPropertiesFromView(Track);
93 result.Thumb.CopyPropertiesFromView(Thumb);
99 /// Apply style to switch.
101 /// <param name="viewStyle">The style to apply.</param>
102 [EditorBrowsable(EditorBrowsableState.Never)]
103 public override void ApplyStyle(ViewStyle viewStyle)
105 base.ApplyStyle(viewStyle);
107 SwitchStyle swStyle = viewStyle as SwitchStyle;
111 if (swStyle.Track != null)
113 Track.ApplyStyle(swStyle.Track);
116 if (swStyle.Thumb != null)
118 Thumb.ApplyStyle(swStyle.Thumb);
124 /// Switch's track part.
126 /// <since_tizen> 8 </since_tizen>
127 public ImageView Track
133 track = new ImageView()
135 PositionUsesPivotPoint = true,
136 ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
137 PivotPoint = Tizen.NUI.PivotPoint.CenterLeft,
138 WidthResizePolicy = ResizePolicyType.FillToParent,
139 HeightResizePolicy = ResizePolicyType.FillToParent
142 var extension = (SwitchExtension)Extension;
143 if (extension != null)
145 track = extension.OnCreateTrack(this, track);
158 /// Switch's thumb part.
160 /// <since_tizen> 8 </since_tizen>
161 public ImageView Thumb
167 thumb = new ImageView()
169 PositionUsesPivotPoint = true,
170 ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
171 PivotPoint = Tizen.NUI.PivotPoint.CenterLeft,
172 WidthResizePolicy = ResizePolicyType.Fixed,
173 HeightResizePolicy = ResizePolicyType.Fixed
176 var extension = (SwitchExtension)Extension;
177 if (extension != null)
179 thumb = extension.OnCreateThumb(this, thumb);
192 /// Background image's resource url selector in Switch.
194 /// <since_tizen> 6 </since_tizen>
195 public StringSelector SwitchBackgroundImageURLSelector
197 get => track == null ? null : new StringSelector((Selector<string>)track.GetValue(ImageView.ResourceUrlSelectorProperty));
198 set => track?.SetValue(ImageView.ResourceUrlSelectorProperty, value);
202 /// Handler image's resource url in Switch.
204 /// <since_tizen> 6 </since_tizen>
205 public string SwitchHandlerImageURL
209 return Thumb.ResourceUrl;
213 Thumb.ResourceUrl = value;
218 /// Handler image's resource url selector in Switch.
219 /// Getter returns copied selector value if exist, null otherwise.
221 /// <since_tizen> 6 </since_tizen>
222 public StringSelector SwitchHandlerImageURLSelector
224 get => thumb == null ? null : new StringSelector((Selector<string>)thumb.GetValue(ImageView.ResourceUrlSelectorProperty));
225 set => thumb?.SetValue(ImageView.ResourceUrlSelectorProperty, value);
229 /// Handler image's size in Switch.
231 /// <since_tizen> 6 </since_tizen>
232 public Size SwitchHandlerImageSize
245 /// Dispose Switch and all children on it.
247 /// <param name="type">Dispose type.</param>
248 /// <since_tizen> 6 </since_tizen>
249 protected override void Dispose(DisposeTypes type)
251 if (disposed) return;
253 if (type == DisposeTypes.Explicit)
255 Utility.Dispose(Thumb);
256 Utility.Dispose(Track);
263 /// Called after a key event is received by the view that has had its focus set.
265 /// <param name="key">The key event.</param>
266 /// <returns>True if the key event should be consumed.</returns>
267 /// <since_tizen> 8 </since_tizen>
268 public override bool OnKey(Key key)
270 return base.OnKey(key);
274 /// Called after a touch event is received by the owning view.<br />
275 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
277 /// <param name="touch">The touch event.</param>
278 /// <returns>True if the event should be consumed.</returns>
279 /// <since_tizen> 8 </since_tizen>
280 public override bool OnTouch(Touch touch)
282 return base.OnTouch(touch);
286 /// Get Switch style.
288 /// <returns>The default switch style.</returns>
289 /// <since_tizen> 8 </since_tizen>
290 protected override ViewStyle CreateViewStyle()
292 return new SwitchStyle();
296 [EditorBrowsable(EditorBrowsableState.Never)]
297 protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
299 base.OnControlStateChanged(controlStateChangedInfo);
306 bool previousSelected = controlStateChangedInfo.PreviousState.Contains(ControlState.Selected);
308 if (previousSelected != IsSelected)
314 private void Initialize()
319 private void OnSelect()
321 ((SwitchExtension)Extension)?.OnSelectedChanged(this);
323 if (SelectedEvent != null)
325 SelectEventArgs eventArgs = new SelectEventArgs();
326 eventArgs.IsSelected = IsSelected;
327 SelectedEvent(this, eventArgs);
330 if (SelectedChanged != null)
332 SelectedChangedEventArgs eventArgs = new SelectedChangedEventArgs();
333 eventArgs.IsSelected = IsSelected;
334 SelectedChanged(this, eventArgs);
339 /// SelectEventArgs is a class to record item selected arguments which will sent to user.
341 /// <since_tizen> 6 </since_tizen>
342 [Obsolete("Deprecated in API8; Will be removed in API10. Please use SelectedChangedEventArgs instead.")]
343 public class SelectEventArgs : EventArgs
345 /// <summary> Select state of Switch </summary>
346 /// <since_tizen> 6 </since_tizen>
347 public bool IsSelected;