using System;
using System.Collections.Specialized;
using System.Linq;
using Artemis.Core;
using Artemis.UI.Shared.Flyouts;
using Artemis.UI.Shared.Providers;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Data;
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.Threading;
using FluentAvalonia.Core;
using Button = FluentAvalonia.UI.Controls.Button;
namespace Artemis.UI.Shared.Controls.GradientPicker;
///
/// Represents a gradient picker box that can be used to edit a gradient
///
public class GradientPickerButton : TemplatedControl
{
///
/// Gets or sets the color gradient.
///
public static readonly StyledProperty ColorGradientProperty =
AvaloniaProperty.Register(nameof(ColorGradient), notifying: ColorGradientChanged);
///
/// Gets or sets a boolean indicating whether the gradient picker should be in compact mode or not.
///
public static readonly StyledProperty IsCompactProperty =
AvaloniaProperty.Register(nameof(IsCompact), defaultBindingMode: BindingMode.TwoWay);
///
/// Gets or sets a storage provider to use for storing and loading gradients.
///
public static readonly StyledProperty StorageProviderProperty =
AvaloniaProperty.Register(nameof(StorageProvider));
///
/// Gets the linear gradient brush representing the color gradient.
///
public static readonly DirectProperty LinearGradientBrushProperty =
AvaloniaProperty.RegisterDirect(nameof(LinearGradientBrush), g => g.LinearGradientBrush);
private ColorGradient? _lastColorGradient;
private Button? _button;
private GradientPickerFlyout? _flyout;
private bool _flyoutActive;
///
/// Gets or sets the color gradient.
///
public ColorGradient? ColorGradient
{
get => GetValue(ColorGradientProperty);
set => SetValue(ColorGradientProperty, value);
}
///
/// Gets or sets a boolean indicating whether the gradient picker should be in compact mode or not.
///
public bool IsCompact
{
get => GetValue(IsCompactProperty);
set => SetValue(IsCompactProperty, value);
}
///
/// Gets or sets a storage provider to use for storing and loading gradients.
///
public IColorGradientStorageProvider? StorageProvider
{
get => GetValue(StorageProviderProperty);
set => SetValue(StorageProviderProperty, value);
}
///
/// Gets the linear gradient brush representing the color gradient.
///
public LinearGradientBrush LinearGradientBrush { get; } = new();
///
/// Raised when the flyout opens.
///
public event TypedEventHandler? FlyoutOpened;
///
/// Raised when the flyout closes.
///
public event TypedEventHandler? FlyoutClosed;
#region Overrides of TemplatedControl
///
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
if (_button != null)
_button.Click -= OnButtonClick;
base.OnApplyTemplate(e);
_button = e.NameScope.Find