ListViewWithSelection.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using NStack;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Terminal.Gui;
  7. namespace UICatalog {
  8. [ScenarioMetadata (Name: "List View With Selection", Description: "ListView with colunns and selection")]
  9. [ScenarioCategory ("Controls")]
  10. class ListViewWithSelection : Scenario {
  11. public CheckBox _customRenderCB;
  12. public CheckBox _allowMarkingCB;
  13. public CheckBox _allowMultipleCB;
  14. public ListView _listView;
  15. public List<Type> _scenarios = Scenario.GetDerivedClasses<Scenario>().OrderBy (t => Scenario.ScenarioMetadata.GetName (t)).ToList ();
  16. public override void Setup ()
  17. {
  18. _customRenderCB = new CheckBox ("Render with columns") {
  19. X = 0,
  20. Y = 0,
  21. Height = 1,
  22. };
  23. Win.Add (_customRenderCB);
  24. _customRenderCB.Toggled += _customRenderCB_Toggled; ;
  25. _allowMarkingCB = new CheckBox ("Allow Marking") {
  26. X = Pos.Right (_customRenderCB) + 1,
  27. Y = 0,
  28. Height = 1,
  29. };
  30. Win.Add (_allowMarkingCB);
  31. _allowMarkingCB.Toggled += AllowMarkingCB_Toggled;
  32. _allowMultipleCB = new CheckBox ("Allow Multi-Select") {
  33. X = Pos.Right (_allowMarkingCB) + 1,
  34. Y = 0,
  35. Height = 1,
  36. Visible = _allowMarkingCB.Checked
  37. };
  38. Win.Add (_allowMultipleCB);
  39. _allowMultipleCB.Toggled += AllowMultipleCB_Toggled;
  40. _listView = new ListView () {
  41. X = 1,
  42. Y = 2,
  43. Height = Dim.Fill (),
  44. Width = Dim.Fill (1),
  45. //ColorScheme = Colors.TopLevel,
  46. AllowsMarking = false,
  47. AllowsMultipleSelection = false
  48. };
  49. Win.Add (_listView);
  50. _listView.SetSource (_scenarios);
  51. }
  52. private void _customRenderCB_Toggled (bool prev)
  53. {
  54. if (prev) {
  55. _listView.SetSource (_scenarios);
  56. } else {
  57. _listView.Source = new ScenarioListDataSource (_scenarios);
  58. }
  59. Win.SetNeedsDisplay ();
  60. }
  61. private void AllowMarkingCB_Toggled (bool prev)
  62. {
  63. _listView.AllowsMarking = !prev;
  64. _allowMultipleCB.Visible = _listView.AllowsMarking;
  65. Win.SetNeedsDisplay ();
  66. }
  67. private void AllowMultipleCB_Toggled (bool prev)
  68. {
  69. _listView.AllowsMultipleSelection = !prev;
  70. Win.SetNeedsDisplay ();
  71. }
  72. // This is basicaly the same implementation used by the UICatalog main window
  73. internal class ScenarioListDataSource : IListDataSource {
  74. int _nameColumnWidth = 30;
  75. private List<Type> scenarios;
  76. BitArray marks;
  77. int count;
  78. public List<Type> Scenarios {
  79. get => scenarios;
  80. set {
  81. if (value != null) {
  82. count = value.Count;
  83. marks = new BitArray (count);
  84. scenarios = value;
  85. }
  86. }
  87. }
  88. public bool IsMarked (int item)
  89. {
  90. if (item >= 0 && item < count)
  91. return marks [item];
  92. return false;
  93. }
  94. public int Count => Scenarios != null ? Scenarios.Count : 0;
  95. public ScenarioListDataSource (List<Type> itemList) => Scenarios = itemList;
  96. public void Render (ListView container, ConsoleDriver driver, bool selected, int item, int col, int line, int width)
  97. {
  98. container.Move (col, line);
  99. // Equivalent to an interpolated string like $"{Scenarios[item].Name, -widtestname}"; if such a thing were possible
  100. var s = String.Format (String.Format ("{{0,{0}}}", -_nameColumnWidth), Scenario.ScenarioMetadata.GetName (Scenarios [item]));
  101. RenderUstr (driver, $"{s} {Scenario.ScenarioMetadata.GetDescription (Scenarios [item])}", col, line, width);
  102. }
  103. public void SetMark (int item, bool value)
  104. {
  105. if (item >= 0 && item < count)
  106. marks [item] = value;
  107. }
  108. // A slightly adapted method from: https://github.com/migueldeicaza/gui.cs/blob/fc1faba7452ccbdf49028ac49f0c9f0f42bbae91/Terminal.Gui/Views/ListView.cs#L433-L461
  109. private void RenderUstr (ConsoleDriver driver, ustring ustr, int col, int line, int width)
  110. {
  111. int used = 0;
  112. int index = 0;
  113. while (index < ustr.Length) {
  114. (var rune, var size) = Utf8.DecodeRune (ustr, index, index - ustr.Length);
  115. var count = Rune.ColumnWidth (rune);
  116. if (used + count >= width) break;
  117. driver.AddRune (rune);
  118. used += count;
  119. index += size;
  120. }
  121. while (used < width) {
  122. driver.AddRune (' ');
  123. used++;
  124. }
  125. }
  126. public IList ToList ()
  127. {
  128. return Scenarios;
  129. }
  130. }
  131. }
  132. }