CheckBoxTableSourceWrapperByIndex.cs 1.9 KB

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