feat: setup demo.

This commit is contained in:
rabbitism
2024-01-20 22:46:23 +08:00
parent 0e91c844a7
commit 497a8c2d02
10 changed files with 143 additions and 4 deletions

View File

@@ -0,0 +1,61 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Media;
using Avalonia.Utilities;
namespace Ursa.Controls;
public class OverlayDialogHost: Canvas
{
public static readonly StyledProperty<string> HostIdProperty = AvaloniaProperty.Register<OverlayDialogHost, string>(
nameof(HostId));
public string HostId
{
get => GetValue(HostIdProperty);
set => SetValue(HostIdProperty, value);
}
private Point _lastPoint;
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
OverlayDialogManager.RegisterOverlayDialogHost(this, HostId);
}
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
OverlayDialogManager.UnregisterOverlayDialogHost(HostId);
base.OnDetachedFromVisualTree(e);
}
protected override void OnPointerMoved(PointerEventArgs e)
{
base.OnPointerMoved(e);
if (e.Source is Control item)
{
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
{
var p = e.GetPosition(this);
var left= p.X - _lastPoint.X;
var top = p.Y - _lastPoint.Y;
left = MathUtilities.Clamp(left, 0, Bounds.Width - item.Bounds.Width);
top = MathUtilities.Clamp(top, 0, Bounds.Height - item.Bounds.Height);
Canvas.SetLeft(item, left);
Canvas.SetTop(item, top);
}
}
}
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
if (e.Source is Control item)
{
_lastPoint = e.GetPosition(item);
}
}
}