ListsAndCombos.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.IO;
  4. using System.Linq;
  5. using Terminal.Gui;
  6. namespace UICatalog.Scenarios;
  7. [ScenarioMetadata ("ListView & ComboBox", "Demonstrates a ListView populating a ComboBox that acts as a filter.")]
  8. [ScenarioCategory ("Controls")]
  9. [ScenarioCategory ("ListView")]
  10. [ScenarioCategory ("ComboBox")]
  11. public class ListsAndCombos : Scenario
  12. {
  13. public override void Setup ()
  14. {
  15. //TODO: Duplicated code in Demo.cs Consider moving to shared assembly
  16. ObservableCollection<string> items = [];
  17. foreach (string dir in new [] { "/etc", @$"{Environment.GetEnvironmentVariable ("SystemRoot")}\System32" })
  18. {
  19. if (Directory.Exists (dir))
  20. {
  21. items = new (Directory.GetFiles (dir)
  22. .Union (Directory.GetDirectories (dir))
  23. .Select (Path.GetFileName)
  24. .Where (x => char.IsLetterOrDigit (x [0]))
  25. .OrderBy (x => x)
  26. .Select (x => x)
  27. .ToList ());
  28. }
  29. }
  30. // ListView
  31. var lbListView = new Label
  32. {
  33. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  34. X = 0,
  35. Width = Dim.Percent (40),
  36. Text = "Listview"
  37. };
  38. var listview = new ListView
  39. {
  40. X = 0,
  41. Y = Pos.Bottom (lbListView) + 1,
  42. Height = Dim.Fill (2),
  43. Width = Dim.Percent (40),
  44. Source = new ListWrapper<string> (items)
  45. };
  46. listview.SelectedItemChanged += (s, e) => lbListView.Text = items [listview.SelectedItem];
  47. Win.Add (lbListView, listview);
  48. var scrollBar = new ScrollBarView (listview, true);
  49. scrollBar.ChangedPosition += (s, e) =>
  50. {
  51. listview.TopItem = scrollBar.Position;
  52. if (listview.TopItem != scrollBar.Position)
  53. {
  54. scrollBar.Position = listview.TopItem;
  55. }
  56. listview.SetNeedsDisplay ();
  57. };
  58. scrollBar.OtherScrollBarView.ChangedPosition += (s, e) =>
  59. {
  60. listview.LeftItem = scrollBar.OtherScrollBarView.Position;
  61. if (listview.LeftItem != scrollBar.OtherScrollBarView.Position)
  62. {
  63. scrollBar.OtherScrollBarView.Position = listview.LeftItem;
  64. }
  65. listview.SetNeedsDisplay ();
  66. };
  67. listview.DrawContent += (s, e) =>
  68. {
  69. scrollBar.Size = listview.Source.Count - 1;
  70. scrollBar.Position = listview.TopItem;
  71. scrollBar.OtherScrollBarView.Size = listview.MaxLength - 1;
  72. scrollBar.OtherScrollBarView.Position = listview.LeftItem;
  73. scrollBar.Refresh ();
  74. };
  75. // ComboBox
  76. var lbComboBox = new Label
  77. {
  78. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  79. X = Pos.Right (lbListView) + 1,
  80. Width = Dim.Percent (40),
  81. Text = "ComboBox"
  82. };
  83. var comboBox = new ComboBox
  84. {
  85. X = Pos.Right (listview) + 1,
  86. Y = Pos.Bottom (lbListView) + 1,
  87. Height = Dim.Fill (2),
  88. Width = Dim.Percent (40)
  89. };
  90. comboBox.SetSource (items);
  91. comboBox.SelectedItemChanged += (s, text) => lbComboBox.Text = text.Value.ToString ();
  92. Win.Add (lbComboBox, comboBox);
  93. var scrollBarCbx = new ScrollBarView (comboBox.Subviews [1], true);
  94. scrollBarCbx.ChangedPosition += (s, e) =>
  95. {
  96. ((ListView)comboBox.Subviews [1]).TopItem = scrollBarCbx.Position;
  97. if (((ListView)comboBox.Subviews [1]).TopItem != scrollBarCbx.Position)
  98. {
  99. scrollBarCbx.Position = ((ListView)comboBox.Subviews [1]).TopItem;
  100. }
  101. comboBox.SetNeedsDisplay ();
  102. };
  103. scrollBarCbx.OtherScrollBarView.ChangedPosition += (s, e) =>
  104. {
  105. ((ListView)comboBox.Subviews [1]).LeftItem = scrollBarCbx.OtherScrollBarView.Position;
  106. if (((ListView)comboBox.Subviews [1]).LeftItem != scrollBarCbx.OtherScrollBarView.Position)
  107. {
  108. scrollBarCbx.OtherScrollBarView.Position = ((ListView)comboBox.Subviews [1]).LeftItem;
  109. }
  110. comboBox.SetNeedsDisplay ();
  111. };
  112. comboBox.DrawContent += (s, e) =>
  113. {
  114. scrollBarCbx.Size = comboBox.Source.Count;
  115. scrollBarCbx.Position = ((ListView)comboBox.Subviews [1]).TopItem;
  116. scrollBarCbx.OtherScrollBarView.Size = ((ListView)comboBox.Subviews [1]).MaxLength - 1;
  117. scrollBarCbx.OtherScrollBarView.Position = ((ListView)comboBox.Subviews [1]).LeftItem;
  118. scrollBarCbx.Refresh ();
  119. };
  120. var btnMoveUp = new Button { X = 1, Y = Pos.Bottom (lbListView), Text = "Move _Up" };
  121. btnMoveUp.Accept += (s, e) => { listview.MoveUp (); };
  122. var btnMoveDown = new Button
  123. {
  124. X = Pos.Right (btnMoveUp) + 1, Y = Pos.Bottom (lbListView), Text = "Move _Down"
  125. };
  126. btnMoveDown.Accept += (s, e) => { listview.MoveDown (); };
  127. Win.Add (btnMoveUp, btnMoveDown);
  128. }
  129. }