XAML-Map-Control/MapControl/Avalonia/PushpinBorder.Avalonia.cs

104 lines
3.6 KiB
C#
Raw Normal View History

2025-08-19 19:43:02 +02:00
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using System;
2025-11-14 16:56:11 +01:00
using Brush = Avalonia.Media.IBrush;
2024-05-25 15:55:31 +02:00
namespace MapControl
{
public partial class PushpinBorder : Decorator
{
public static readonly StyledProperty<CornerRadius> CornerRadiusProperty =
DependencyPropertyHelper.Register<PushpinBorder, CornerRadius>(nameof(CornerRadius), new CornerRadius());
public static readonly StyledProperty<Size> ArrowSizeProperty =
DependencyPropertyHelper.Register<PushpinBorder, Size>(nameof(ArrowSize), new Size(10d, 20d));
public static readonly StyledProperty<double> BorderWidthProperty =
DependencyPropertyHelper.Register<PushpinBorder, double>(nameof(BorderWidth));
2024-05-27 11:18:14 +02:00
public static readonly StyledProperty<Brush> BackgroundProperty =
DependencyPropertyHelper.Register<PushpinBorder, Brush>(nameof(Background));
2024-05-25 15:55:31 +02:00
2024-05-27 11:18:14 +02:00
public static readonly StyledProperty<Brush> BorderBrushProperty =
DependencyPropertyHelper.Register<PushpinBorder, Brush>(nameof(BorderBrush));
2024-05-25 15:55:31 +02:00
static PushpinBorder()
{
AffectsMeasure<PushpinBorder>(ArrowSizeProperty, BorderWidthProperty, CornerRadiusProperty);
2025-06-10 09:13:20 +02:00
AffectsRender<PushpinBorder>(BackgroundProperty, BorderBrushProperty);
2024-05-25 15:55:31 +02:00
}
public double ActualWidth => Bounds.Width;
public double ActualHeight => Bounds.Height;
2024-05-25 15:55:31 +02:00
public CornerRadius CornerRadius
{
get => GetValue(CornerRadiusProperty);
set => SetValue(CornerRadiusProperty, value);
}
2024-05-27 11:18:14 +02:00
public Brush Background
2024-05-25 15:55:31 +02:00
{
get => GetValue(BackgroundProperty);
set => SetValue(BackgroundProperty, value);
}
2024-05-27 11:18:14 +02:00
public Brush BorderBrush
2024-05-25 15:55:31 +02:00
{
get => GetValue(BorderBrushProperty);
set => SetValue(BorderBrushProperty, value);
}
protected override Size MeasureOverride(Size constraint)
{
var width = 2d * BorderWidth + Padding.Left + Padding.Right;
var height = 2d * BorderWidth + Padding.Top + Padding.Bottom;
if (Child != null)
{
Child.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
width += Child.DesiredSize.Width;
height += Child.DesiredSize.Height;
}
var minWidth = BorderWidth + Math.Max(
CornerRadius.TopLeft + CornerRadius.TopRight,
CornerRadius.BottomLeft + CornerRadius.BottomRight + ArrowSize.Width);
var minHeight = BorderWidth + Math.Max(
CornerRadius.TopLeft + CornerRadius.BottomLeft,
CornerRadius.TopRight + CornerRadius.BottomRight);
return new Size(
Math.Max(width, minWidth),
Math.Max(height, minHeight) + ArrowSize.Height);
}
protected override Size ArrangeOverride(Size size)
{
2024-06-01 11:27:13 +02:00
Child?.Arrange(new Rect(
BorderWidth + Padding.Left,
BorderWidth + Padding.Top,
Child.DesiredSize.Width,
Child.DesiredSize.Height));
2024-05-25 15:55:31 +02:00
2024-06-01 11:27:13 +02:00
return DesiredSize;
2024-05-25 15:55:31 +02:00
}
public override void Render(DrawingContext drawingContext)
{
var pen = new Pen
{
Brush = BorderBrush,
Thickness = BorderWidth,
LineJoin = PenLineJoin.Round
};
drawingContext.DrawGeometry(Background, pen, BuildGeometry());
base.Render(drawingContext);
}
}
}