Skip to content

Commit d73322e

Browse files
committed
Improve header and cell text alignment in grid
Refactored header height calculation into a dedicated method and adjusted text alignment logic for both headers and cells in Skia rendering. These changes improve visual consistency, especially for centered text in headers without child elements, and clarify padding calculations. Minor code cleanup and comments added for maintainability.
1 parent a0227e8 commit d73322e

3 files changed

Lines changed: 56 additions & 26 deletions

File tree

src/HierarchyGrid.Avalonia/Grid.axaml.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Avalonia.Controls.Shapes;
77
using Avalonia.Data;
88
using Avalonia.Input;
9+
using Avalonia.Layout;
910
using Avalonia.Media;
1011
using Avalonia.ReactiveUI;
1112
using Avalonia.VisualTree;
@@ -22,7 +23,7 @@ namespace HierarchyGrid.Avalonia;
2223
public partial class Grid : ReactiveUserControl<HierarchyGridViewModel>
2324
{
2425
private readonly Flyout _tooltip;
25-
private Rectangle _tooltipRectangle;
26+
private readonly Rectangle _tooltipRectangle;
2627
private ContextMenu? _contextMenu;
2728

2829
public Grid()
@@ -328,6 +329,11 @@ private void ShowTooltip(PositionedCell pCell)
328329
Canvas.SetLeft(_tooltipRectangle, pCell.Left + 3);
329330
Canvas.SetTop(_tooltipRectangle, pCell.Top + 3);
330331

332+
// var border = new Border() { HorizontalAlignment = HorizontalAlignment.Stretch };
333+
// border.Background = Brushes.Wheat;
334+
// border.Child = new TextBlock() { Text = text.Trim() };
335+
// _tooltip.Content = border;
336+
331337
_tooltip.Content = text.Trim();
332338
_tooltip.Placement = PlacementMode.Bottom;
333339
_tooltip.ShowAt(_tooltipRectangle);

src/HierarchyGrid.Skia/CanvasExtensions.cs

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -280,15 +280,11 @@ private static void DrawColumnHeader(
280280
int column,
281281
HierarchyDefinition hdef,
282282
double width,
283-
double screenScale
283+
double screenScale,
284+
bool lastColumn = false
284285
)
285286
{
286-
var height = hdef is { IsExpanded: true, HasChild: true }
287-
? viewModel.ColumnsHeadersHeight[hdef.Level]
288-
: Enumerable
289-
.Range(hdef.Level, viewModel.ColumnsHeadersHeight.Length - hdef.Level)
290-
.Select(x => viewModel.ColumnsHeadersHeight[x])
291-
.Sum();
287+
double height = ComputeHeaderHeight(viewModel, hdef);
292288

293289
var top = Enumerable
294290
.Range(0, hdef.Level)
@@ -320,6 +316,25 @@ double screenScale
320316
currentPosition += width;
321317
}
322318

319+
/// <summary>
320+
/// If the definition is expanded and has any child element,
321+
/// it should span on only one row, using the height of its level.
322+
/// Otherwise, it should span until the end of the headers display.
323+
/// </summary>
324+
private static double ComputeHeaderHeight(
325+
HierarchyGridViewModel viewModel,
326+
HierarchyDefinition hdef
327+
)
328+
{
329+
var height = hdef is { IsExpanded: true, HasChild: true }
330+
? viewModel.ColumnsHeadersHeight[hdef.Level]
331+
: Enumerable
332+
.Range(hdef.Level, viewModel.ColumnsHeadersHeight.Length - hdef.Level)
333+
.Select(x => viewModel.ColumnsHeadersHeight[x])
334+
.Sum();
335+
return height;
336+
}
337+
323338
private static void DrawParentColumnHeader(
324339
this SKCanvas canvas,
325340
HierarchyGridViewModel viewModel,
@@ -613,7 +628,7 @@ private static void RenderHeaderText(
613628
double screenScale,
614629
RenderInfo renderInfo,
615630
string fontFamily = "",
616-
float headerTextSize = 16f
631+
float headerFontSize = 16f
617632
)
618633
{
619634
TextDrawer.Clear();
@@ -622,20 +637,30 @@ private static void RenderHeaderText(
622637
hdef.Content?.ToString() ?? string.Empty,
623638
new Style
624639
{
625-
FontSize = headerTextSize,
640+
FontSize = headerFontSize,
626641
TextColor = renderInfo.ForegroundColor,
627642
FontWeight = 600,
628643
FontFamily = !string.IsNullOrEmpty(fontFamily) ? fontFamily : "Sans Serif",
629644
}
630645
);
631-
TextDrawer.MaxHeight = (float)((height - 10) * screenScale);
632-
TextDrawer.MaxWidth = (float)((width - 24) * screenScale);
633646

634-
TextDrawer.Paint(
635-
canvas,
636-
new SKPoint((float)((left + 22) * screenScale), (float)((top + 6) * screenScale)),
637-
TextPaintOptions
638-
);
647+
float textVPadding = (float)(height - (headerFontSize * screenScale));
648+
float textHPadding = 22f;
649+
650+
TextDrawer.MaxHeight = (float)(height * screenScale);
651+
TextDrawer.MaxWidth = (float)((width - textHPadding) * screenScale);
652+
653+
/* We can center elements that have no child elements,
654+
otherwise the text might be out of drawn screen */
655+
float x = hdef.HasChild
656+
? (float)((left + 22) * screenScale)
657+
: (float)((left + ((width - TextDrawer.MeasuredWidth) / 2)) * screenScale);
658+
659+
float y = hdef.HasChild
660+
? (float)((top + 6) * screenScale)
661+
: (float)((top + (textVPadding / 2)) * screenScale);
662+
663+
TextDrawer.Paint(canvas, new SKPoint(x, y), TextPaintOptions);
639664
}
640665

641666
private static Option<SKPath> GetHeaderDecorator(
@@ -928,10 +953,10 @@ private static void RenderCellText(
928953
RenderInfo renderInfo
929954
)
930955
{
931-
var fontSize = viewModel.CellFontSize;
956+
float fontSize = viewModel.CellFontSize;
932957

933958
float textHPadding = (float)((6f + theme.SelectionBorderThickness) * screenScale);
934-
var textVPadding = (float)(cell.Height - (fontSize * screenScale));
959+
float textVPadding = (float)(cell.Height - (fontSize * screenScale));
935960

936961
var rightDecorPadding = (
937962
from rd in cell.ResultSet.RightDecor
@@ -968,8 +993,8 @@ select svg.Picture.CullRect.Width * screenScale
968993
TextDrawer.Paint(
969994
canvas,
970995
new SKPoint(
971-
(float)(cell.Left * screenScale) + (textHPadding / 2) + leftDecorPadding,
972-
(float)(cell.Top * screenScale) + (textVPadding / 2)
996+
(float)((cell.Left + (textHPadding / 2) + leftDecorPadding) * screenScale),
997+
(float)((cell.Top + (textVPadding / 2)) * screenScale)
973998
),
974999
TextPaintOptions
9751000
);

src/HierarchyGrid.Skia/HierarchyGridDrawer.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ public static async Task Draw(
3232
var previousGlobalCoordinates = viewModel
3333
.GlobalHeadersCoordinates.Select(t => (t.Coord, t.Guid))
3434
.ToList();
35-
35+
3636
viewModel.ClearCoordinates();
3737

3838
canvas.DrawGlobalHeaders(viewModel, theme, previousGlobalCoordinates, screenScale);
39-
39+
4040
canvas.DrawCells(
4141
viewModel,
4242
theme,
4343
viewModel.GetDrawnCells(width, height, invalidate),
4444
screenScale
4545
);
46-
46+
4747
canvas.DrawColumnHeaders(
4848
viewModel,
4949
theme,
@@ -52,7 +52,7 @@ public static async Task Draw(
5252
ref headerCount,
5353
screenScale
5454
);
55-
55+
5656
canvas.DrawRowHeaders(
5757
viewModel,
5858
theme,
@@ -71,7 +71,6 @@ public static async Task Draw(
7171
paint.IsAntialias = true;
7272
paint.Color = theme.ForegroundColor;
7373

74-
// TODO: Check under MacOS
7574
canvas.DrawText(
7675
viewModel.StatusMessage ?? "NO MESSAGE",
7776
(float)screenScale * width / 2,

0 commit comments

Comments
 (0)