ListViewWithSelection.cs 9.2 KB

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