ListViewWithSelection.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using NStack;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Terminal.Gui;
  7. using Attribute = Terminal.Gui.Attribute;
  8. namespace UICatalog.Scenarios {
  9. [ScenarioMetadata (Name: "List View With Selection", Description: "ListView with columns and selection")]
  10. [ScenarioCategory ("Controls"), ScenarioCategory ("ListView")]
  11. public class ListViewWithSelection : Scenario {
  12. public CheckBox _customRenderCB;
  13. public CheckBox _allowMarkingCB;
  14. public CheckBox _allowMultipleCB;
  15. public ListView _listView;
  16. public List<Type> _scenarios = Scenario.GetDerivedClasses<Scenario>().OrderBy (t => Scenario.ScenarioMetadata.GetName (t)).ToList ();
  17. public override void Setup ()
  18. {
  19. _customRenderCB = new CheckBox ("Render with columns") {
  20. X = 0,
  21. Y = 0,
  22. Height = 1,
  23. };
  24. Win.Add (_customRenderCB);
  25. _customRenderCB.Toggled += _customRenderCB_Toggled;
  26. _allowMarkingCB = new CheckBox ("Allow Marking") {
  27. X = Pos.Right (_customRenderCB) + 1,
  28. Y = 0,
  29. Height = 1,
  30. };
  31. Win.Add (_allowMarkingCB);
  32. _allowMarkingCB.Toggled += AllowMarkingCB_Toggled;
  33. _allowMultipleCB = new CheckBox ("Allow Multi-Select") {
  34. X = Pos.Right (_allowMarkingCB) + 1,
  35. Y = 0,
  36. Height = 1,
  37. Visible = _allowMarkingCB.Checked
  38. };
  39. Win.Add (_allowMultipleCB);
  40. _allowMultipleCB.Toggled += AllowMultipleCB_Toggled;
  41. _listView = new ListView () {
  42. X = 1,
  43. Y = 2,
  44. Height = Dim.Fill (),
  45. Width = Dim.Fill (1),
  46. //ColorScheme = Colors.TopLevel,
  47. AllowsMarking = false,
  48. AllowsMultipleSelection = false
  49. };
  50. _listView.RowRender += ListView_RowRender;
  51. Win.Add (_listView);
  52. var _scrollBar = new ScrollBarView (_listView, true);
  53. _scrollBar.ChangedPosition += () => {
  54. _listView.TopItem = _scrollBar.Position;
  55. if (_listView.TopItem != _scrollBar.Position) {
  56. _scrollBar.Position = _listView.TopItem;
  57. }
  58. _listView.SetNeedsDisplay ();
  59. };
  60. _scrollBar.OtherScrollBarView.ChangedPosition += () => {
  61. _listView.LeftItem = _scrollBar.OtherScrollBarView.Position;
  62. if (_listView.LeftItem != _scrollBar.OtherScrollBarView.Position) {
  63. _scrollBar.OtherScrollBarView.Position = _listView.LeftItem;
  64. }
  65. _listView.SetNeedsDisplay ();
  66. };
  67. _listView.DrawContent += (e) => {
  68. _scrollBar.Size = _listView.Source.Count - 1;
  69. _scrollBar.Position = _listView.TopItem;
  70. _scrollBar.OtherScrollBarView.Size = _listView.Maxlength - 1;
  71. _scrollBar.OtherScrollBarView.Position = _listView.LeftItem;
  72. _scrollBar.Refresh ();
  73. };
  74. _listView.SetSource (_scenarios);
  75. var k = "Keep Content Always In Viewport";
  76. var keepCheckBox = new CheckBox (k, _scrollBar.AutoHideScrollBars) {
  77. X = Pos.AnchorEnd (k.Length + 3),
  78. Y = 0,
  79. };
  80. keepCheckBox.Toggled += (_) => _scrollBar.KeepContentAlwaysInViewport = keepCheckBox.Checked;
  81. Win.Add (keepCheckBox);
  82. }
  83. private void ListView_RowRender (ListViewRowEventArgs obj)
  84. {
  85. if (obj.Row == _listView.SelectedItem) {
  86. return;
  87. }
  88. if (_listView.AllowsMarking && _listView.Source.IsMarked (obj.Row)) {
  89. obj.RowAttribute = new Attribute (Color.BrightRed, Color.BrightYellow);
  90. return;
  91. }
  92. if (obj.Row % 2 == 0) {
  93. obj.RowAttribute = new Attribute (Color.BrightGreen, Color.Magenta);
  94. } else {
  95. obj.RowAttribute = new Attribute (Color.BrightMagenta, Color.Green);
  96. }
  97. }
  98. private void _customRenderCB_Toggled (bool prev)
  99. {
  100. if (prev) {
  101. _listView.SetSource (_scenarios);
  102. } else {
  103. _listView.Source = new ScenarioListDataSource (_scenarios);
  104. }
  105. Win.SetNeedsDisplay ();
  106. }
  107. private void AllowMarkingCB_Toggled (bool prev)
  108. {
  109. _listView.AllowsMarking = !prev;
  110. _allowMultipleCB.Visible = _listView.AllowsMarking;
  111. Win.SetNeedsDisplay ();
  112. }
  113. private void AllowMultipleCB_Toggled (bool prev)
  114. {
  115. _listView.AllowsMultipleSelection = !prev;
  116. Win.SetNeedsDisplay ();
  117. }
  118. // This is basically the same implementation used by the UICatalog main window
  119. internal class ScenarioListDataSource : IListDataSource {
  120. int _nameColumnWidth = 30;
  121. private List<Type> scenarios;
  122. BitArray marks;
  123. int count, len;
  124. public List<Type> Scenarios {
  125. get => scenarios;
  126. set {
  127. if (value != null) {
  128. count = value.Count;
  129. marks = new BitArray (count);
  130. scenarios = value;
  131. len = GetMaxLengthItem ();
  132. }
  133. }
  134. }
  135. public bool IsMarked (int item)
  136. {
  137. if (item >= 0 && item < count)
  138. return marks [item];
  139. return false;
  140. }
  141. public int Count => Scenarios != null ? Scenarios.Count : 0;
  142. public int Length => len;
  143. public ScenarioListDataSource (List<Type> itemList) => Scenarios = itemList;
  144. public void Render (ListView container, ConsoleDriver driver, bool selected, int item, int col, int line, int width, int start = 0)
  145. {
  146. container.Move (col, line);
  147. // Equivalent to an interpolated string like $"{Scenarios[item].Name, -widtestname}"; if such a thing were possible
  148. var s = String.Format (String.Format ("{{0,{0}}}", -_nameColumnWidth), Scenario.ScenarioMetadata.GetName (Scenarios [item]));
  149. RenderUstr (driver, $"{s} {Scenario.ScenarioMetadata.GetDescription (Scenarios [item])}", col, line, width, start);
  150. }
  151. public void SetMark (int item, bool value)
  152. {
  153. if (item >= 0 && item < count)
  154. marks [item] = value;
  155. }
  156. int GetMaxLengthItem ()
  157. {
  158. if (scenarios?.Count == 0) {
  159. return 0;
  160. }
  161. int maxLength = 0;
  162. for (int i = 0; i < scenarios.Count; i++) {
  163. var s = String.Format (String.Format ("{{0,{0}}}", -_nameColumnWidth), Scenario.ScenarioMetadata.GetName (Scenarios [i]));
  164. var sc = $"{s} {Scenario.ScenarioMetadata.GetDescription (Scenarios [i])}";
  165. var l = sc.Length;
  166. if (l > maxLength) {
  167. maxLength = l;
  168. }
  169. }
  170. return maxLength;
  171. }
  172. // A slightly adapted method from: https://github.com/gui-cs/Terminal.Gui/blob/fc1faba7452ccbdf49028ac49f0c9f0f42bbae91/Terminal.Gui/Views/ListView.cs#L433-L461
  173. private void RenderUstr (ConsoleDriver driver, ustring ustr, int col, int line, int width, int start = 0)
  174. {
  175. int used = 0;
  176. int index = start;
  177. while (index < ustr.Length) {
  178. (var rune, var size) = Utf8.DecodeRune (ustr, index, index - ustr.Length);
  179. var count = Rune.ColumnWidth (rune);
  180. if (used + count >= width) break;
  181. driver.AddRune (rune);
  182. used += count;
  183. index += size;
  184. }
  185. while (used < width) {
  186. driver.AddRune (' ');
  187. used++;
  188. }
  189. }
  190. public IList ToList ()
  191. {
  192. return Scenarios;
  193. }
  194. }
  195. }
  196. }