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 Tizen.NUI.BaseComponents;
19 using System.ComponentModel;
21 namespace Tizen.NUI.Components
24 /// Switch is one kind of common component, it can be used as selector.
25 /// User can handle Navigation by adding/inserting/deleting NavigationItem.
27 /// <since_tizen> 6 </since_tizen>
28 public class Switch : Button
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;
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 public event EventHandler<SelectEventArgs> SelectedEvent;
72 /// Get style of switch.
74 /// <since_tizen> 8 </since_tizen>
75 public new SwitchStyle Style => ViewStyle as SwitchStyle;
78 /// Background image's resource url in Switch.
80 /// <since_tizen> 6 </since_tizen>
81 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
82 [EditorBrowsable(EditorBrowsableState.Never)]
83 public string SwitchBackgroundImageURL
87 return Style?.Track?.ResourceUrl?.All;
91 if (null != value && null != Style?.Track)
93 Style.Track.ResourceUrl = value;
99 /// Background image's resource url selector in Switch.
101 /// <since_tizen> 6 </since_tizen>
102 public StringSelector SwitchBackgroundImageURLSelector
106 StringSelector strSl = new StringSelector();
107 strSl.Clone(Style?.Track?.ResourceUrl);
112 if (null != value && null != Style?.Track)
114 Style.Track.ResourceUrl = value;
120 /// Handler image's resource url in Switch.
122 /// <since_tizen> 6 </since_tizen>
123 public string SwitchHandlerImageURL
127 return Style?.Thumb?.ResourceUrl?.All;
131 if (null != value && null != Style?.Thumb)
133 Style.Thumb.ResourceUrl = value;
139 /// Handler image's resource url selector in Switch.
141 /// <since_tizen> 6 </since_tizen>
142 public StringSelector SwitchHandlerImageURLSelector
146 StringSelector strSl = new StringSelector();
147 strSl.Clone(Style?.Thumb?.ResourceUrl);
152 if (null != value && null != Style?.Thumb)
154 Style.Thumb.ResourceUrl = value;
160 /// Handler image's size in Switch.
162 /// <since_tizen> 6 </since_tizen>
163 public Size SwitchHandlerImageSize
167 return Style?.Thumb?.Size;
171 if (null != Style?.Thumb)
173 Style.Thumb.Size = value;
179 /// Dispose Switch and all children on it.
181 /// <param name="type">Dispose type.</param>
182 /// <since_tizen> 6 </since_tizen>
183 protected override void Dispose(DisposeTypes type)
185 if (disposed) return;
187 if (type == DisposeTypes.Explicit)
189 if (null != handlerAni)
191 if (handlerAni.State == Animation.States.Playing)
195 handlerAni.Dispose();
199 Utility.Dispose(thumbImage);
200 Utility.Dispose(trackImage);
207 /// Called after a key event is received by the view that has had its focus set.
209 /// <param name="key">The key event.</param>
210 /// <returns>True if the key event should be consumed.</returns>
211 /// <since_tizen> 6 </since_tizen>
212 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
213 [EditorBrowsable(EditorBrowsableState.Never)]
214 public override bool OnKey(Key key)
216 if (!IsEnabled || null == key) return false;
218 bool ret = base.OnKey(key);
219 if (key.State == Key.StateType.Up)
221 if (key.KeyPressedName == "Return")
231 /// Called after a touch event is received by the owning view.<br />
232 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
234 /// <param name="touch">The touch event.</param>
235 /// <returns>True if the event should be consumed.</returns>
236 /// <since_tizen> 6 </since_tizen>
237 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
238 [EditorBrowsable(EditorBrowsableState.Never)]
239 public override bool OnTouch(Touch touch)
241 if(!IsEnabled || null == touch) return false;
243 PointStateType state = touch.GetState(0);
244 bool ret = base.OnTouch(touch);
247 case PointStateType.Up:
257 /// Get Switch style.
259 /// <returns>The default switch style.</returns>
260 /// <since_tizen> 8 </since_tizen>
261 protected override ViewStyle GetViewStyle()
263 return new SwitchStyle();
266 private void Initialize()
268 Style.IsSelectable = true;
269 handlerAni = new Animation(aniTime);
270 trackImage = new ImageView()
272 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
273 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
274 PositionUsesPivotPoint = true,
275 WidthResizePolicy = ResizePolicyType.FillToParent,
276 HeightResizePolicy = ResizePolicyType.FillToParent,
277 Name = "SwitchBackgroundImage",
280 trackImage.ApplyStyle(Style.Track);
282 thumbImage = new ImageView()
284 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
285 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
286 PositionUsesPivotPoint = true,
287 Name = "SwitchHandlerImage",
289 trackImage.Add(thumbImage);
290 thumbImage.ApplyStyle(Style.Thumb);
294 /// Theme change callback when theme is changed, this callback will be trigger.
296 /// <param name="sender">The sender</param>
297 /// <param name="e">The event data</param>
298 /// <since_tizen> 8 </since_tizen>
299 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
301 SwitchStyle switchStyle = StyleManager.Instance.GetViewStyle(style) as SwitchStyle;
302 if (null != switchStyle)
304 Style.CopyFrom(switchStyle);
308 private void OnSelect()
310 if (handlerAni.State == Animation.States.Playing)
315 handlerAni.AnimateTo(thumbImage, "PositionX", Size2D.Width - thumbImage.Size2D.Width - thumbImage.Position2D.X);
316 trackImage.Opacity = 0.5f; ///////need defined by UX
317 handlerAni.AnimateTo(trackImage, "Opacity", 1);
320 if (SelectedEvent != null)
322 SelectEventArgs eventArgs = new SelectEventArgs();
323 eventArgs.IsSelected = IsSelected;
324 SelectedEvent(this, eventArgs);
329 /// SelectEventArgs is a class to record item selected arguments which will sent to user.
331 /// <since_tizen> 6 </since_tizen>
332 public class SelectEventArgs : EventArgs
334 /// <summary> Select state of Switch </summary>
335 /// <since_tizen> 6 </since_tizen>
336 public bool IsSelected;