/*
* Copyright(c) 2019 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.ComponentModel;
using Tizen.NUI.BaseComponents;
using Tizen.NUI.Components.Extension;
namespace Tizen.NUI.Components
{
///
/// Switch is one kind of common component, it can be used as selector.
/// User can handle Navigation by adding/inserting/deleting NavigationItem.
///
/// 6
public class Switch : Button
{
private ImageView track = null;
private ImageView thumb = null;
static Switch() { }
///
/// Creates a new instance of a Switch.
///
/// 6
public Switch() : base()
{
Initialize();
}
///
/// Creates a new instance of a Switch with style.
///
/// Create Switch by special style defined in UX.
/// 8
public Switch(string style) : base(style)
{
Initialize();
}
///
/// Creates a new instance of a Switch with style.
///
/// Create Switch by style customized by user.
/// 8
public Switch(SwitchStyle switchStyle) : base(switchStyle)
{
Initialize();
}
///
/// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.
///
/// 6
[Obsolete("Deprecated in API8; Will be removed in API10. Please use SelectedChanged event instead.")]
public event EventHandler SelectedEvent;
///
/// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.
///
/// 8
public event EventHandler SelectedChanged;
///
/// Return a copied Style instance of Switch
///
///
/// It returns copied Style instance and changing it does not effect to the Switch.
/// Style setting is possible by using constructor or the function of ApplyStyle(ViewStyle viewStyle)
///
/// 8
public new SwitchStyle Style
{
get
{
var result = new SwitchStyle(ViewStyle as SwitchStyle);
result.CopyPropertiesFromView(this);
result.Track.CopyPropertiesFromView(Track);
result.Thumb.CopyPropertiesFromView(Thumb);
return result;
}
}
///
/// Apply style to switch.
///
/// The style to apply.
[EditorBrowsable(EditorBrowsableState.Never)]
public override void ApplyStyle(ViewStyle viewStyle)
{
base.ApplyStyle(viewStyle);
SwitchStyle swStyle = viewStyle as SwitchStyle;
if (null != swStyle)
{
if (swStyle.Track != null)
{
Track.ApplyStyle(swStyle.Track);
}
if (swStyle.Thumb != null)
{
Thumb.ApplyStyle(swStyle.Thumb);
}
}
}
///
/// Switch's track part.
///
/// 8
public ImageView Track
{
get
{
if (track == null)
{
track = new ImageView()
{
PositionUsesPivotPoint = true,
ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
PivotPoint = Tizen.NUI.PivotPoint.CenterLeft,
WidthResizePolicy = ResizePolicyType.FillToParent,
HeightResizePolicy = ResizePolicyType.FillToParent
};
var extension = (SwitchExtension)Extension;
if (extension != null)
{
track = extension.OnCreateTrack(this, track);
}
Add(track);
}
return track;
}
internal set
{
track = value;
}
}
///
/// Switch's thumb part.
///
/// 8
public ImageView Thumb
{
get
{
if (thumb == null)
{
thumb = new ImageView()
{
PositionUsesPivotPoint = true,
ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
PivotPoint = Tizen.NUI.PivotPoint.CenterLeft,
WidthResizePolicy = ResizePolicyType.Fixed,
HeightResizePolicy = ResizePolicyType.Fixed
};
var extension = (SwitchExtension)Extension;
if (extension != null)
{
thumb = extension.OnCreateThumb(this, thumb);
}
Add(thumb);
}
return thumb;
}
internal set
{
thumb = value;
}
}
///
/// Background image's resource url selector in Switch.
///
/// 6
public StringSelector SwitchBackgroundImageURLSelector
{
get => track == null ? null : new StringSelector((Selector)track.GetValue(ImageView.ResourceUrlSelectorProperty));
set => track?.SetValue(ImageView.ResourceUrlSelectorProperty, value);
}
///
/// Handler image's resource url in Switch.
///
/// 6
public string SwitchHandlerImageURL
{
get
{
return Thumb.ResourceUrl;
}
set
{
Thumb.ResourceUrl = value;
}
}
///
/// Handler image's resource url selector in Switch.
/// Getter returns copied selector value if exist, null otherwise.
///
/// 6
public StringSelector SwitchHandlerImageURLSelector
{
get => thumb == null ? null : new StringSelector((Selector)thumb.GetValue(ImageView.ResourceUrlSelectorProperty));
set => thumb?.SetValue(ImageView.ResourceUrlSelectorProperty, value);
}
///
/// Handler image's size in Switch.
///
/// 6
public Size SwitchHandlerImageSize
{
get
{
return Thumb.Size;
}
set
{
Thumb.Size = value;
}
}
///
/// Dispose Switch and all children on it.
///
/// Dispose type.
/// 6
protected override void Dispose(DisposeTypes type)
{
if (disposed) return;
if (type == DisposeTypes.Explicit)
{
Utility.Dispose(Thumb);
Utility.Dispose(Track);
}
base.Dispose(type);
}
///
/// Called after a key event is received by the view that has had its focus set.
///
/// The key event.
/// True if the key event should be consumed.
/// 8
public override bool OnKey(Key key)
{
return base.OnKey(key);
}
///
/// Called after a touch event is received by the owning view.
/// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).
///
/// The touch event.
/// True if the event should be consumed.
/// 8
[Obsolete("Deprecated in API8; Will be removed in API10. Please use OnClicked instead.")]
public override bool OnTouch(Touch touch)
{
return base.OnTouch(touch);
}
///
/// Get Switch style.
///
/// The default switch style.
/// 8
protected override ViewStyle CreateViewStyle()
{
return new SwitchStyle();
}
///
[EditorBrowsable(EditorBrowsableState.Never)]
protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
{
base.OnControlStateChanged(controlStateChangedInfo);
if (!IsSelectable)
{
return;
}
bool previousSelected = controlStateChangedInfo.PreviousState.Contains(ControlState.Selected);
if (previousSelected != IsSelected)
{
OnSelect();
}
}
private void Initialize()
{
IsSelectable = true;
}
private void OnSelect()
{
((SwitchExtension)Extension)?.OnSelectedChanged(this);
if (SelectedEvent != null)
{
SelectEventArgs eventArgs = new SelectEventArgs();
eventArgs.IsSelected = IsSelected;
SelectedEvent(this, eventArgs);
}
if (SelectedChanged != null)
{
SelectedChangedEventArgs eventArgs = new SelectedChangedEventArgs();
eventArgs.IsSelected = IsSelected;
SelectedChanged(this, eventArgs);
}
}
///
/// SelectEventArgs is a class to record item selected arguments which will sent to user.
///
/// 6
[Obsolete("Deprecated in API8; Will be removed in API10. Please use SelectedChangedEventArgs instead.")]
public class SelectEventArgs : EventArgs
{
/// Select state of Switch
/// 6
public bool IsSelected;
}
}
}