ListViewWithSelection.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using NStack;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Terminal.Gui;
  7. namespace UICatalog {
  8. [ScenarioMetadata (Name: "List View With Selection", Description: "ListView with columns and selection")]
  9. [ScenarioCategory ("Controls")]
  10. class ListViewWithSelection : Scenario {
  11. public CheckBox _customRenderCB;
  12. public CheckBox _allowMarkingCB;
  13. public CheckBox _allowMultipleCB;
  14. public ListView _listView;
  15. public List<Type> _scenarios = Scenario.GetDerivedClasses<Scenario>().OrderBy (t => Scenario.ScenarioMetadata.GetName (t)).ToList ();
  16. public override void Setup ()
  17. {
  18. _customRenderCB = new CheckBox ("Render with columns") {
  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 = _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.TopLevel,
  46. AllowsMarking = false,
  47. AllowsMultipleSelection = false
  48. };
  49. Win.Add (_listView);
  50. var vertical = new ScrollBarView (_listView, true);
  51. vertical.ChangedPosition += () => {
  52. _listView.TopItem = vertical.Position;
  53. if (_listView.TopItem != vertical.Position) {
  54. vertical.Position = _listView.TopItem;
  55. }
  56. _listView.SetNeedsDisplay ();
  57. };
  58. _listView.DrawContent += (e) => {
  59. vertical.Size = _listView.Source.Count;
  60. vertical.Position = _listView.TopItem;
  61. vertical.ColorScheme = _listView.ColorScheme;
  62. if (vertical.ShowScrollIndicator) {
  63. vertical.Redraw (e);
  64. }
  65. };
  66. _listView.SetSource (_scenarios);
  67. var k = "Keep Content Always In Viewport";
  68. var keepCheckBox = new CheckBox (k, vertical.AutoHideScrollBars) {
  69. X = Pos.AnchorEnd (k.Length + 3),
  70. Y = 0,
  71. };
  72. keepCheckBox.Toggled += (_) => vertical.KeepContentAlwaysInViewport = keepCheckBox.Checked;
  73. Win.Add (keepCheckBox);
  74. }
  75. private void _customRenderCB_Toggled (bool prev)
  76. {
  77. if (prev) {
  78. _listView.SetSource (_scenarios);
  79. } else {
  80. _listView.Source = new ScenarioListDataSource (_scenarios);
  81. }
  82. Win.SetNeedsDisplay ();
  83. }
  84. private void AllowMarkingCB_Toggled (bool prev)
  85. {
  86. _listView.AllowsMarking = !prev;
  87. _allowMultipleCB.Visible = _listView.AllowsMarking;
  88. Win.SetNeedsDisplay ();
  89. }
  90. private void AllowMultipleCB_Toggled (bool prev)
  91. {
  92. _listView.AllowsMultipleSelection = !prev;
  93. Win.SetNeedsDisplay ();
  94. }
  95. // This is basically the same implementation used by the UICatalog main window
  96. internal class ScenarioListDataSource : IListDataSource {
  97. int _nameColumnWidth = 30;
  98. private List<Type> scenarios;
  99. BitArray marks;
  100. int count;
  101. public List<Type> Scenarios {
  102. get => scenarios;
  103. set {
  104. if (value != null) {
  105. count = value.Count;
  106. marks = new BitArray (count);
  107. scenarios = value;
  108. }
  109. }
  110. }
  111. public bool IsMarked (int item)
  112. {
  113. if (item >= 0 && item < count)
  114. return marks [item];
  115. return false;
  116. }
  117. public int Count => Scenarios != null ? Scenarios.Count : 0;
  118. public ScenarioListDataSource (List<Type> itemList) => Scenarios = itemList;
  119. public void Render (ListView container, ConsoleDriver driver, bool selected, int item, int col, int line, int width)
  120. {
  121. container.Move (col, line);
  122. // Equivalent to an interpolated string like $"{Scenarios[item].Name, -widtestname}"; if such a thing were possible
  123. var s = String.Format (String.Format ("{{0,{0}}}", -_nameColumnWidth), Scenario.ScenarioMetadata.GetName (Scenarios [item]));
  124. RenderUstr (driver, $"{s} {Scenario.ScenarioMetadata.GetDescription (Scenarios [item])}", col, line, width);
  125. }
  126. public void SetMark (int item, bool value)
  127. {
  128. if (item >= 0 && item < count)
  129. marks [item] = value;
  130. }
  131. // A slightly adapted method from: https://github.com/migueldeicaza/gui.cs/blob/fc1faba7452ccbdf49028ac49f0c9f0f42bbae91/Terminal.Gui/Views/ListView.cs#L433-L461
  132. private void RenderUstr (ConsoleDriver driver, ustring ustr, int col, int line, int width)
  133. {
  134. int used = 0;
  135. int index = 0;
  136. while (index < ustr.Length) {
  137. (var rune, var size) = Utf8.DecodeRune (ustr, index, index - ustr.Length);
  138. var count = Rune.ColumnWidth (rune);
  139. if (used + count >= width) break;
  140. driver.AddRune (rune);
  141. used += count;
  142. index += size;
  143. }
  144. while (used < width) {
  145. driver.AddRune (' ');
  146. used++;
  147. }
  148. }
  149. public IList ToList ()
  150. {
  151. return Scenarios;
  152. }
  153. }
  154. }
  155. }