Skip to content

Commit bbbc054

Browse files
committed
Refactor grid view data/layout; remove GridViewDetails
- Remove GridViewDetails; manage column widths/layout in OutGridViewWindow - Refactor GridViewDataSource: simplify IListDataSource, add viewportX to Render, make MaxItemLength settable - Replace _listViewSource with _filteredSource for clarity; update filtering and event logic - Ensure headers and column widths update with data/filter changes - Remove redundant code and reflection; improve naming and style - Update Header view for clarity; update launchSettings.json debug profiles - Comment out Terminal.Gui package reference for manual management
2 parents fa17dff + 627ae55 commit bbbc054

6 files changed

Lines changed: 74 additions & 144 deletions

File tree

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
<PackageVersion Include="Microsoft.PowerShell.SDK" Version="7.4.7" />
44
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
55
<PackageVersion Include="System.Management.Automation" Version="7.4.7" />
6-
<PackageVersion Include="Terminal.Gui" Version="2.0.0-alpha.3390" />
6+
<!--<PackageVersion Include="Terminal.Gui" Version="2.0.0-develop.4947" />-->
77
</ItemGroup>
88
</Project>

src/Microsoft.PowerShell.ConsoleGuiTools/GridViewDataSource.cs

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections;
66
using System.Collections.Generic;
77
using System.Collections.Specialized;
8+
using System.Linq;
89
using Terminal.Gui.App;
910
using Terminal.Gui.Views;
1011

@@ -20,23 +21,17 @@ internal sealed class GridViewDataSource : IListDataSource
2021
/// </summary>
2122
public List<GridViewRow> GridViewRowList { get; set; }
2223

23-
/// <summary>
24-
/// Gets the number of rows in the data source.
25-
/// </summary>
24+
/// <inheritdoc/>
2625
public int Count => GridViewRowList.Count;
2726

28-
/// <inheritdoc />
29-
public int MaxItemLength { get; }
27+
/// <inheritdoc/>
28+
public int MaxItemLength { get; set; }
3029

31-
/// <summary>
32-
/// Gets or sets a value indicating whether to suspend raising the <see cref="CollectionChanged" /> event.
33-
/// </summary>
30+
/// <inheritdoc/>
3431
public bool SuspendCollectionChangedEvent { get; set; }
3532

3633
#pragma warning disable CS0067
37-
/// <summary>
38-
/// Occurs when the collection changes.
39-
/// </summary>
34+
/// <inheritdoc/>
4035
public event NotifyCollectionChangedEventHandler? CollectionChanged;
4136
#pragma warning restore CS0067
4237

@@ -49,24 +44,21 @@ public GridViewDataSource(List<GridViewRow> itemList)
4944
GridViewRowList = itemList;
5045
}
5146

52-
/// <summary>
53-
/// Renders a specific item in the list view at the specified position.
54-
/// </summary>
55-
/// <param name="listView">The list view to render into.</param>
56-
/// <param name="selected">A value indicating whether the item is selected.</param>
57-
/// <param name="item">The index of the item to render.</param>
58-
/// <param name="col">The column position to start rendering.</param>
59-
/// <param name="line">The line position to render on.</param>
60-
/// <param name="width">The width available for rendering.</param>
61-
/// <param name="start">The starting position within the item's display string.</param>
62-
public void Render(ListView listView, bool selected, int item, int col, int line, int width, int start = 0)
47+
/// <inheritdoc/>
48+
public void Render(ListView listView, bool selected, int item, int col, int line, int width, int viewportX)
6349
{
64-
listView.Move(col - start, line);
50+
GridViewRow? row = GridViewRowList[item];
51+
string displayString = row?.DisplayString ?? string.Empty;
52+
53+
displayString = viewportX switch
54+
{
55+
// Truncate the start of the string to skip characters scrolled out of view
56+
> 0 when displayString.Length > viewportX => displayString[viewportX..],
57+
> 0 when displayString.Length <= viewportX => string.Empty,
58+
_ => displayString
59+
};
6560

66-
var driver = listView.App?.Driver;
67-
var row = GridViewRowList[item];
68-
string displayString = row.DisplayString ?? string.Empty;
69-
// Pad right of display string with spaces to fill width
61+
// Pad right of display string with spaces to fill width, or truncate if too long
7062
if (displayString.Length < width)
7163
{
7264
displayString = displayString.PadRight(width);
@@ -75,28 +67,17 @@ public void Render(ListView listView, bool selected, int item, int col, int line
7567
{
7668
displayString = displayString[..width];
7769
}
78-
driver!.AddStr(displayString);
70+
71+
listView.AddStr(displayString);
7972
}
8073

81-
/// <summary>
82-
/// Determines whether the specified item is marked.
83-
/// </summary>
84-
/// <param name="item">The index of the item to check.</param>
85-
/// <returns><see langword="true" /> if the item is marked; otherwise, <see langword="false" />.</returns>
74+
/// <inheritdoc/>
8675
public bool IsMarked(int item)
8776
{
88-
if (item < 0 || item >= GridViewRowList.Count)
89-
{
90-
return false;
91-
}
92-
return GridViewRowList[item].IsMarked;
77+
return item < GridViewRowList.Count && GridViewRowList[item].IsMarked;
9378
}
9479

95-
/// <summary>
96-
/// Sets the marked state of the specified item and raises the <see cref="MarkChanged" /> event.
97-
/// </summary>
98-
/// <param name="item">The index of the item to mark or unmark.</param>
99-
/// <param name="value"><see langword="true" /> to mark the item; <see langword="false" /> to unmark it.</param>
80+
/// <inheritdoc/>
10081
public void SetMark(int item, bool value)
10182
{
10283
var oldValue = GridViewRowList[item].IsMarked;
@@ -143,4 +124,4 @@ public void Dispose()
143124
{
144125
// No resources to dispose currently
145126
}
146-
}
127+
}

src/Microsoft.PowerShell.ConsoleGuiTools/GridViewDetails.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/Microsoft.PowerShell.ConsoleGuiTools/Header.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
// Licensed under the MIT License.
33

44
using System.Collections.Generic;
5-
using System.Reflection;
6-
using System.Reflection.PortableExecutable;
75
using Terminal.Gui.Drawing;
86
using Terminal.Gui.ViewBase;
97
using Terminal.Gui.Views;
108

119
namespace Microsoft.PowerShell.ConsoleGuiTools;
1210

1311
/// <summary>
14-
/// A specialized view for displaying grid column headers with individual subviews for each column.
12+
/// A specialized view for displaying grid column headers with individual subviews for each column. This is added to
13+
/// the top of the Padding of the ListView.
1514
/// </summary>
1615
internal sealed class Header : View
1716
{
@@ -28,21 +27,20 @@ public override void EndInit()
2827

2928

3029
// We are a subview of the ListView.Padding.
31-
if (SuperView is Padding padding)
32-
{
33-
padding.Parent?.ViewportChanged += ListViewOnViewportChanged;
34-
}
30+
if (SuperView is Padding padding) padding.Parent?.ViewportChanged += ListViewOnViewportChanged;
31+
32+
return;
3533

3634
void ListViewOnViewportChanged(object? sender, DrawEventArgs e)
3735
{
38-
if (sender is ListView listView)
36+
if (sender is ListView listView)
3937
Viewport = Viewport with { X = listView.Viewport.X };
4038
}
4139
}
4240

4341
protected override void OnSubViewLayout(LayoutEventArgs args)
4442
{
45-
if (SuperView is Padding { Parent: ListView listView })
43+
if (SuperView is Padding { Parent: ListView listView })
4644
SetContentSize(GetContentSize() with { Width = listView.GetContentSize().Width });
4745

4846
base.OnSubViewLayout(args);

0 commit comments

Comments
 (0)