ListViewWithSelection.cs 6.4 KB

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