ListsAndCombos.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 Main ()
  14. {
  15. Application.Init ();
  16. //TODO: Duplicated code in Demo.cs Consider moving to shared assembly
  17. ObservableCollection<string> items = [];
  18. foreach (string dir in new [] { "/etc", @$"{Environment.GetEnvironmentVariable ("SystemRoot")}\System32" })
  19. {
  20. if (Directory.Exists (dir))
  21. {
  22. items = new (Directory.GetFiles (dir)
  23. .Union (Directory.GetDirectories (dir))
  24. .Select (Path.GetFileName)
  25. .Where (x => char.IsLetterOrDigit (x [0]))
  26. .OrderBy (x => x)
  27. .Select (x => x)
  28. .ToList ());
  29. }
  30. }
  31. var win = new Window { Title = GetQuitKeyAndName () };
  32. // ListView
  33. var lbListView = new Label
  34. {
  35. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  36. X = 0,
  37. Width = Dim.Percent (40),
  38. Text = "Listview"
  39. };
  40. var listview = new ListView
  41. {
  42. X = 0,
  43. Y = Pos.Bottom (lbListView) + 1,
  44. Height = Dim.Fill (2),
  45. Width = Dim.Percent (40),
  46. Source = new ListWrapper<string> (items)
  47. };
  48. listview.SelectedItemChanged += (s, e) => lbListView.Text = items [listview.SelectedItem];
  49. win.Add (lbListView, 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 - 1;
  72. scrollBar.Position = listview.TopItem;
  73. scrollBar.OtherScrollBarView.Size = listview.MaxLength - 1;
  74. scrollBar.OtherScrollBarView.Position = listview.LeftItem;
  75. scrollBar.Refresh ();
  76. };
  77. // ComboBox
  78. var lbComboBox = new Label
  79. {
  80. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  81. X = Pos.Right (lbListView) + 1,
  82. Width = Dim.Percent (40),
  83. Text = "ComboBox"
  84. };
  85. var comboBox = new ComboBox
  86. {
  87. X = Pos.Right (listview) + 1,
  88. Y = Pos.Bottom (lbListView) + 1,
  89. Height = Dim.Fill (2),
  90. Width = Dim.Percent (40)
  91. };
  92. comboBox.SetSource (items);
  93. comboBox.SelectedItemChanged += (s, text) => lbComboBox.Text = text.Value.ToString ();
  94. win.Add (lbComboBox, comboBox);
  95. var scrollBarCbx = new ScrollBarView (comboBox.Subviews [1], true);
  96. scrollBarCbx.ChangedPosition += (s, e) =>
  97. {
  98. ((ListView)comboBox.Subviews [1]).TopItem = scrollBarCbx.Position;
  99. if (((ListView)comboBox.Subviews [1]).TopItem != scrollBarCbx.Position)
  100. {
  101. scrollBarCbx.Position = ((ListView)comboBox.Subviews [1]).TopItem;
  102. }
  103. comboBox.SetNeedsDisplay ();
  104. };
  105. scrollBarCbx.OtherScrollBarView.ChangedPosition += (s, e) =>
  106. {
  107. ((ListView)comboBox.Subviews [1]).LeftItem = scrollBarCbx.OtherScrollBarView.Position;
  108. if (((ListView)comboBox.Subviews [1]).LeftItem != scrollBarCbx.OtherScrollBarView.Position)
  109. {
  110. scrollBarCbx.OtherScrollBarView.Position = ((ListView)comboBox.Subviews [1]).LeftItem;
  111. }
  112. comboBox.SetNeedsDisplay ();
  113. };
  114. comboBox.DrawContent += (s, e) =>
  115. {
  116. scrollBarCbx.Size = comboBox.Source.Count;
  117. scrollBarCbx.Position = ((ListView)comboBox.Subviews [1]).TopItem;
  118. scrollBarCbx.OtherScrollBarView.Size = ((ListView)comboBox.Subviews [1]).MaxLength - 1;
  119. scrollBarCbx.OtherScrollBarView.Position = ((ListView)comboBox.Subviews [1]).LeftItem;
  120. scrollBarCbx.Refresh ();
  121. };
  122. var btnMoveUp = new Button { X = 1, Y = Pos.Bottom (lbListView), Text = "Move _Up" };
  123. btnMoveUp.Accept += (s, e) => { listview.MoveUp (); };
  124. var btnMoveDown = new Button
  125. {
  126. X = Pos.Right (btnMoveUp) + 1, Y = Pos.Bottom (lbListView), Text = "Move _Down"
  127. };
  128. btnMoveDown.Accept += (s, e) => { listview.MoveDown (); };
  129. win.Add (btnMoveUp, btnMoveDown);
  130. Application.Run (win);
  131. win.Dispose ();
  132. Application.Shutdown ();
  133. }
  134. }