IListDataSource.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #nullable disable
  2. using System.Collections;
  3. using System.Collections.Specialized;
  4. namespace Terminal.Gui.Views;
  5. /// <summary>
  6. /// Provides data and rendering for <see cref="ListView"/>. Implement this interface to provide custom rendering
  7. /// or to wrap custom data sources.
  8. /// </summary>
  9. /// <remarks>
  10. /// <para>
  11. /// The default implementation is <see cref="ListWrapper{T}"/> which renders items using
  12. /// <see cref="object.ToString()"/>.
  13. /// </para>
  14. /// <para>
  15. /// Implementors must manage their own marking state and raise <see cref="CollectionChanged"/> when the
  16. /// underlying data changes.
  17. /// </para>
  18. /// </remarks>
  19. public interface IListDataSource : IDisposable
  20. {
  21. /// <summary>
  22. /// Raised when items are added, removed, moved, or the entire collection is refreshed.
  23. /// </summary>
  24. /// <remarks>
  25. /// <see cref="ListView"/> subscribes to this event to update its display and content size when the data
  26. /// changes. Implementations should raise this event whenever the underlying collection changes, unless
  27. /// <see cref="SuspendCollectionChangedEvent"/> is <see langword="true"/>.
  28. /// </remarks>
  29. event NotifyCollectionChangedEventHandler CollectionChanged;
  30. /// <summary>Gets the number of items in the data source.</summary>
  31. int Count { get; }
  32. /// <summary>Determines whether the specified item is marked.</summary>
  33. /// <param name="item">The zero-based index of the item.</param>
  34. /// <returns><see langword="true"/> if the item is marked; otherwise <see langword="false"/>.</returns>
  35. /// <remarks>
  36. /// <see cref="ListView"/> calls this method to determine whether to render the item with a mark indicator when
  37. /// <see cref="ListView.AllowsMarking"/> is <see langword="true"/>.
  38. /// </remarks>
  39. bool IsMarked (int item);
  40. /// <summary>Gets the width in columns of the widest item in the data source.</summary>
  41. /// <remarks>
  42. /// <see cref="ListView"/> uses this value to set its horizontal content size for scrolling.
  43. /// </remarks>
  44. int Length { get; }
  45. /// <summary>Renders the specified item to the <see cref="ListView"/>.</summary>
  46. /// <param name="listView">The <see cref="ListView"/> to render to.</param>
  47. /// <param name="selected">
  48. /// <see langword="true"/> if the item is currently selected; otherwise <see langword="false"/>.
  49. /// </param>
  50. /// <param name="item">The zero-based index of the item to render.</param>
  51. /// <param name="col">The column in <paramref name="listView"/> where rendering starts.</param>
  52. /// <param name="line">The line in <paramref name="listView"/> where rendering occurs.</param>
  53. /// <param name="width">The width available for rendering.</param>
  54. /// <param name="viewportX">The horizontal scroll offset.</param>
  55. /// <remarks>
  56. /// <para>
  57. /// <see cref="ListView"/> calls this method for each visible item during rendering. The color scheme will be
  58. /// set based on selection state before this method is called.
  59. /// </para>
  60. /// <para>
  61. /// Implementations must fill the entire <paramref name="width"/> to avoid rendering artifacts.
  62. /// </para>
  63. /// </remarks>
  64. void Render (
  65. ListView listView,
  66. bool selected,
  67. int item,
  68. int col,
  69. int line,
  70. int width,
  71. int viewportX = 0
  72. );
  73. /// <summary>Sets the marked state of the specified item.</summary>
  74. /// <param name="item">The zero-based index of the item.</param>
  75. /// <param name="value"><see langword="true"/> to mark the item; <see langword="false"/> to unmark it.</param>
  76. /// <remarks>
  77. /// <see cref="ListView"/> calls this method when the user toggles marking (e.g., via the SPACE key) if
  78. /// <see cref="ListView.AllowsMarking"/> is <see langword="true"/>.
  79. /// </remarks>
  80. void SetMark (int item, bool value);
  81. /// <summary>
  82. /// Gets or sets whether the <see cref="CollectionChanged"/> event should be suppressed.
  83. /// </summary>
  84. /// <remarks>
  85. /// Set to <see langword="true"/> to prevent <see cref="CollectionChanged"/> from being raised during bulk
  86. /// operations. Set back to <see langword="false"/> to resume event notifications.
  87. /// </remarks>
  88. bool SuspendCollectionChangedEvent { get; set; }
  89. /// <summary>Returns the underlying data source as an <see cref="IList"/>.</summary>
  90. /// <returns>The data source as an <see cref="IList"/>.</returns>
  91. /// <remarks>
  92. /// <see cref="ListView"/> uses this method to access individual items for events like
  93. /// <see cref="ListView.SelectedItemChanged"/> and to enable keyboard search via
  94. /// <see cref="ListView.KeystrokeNavigator"/>.
  95. /// </remarks>
  96. IList ToList ();
  97. }