ListViewWithSelection.cs 9.1 KB

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