|
1 |
| -using Avalonia; |
| 1 | +using System; |
| 2 | +using Avalonia; |
2 | 3 | using Avalonia.Automation;
|
3 | 4 | using Avalonia.Automation.Peers;
|
4 | 5 | using Avalonia.Controls;
|
5 | 6 | using Avalonia.Controls.Automation.Peers;
|
| 7 | +using Avalonia.Layout; |
6 | 8 | using Avalonia.Media;
|
7 | 9 | using Avalonia.Metadata;
|
8 | 10 |
|
@@ -77,25 +79,48 @@ public sealed override void Render(DrawingContext context)
|
77 | 79 | {
|
78 | 80 | var source = Source;
|
79 | 81 |
|
80 |
| - if (source != null && Bounds.Width > 0 && Bounds.Height > 0) |
| 82 | + if (source == null || Bounds is not {Width: > 0, Height: > 0}) return; |
| 83 | + |
| 84 | + var viewPort = new Rect(Bounds.Size); |
| 85 | + var sourceSize = source.Size; |
| 86 | + |
| 87 | + var scale = Stretch.CalculateScaling(Bounds.Size, sourceSize, StretchDirection); |
| 88 | + var scaledSize = sourceSize * scale; |
| 89 | + |
| 90 | + // Calculate starting points for dest |
| 91 | + var destX = HorizontalAlignment switch |
81 | 92 | {
|
82 |
| - Rect viewPort = new Rect(Bounds.Size); |
83 |
| - Size sourceSize = source.Size; |
84 |
| - |
85 |
| - Vector scale = Stretch.CalculateScaling(Bounds.Size, sourceSize, StretchDirection); |
86 |
| - Size scaledSize = sourceSize * scale; |
87 |
| - Rect destRect = viewPort |
88 |
| - .CenterRect(new Rect(scaledSize)) |
89 |
| - .WithX(0) |
90 |
| - .WithY(0) |
91 |
| - .Intersect(viewPort); |
92 |
| - Rect sourceRect = new Rect(sourceSize) |
93 |
| - .CenterRect(new Rect(destRect.Size / scale)) |
94 |
| - .WithX(0) |
95 |
| - .WithY(0); |
96 |
| - |
97 |
| - context.DrawImage(source, sourceRect, destRect); |
98 |
| - } |
| 93 | + HorizontalAlignment.Left => 0, |
| 94 | + HorizontalAlignment.Center => (int) (viewPort.Width - scaledSize.Width) / 2, |
| 95 | + HorizontalAlignment.Right => (int) (viewPort.Width - scaledSize.Width), |
| 96 | + // Stretch is default, use center |
| 97 | + HorizontalAlignment.Stretch => (int) (viewPort.Width - scaledSize.Width) / 2, |
| 98 | + _ => throw new ArgumentException(nameof(HorizontalAlignment)) |
| 99 | + }; |
| 100 | + |
| 101 | + var destRect = viewPort |
| 102 | + .CenterRect(new Rect(scaledSize)) |
| 103 | + .WithX(destX) |
| 104 | + .WithY(0) |
| 105 | + .Intersect(viewPort); |
| 106 | + var destRectUnscaledSize = destRect.Size / scale; |
| 107 | + |
| 108 | + var sourceX = HorizontalAlignment switch |
| 109 | + { |
| 110 | + HorizontalAlignment.Left => 0, |
| 111 | + HorizontalAlignment.Center => (int) (sourceSize - destRectUnscaledSize).Width / 2, |
| 112 | + HorizontalAlignment.Right => (int) (sourceSize - destRectUnscaledSize).Width, |
| 113 | + // Stretch is default, use center |
| 114 | + HorizontalAlignment.Stretch => (int) (sourceSize - destRectUnscaledSize).Width / 2, |
| 115 | + _ => throw new ArgumentException(nameof(HorizontalAlignment)) |
| 116 | + }; |
| 117 | + |
| 118 | + var sourceRect = new Rect(sourceSize) |
| 119 | + .CenterRect(new Rect(destRect.Size / scale)) |
| 120 | + .WithX(sourceX) |
| 121 | + .WithY(0); |
| 122 | + |
| 123 | + context.DrawImage(source, sourceRect, destRect); |
99 | 124 | }
|
100 | 125 |
|
101 | 126 | /// <summary>
|
|
0 commit comments