using System;
using System.Collections.Generic;
using System.Text;
using Avalonia.Interactivity;
namespace Avalonia.Controls
{
///
/// Represents spin directions that are valid.
///
[Flags]
public enum ValidSpinDirections
{
///
/// Can not increase nor decrease.
///
None = 0,
///
/// Can increase.
///
Increase = 1,
///
/// Can decrease.
///
Decrease = 2
}
///
/// Represents spin directions that could be initiated by the end-user.
///
public enum SpinDirection
{
///
/// Represents a spin initiated by the end-user in order to Increase a value.
///
Increase = 0,
///
/// Represents a spin initiated by the end-user in order to Decrease a value.
///
Decrease = 1
}
///
/// Provides data for the Spinner.Spin event.
///
public class SpinEventArgs : RoutedEventArgs
{
///
/// Gets the SpinDirection for the spin that has been initiated by the end-user.
///
public SpinDirection Direction { get; }
///
/// Get or set whheter the spin event originated from a mouse wheel event.
///
public bool UsingMouseWheel{ get; }
///
/// Initializes a new instance of the SpinEventArgs class.
///
/// Spin direction.
public SpinEventArgs(SpinDirection direction)
{
Direction = direction;
}
public SpinEventArgs(RoutedEvent routedEvent, SpinDirection direction)
: base(routedEvent)
{
Direction = direction;
}
public SpinEventArgs(SpinDirection direction, bool usingMouseWheel)
{
Direction = direction;
UsingMouseWheel = usingMouseWheel;
}
public SpinEventArgs(RoutedEvent routedEvent, SpinDirection direction, bool usingMouseWheel)
: base(routedEvent)
{
Direction = direction;
UsingMouseWheel = usingMouseWheel;
}
}
///
/// Base class for controls that represents controls that can spin.
///
public abstract class Spinner : ContentControl
{
///
/// Defines the property.
///
public static readonly StyledProperty ValidSpinDirectionProperty =
AvaloniaProperty.Register(nameof(ValidSpinDirection),
ValidSpinDirections.Increase | ValidSpinDirections.Decrease);
///
/// Defines the event.
///
public static readonly RoutedEvent SpinEvent =
RoutedEvent.Register(nameof(Spin), RoutingStrategies.Bubble);
///
/// Initializes static members of the class.
///
static Spinner()
{
ValidSpinDirectionProperty.Changed.Subscribe(OnValidSpinDirectionPropertyChanged);
}
///
/// Occurs when spinning is initiated by the end-user.
///
public event EventHandler Spin
{
add { AddHandler(SpinEvent, value); }
remove { RemoveHandler(SpinEvent, value); }
}
///
/// Gets or sets allowed for this control.
///
public ValidSpinDirections ValidSpinDirection
{
get { return GetValue(ValidSpinDirectionProperty); }
set { SetValue(ValidSpinDirectionProperty, value); }
}
///
/// Called when valid spin direction changed.
///
/// The old value.
/// The new value.
protected virtual void OnValidSpinDirectionChanged(ValidSpinDirections oldValue, ValidSpinDirections newValue)
{
}
///
/// Raises the OnSpin event when spinning is initiated by the end-user.
///
/// Spin event args.
protected virtual void OnSpin(SpinEventArgs e)
{
var valid = e.Direction == SpinDirection.Increase
? ValidSpinDirections.Increase
: ValidSpinDirections.Decrease;
//Only raise the event if spin is allowed.
if ((ValidSpinDirection & valid) == valid)
{
RaiseEvent(e);
}
}
///
/// Called when the property value changed.
///
/// The event args.
private static void OnValidSpinDirectionPropertyChanged(AvaloniaPropertyChangedEventArgs e)
{
if (e.Sender is Spinner spinner)
{
var oldValue = (ValidSpinDirections)e.OldValue;
var newValue = (ValidSpinDirections)e.NewValue;
spinner.OnValidSpinDirectionChanged(oldValue, newValue);
}
}
}
}