ListsAndCombos.cs 4.3 KB

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