Skip to content

Commit 4a4acc3

Browse files
committed
Restore scale change
1 parent d73322e commit 4a4acc3

6 files changed

Lines changed: 94 additions & 57 deletions

File tree

src/HierarchyGrid.Avalonia/Grid.axaml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ HierarchyGridViewModel viewModel
222222
var width = canvas.LocalClipBounds.Width;
223223
var height = canvas.LocalClipBounds.Height;
224224

225-
var scale = 1d;
226-
await HierarchyGridDrawer.Draw(viewModel, canvas, width, height, scale, false);
225+
// Avalonia does not need screen scale adjustment with windows zoomed display
226+
await HierarchyGridDrawer.Draw(viewModel, canvas, width, height, 1d, false);
227227
}
228228

229229
private static void SkiaElement_PointerMove(
@@ -233,7 +233,7 @@ HierarchyGridViewModel viewModel
233233
)
234234
{
235235
var position = args.GetPosition(element);
236-
viewModel.HandleMouseOver(position.X, position.Y, 1);
236+
viewModel.HandleMouseOver(position.X, position.Y, viewModel.Scale);
237237
}
238238

239239
private static void SkiaElement_PointerExit(HierarchyGridViewModel viewModel)

src/HierarchyGrid.Definitions/ElementCoordinates.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,31 @@ public readonly record struct ElementCoordinates
1313
public double Right { get; init; }
1414
public double Bottom { get; init; }
1515

16-
public ElementCoordinates(double left, double top, double right, double bottom) =>
17-
(Left, Top, Right, Bottom) = (left, top, right, bottom);
18-
19-
public ElementCoordinates(PositionedCell cell) =>
16+
public ElementCoordinates(
17+
double left,
18+
double top,
19+
double right,
20+
double bottom,
21+
double screenScale
22+
) =>
2023
(Left, Top, Right, Bottom) = (
21-
cell.Left,
22-
cell.Top,
23-
cell.Left + cell.Width,
24-
cell.Top + cell.Height
24+
screenScale * left,
25+
screenScale * top,
26+
screenScale * right,
27+
screenScale * bottom
2528
);
2629

2730
public bool Contains(double x, double y) => Left <= x && x <= Right && Top <= y && y <= Bottom;
2831

2932
public double Height => Math.Abs(Top - Bottom);
33+
34+
public ElementCoordinates(PositionedCell cell, double screenScale) =>
35+
(Left, Top, Right, Bottom) = (
36+
screenScale * cell.Left,
37+
screenScale * cell.Top,
38+
screenScale * (cell.Left + cell.Width),
39+
screenScale * (cell.Top + cell.Height)
40+
);
41+
3042
public double Width => Math.Abs(Right - Left);
3143
}

src/HierarchyGrid.Definitions/HierarchyGridViewModel.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,14 +749,19 @@ var definition in ColumnsDefinitions
749749
/// <param name="height">The height of the area for which drawn cells are being retrieved.</param>
750750
/// <param name="invalidate">Specifies whether the existing drawn cells should be invalidated and recomputed.</param>
751751
/// <returns>A sequence of <see cref="PositionedCell"/> instances representing the cells currently drawn within the specified area.</returns>
752-
public Seq<PositionedCell> GetDrawnCells(double width, double height, bool invalidate)
752+
public Seq<PositionedCell> GetDrawnCells(
753+
double width,
754+
double height,
755+
bool invalidate,
756+
double screenScale = 1d
757+
)
753758
{
754759
DrawnCells = GetDrawnCells(
755760
HorizontalOffset,
756761
VerticalOffset,
757762
width,
758763
height,
759-
Scale,
764+
screenScale * Scale,
760765
invalidate
761766
);
762767
return DrawnCells;

src/HierarchyGrid.Skia/CanvasExtensions.cs

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal static void DrawGlobalHeaders(
4141
HierarchyGridViewModel viewModel,
4242
SkiaTheme theme,
4343
IList<(ElementCoordinates, Guid)> previousGlobalCoordinates,
44-
double screenScale = 1d
44+
double screenScale
4545
)
4646
{
4747
// Don't draw if structure doesn't allow it
@@ -164,17 +164,19 @@ private static void DrawGlobalHeader(
164164
GlobalHeader globalHeader = GlobalHeader.Local
165165
)
166166
{
167+
var actualScale = screenScale * viewModel.Scale;
167168
var rect = SKRect.Create(
168-
(float)(left * screenScale),
169-
(float)(top * screenScale),
170-
(float)(width * screenScale),
171-
(float)(height * screenScale)
169+
(float)(left * actualScale),
170+
(float)(top * actualScale),
171+
(float)(width * actualScale),
172+
(float)(height * actualScale)
172173
);
173174
var coordinates = new ElementCoordinates(
174-
left * screenScale,
175-
top * screenScale,
176-
(left + width) * screenScale,
177-
(top + height) * screenScale
175+
left,
176+
top,
177+
(left + width),
178+
(top + height),
179+
actualScale
178180
);
179181

180182
var isHovered = previousGlobalCoordinates
@@ -195,8 +197,8 @@ private static void DrawGlobalHeader(
195197

196198
var decorator = GetGlobalHeaderDecorator(
197199
!expanded,
198-
left * screenScale,
199-
top * screenScale,
200+
left * actualScale,
201+
top * actualScale,
200202
globalHeader
201203
);
202204
paint.Color = isHovered
@@ -221,9 +223,11 @@ internal static void DrawColumnHeaders(
221223
Func<HierarchyGridViewModel, HierarchyDefinition[]> selector,
222224
float availableWidth,
223225
ref int headerCount,
224-
double screenScale = 1d
226+
double screenScale
225227
)
226228
{
229+
var actualAvailableWidth = availableWidth / viewModel.Scale;
230+
227231
viewModel.ColumnsParents.Clear();
228232
var hdefs = selector(viewModel).ToArr();
229233

@@ -252,7 +256,7 @@ internal static void DrawColumnHeaders(
252256
column++;
253257
}
254258

255-
while (column < hdefs.Length && currentPosition < availableWidth)
259+
while (column < hdefs.Length && currentPosition < actualAvailableWidth)
256260
{
257261
var hdef = hdefs[column];
258262
var width = viewModel.ColumnsWidths[column];
@@ -402,6 +406,8 @@ internal static void DrawRowHeaders(
402406
double screenScale = 1d
403407
)
404408
{
409+
var actualAvailableHeight = availableHeight / viewModel.Scale;
410+
405411
viewModel.RowsParents.Clear();
406412
var hdefs = selector(viewModel).ToArr();
407413

@@ -430,7 +436,7 @@ internal static void DrawRowHeaders(
430436
row++;
431437
}
432438

433-
while (row < hdefs.Length && currentPosition < availableHeight)
439+
while (row < hdefs.Length && currentPosition < actualAvailableHeight)
434440
{
435441
var hdef = hdefs[row];
436442
var height = viewModel.RowsHeights[row];
@@ -572,11 +578,12 @@ private static void DrawHeader(
572578
double screenScale
573579
)
574580
{
581+
var actualScale = screenScale * viewModel.Scale;
575582
var rect = SKRect.Create(
576-
(float)(left * screenScale),
577-
(float)(top * screenScale),
578-
(float)(width * screenScale),
579-
(float)(height * screenScale)
583+
(float)(left * actualScale),
584+
(float)(top * actualScale),
585+
(float)(width * actualScale),
586+
(float)(height * actualScale)
580587
);
581588

582589
var renderInfo = RenderInfo.FindRender(viewModel, theme, hdef);
@@ -590,7 +597,7 @@ double screenScale
590597
paint.Color = theme.BorderColor;
591598
canvas.DrawRect(rect, paint);
592599

593-
GetHeaderDecorator(hdef, left * screenScale, top * screenScale)
600+
GetHeaderDecorator(hdef, left * actualScale, top * actualScale)
594601
.IfSome(decorator =>
595602
{
596603
using var localPaint = new SKPaint();
@@ -606,13 +613,19 @@ double screenScale
606613
top,
607614
width,
608615
height,
609-
screenScale,
616+
actualScale,
610617
renderInfo,
611618
viewModel.HeaderFontFamily,
612619
viewModel.HeaderFontSize
613620
);
614621

615-
var coordinates = new ElementCoordinates(left, top, left + width, top + height);
622+
var coordinates = new ElementCoordinates(
623+
left,
624+
top,
625+
left + width,
626+
top + height,
627+
actualScale
628+
);
616629
viewModel.HeadersCoordinates.Add(new(coordinates, new(coordinates, hdef)));
617630

618631
headerCount++;
@@ -776,12 +789,11 @@ internal static void DrawCells(
776789
this SKCanvas canvas,
777790
HierarchyGridViewModel viewModel,
778791
SkiaTheme theme,
779-
IEnumerable<PositionedCell> cells,
780-
double screenScale = 1d
792+
IEnumerable<PositionedCell> cells
781793
)
782794
{
783795
foreach (var cell in cells)
784-
canvas.DrawCell(viewModel, theme, cell, screenScale);
796+
canvas.DrawCell(viewModel, theme, cell);
785797
}
786798

787799
private static void DrawCell(
@@ -792,11 +804,13 @@ private static void DrawCell(
792804
double screenScale = 1d
793805
)
794806
{
807+
var actualScale = screenScale * viewModel.Scale;
808+
795809
var rect = SKRect.Create(
796-
(float)(cell.Left * screenScale),
797-
(float)(cell.Top * screenScale),
798-
(float)(cell.Width * screenScale),
799-
(float)(cell.Height * screenScale)
810+
(float)(cell.Left * actualScale),
811+
(float)(cell.Top * actualScale),
812+
(float)(cell.Width * actualScale),
813+
(float)(cell.Height * actualScale)
800814
);
801815

802816
var renderInfo = RenderInfo.FindRender(viewModel, theme, cell);
@@ -840,10 +854,10 @@ private static void DrawCell(
840854
localPaint.Color = fci.BackgroundColor.ToSKColor();
841855

842856
rect = SKRect.Create(
843-
(float)(cell.Left * screenScale) + borderThickness,
844-
(float)(cell.Top * screenScale) + borderThickness,
845-
(float)(cell.Width * screenScale) - (borderThickness + 1f),
846-
(float)(cell.Height * screenScale) - (borderThickness + 1f)
857+
(float)(cell.Left * actualScale) + borderThickness,
858+
(float)(cell.Top * actualScale) + borderThickness,
859+
(float)(cell.Width * actualScale) - (borderThickness + 1f),
860+
(float)(cell.Height * actualScale) - (borderThickness + 1f)
847861
);
848862
canvas.DrawRect(rect, localPaint);
849863

@@ -854,13 +868,13 @@ private static void DrawCell(
854868
canvas.DrawRect(rect, localPaint);
855869
});
856870

857-
DrawSelectionBorder(canvas, viewModel, theme, cell, screenScale, paint, ref rect);
871+
DrawSelectionBorder(canvas, viewModel, theme, cell, actualScale, paint, ref rect);
858872

859-
RenderCellText(canvas, viewModel, theme, cell, screenScale, renderInfo);
860-
RenderLeftSideDecor(canvas, cell, renderInfo, screenScale);
861-
RenderRightSideDecor(canvas, cell, renderInfo, screenScale);
873+
RenderCellText(canvas, viewModel, theme, cell, actualScale, renderInfo);
874+
RenderLeftSideDecor(canvas, cell, renderInfo, actualScale);
875+
RenderRightSideDecor(canvas, cell, renderInfo, actualScale);
862876

863-
viewModel.CellsCoordinates.Add((new(cell), cell));
877+
viewModel.CellsCoordinates.Add((new(cell, viewModel.Scale), cell));
864878
}
865879

866880
private static void RenderRightSideDecor(

src/HierarchyGrid.Skia/HierarchyGridDrawer.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static async Task Draw(
1313
SKCanvas canvas,
1414
float width,
1515
float height,
16-
double screenScale = 1d,
16+
double screenScale,
1717
bool invalidate = false
1818
)
1919
{
@@ -23,7 +23,7 @@ public static async Task Draw(
2323
using var paintBackground = new SKPaint();
2424
paintBackground.Color = theme.BackgroundColor;
2525
paintBackground.Style = SKPaintStyle.StrokeAndFill;
26-
var rectBackground = SKRect.Create(width, height);
26+
var rectBackground = SKRect.Create(width, height); // TODO: check scale
2727
canvas.DrawRect(rectBackground, paintBackground);
2828

2929
if (viewModel.HasData)
@@ -40,8 +40,7 @@ public static async Task Draw(
4040
canvas.DrawCells(
4141
viewModel,
4242
theme,
43-
viewModel.GetDrawnCells(width, height, invalidate),
44-
screenScale
43+
viewModel.GetDrawnCells(width, height, invalidate, screenScale)
4544
);
4645

4746
canvas.DrawColumnHeaders(
@@ -71,10 +70,13 @@ public static async Task Draw(
7170
paint.IsAntialias = true;
7271
paint.Color = theme.ForegroundColor;
7372

73+
var resultingScale = screenScale * viewModel.Scale;
74+
75+
// TODO: should probably not take all scale into account
7476
canvas.DrawText(
7577
viewModel.StatusMessage ?? "NO MESSAGE",
76-
(float)screenScale * width / 2,
77-
(float)screenScale * height / 2,
78+
(float)resultingScale * width / 2,
79+
(float)resultingScale * height / 2,
7880
SKTextAlign.Center,
7981
font,
8082
paint

src/HierarchyGrid/Grid.xaml.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ await HierarchyGridDrawer.Draw(
117117
.MouseMove.Subscribe(args =>
118118
{
119119
var position = args.GetPosition(view.SkiaElement);
120-
viewModel.HandleMouseOver(position.X, position.Y, view.ScreenScale);
120+
viewModel.HandleMouseOver(
121+
position.X,
122+
position.Y,
123+
viewModel.Scale * view.ScreenScale
124+
);
121125
})
122126
.DisposeWith(disposables);
123127

0 commit comments

Comments
 (0)