feat: extract common button style.

This commit is contained in:
rabbitism
2024-01-23 19:55:23 +08:00
parent 7c29320ad9
commit 61ebba897b
9 changed files with 137 additions and 49 deletions

View File

@@ -2,16 +2,20 @@ using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
namespace Ursa.Controls;
[TemplatePart(PART_CloseButton, typeof(Button))]
[TemplatePart(PART_TitleArea, typeof(Panel))]
public class DialogControl: ContentControl
{
public const string PART_CloseButton = "PART_CloseButton";
public const string PART_TitleArea = "PART_TitleArea";
private Button? _closeButton;
private Panel? _titleArea;
public event EventHandler<object?>? OnClose;
static DialogControl()
@@ -32,6 +36,7 @@ public class DialogControl: ContentControl
}
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
@@ -40,13 +45,30 @@ public class DialogControl: ContentControl
{
_closeButton.Click -= Close;
}
_titleArea?.RemoveHandler(PointerMovedEvent, OnTitlePointerMove);
_titleArea?.RemoveHandler(PointerPressedEvent, OnTitlePointerPressed);
_closeButton = e.NameScope.Find<Button>(PART_CloseButton);
_titleArea = e.NameScope.Find<Panel>(PART_TitleArea);
if (_closeButton is not null)
{
_closeButton.Click += Close;
}
_titleArea?.AddHandler(PointerMovedEvent, OnTitlePointerMove, RoutingStrategies.Bubble);
_titleArea?.AddHandler(PointerPressedEvent, OnTitlePointerPressed, RoutingStrategies.Bubble);
}
private void OnTitlePointerPressed(object sender, PointerPressedEventArgs e)
{
e.Source = this;
}
private void OnTitlePointerMove(object sender, PointerEventArgs e)
{
e.Source = this;
}
public Task<T> ShowAsync<T>()
{

View File

@@ -15,12 +15,6 @@ public class OverlayDialogHost: Canvas
{
private readonly List<DialogControl> _dialogs = new();
private readonly List<DialogControl> _modalDialogs = new();
private Rectangle _overlayMask = new()
{
Fill = new SolidColorBrush(new Color(1, 0, 0, 0)),
[Rectangle.ZIndexProperty] = 0,
};
public static readonly StyledProperty<string> HostIdProperty = AvaloniaProperty.Register<OverlayDialogHost, string>(
nameof(HostId));
@@ -66,37 +60,27 @@ public class OverlayDialogHost: Canvas
protected override void OnPointerMoved(PointerEventArgs e)
{
base.OnPointerMoved(e);
if (e.Source is Control item)
if (e.Source is DialogControl item)
{
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
{
var parent = item.FindAncestorOfType<DialogControl>();
if (parent is null)
{
return;
}
var p = e.GetPosition(this);
var left= p.X - _lastPoint.X;
var top = p.Y - _lastPoint.Y;
left = MathUtilities.Clamp(left, 0, Bounds.Width - parent.Bounds.Width);
top = MathUtilities.Clamp(top, 0, Bounds.Height - parent.Bounds.Height);
Canvas.SetLeft(parent, left);
Canvas.SetTop(parent, top);
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)
// base.OnPointerPressed(e);
if (e.Source is DialogControl item)
{
var parent = item.FindAncestorOfType<DialogControl>();
if (parent is null)
{
return;
}
_lastPoint = e.GetPosition(parent);
_lastPoint = e.GetPosition(item);
}
}