CheckBoxTableSourceWrapperByIndex.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #nullable disable
  2. namespace Terminal.Gui.Views;
  3. /// <summary>Implementation of <see cref="CheckBoxTableSourceWrapperBase"/> which records toggled rows by their row number.</summary>
  4. public class CheckBoxTableSourceWrapperByIndex : CheckBoxTableSourceWrapperBase
  5. {
  6. /// <inheritdoc/>
  7. public CheckBoxTableSourceWrapperByIndex (TableView tableView, ITableSource toWrap) : base (tableView, toWrap) { }
  8. /// <summary>
  9. /// Gets the collection of all the checked rows in the <see cref="CheckBoxTableSourceWrapperBase.Wrapping"/>
  10. /// <see cref="ITableSource"/>.
  11. /// </summary>
  12. public HashSet<int> CheckedRows { get; private set; } = new ();
  13. /// <inheritdoc/>
  14. protected override void ClearAllToggles () { CheckedRows.Clear (); }
  15. /// <inheritdoc/>
  16. protected override bool IsChecked (int row) { return CheckedRows.Contains (row); }
  17. /// <inheritdoc/>
  18. protected override void ToggleAllRows ()
  19. {
  20. if (CheckedRows.Count == Rows)
  21. {
  22. // select none
  23. ClearAllToggles ();
  24. }
  25. else
  26. {
  27. // select all
  28. CheckedRows = new HashSet<int> (Enumerable.Range (0, Rows));
  29. }
  30. }
  31. /// <inheritdoc/>
  32. protected override void ToggleRow (int row)
  33. {
  34. if (CheckedRows.Contains (row))
  35. {
  36. CheckedRows.Remove (row);
  37. }
  38. else
  39. {
  40. CheckedRows.Add (row);
  41. }
  42. }
  43. /// <inheritdoc/>
  44. protected override void ToggleRows (int [] range)
  45. {
  46. // if all are ticked untick them
  47. if (range.All (CheckedRows.Contains))
  48. {
  49. // select none
  50. foreach (int r in range)
  51. {
  52. CheckedRows.Remove (r);
  53. }
  54. }
  55. else
  56. {
  57. // otherwise tick all
  58. foreach (int r in range)
  59. {
  60. CheckedRows.Add (r);
  61. }
  62. }
  63. }
  64. }