TabViewExample.cs 8.9 KB

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