TabViewExample.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 FrameView
  122. {
  123. X = Pos.Right (_tabView),
  124. Y = 1,
  125. Width = Dim.Fill (),
  126. Height = Dim.Fill (1),
  127. Title = "About"
  128. };
  129. frameRight.Add (
  130. new TextView
  131. {
  132. Text = "This demos the tabs control\nSwitch between tabs using cursor keys",
  133. Width = Dim.Fill (),
  134. Height = Dim.Fill ()
  135. }
  136. );
  137. appWindow.Add (frameRight);
  138. var frameBelow = new FrameView
  139. {
  140. X = 0,
  141. Y = Pos.Bottom (_tabView),
  142. Width = _tabView.Width,
  143. Height = Dim.Fill (1),
  144. Title = "Bottom Frame"
  145. };
  146. frameBelow.Add (
  147. new TextView
  148. {
  149. Text =
  150. "This frame exists to check you can still tab here\nand that the tab control doesn't overspill it's bounds",
  151. Width = Dim.Fill (),
  152. Height = Dim.Fill ()
  153. }
  154. );
  155. appWindow.Add (frameBelow);
  156. var statusBar = new StatusBar ([new (Application.QuitKey, "Quit", Quit)]);
  157. appWindow.Add (statusBar);
  158. // Run - Start the application.
  159. Application.Run (appWindow);
  160. appWindow.Dispose ();
  161. // Shutdown - Calling Application.Shutdown is required.
  162. Application.Shutdown ();
  163. }
  164. private void AddBlankTab () { _tabView.AddTab (new (), false); }
  165. private View GetBigTextFileTab ()
  166. {
  167. var text = new TextView { Width = Dim.Fill (), Height = Dim.Fill () };
  168. var sb = new StringBuilder ();
  169. for (var y = 0; y < 300; y++)
  170. {
  171. for (var x = 0; x < 500; x++)
  172. {
  173. sb.Append ((x + y) % 2 == 0 ? '1' : '0');
  174. }
  175. sb.AppendLine ();
  176. }
  177. text.Text = sb.ToString ();
  178. return text;
  179. }
  180. private View GetInteractiveTab ()
  181. {
  182. var interactiveTab = new View { Width = Dim.Fill (), Height = Dim.Fill () };
  183. var lblName = new Label { Text = "Name:" };
  184. interactiveTab.Add (lblName);
  185. var tbName = new TextField { X = Pos.Right (lblName), Width = 10 };
  186. interactiveTab.Add (tbName);
  187. var lblAddr = new Label { Y = 1, Text = "Address:" };
  188. interactiveTab.Add (lblAddr);
  189. var tbAddr = new TextField { X = Pos.Right (lblAddr), Y = 1, Width = 10 };
  190. interactiveTab.Add (tbAddr);
  191. return interactiveTab;
  192. }
  193. private void Quit () { Application.RequestStop (); }
  194. private void SetTabsOnBottom ()
  195. {
  196. _miTabsOnBottom.Checked = !_miTabsOnBottom.Checked;
  197. _tabView.Style.TabsOnBottom = (bool)_miTabsOnBottom.Checked;
  198. _tabView.ApplyStyleChanges ();
  199. }
  200. private void ShowBorder ()
  201. {
  202. _miShowBorder.Checked = !_miShowBorder.Checked;
  203. _tabView.Style.ShowBorder = (bool)_miShowBorder.Checked;
  204. _tabView.ApplyStyleChanges ();
  205. }
  206. private void ShowTabViewBorder ()
  207. {
  208. _miShowTabViewBorder.Checked = !_miShowTabViewBorder.Checked;
  209. _tabView.BorderStyle = _miShowTabViewBorder.Checked == true
  210. ? _tabView.BorderStyle = LineStyle.Single
  211. : LineStyle.None;
  212. _tabView.ApplyStyleChanges ();
  213. }
  214. private void ShowTopLine ()
  215. {
  216. _miShowTopLine.Checked = !_miShowTopLine.Checked;
  217. _tabView.Style.ShowTopLine = (bool)_miShowTopLine.Checked;
  218. _tabView.ApplyStyleChanges ();
  219. }
  220. }