TabViewExample.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System.Linq;
  2. using Terminal.Gui;
  3. namespace UICatalog.Scenarios;
  4. [ScenarioMetadata (Name: "Tab View", Description: "Demos TabView control with limited screen space in Absolute layout.")]
  5. [ScenarioCategory ("Controls"), ScenarioCategory ("TabView")]
  6. public class TabViewExample : Scenario {
  7. TabView _tabView;
  8. MenuItem _miShowTopLine;
  9. MenuItem _miShowBorder;
  10. MenuItem _miTabsOnBottom;
  11. MenuItem _miShowTabViewBorder;
  12. public override void Setup ()
  13. {
  14. Win.Title = this.GetName ();
  15. Win.Y = 1; // menu
  16. Win.Height = Dim.Fill (1); // status bar
  17. var menu = new MenuBar (new MenuBarItem [] {
  18. new MenuBarItem ("_File", new MenuItem [] {
  19. new MenuItem ("_Add Blank Tab", "", () => AddBlankTab()),
  20. new MenuItem ("_Clear SelectedTab", "", () => _tabView.SelectedTab=null),
  21. new MenuItem ("_Quit", "", () => Quit()),
  22. }),
  23. new MenuBarItem ("_View", new MenuItem [] {
  24. _miShowTopLine = new MenuItem ("_Show Top Line", "", () => ShowTopLine()){
  25. Checked = true,
  26. CheckType = MenuItemCheckStyle.Checked
  27. },
  28. _miShowBorder = new MenuItem ("_Show Border", "", () => ShowBorder()){
  29. Checked = true,
  30. CheckType = MenuItemCheckStyle.Checked
  31. },
  32. _miTabsOnBottom = new MenuItem ("_Tabs On Bottom", "", () => SetTabsOnBottom()){
  33. Checked = false,
  34. CheckType = MenuItemCheckStyle.Checked
  35. },
  36. _miShowTabViewBorder = new MenuItem ("_Show TabView Border", "", () => ShowTabViewBorder()){
  37. Checked = true,
  38. CheckType = MenuItemCheckStyle.Checked
  39. }
  40. })
  41. });
  42. Application.Top.Add (menu);
  43. _tabView = new TabView () {
  44. X = 0,
  45. Y = 0,
  46. Width = 60,
  47. Height = 20,
  48. BorderStyle = LineStyle.Single
  49. };
  50. _tabView.AddTab (new Tab () { DisplayText = "Tab1", View = new Label ("hodor!") }, false);
  51. _tabView.AddTab (new Tab () { DisplayText = "Tab2", View = new TextField ("durdur") }, false);
  52. _tabView.AddTab (new Tab () { DisplayText = "Interactive Tab", View = GetInteractiveTab () }, false);
  53. _tabView.AddTab (new Tab () { DisplayText = "Big Text", View = GetBigTextFileTab () }, false);
  54. _tabView.AddTab (new Tab () {
  55. DisplayText = "Long name Tab, I mean seriously long. Like you would not believe how long this tab's name is its just too much really woooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooowwww thats long",
  56. View = new Label ("This tab has a very long name which should be truncated. See TabView.MaxTabTextWidth")
  57. }, false);
  58. _tabView.AddTab (new Tab () { DisplayText = "Les Mise" + '\u0301' + "rables", View = new Label ("This tab name is unicode") }, false);
  59. _tabView.AddTab (new Tab () { DisplayText = "Les Mise" + '\u0328' + '\u0301' + "rables", View = new Label ("This tab name has two combining marks. Only one will show due to Issue #2616.") }, false);
  60. for (int i = 0; i < 100; i++) {
  61. _tabView.AddTab (new Tab () { DisplayText = $"Tab{i}", View = new Label($"Welcome to tab {i}") }, false);
  62. }
  63. _tabView.SelectedTab = _tabView.Tabs.First ();
  64. Win.Add (_tabView);
  65. var frameRight = new FrameView ("About") {
  66. X = Pos.Right (_tabView),
  67. Y = 0,
  68. Width = Dim.Fill (),
  69. Height = Dim.Fill (),
  70. };
  71. frameRight.Add (new TextView () {
  72. Text = "This demos the tabs control\nSwitch between tabs using cursor keys",
  73. Width = Dim.Fill (),
  74. Height = Dim.Fill ()
  75. });
  76. Win.Add (frameRight);
  77. var frameBelow = new FrameView ("Bottom Frame") {
  78. X = 0,
  79. Y = Pos.Bottom (_tabView),
  80. Width = _tabView.Width,
  81. Height = Dim.Fill (),
  82. };
  83. frameBelow.Add (new TextView () {
  84. Text = "This frame exists to check you can still tab here\nand that the tab control doesn't overspill it's bounds",
  85. Width = Dim.Fill (),
  86. Height = Dim.Fill ()
  87. });
  88. Win.Add (frameBelow);
  89. var statusBar = new StatusBar (new StatusItem [] {
  90. new StatusItem(Application.QuitKey, $"{Application.QuitKey} to Quit", () => Quit()),
  91. });
  92. Application.Top.Add (statusBar);
  93. }
  94. private void AddBlankTab ()
  95. {
  96. _tabView.AddTab (new Tab (), false);
  97. }
  98. private View GetInteractiveTab ()
  99. {
  100. var interactiveTab = new View () {
  101. Width = Dim.Fill (),
  102. Height = Dim.Fill ()
  103. };
  104. var lblName = new Label ("Name:");
  105. interactiveTab.Add (lblName);
  106. var tbName = new TextField () {
  107. X = Pos.Right (lblName),
  108. Width = 10
  109. };
  110. interactiveTab.Add (tbName);
  111. var lblAddr = new Label ("Address:") {
  112. Y = 1
  113. };
  114. interactiveTab.Add (lblAddr);
  115. var tbAddr = new TextField () {
  116. X = Pos.Right (lblAddr),
  117. Y = 1,
  118. Width = 10
  119. };
  120. interactiveTab.Add (tbAddr);
  121. return interactiveTab;
  122. }
  123. private View GetBigTextFileTab ()
  124. {
  125. var text = new TextView () {
  126. Width = Dim.Fill (),
  127. Height = Dim.Fill ()
  128. };
  129. var sb = new System.Text.StringBuilder ();
  130. for (int y = 0; y < 300; y++) {
  131. for (int x = 0; x < 500; x++) {
  132. sb.Append ((x + y) % 2 == 0 ? '1' : '0');
  133. }
  134. sb.AppendLine ();
  135. }
  136. text.Text = sb.ToString ();
  137. return text;
  138. }
  139. private void ShowTopLine ()
  140. {
  141. _miShowTopLine.Checked = !_miShowTopLine.Checked;
  142. _tabView.Style.ShowTopLine = (bool)_miShowTopLine.Checked;
  143. _tabView.ApplyStyleChanges ();
  144. }
  145. private void ShowBorder ()
  146. {
  147. _miShowBorder.Checked = !_miShowBorder.Checked;
  148. _tabView.Style.ShowBorder = (bool)_miShowBorder.Checked;
  149. _tabView.ApplyStyleChanges ();
  150. }
  151. private void SetTabsOnBottom ()
  152. {
  153. _miTabsOnBottom.Checked = !_miTabsOnBottom.Checked;
  154. _tabView.Style.TabsOnBottom = (bool)_miTabsOnBottom.Checked;
  155. _tabView.ApplyStyleChanges ();
  156. }
  157. private void ShowTabViewBorder ()
  158. {
  159. _miShowTabViewBorder.Checked = !_miShowTabViewBorder.Checked;
  160. _tabView.BorderStyle = _miShowTabViewBorder.Checked == true ? _tabView.BorderStyle = LineStyle.Single
  161. : LineStyle.None;
  162. _tabView.ApplyStyleChanges ();
  163. }
  164. private void Quit ()
  165. {
  166. Application.RequestStop ();
  167. }
  168. }