Skip to content

Commit 28e5c75

Browse files
authored
GridColumnManager improvements (#5433)
The previous code had two issues that are addressed here: - Inconsistent use of string comparers for column names. - O(N) scan through the column collection for lookups. This PR uses a dictionary for columns, which ensures consistent comparer use, and gives O(1) lookup.
1 parent 25df905 commit 28e5c75

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed
Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,57 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Text;
45
using Aspire.Dashboard.Components.Resize;
56
using Aspire.Dashboard.Model;
67

78
namespace Aspire.Dashboard.Components;
89

9-
public class GridColumnManager
10+
public class GridColumnManager(IEnumerable<GridColumn> columns, DimensionManager dimensionManager)
1011
{
11-
private readonly GridColumn[] _columns;
12-
private readonly DimensionManager _dimensionManager;
12+
private readonly Dictionary<string, GridColumn> _columnById
13+
= columns.ToDictionary(c => c.Name, StringComparers.GridColumn);
1314

14-
public GridColumnManager(GridColumn[] columns, DimensionManager dimensionManager)
15+
/// <summary>
16+
/// Gets whether the column is known, visible, and has a width for the current viewport.
17+
/// </summary>
18+
public bool IsColumnVisible(string columnName)
1519
{
16-
if (columns.DistinctBy(c => c.Name, StringComparers.GridColumn).Count() != columns.Length)
17-
{
18-
throw new InvalidOperationException("There are duplicate columns");
19-
}
20-
21-
_columns = columns;
22-
_dimensionManager = dimensionManager;
20+
return _columnById.TryGetValue(columnName, out var column) // Is a known column.
21+
&& GetColumnWidth(column) is not null // Has width for current viewport.
22+
&& column.IsVisible?.Invoke() is null or true; // Is visible.
2323
}
2424

25-
public bool IsColumnVisible(string columnId)
26-
{
27-
return GetColumnWidth(_columns.First(column => column.Name == columnId)) is not null;
28-
}
29-
30-
private string? GetColumnWidth(GridColumn column)
25+
/// <summary>
26+
/// Gets a string that can be used as the value for the grid-template-columns CSS property.
27+
/// For example, <c>1fr 2fr 1fr</c>.
28+
/// </summary>
29+
/// <returns></returns>
30+
public string GetGridTemplateColumns()
3131
{
32-
if (column.IsVisible is not null && !column.IsVisible())
33-
{
34-
return null;
35-
}
32+
StringBuilder sb = new();
3633

37-
if (_dimensionManager.ViewportInformation.IsDesktop)
34+
foreach (var (_, column) in _columnById)
3835
{
39-
return column.DesktopWidth;
36+
if (column.IsVisible?.Invoke() is null or true &&
37+
GetColumnWidth(column) is string width)
38+
{
39+
if (sb.Length > 0)
40+
{
41+
sb.Append(' ');
42+
}
43+
44+
sb.Append(width);
45+
}
4046
}
4147

42-
return column.MobileWidth;
48+
return sb.ToString();
4349
}
4450

45-
public string GetGridTemplateColumns()
51+
private string? GetColumnWidth(GridColumn column)
4652
{
47-
var visibleColumns = _columns
48-
.Select(GetColumnWidth)
49-
.Where(s => s is not null)
50-
.Select(s => s!);
51-
52-
return string.Join(" ", visibleColumns);
53+
return dimensionManager.ViewportInformation.IsDesktop
54+
? column.DesktopWidth
55+
: column.MobileWidth;
5356
}
5457
}

0 commit comments

Comments
 (0)