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> 6 </since_tizen>
50 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
51 [EditorBrowsable(EditorBrowsableState.Never)]
52 public Switch(string style) : base(style)
58 /// Creates a new instance of a Switch with style.
60 /// <param name="style">Create Switch by style customized by user.</param>
61 /// <since_tizen> 6 </since_tizen>
62 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
63 [EditorBrowsable(EditorBrowsableState.Never)]
64 public Switch(SwitchStyle style) : base(style)
70 /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
72 /// <since_tizen> 6 </since_tizen>
73 public event EventHandler<SelectEventArgs> SelectedEvent;
75 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
76 [EditorBrowsable(EditorBrowsableState.Never)]
77 public new SwitchStyle Style => ViewStyle as SwitchStyle;
80 /// Background image's resource url in Switch.
82 /// <since_tizen> 6 </since_tizen>
83 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
84 [EditorBrowsable(EditorBrowsableState.Never)]
85 public string SwitchBackgroundImageURL
89 return Style?.Track?.ResourceUrl?.All;
93 if (null != value && null != Style?.Track)
95 Style.Track.ResourceUrl = value;
100 private StringSelector switchBackgroundImageURLSelector = new StringSelector();
102 /// Background image's resource url selector in Switch.
104 /// <since_tizen> 6 </since_tizen>
105 public StringSelector SwitchBackgroundImageURLSelector
109 return switchBackgroundImageURLSelector;
113 switchBackgroundImageURLSelector.Clone(value);
118 /// Handler image's resource url in Switch.
120 /// <since_tizen> 6 </since_tizen>
121 public string SwitchHandlerImageURL
125 return Style?.Thumb?.ResourceUrl?.All;
129 if (null != value && null != Style?.Thumb)
131 Style.Thumb.ResourceUrl = value;
136 private StringSelector switchHandlerImageURLSelector = new StringSelector();
138 /// Handler image's resource url selector in Switch.
140 /// <since_tizen> 6 </since_tizen>
141 public StringSelector SwitchHandlerImageURLSelector
145 return switchHandlerImageURLSelector;
149 switchHandlerImageURLSelector.Clone(value);
154 /// Handler image's size in Switch.
156 /// <since_tizen> 6 </since_tizen>
157 public Size SwitchHandlerImageSize
161 return Style?.Thumb?.Size;
165 if (null != Style?.Thumb)
167 Style.Thumb.Size = value;
173 /// Dispose Switch and all children on it.
175 /// <param name="type">Dispose type.</param>
176 /// <since_tizen> 6 </since_tizen>
177 protected override void Dispose(DisposeTypes type)
179 if (disposed) return;
181 if (type == DisposeTypes.Explicit)
183 if (null != handlerAni)
185 if (handlerAni.State == Animation.States.Playing)
189 handlerAni.Dispose();
193 Utility.Dispose(thumbImage);
194 Utility.Dispose(trackImage);
201 /// Called after a key event is received by the view that has had its focus set.
203 /// <param name="key">The key event.</param>
204 /// <returns>True if the key event should be consumed.</returns>
205 /// <since_tizen> 6 </since_tizen>
206 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
207 [EditorBrowsable(EditorBrowsableState.Never)]
208 public override bool OnKey(Key key)
210 if (!IsEnabled || null == key) return false;
212 bool ret = base.OnKey(key);
213 if (key.State == Key.StateType.Up)
215 if (key.KeyPressedName == "Return")
225 /// Called after a touch event is received by the owning view.<br />
226 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
228 /// <param name="touch">The touch event.</param>
229 /// <returns>True if the event should be consumed.</returns>
230 /// <since_tizen> 6 </since_tizen>
231 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
232 [EditorBrowsable(EditorBrowsableState.Never)]
233 public override bool OnTouch(Touch touch)
235 if(!IsEnabled || null == touch) return false;
237 PointStateType state = touch.GetState(0);
238 bool ret = base.OnTouch(touch);
241 case PointStateType.Up:
251 /// Get Switch attribues.
253 /// <since_tizen> 6 </since_tizen>
254 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
255 [EditorBrowsable(EditorBrowsableState.Never)]
256 protected override ViewStyle GetViewStyle()
258 return new SwitchStyle();
261 private void Initialize()
263 Style.IsSelectable = true;
264 handlerAni = new Animation(aniTime);
265 trackImage = new ImageView()
267 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
268 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
269 PositionUsesPivotPoint = true,
270 WidthResizePolicy = ResizePolicyType.FillToParent,
271 HeightResizePolicy = ResizePolicyType.FillToParent,
272 Name = "SwitchBackgroundImage",
275 trackImage.ApplyStyle(Style.Track);
277 thumbImage = new ImageView()
279 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
280 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
281 PositionUsesPivotPoint = true,
282 Name = "SwitchHandlerImage",
284 trackImage.Add(thumbImage);
285 thumbImage.ApplyStyle(Style.Thumb);
289 /// Theme change callback when theme is changed, this callback will be trigger.
291 /// <since_tizen> 6 </since_tizen>
292 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
293 [EditorBrowsable(EditorBrowsableState.Never)]
294 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
296 SwitchStyle tempAttributes = StyleManager.Instance.GetViewStyle(style) as SwitchStyle;
297 if (null != tempAttributes)
299 Style.CopyFrom(tempAttributes);
303 private void OnSelect()
305 if (handlerAni.State == Animation.States.Playing)
310 handlerAni.AnimateTo(thumbImage, "PositionX", Size2D.Width - thumbImage.Size2D.Width - thumbImage.Position2D.X);
311 trackImage.Opacity = 0.5f; ///////need defined by UX
312 handlerAni.AnimateTo(trackImage, "Opacity", 1);
315 if (SelectedEvent != null)
317 SelectEventArgs eventArgs = new SelectEventArgs();
318 eventArgs.IsSelected = IsSelected;
319 SelectedEvent(this, eventArgs);
324 /// SelectEventArgs is a class to record item selected arguments which will sent to user.
326 /// <since_tizen> 6 </since_tizen>
327 public class SelectEventArgs : EventArgs
329 /// <summary> Select state of Switch </summary>
330 /// <since_tizen> 6 </since_tizen>
331 public bool IsSelected;