TabViewExample.cs 9.3 KB

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