2 * Copyright(c) 2021 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 System.Diagnostics;
20 using Tizen.NUI.BaseComponents;
21 using Tizen.NUI.Components.Extension;
23 namespace Tizen.NUI.Components
26 /// Switch is a kind of <see cref="Button"/> component that uses icon part as a toggle shape.
27 /// The icon part consists of track and thumb.
29 /// <since_tizen> 6 </since_tizen>
30 public partial class Switch : Button
32 private ImageView thumb = null;
37 /// Creates a new instance of a Switch.
39 /// <since_tizen> 6 </since_tizen>
40 public Switch() : base()
45 /// Creates a new instance of a Switch with style.
47 /// <param name="style">Create Switch by special style defined in UX.</param>
48 /// <since_tizen> 8 </since_tizen>
49 public Switch(string style) : base(style)
54 /// Creates a new instance of a Switch with style.
56 /// <param name="switchStyle">Create Switch by style customized by user.</param>
57 /// <since_tizen> 8 </since_tizen>
58 public Switch(SwitchStyle switchStyle) : base(switchStyle)
63 /// Initialize AT-SPI object.
65 [EditorBrowsable(EditorBrowsableState.Never)]
66 public override void OnInitialize()
69 AccessibilityRole = Role.ToggleButton;
77 /// Informs AT-SPI bridge about the set of AT-SPI states associated with this object.
79 [EditorBrowsable(EditorBrowsableState.Never)]
80 protected override AccessibilityStates AccessibilityCalculateStates()
82 var states = base.AccessibilityCalculateStates();
84 states[AccessibilityState.Checked] = this.IsSelected;
90 /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
92 /// <since_tizen> 6 </since_tizen>
93 [Obsolete("Deprecated in API8; Will be removed in API10. Please use SelectedChanged event instead.")]
94 public event EventHandler<SelectEventArgs> SelectedEvent;
97 /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.
99 /// <since_tizen> 8 </since_tizen>
100 public event EventHandler<SelectedChangedEventArgs> SelectedChanged;
103 /// Return currently applied style.
106 /// Modifying contents in style may cause unexpected behaviour.
108 /// <since_tizen> 8 </since_tizen>
109 public new SwitchStyle Style => (SwitchStyle)(ViewStyle as SwitchStyle)?.Clone();
112 /// Apply style to switch.
114 /// <param name="viewStyle">The style to apply.</param>
115 [EditorBrowsable(EditorBrowsableState.Never)]
116 public override void ApplyStyle(ViewStyle viewStyle)
118 if (viewStyle is SwitchStyle switchStyle)
120 if (Extension is SwitchExtension extension)
124 Icon = extension.OnCreateTrack(this, Icon);
125 thumb = extension.OnCreateThumb(this, thumb);
130 if (switchStyle.Track != null)
132 Track.ApplyStyle(switchStyle.Track);
135 if (switchStyle.Thumb != null)
137 Thumb.ApplyStyle(switchStyle.Thumb);
141 base.ApplyStyle(viewStyle);
145 /// Switch's track part.
147 /// <since_tizen> 8 </since_tizen>
148 public ImageView Track
158 /// Switch's thumb part.
160 /// <since_tizen> 8 </since_tizen>
161 public ImageView Thumb
171 /// Switch's track part image url selector.
173 /// <since_tizen> 6 </since_tizen>
174 public StringSelector SwitchBackgroundImageURLSelector
178 return GetValue(SwitchBackgroundImageURLSelectorProperty) as StringSelector;
182 SetValue(SwitchBackgroundImageURLSelectorProperty, value);
183 NotifyPropertyChanged();
186 private StringSelector InternalSwitchBackgroundImageURLSelector
188 get => Icon?.ResourceUrlSelector == null ? null : new StringSelector(Icon.ResourceUrlSelector);
191 Debug.Assert(Icon != null);
192 Icon.ResourceUrlSelector = value;
197 /// Handler image's resource url in Switch.
199 /// <since_tizen> 6 </since_tizen>
200 public string SwitchHandlerImageURL
204 return GetValue(SwitchHandlerImageURLProperty) as string;
208 SetValue(SwitchHandlerImageURLProperty, value);
209 NotifyPropertyChanged();
212 private string InternalSwitchHandlerImageURL
216 return Thumb.ResourceUrl;
220 Thumb.ResourceUrl = value;
225 /// Handler image's resource url selector in Switch.
226 /// Getter returns copied selector value if exist, null otherwise.
228 /// <since_tizen> 6 </since_tizen>
229 public StringSelector SwitchHandlerImageURLSelector
233 return GetValue(SwitchHandlerImageURLSelectorProperty) as StringSelector;
237 SetValue(SwitchHandlerImageURLSelectorProperty, value);
238 NotifyPropertyChanged();
241 private StringSelector InternalSwitchHandlerImageURLSelector
243 get => new StringSelector(thumb.ResourceUrlSelector);
246 Debug.Assert(thumb != null);
247 thumb.ResourceUrlSelector = value;
252 /// Handler image's size in Switch.
254 /// <since_tizen> 6 </since_tizen>
255 public Size SwitchHandlerImageSize
259 return GetValue(SwitchHandlerImageSizeProperty) as Size;
263 SetValue(SwitchHandlerImageSizeProperty, value);
264 NotifyPropertyChanged();
267 private Size InternalSwitchHandlerImageSize
280 /// Dispose Switch and all children on it.
282 /// <param name="type">Dispose type.</param>
283 /// <since_tizen> 6 </since_tizen>
284 protected override void Dispose(DisposeTypes type)
286 if (disposed) return;
288 if (type == DisposeTypes.Explicit)
290 Utility.Dispose(thumb);
297 /// Called after a key event is received by the view that has had its focus set.
299 /// <param name="key">The key event.</param>
300 /// <returns>True if the key event should be consumed.</returns>
301 /// <since_tizen> 8 </since_tizen>
302 public override bool OnKey(Key key)
304 return base.OnKey(key);
308 /// Called after a touch event is received by the owning view.<br />
309 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
311 /// <param name="touch">The touch event.</param>
312 /// <returns>True if the event should be consumed.</returns>
313 /// <since_tizen> 8 </since_tizen>
314 [Obsolete("Deprecated in API8; Will be removed in API10. Please use OnClicked instead.")]
315 #pragma warning disable CS0809 // Obsolete member overrides non-obsolete member, It will be removed in API10
316 public override bool OnTouch(Touch touch)
317 #pragma warning restore CS0809 // Obsolete member overrides non-obsolete member, It will be removed in API10
319 return base.OnTouch(touch);
323 /// Get Switch style.
325 /// <returns>The default switch style.</returns>
326 /// <since_tizen> 8 </since_tizen>
327 protected override ViewStyle CreateViewStyle()
329 return new SwitchStyle();
333 [EditorBrowsable(EditorBrowsableState.Never)]
334 protected override ImageView CreateIcon()
336 var icon = new ImageView()
338 AccessibilityHighlightable = false,
339 EnableControlStatePropagation = true
342 thumb = new ImageView();
349 [EditorBrowsable(EditorBrowsableState.Never)]
350 protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
352 base.OnControlStateChanged(controlStateChangedInfo);
359 bool previousSelected = controlStateChangedInfo.PreviousState.Contains(ControlState.Selected);
361 if (previousSelected != IsSelected)
367 private void OnSelect()
369 if (Accessibility.Accessibility.IsEnabled && IsHighlighted)
371 EmitAccessibilityStateChangedEvent(AccessibilityState.Checked, IsSelected);
374 ((SwitchExtension)Extension)?.OnSelectedChanged(this);
376 if (SelectedEvent != null)
378 SelectEventArgs eventArgs = new SelectEventArgs();
379 eventArgs.IsSelected = IsSelected;
380 SelectedEvent(this, eventArgs);
383 if (SelectedChanged != null)
385 SelectedChangedEventArgs eventArgs = new SelectedChangedEventArgs();
386 eventArgs.IsSelected = IsSelected;
387 SelectedChanged(this, eventArgs);
392 /// SelectEventArgs is a class to record item selected arguments which will sent to user.
394 /// <since_tizen> 6 </since_tizen>
395 /// It will be removed in API10
396 [Obsolete("Deprecated in API8; Will be removed in API10. Please use SelectedChangedEventArgs instead.")]
397 [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:Do not declare visible instance fields")]
398 [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
399 public class SelectEventArgs : EventArgs
401 /// <summary> Select state of Switch </summary>
402 /// <since_tizen> 6 </since_tizen>
403 public bool IsSelected;