TabViewExample.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Terminal.Gui;
  8. using static UICatalog.Scenario;
  9. namespace UICatalog.Scenarios {
  10. [ScenarioMetadata (Name: "Tab View", Description: "Demos TabView control with limited screen space in Absolute layout.")]
  11. [ScenarioCategory ("Controls"), ScenarioCategory ("TabView")]
  12. public class TabViewExample : Scenario {
  13. TabView tabView;
  14. MenuItem miShowTopLine;
  15. MenuItem miShowBorder;
  16. MenuItem miTabsOnBottom;
  17. public override void Setup ()
  18. {
  19. Win.Title = this.GetName ();
  20. Win.Y = 1; // menu
  21. Win.Height = Dim.Fill (1); // status bar
  22. Application.Top.LayoutSubviews ();
  23. var menu = new MenuBar (new MenuBarItem [] {
  24. new MenuBarItem ("_File", new MenuItem [] {
  25. new MenuItem ("_Add Blank Tab", "", () => AddBlankTab()),
  26. new MenuItem ("_Clear SelectedTab", "", () => tabView.SelectedTab=null),
  27. new MenuItem ("_Quit", "", () => Quit()),
  28. }),
  29. new MenuBarItem ("_View", new MenuItem [] {
  30. miShowTopLine = new MenuItem ("_Show Top Line", "", () => ShowTopLine()){
  31. Checked = true,
  32. CheckType = MenuItemCheckStyle.Checked
  33. },
  34. miShowBorder = new MenuItem ("_Show Border", "", () => ShowBorder()){
  35. Checked = true,
  36. CheckType = MenuItemCheckStyle.Checked
  37. },
  38. miTabsOnBottom = new MenuItem ("_Tabs On Bottom", "", () => SetTabsOnBottom()){
  39. Checked = false,
  40. CheckType = MenuItemCheckStyle.Checked
  41. }
  42. })
  43. });
  44. Application.Top.Add (menu);
  45. tabView = new TabView () {
  46. X = 0,
  47. Y = 0,
  48. Width = 60,
  49. Height = 20,
  50. };
  51. tabView.AddTab (new TabView.Tab ("Tab1", new Label ("hodor!")), false);
  52. tabView.AddTab (new TabView.Tab ("Tab2", new Label ("durdur")), false);
  53. tabView.AddTab (new TabView.Tab ("Interactive Tab", GetInteractiveTab ()), false);
  54. tabView.AddTab (new TabView.Tab ("Big Text", GetBigTextFileTab ()), false);
  55. tabView.AddTab (new TabView.Tab (
  56. "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",
  57. new Label ("This tab has a very long name which should be truncated. See TabView.MaxTabTextWidth")),
  58. false);
  59. tabView.AddTab (new TabView.Tab ("Les Mise" + Char.ConvertFromUtf32 (Int32.Parse ("0301", NumberStyles.HexNumber)) + "rables", new Label ("This tab name is unicode")), false);
  60. for (int i = 0; i < 100; i++) {
  61. tabView.AddTab (new TabView.Tab ($"Tab{i}", 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 TabView.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 Quit ()
  158. {
  159. Application.RequestStop ();
  160. }
  161. }
  162. }