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;
36 /// Creates a new instance of a Switch.
38 /// <since_tizen> 6 </since_tizen>
39 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> 6 </since_tizen>
49 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
50 [EditorBrowsable(EditorBrowsableState.Never)]
51 public Switch(string style) : base(style)
57 /// Creates a new instance of a Switch with style.
59 /// <param name="style">Create Switch by style customized by user.</param>
60 /// <since_tizen> 6 </since_tizen>
61 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
62 [EditorBrowsable(EditorBrowsableState.Never)]
63 public Switch(SwitchStyle style) : base(style)
69 /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
71 /// <since_tizen> 6 </since_tizen>
72 public event EventHandler<SelectEventArgs> SelectedEvent;
74 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
75 [EditorBrowsable(EditorBrowsableState.Never)]
76 public new SwitchStyle Style => ViewStyle as SwitchStyle;
79 /// Background image's resource url in Switch.
81 /// <since_tizen> 6 </since_tizen>
82 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
83 [EditorBrowsable(EditorBrowsableState.Never)]
84 public string SwitchBackgroundImageURL
88 return Style?.Track?.ResourceUrl?.All;
92 if (null != value && null != Style?.Track)
94 Style.Track.ResourceUrl = value;
100 /// Background image's resource url selector in Switch.
102 /// <since_tizen> 6 </since_tizen>
103 public StringSelector SwitchBackgroundImageURLSelector
107 return (StringSelector)Style?.Track?.ResourceUrl;
111 if (null != value && null != Style?.Track)
113 Style.Track.ResourceUrl = value.Clone() as StringSelector;
119 /// Handler image's resource url in Switch.
121 /// <since_tizen> 6 </since_tizen>
122 public string SwitchHandlerImageURL
126 return Style?.Thumb?.ResourceUrl?.All;
130 if (null != value && null != Style?.Thumb)
132 Style.Thumb.ResourceUrl = value;
138 /// Handler image's resource url selector in Switch.
140 /// <since_tizen> 6 </since_tizen>
141 public StringSelector SwitchHandlerImageURLSelector
145 return (StringSelector)Style?.Thumb?.ResourceUrl;
149 if (null != value && null != Style?.Thumb)
151 Style.Thumb.ResourceUrl = value.Clone() as StringSelector;
157 /// Handler image's size in Switch.
159 /// <since_tizen> 6 </since_tizen>
160 public Size SwitchHandlerImageSize
164 return Style?.Thumb?.Size;
168 if (null != Style?.Thumb)
170 Style.Thumb.Size = value;
176 /// Dispose Switch and all children on it.
178 /// <param name="type">Dispose type.</param>
179 /// <since_tizen> 6 </since_tizen>
180 protected override void Dispose(DisposeTypes type)
182 if (disposed) return;
184 if (type == DisposeTypes.Explicit)
186 if (null != handlerAni)
188 if (handlerAni.State == Animation.States.Playing)
192 handlerAni.Dispose();
196 Utility.Dispose(thumbImage);
197 Utility.Dispose(trackImage);
204 /// Called after a key event is received by the view that has had its focus set.
206 /// <param name="key">The key event.</param>
207 /// <returns>True if the key event should be consumed.</returns>
208 /// <since_tizen> 6 </since_tizen>
209 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
210 [EditorBrowsable(EditorBrowsableState.Never)]
211 public override bool OnKey(Key key)
213 if (!IsEnabled) return false;
215 bool ret = base.OnKey(key);
216 if (key.State == Key.StateType.Up)
218 if (key.KeyPressedName == "Return")
228 /// Called after a touch event is received by the owning view.<br />
229 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
231 /// <param name="touch">The touch event.</param>
232 /// <returns>True if the event should be consumed.</returns>
233 /// <since_tizen> 6 </since_tizen>
234 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
235 [EditorBrowsable(EditorBrowsableState.Never)]
236 public override bool OnTouch(Touch touch)
238 if(!IsEnabled) return false;
240 PointStateType state = touch.GetState(0);
241 bool ret = base.OnTouch(touch);
244 case PointStateType.Up:
254 /// Get Switch attribues.
256 /// <since_tizen> 6 </since_tizen>
257 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
258 [EditorBrowsable(EditorBrowsableState.Never)]
259 protected override ViewStyle GetViewStyle()
261 return new SwitchStyle();
264 private void Initialize()
266 Style.IsSelectable = true;
267 handlerAni = new Animation(aniTime);
268 trackImage = new ImageView()
270 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
271 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
272 PositionUsesPivotPoint = true,
273 WidthResizePolicy = ResizePolicyType.FillToParent,
274 HeightResizePolicy = ResizePolicyType.FillToParent,
275 Name = "SwitchBackgroundImage",
278 trackImage.ApplyStyle(Style.Track);
280 thumbImage = new ImageView()
282 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
283 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
284 PositionUsesPivotPoint = true,
285 Name = "SwitchHandlerImage",
287 trackImage.Add(thumbImage);
288 thumbImage.ApplyStyle(Style.Thumb);
292 /// Theme change callback when theme is changed, this callback will be trigger.
294 /// <since_tizen> 6 </since_tizen>
295 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
296 [EditorBrowsable(EditorBrowsableState.Never)]
297 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
299 SwitchStyle tempAttributes = StyleManager.Instance.GetAttributes(style) as SwitchStyle;
300 if (null != tempAttributes)
302 Style.CopyFrom(tempAttributes);
306 private void OnSelect()
308 if (handlerAni.State == Animation.States.Playing)
313 handlerAni.AnimateTo(thumbImage, "PositionX", Size2D.Width - thumbImage.Size2D.Width - thumbImage.Position2D.X);
314 trackImage.Opacity = 0.5f; ///////need defined by UX
315 handlerAni.AnimateTo(trackImage, "Opacity", 1);
318 if (SelectedEvent != null)
320 SelectEventArgs eventArgs = new SelectEventArgs();
321 eventArgs.IsSelected = IsSelected;
322 SelectedEvent(this, eventArgs);
327 /// SelectEventArgs is a class to record item selected arguments which will sent to user.
329 /// <since_tizen> 6 </since_tizen>
330 public class SelectEventArgs : EventArgs
332 /// <summary> Select state of Switch </summary>
333 /// <since_tizen> 6 </since_tizen>
334 public bool IsSelected;