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,25 +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- /// <summary>
29- /// Gets the number of rows in the data source.
30- /// </summary>
31- public int Length => GridViewRowList . Count ;
27+ /// <inheritdoc/>
28+ public int MaxItemLength { get ; set ; }
3229
33- /// <summary>
34- /// Gets or sets a value indicating whether to suspend raising the <see cref="CollectionChanged" /> event.
35- /// </summary>
30+ /// <inheritdoc/>
3631 public bool SuspendCollectionChangedEvent { get ; set ; }
3732
3833#pragma warning disable CS0067
39- /// <summary>
40- /// Occurs when the collection changes.
41- /// </summary>
34+ /// <inheritdoc/>
4235 public event NotifyCollectionChangedEventHandler ? CollectionChanged ;
4336#pragma warning restore CS0067
4437
@@ -51,24 +44,21 @@ public GridViewDataSource(List<GridViewRow> itemList)
5144 GridViewRowList = itemList ;
5245 }
5346
54- /// <summary>
55- /// Renders a specific item in the list view at the specified position.
56- /// </summary>
57- /// <param name="listView">The list view to render into.</param>
58- /// <param name="selected">A value indicating whether the item is selected.</param>
59- /// <param name="item">The index of the item to render.</param>
60- /// <param name="col">The column position to start rendering.</param>
61- /// <param name="line">The line position to render on.</param>
62- /// <param name="width">The width available for rendering.</param>
63- /// <param name="start">The starting position within the item's display string.</param>
64- 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 )
6549 {
66- listView . Move ( col - start , line ) ;
50+ GridViewRow ? row = GridViewRowList [ item ] ;
51+ string displayString = row ? . DisplayString ?? string . Empty ;
6752
68- var driver = listView . App ? . Driver ;
69- var row = GridViewRowList [ item ] ;
70- string displayString = row . DisplayString ?? string . Empty ;
71- // Pad right of display string with spaces to fill width
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+ } ;
60+
61+ // Pad right of display string with spaces to fill width, or truncate if too long
7262 if ( displayString . Length < width )
7363 {
7464 displayString = displayString . PadRight ( width ) ;
@@ -77,21 +67,17 @@ public void Render(ListView listView, bool selected, int item, int col, int line
7767 {
7868 displayString = displayString [ ..width ] ;
7969 }
80- driver ! . AddStr ( displayString ) ;
70+
71+ listView . AddStr ( displayString ) ;
8172 }
8273
83- /// <summary>
84- /// Determines whether the specified item is marked.
85- /// </summary>
86- /// <param name="item">The index of the item to check.</param>
87- /// <returns><see langword="true" /> if the item is marked; otherwise, <see langword="false" />.</returns>
88- public bool IsMarked ( int item ) => GridViewRowList [ item ] . IsMarked ;
74+ /// <inheritdoc/>
75+ public bool IsMarked ( int item )
76+ {
77+ return item < GridViewRowList . Count && GridViewRowList [ item ] . IsMarked ;
78+ }
8979
90- /// <summary>
91- /// Sets the marked state of the specified item and raises the <see cref="MarkChanged" /> event.
92- /// </summary>
93- /// <param name="item">The index of the item to mark or unmark.</param>
94- /// <param name="value"><see langword="true" /> to mark the item; <see langword="false" /> to unmark it.</param>
80+ /// <inheritdoc/>
9581 public void SetMark ( int item , bool value )
9682 {
9783 var oldValue = GridViewRowList [ item ] . IsMarked ;
0 commit comments