ListViewWithSelection.cs 6.5 KB

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