Files
Semi.Avalonia/demo/Semi.Avalonia.Demo/Controls/ColorDetailControl.cs

126 lines
4.0 KiB
C#
Raw Normal View History

2023-02-13 00:14:56 +08:00
using System.Globalization;
2024-12-29 23:04:46 +08:00
using System.Threading.Tasks;
2023-02-13 00:14:56 +08:00
using Avalonia;
2023-04-23 01:10:23 +08:00
using Avalonia.Controls;
2023-02-13 00:14:56 +08:00
using Avalonia.Controls.Primitives;
2026-02-06 20:24:35 +08:00
using Avalonia.Input.Platform;
2023-02-13 00:14:56 +08:00
using Avalonia.Media;
2024-12-29 23:04:46 +08:00
using Semi.Avalonia.Demo.Converters;
2023-02-13 00:14:56 +08:00
namespace Semi.Avalonia.Demo.Controls;
2024-12-24 16:39:57 +08:00
public class ColorDetailControl : TemplatedControl
2023-02-13 00:14:56 +08:00
{
public const string KEY_ResourceKey = "ResourceKey";
public const string KEY_Hex = "Hex";
2024-12-29 23:04:46 +08:00
public const string KEY_Hex2 = "Hex2";
2023-02-13 00:14:56 +08:00
public const string KEY_Opacity = "Opacity";
2023-03-27 11:25:47 +08:00
public const string KEY_ColorResourceKey = "ColorResourceKey";
2024-12-24 16:39:57 +08:00
public static readonly StyledProperty<string?> ResourceKeyProperty =
AvaloniaProperty.Register<ColorDetailControl, string?>(nameof(ResourceKey));
2023-02-13 00:14:56 +08:00
public string? ResourceKey
{
get => GetValue(ResourceKeyProperty);
set => SetValue(ResourceKeyProperty, value);
}
2024-12-24 16:39:57 +08:00
public static readonly StyledProperty<string?> ResourceNameProperty =
AvaloniaProperty.Register<ColorDetailControl, string?>(nameof(ResourceName));
2023-02-13 00:14:56 +08:00
public string? ResourceName
{
get => GetValue(ResourceNameProperty);
set => SetValue(ResourceNameProperty, value);
}
2024-12-24 16:39:57 +08:00
public static readonly StyledProperty<string?> ColorResourceKeyProperty =
AvaloniaProperty.Register<ColorDetailControl, string?>(nameof(ColorResourceKey));
2023-03-27 11:25:47 +08:00
public string? ColorResourceKey
{
get => GetValue(ColorResourceKeyProperty);
set => SetValue(ColorResourceKeyProperty, value);
}
2024-12-24 16:39:57 +08:00
public static readonly DirectProperty<ColorDetailControl, string?> HexProperty =
AvaloniaProperty.RegisterDirect<ColorDetailControl, string?>(nameof(Hex), o => o.Hex);
2023-02-13 00:14:56 +08:00
private string? _hex;
2024-12-24 16:39:57 +08:00
2023-02-13 00:14:56 +08:00
public string? Hex
{
get => _hex;
private set => SetAndRaise(HexProperty, ref _hex, value);
}
2024-12-24 16:39:57 +08:00
2024-12-29 23:04:46 +08:00
private string? _hex2;
public static readonly DirectProperty<ColorDetailControl, string?> Hex2Property =
AvaloniaProperty.RegisterDirect<ColorDetailControl, string?>(nameof(Hex2), o => o.Hex2);
public string? Hex2
{
get => _hex2;
set => SetAndRaise(Hex2Property, ref _hex2, value);
}
2024-12-24 16:39:57 +08:00
public static readonly DirectProperty<ColorDetailControl, string?> OpacityNumberProperty =
AvaloniaProperty.RegisterDirect<ColorDetailControl, string?>(nameof(OpacityNumber), o => o.OpacityNumber);
2023-02-13 00:14:56 +08:00
private string? _opacityNumber;
2024-12-24 16:39:57 +08:00
2023-02-13 00:14:56 +08:00
public string? OpacityNumber
{
get => _opacityNumber;
private set => SetAndRaise(OpacityNumberProperty, ref _opacityNumber, value);
}
2024-12-24 16:39:57 +08:00
2023-02-13 00:14:56 +08:00
static ColorDetailControl()
{
BackgroundProperty.Changed.AddClassHandler<ColorDetailControl>((o, e) => o.OnBackgroundChanged(e));
}
private void OnBackgroundChanged(AvaloniaPropertyChangedEventArgs args)
{
var color = args.GetNewValue<IBrush>();
2024-12-29 23:04:46 +08:00
if (color is ISolidColorBrush brush)
2023-02-13 00:14:56 +08:00
{
2024-12-29 23:04:46 +08:00
var hex1 = ColorConverter.ToHex.Convert(brush.Color, typeof(string), false, CultureInfo.InvariantCulture);
var hex2 = ColorConverter.ToHex.Convert(brush.Color, typeof(string), true, CultureInfo.InvariantCulture);
Hex = hex1 as string;
Hex2 = hex2 as string;
OpacityNumber = brush.Opacity.ToString(CultureInfo.InvariantCulture);
2023-02-13 00:14:56 +08:00
}
else
{
Hex = null;
Hex2 = null;
OpacityNumber = null;
}
2023-02-13 00:14:56 +08:00
}
2024-12-29 23:04:46 +08:00
public async Task Copy(object o)
2023-02-13 00:14:56 +08:00
{
string? text = null;
if (o is string s)
{
2024-12-29 23:04:46 +08:00
text = s switch
2023-02-13 00:14:56 +08:00
{
2024-12-29 23:04:46 +08:00
KEY_ResourceKey => ResourceKey,
KEY_Hex => Hex,
KEY_Hex2 => Hex2,
KEY_Opacity => OpacityNumber,
KEY_ColorResourceKey => ColorResourceKey,
_ => string.Empty
};
2023-02-13 00:14:56 +08:00
}
2023-04-23 01:10:23 +08:00
var toplevel = TopLevel.GetTopLevel(this);
if (toplevel?.Clipboard is { } c)
2023-02-13 00:14:56 +08:00
{
2024-12-24 16:39:57 +08:00
await c.SetTextAsync(text ?? string.Empty);
2023-02-13 00:14:56 +08:00
}
}
}