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 switchBackgroundImage;
32 private ImageView switchHandlerImage;
33 private Animation handlerAni = null;
34 private SwitchAttributes switchAttributes;
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 attributes.
60 /// <param name="attrs">Create Switch by attributes customized by user.</param>
61 /// <since_tizen> 6 </since_tizen>
62 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
63 [EditorBrowsable(EditorBrowsableState.Never)]
64 public Switch(SwitchAttributes attrs) : base(attrs)
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;
76 /// Background image's resource url in Switch.
78 /// <since_tizen> 6 </since_tizen>
79 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
80 [EditorBrowsable(EditorBrowsableState.Never)]
81 public string SwitchBackgroundImageURL
85 return switchAttributes?.SwitchBackgroundImageAttributes?.ResourceURL?.All;
91 CreateSwitchBackgroundImageAttributes();
92 if (switchAttributes.SwitchBackgroundImageAttributes.ResourceURL == null)
94 switchAttributes.SwitchBackgroundImageAttributes.ResourceURL = new StringSelector();
96 switchAttributes.SwitchBackgroundImageAttributes.ResourceURL.All = value;
103 /// Background image's resource url selector in Switch.
105 /// <since_tizen> 6 </since_tizen>
106 public StringSelector SwitchBackgroundImageURLSelector
110 return switchAttributes?.SwitchBackgroundImageAttributes?.ResourceURL;
116 CreateSwitchBackgroundImageAttributes();
117 switchAttributes.SwitchBackgroundImageAttributes.ResourceURL = value.Clone() as StringSelector;
124 /// Handler image's resource url in Switch.
126 /// <since_tizen> 6 </since_tizen>
127 public string SwitchHandlerImageURL
131 return switchAttributes?.SwitchHandlerImageAttributes?.ResourceURL?.All;
137 CreateSwitchHandlerImageAttributes();
138 if (switchAttributes.SwitchHandlerImageAttributes.ResourceURL == null)
140 switchAttributes.SwitchHandlerImageAttributes.ResourceURL = new StringSelector();
142 switchAttributes.SwitchHandlerImageAttributes.ResourceURL.All = value;
149 /// Handler image's resource url selector in Switch.
151 /// <since_tizen> 6 </since_tizen>
152 public StringSelector SwitchHandlerImageURLSelector
156 return switchAttributes?.SwitchHandlerImageAttributes?.ResourceURL;
162 CreateSwitchHandlerImageAttributes();
163 switchAttributes.SwitchHandlerImageAttributes.ResourceURL = value.Clone() as StringSelector;
170 /// Handler image's size in Switch.
172 /// <since_tizen> 6 </since_tizen>
173 public Size SwitchHandlerImageSize
177 return switchAttributes?.SwitchHandlerImageAttributes?.Size ?? new Size(0, 0);
181 CreateSwitchHandlerImageAttributes();
182 switchAttributes.SwitchHandlerImageAttributes.Size = value;
188 /// Dispose Switch and all children on it.
190 /// <param name="type">Dispose type.</param>
191 /// <since_tizen> 6 </since_tizen>
192 protected override void Dispose(DisposeTypes type)
199 if (type == DisposeTypes.Explicit)
201 if (handlerAni != null)
203 if (handlerAni.State == Animation.States.Playing)
207 handlerAni.Dispose();
211 if (switchHandlerImage != null)
213 Utility.Dispose(switchHandlerImage);
215 if (switchBackgroundImage != null)
217 Utility.Dispose(switchBackgroundImage);
225 /// Update Switch by attributes.
227 /// <since_tizen> 6 </since_tizen>
228 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
229 [EditorBrowsable(EditorBrowsableState.Never)]
230 protected override void OnUpdate()
234 if (switchAttributes.SwitchBackgroundImageAttributes != null)
236 if (switchBackgroundImage == null)
238 switchBackgroundImage = new ImageView()
240 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
241 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
242 PositionUsesPivotPoint = true,
243 WidthResizePolicy = ResizePolicyType.FillToParent,
244 HeightResizePolicy = ResizePolicyType.FillToParent,
246 switchBackgroundImage.Name = "SwitchBackgroundImage";
247 Add(switchBackgroundImage);
249 ApplyAttributes(switchBackgroundImage, switchAttributes.SwitchBackgroundImageAttributes);
251 if (switchAttributes.SwitchHandlerImageAttributes != null)
253 if (switchHandlerImage == null)
255 switchHandlerImage = new ImageView()
257 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
258 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
259 PositionUsesPivotPoint = true,
261 switchHandlerImage.Name = "SwitchHandlerImage";
262 switchBackgroundImage.Add(switchHandlerImage);
264 ApplyAttributes(switchHandlerImage, switchAttributes.SwitchHandlerImageAttributes);
270 /// Called after a key event is received by the view that has had its focus set.
272 /// <param name="key">The key event.</param>
273 /// <returns>True if the key event should be consumed.</returns>
274 /// <since_tizen> 6 </since_tizen>
275 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
276 [EditorBrowsable(EditorBrowsableState.Never)]
277 public override bool OnKey(Key key)
279 if (IsEnabled == false)
283 bool ret = base.OnKey(key);
284 if (key.State == Key.StateType.Up)
286 if (key.KeyPressedName == "Return")
296 /// Called after a touch event is received by the owning view.<br />
297 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
299 /// <param name="touch">The touch event.</param>
300 /// <returns>True if the event should be consumed.</returns>
301 /// <since_tizen> 6 </since_tizen>
302 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
303 [EditorBrowsable(EditorBrowsableState.Never)]
304 public override bool OnTouch(Touch touch)
306 if(IsEnabled == false)
310 PointStateType state = touch.GetState(0);
311 bool ret = base.OnTouch(touch);
314 case PointStateType.Up:
324 /// Get Switch attribues.
326 /// <since_tizen> 6 </since_tizen>
327 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
328 [EditorBrowsable(EditorBrowsableState.Never)]
329 protected override Attributes GetAttributes()
331 return new SwitchAttributes();
334 private void Initialize()
336 switchAttributes = attributes as SwitchAttributes;
337 if (switchAttributes == null)
339 throw new Exception("Switch attribute parse error.");
342 switchAttributes.IsSelectable = true;
343 CreateHandlerAnimation();
347 /// Theme change callback when theme is changed, this callback will be trigger.
349 /// <since_tizen> 6 </since_tizen>
350 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
351 [EditorBrowsable(EditorBrowsableState.Never)]
352 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
354 SwitchAttributes tempAttributes = StyleManager.Instance.GetAttributes(style) as SwitchAttributes;
355 if (tempAttributes != null)
357 attributes = switchAttributes = tempAttributes;
362 private void CreateSwitchBackgroundImageAttributes()
364 if (switchAttributes.SwitchBackgroundImageAttributes == null)
366 switchAttributes.SwitchBackgroundImageAttributes = new ImageAttributes()
368 PositionUsesPivotPoint = true,
369 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
370 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
375 private void CreateSwitchHandlerImageAttributes()
377 if (switchAttributes.SwitchHandlerImageAttributes == null)
379 switchAttributes.SwitchHandlerImageAttributes = new ImageAttributes()
381 PositionUsesPivotPoint = true,
386 private void CreateHandlerAnimation()
388 if (handlerAni == null)
390 handlerAni = new Animation(aniTime);
394 private void OnSelect()
396 if (handlerAni.State == Animation.States.Playing)
401 if (switchHandlerImage != null)
403 handlerAni.AnimateTo(switchHandlerImage, "PositionX", Size2D.Width - switchHandlerImage.Size2D.Width - switchHandlerImage.Position2D.X);
405 if (switchBackgroundImage != null)
407 switchBackgroundImage.Opacity = 0.5f; ///////need defined by UX
408 handlerAni.AnimateTo(switchBackgroundImage, "Opacity", 1);
412 if (SelectedEvent != null)
414 SelectEventArgs eventArgs = new SelectEventArgs();
415 eventArgs.IsSelected = IsSelected;
416 SelectedEvent(this, eventArgs);
421 /// SelectEventArgs is a class to record item selected arguments which will sent to user.
423 /// <since_tizen> 6 </since_tizen>
424 public class SelectEventArgs : EventArgs
426 /// <summary> Select state of Switch </summary>
427 /// <since_tizen> 6 </since_tizen>
428 public bool IsSelected;