55using System . Collections ;
66using System . Collections . Generic ;
77using System . Collections . Specialized ;
8+ using System . Linq ;
89using Terminal . Gui . App ;
910using 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+ }
0 commit comments