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 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 SetAccessibilityConstructor(Role.ToggleButton);
78 /// Informs AT-SPI bridge about the set of AT-SPI states associated with this object.
80 [EditorBrowsable(EditorBrowsableState.Never)]
81 protected override AccessibilityStates AccessibilityCalculateStates(ulong states)
83 var accessibilityStates = base.AccessibilityCalculateStates(states);
84 FlagSetter(ref accessibilityStates, AccessibilityStates.Checked, this.IsSelected);
85 return accessibilityStates;
89 /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
91 /// <since_tizen> 6 </since_tizen>
92 [Obsolete("Deprecated in API8; Will be removed in API10. Please use SelectedChanged event instead.")]
93 public event EventHandler<SelectEventArgs> SelectedEvent;
96 /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.
98 /// <since_tizen> 8 </since_tizen>
99 public event EventHandler<SelectedChangedEventArgs> SelectedChanged;
102 /// Return currently applied style.
105 /// Modifying contents in style may cause unexpected behaviour.
107 /// <since_tizen> 8 </since_tizen>
108 public new SwitchStyle Style => (SwitchStyle)(ViewStyle as SwitchStyle)?.Clone();
111 /// Apply style to switch.
113 /// <param name="viewStyle">The style to apply.</param>
114 [EditorBrowsable(EditorBrowsableState.Never)]
115 public override void ApplyStyle(ViewStyle viewStyle)
117 if (viewStyle is SwitchStyle switchStyle)
119 if (Extension is SwitchExtension extension)
123 Icon = extension.OnCreateTrack(this, Icon);
124 thumb = extension.OnCreateThumb(this, thumb);
129 if (switchStyle.Track != null)
131 Track.ApplyStyle(switchStyle.Track);
134 if (switchStyle.Thumb != null)
136 Thumb.ApplyStyle(switchStyle.Thumb);
140 base.ApplyStyle(viewStyle);
144 /// Switch's track part.
146 /// <since_tizen> 8 </since_tizen>
147 public ImageView Track
157 /// Switch's thumb part.
159 /// <since_tizen> 8 </since_tizen>
160 public ImageView Thumb
170 /// Switch's track part image url selector.
172 /// <since_tizen> 6 </since_tizen>
173 public StringSelector SwitchBackgroundImageURLSelector
175 get => new StringSelector(Icon.ResourceUrlSelector);
178 Debug.Assert(Icon != null);
179 Icon.ResourceUrlSelector = value;
184 /// Handler image's resource url in Switch.
186 /// <since_tizen> 6 </since_tizen>
187 public string SwitchHandlerImageURL
191 return Thumb.ResourceUrl;
195 Thumb.ResourceUrl = value;
200 /// Handler image's resource url selector in Switch.
201 /// Getter returns copied selector value if exist, null otherwise.
203 /// <since_tizen> 6 </since_tizen>
204 public StringSelector SwitchHandlerImageURLSelector
206 get => new StringSelector(thumb.ResourceUrlSelector);
209 Debug.Assert(thumb != null);
210 thumb.ResourceUrlSelector = value;
215 /// Handler image's size in Switch.
217 /// <since_tizen> 6 </since_tizen>
218 public Size SwitchHandlerImageSize
231 /// Dispose Switch and all children on it.
233 /// <param name="type">Dispose type.</param>
234 /// <since_tizen> 6 </since_tizen>
235 protected override void Dispose(DisposeTypes type)
237 if (disposed) return;
239 if (type == DisposeTypes.Explicit)
241 Utility.Dispose(thumb);
248 /// Called after a key event is received by the view that has had its focus set.
250 /// <param name="key">The key event.</param>
251 /// <returns>True if the key event should be consumed.</returns>
252 /// <since_tizen> 8 </since_tizen>
253 public override bool OnKey(Key key)
255 return base.OnKey(key);
259 /// Called after a touch event is received by the owning view.<br />
260 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
262 /// <param name="touch">The touch event.</param>
263 /// <returns>True if the event should be consumed.</returns>
264 /// <since_tizen> 8 </since_tizen>
265 [Obsolete("Deprecated in API8; Will be removed in API10. Please use OnClicked instead.")]
266 #pragma warning disable CS0809 // Obsolete member overrides non-obsolete member, It will be removed in API10
267 public override bool OnTouch(Touch touch)
268 #pragma warning restore CS0809 // Obsolete member overrides non-obsolete member, It will be removed in API10
270 return base.OnTouch(touch);
274 /// Get Switch style.
276 /// <returns>The default switch style.</returns>
277 /// <since_tizen> 8 </since_tizen>
278 protected override ViewStyle CreateViewStyle()
280 return new SwitchStyle();
284 [EditorBrowsable(EditorBrowsableState.Never)]
285 protected override ImageView CreateIcon()
287 var icon = new ImageView()
289 AccessibilityHighlightable = false,
290 EnableControlStatePropagation = true
293 thumb = new ImageView();
300 [EditorBrowsable(EditorBrowsableState.Never)]
301 protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
303 base.OnControlStateChanged(controlStateChangedInfo);
310 bool previousSelected = controlStateChangedInfo.PreviousState.Contains(ControlState.Selected);
312 if (previousSelected != IsSelected)
318 private void OnSelect()
320 if (Accessibility.Accessibility.Enabled && IsHighlighted)
322 EmitAccessibilityStatesChangedEvent(AccessibilityStates.Checked, IsSelected);
325 ((SwitchExtension)Extension)?.OnSelectedChanged(this);
327 if (SelectedEvent != null)
329 SelectEventArgs eventArgs = new SelectEventArgs();
330 eventArgs.IsSelected = IsSelected;
331 SelectedEvent(this, eventArgs);
334 if (SelectedChanged != null)
336 SelectedChangedEventArgs eventArgs = new SelectedChangedEventArgs();
337 eventArgs.IsSelected = IsSelected;
338 SelectedChanged(this, eventArgs);
343 /// SelectEventArgs is a class to record item selected arguments which will sent to user.
345 /// <since_tizen> 6 </since_tizen>
346 /// It will be removed in API10
347 [Obsolete("Deprecated in API8; Will be removed in API10. Please use SelectedChangedEventArgs instead.")]
348 [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:Do not declare visible instance fields")]
349 [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
350 public class SelectEventArgs : EventArgs
352 /// <summary> Select state of Switch </summary>
353 /// <since_tizen> 6 </since_tizen>
354 public bool IsSelected;