ListViewWithSelection.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.Scenarios {
  8. [ScenarioMetadata (Name: "List View With Selection", Description: "ListView with columns and selection")]
  9. [ScenarioCategory ("Controls")]
  10. public 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. var _scrollBar = new ScrollBarView (_listView, true);
  51. _scrollBar.ChangedPosition += () => {
  52. _listView.TopItem = _scrollBar.Position;
  53. if (_listView.TopItem != _scrollBar.Position) {
  54. _scrollBar.Position = _listView.TopItem;
  55. }
  56. _listView.SetNeedsDisplay ();
  57. };
  58. _scrollBar.OtherScrollBarView.ChangedPosition += () => {
  59. _listView.LeftItem = _scrollBar.OtherScrollBarView.Position;
  60. if (_listView.LeftItem != _scrollBar.OtherScrollBarView.Position) {
  61. _scrollBar.OtherScrollBarView.Position = _listView.LeftItem;
  62. }
  63. _listView.SetNeedsDisplay ();
  64. };
  65. _listView.DrawContent += (e) => {
  66. _scrollBar.Size = _listView.Source.Count - 1;
  67. _scrollBar.Position = _listView.TopItem;
  68. _scrollBar.OtherScrollBarView.Size = _listView.Maxlength - 1;
  69. _scrollBar.OtherScrollBarView.Position = _listView.LeftItem;
  70. _scrollBar.Refresh ();
  71. };
  72. _listView.SetSource (_scenarios);
  73. var k = "Keep Content Always In Viewport";
  74. var keepCheckBox = new CheckBox (k, _scrollBar.AutoHideScrollBars) {
  75. X = Pos.AnchorEnd (k.Length + 3),
  76. Y = 0,
  77. };
  78. keepCheckBox.Toggled += (_) => _scrollBar.KeepContentAlwaysInViewport = keepCheckBox.Checked;
  79. Win.Add (keepCheckBox);
  80. }
  81. private void _customRenderCB_Toggled (bool prev)
  82. {
  83. if (prev) {
  84. _listView.SetSource (_scenarios);
  85. } else {
  86. _listView.Source = new ScenarioListDataSource (_scenarios);
  87. }
  88. Win.SetNeedsDisplay ();
  89. }
  90. private void AllowMarkingCB_Toggled (bool prev)
  91. {
  92. _listView.AllowsMarking = !prev;
  93. _allowMultipleCB.Visible = _listView.AllowsMarking;
  94. Win.SetNeedsDisplay ();
  95. }
  96. private void AllowMultipleCB_Toggled (bool prev)
  97. {
  98. _listView.AllowsMultipleSelection = !prev;
  99. Win.SetNeedsDisplay ();
  100. }
  101. // This is basically the same implementation used by the UICatalog main window
  102. internal class ScenarioListDataSource : IListDataSource {
  103. int _nameColumnWidth = 30;
  104. private List<Type> scenarios;
  105. BitArray marks;
  106. int count, len;
  107. public List<Type> Scenarios {
  108. get => scenarios;
  109. set {
  110. if (value != null) {
  111. count = value.Count;
  112. marks = new BitArray (count);
  113. scenarios = value;
  114. len = GetMaxLengthItem ();
  115. }
  116. }
  117. }
  118. public bool IsMarked (int item)
  119. {
  120. if (item >= 0 && item < count)
  121. return marks [item];
  122. return false;
  123. }
  124. public int Count => Scenarios != null ? Scenarios.Count : 0;
  125. public int Length => len;
  126. public ScenarioListDataSource (List<Type> itemList) => Scenarios = itemList;
  127. public void Render (ListView container, ConsoleDriver driver, bool selected, int item, int col, int line, int width, int start = 0)
  128. {
  129. container.Move (col, line);
  130. // Equivalent to an interpolated string like $"{Scenarios[item].Name, -widtestname}"; if such a thing were possible
  131. var s = String.Format (String.Format ("{{0,{0}}}", -_nameColumnWidth), Scenario.ScenarioMetadata.GetName (Scenarios [item]));
  132. RenderUstr (driver, $"{s} {Scenario.ScenarioMetadata.GetDescription (Scenarios [item])}", col, line, width, start);
  133. }
  134. public void SetMark (int item, bool value)
  135. {
  136. if (item >= 0 && item < count)
  137. marks [item] = value;
  138. }
  139. int GetMaxLengthItem ()
  140. {
  141. if (scenarios?.Count == 0) {
  142. return 0;
  143. }
  144. int maxLength = 0;
  145. for (int i = 0; i < scenarios.Count; i++) {
  146. var s = String.Format (String.Format ("{{0,{0}}}", -_nameColumnWidth), Scenario.ScenarioMetadata.GetName (Scenarios [i]));
  147. var sc = $"{s} {Scenario.ScenarioMetadata.GetDescription (Scenarios [i])}";
  148. var l = sc.Length;
  149. if (l > maxLength) {
  150. maxLength = l;
  151. }
  152. }
  153. return maxLength;
  154. }
  155. // A slightly adapted method from: https://github.com/migueldeicaza/gui.cs/blob/fc1faba7452ccbdf49028ac49f0c9f0f42bbae91/Terminal.Gui/Views/ListView.cs#L433-L461
  156. private void RenderUstr (ConsoleDriver driver, ustring ustr, int col, int line, int width, int start = 0)
  157. {
  158. int used = 0;
  159. int index = start;
  160. while (index < ustr.Length) {
  161. (var rune, var size) = Utf8.DecodeRune (ustr, index, index - ustr.Length);
  162. var count = Rune.ColumnWidth (rune);
  163. if (used + count >= width) break;
  164. driver.AddRune (rune);
  165. used += count;
  166. index += size;
  167. }
  168. while (used < width) {
  169. driver.AddRune (' ');
  170. used++;
  171. }
  172. }
  173. public IList ToList ()
  174. {
  175. return Scenarios;
  176. }
  177. }
  178. }
  179. }