TabViewExample.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using System.Linq;
  2. using System.Text;
  3. namespace UICatalog.Scenarios;
  4. [ScenarioMetadata ("Tab View", "Demos TabView control with limited screen space in Absolute layout.")]
  5. [ScenarioCategory ("Controls")]
  6. [ScenarioCategory ("TabView")]
  7. public class TabViewExample : Scenario
  8. {
  9. private MenuItem _miShowBorder;
  10. private MenuItem _miShowTabViewBorder;
  11. private MenuItem _miShowTopLine;
  12. private MenuItem _miTabsOnBottom;
  13. private TabView _tabView;
  14. public override void Main ()
  15. {
  16. // Init
  17. Application.Init ();
  18. // Setup - Create a top-level application window and configure it.
  19. Toplevel appWindow = new ();
  20. var menu = new MenuBar
  21. {
  22. Menus =
  23. [
  24. new (
  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 (
  38. "_View",
  39. new []
  40. {
  41. _miShowTopLine =
  42. new ("_Show Top Line", "", ShowTopLine)
  43. {
  44. Checked = true, CheckType = MenuItemCheckStyle.Checked
  45. },
  46. _miShowBorder =
  47. new ("_Show Border", "", ShowBorder)
  48. {
  49. Checked = true, CheckType = MenuItemCheckStyle.Checked
  50. },
  51. _miTabsOnBottom =
  52. new ("_Tabs On Bottom", "", SetTabsOnBottom)
  53. {
  54. Checked = false, CheckType = MenuItemCheckStyle.Checked
  55. },
  56. _miShowTabViewBorder =
  57. new (
  58. "_Show TabView Border",
  59. "",
  60. ShowTabViewBorder
  61. ) { Checked = true, CheckType = MenuItemCheckStyle.Checked }
  62. }
  63. )
  64. ]
  65. };
  66. appWindow.Add (menu);
  67. _tabView = new()
  68. {
  69. Title = "_Tab View",
  70. X = 0,
  71. Y = 1,
  72. Width = 60,
  73. Height = 20,
  74. BorderStyle = LineStyle.Single
  75. };
  76. _tabView.AddTab (new() { DisplayText = "Tab_1", View = new Label { Text = "hodor!" } }, false);
  77. _tabView.AddTab (new() { DisplayText = "Tab_2", View = new TextField { Text = "durdur", Width = 10 } }, 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. CanFocus = true
  131. };
  132. frameRight.Add (
  133. new TextView
  134. {
  135. Text = "This demos the tabs control\nSwitch between tabs using cursor keys.\nThis TextView has AllowsTab = false, so tab should nav too.",
  136. Width = Dim.Fill (),
  137. Height = Dim.Fill (),
  138. AllowsTab = false,
  139. }
  140. );
  141. appWindow.Add (frameRight);
  142. var frameBelow = new View
  143. {
  144. X = 0,
  145. Y = Pos.Bottom (_tabView),
  146. Width = _tabView.Width,
  147. Height = Dim.Fill (1),
  148. Title = "B_ottom Frame",
  149. BorderStyle = LineStyle.Single,
  150. TabStop = TabBehavior.TabStop,
  151. CanFocus = true
  152. };
  153. frameBelow.Add (
  154. new TextView
  155. {
  156. Text =
  157. "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.",
  158. Width = Dim.Fill (),
  159. Height = Dim.Fill (),
  160. }
  161. );
  162. appWindow.Add (frameBelow);
  163. var statusBar = new StatusBar ([new (Application.QuitKey, "Quit", Quit)]);
  164. appWindow.Add (statusBar);
  165. // Run - Start the application.
  166. Application.Run (appWindow);
  167. appWindow.Dispose ();
  168. // Shutdown - Calling Application.Shutdown is required.
  169. Application.Shutdown ();
  170. }
  171. private void AddBlankTab () { _tabView.AddTab (new (), false); }
  172. private View GetBigTextFileTab ()
  173. {
  174. var text = new TextView { Width = Dim.Fill (), Height = Dim.Fill () };
  175. var sb = new StringBuilder ();
  176. for (var y = 0; y < 300; y++)
  177. {
  178. for (var x = 0; x < 500; x++)
  179. {
  180. sb.Append ((x + y) % 2 == 0 ? '1' : '0');
  181. }
  182. sb.AppendLine ();
  183. }
  184. text.Text = sb.ToString ();
  185. return text;
  186. }
  187. private View GetInteractiveTab ()
  188. {
  189. var interactiveTab = new View
  190. {
  191. Width = Dim.Fill (), Height = Dim.Fill (),
  192. CanFocus = true
  193. };
  194. var lblName = new Label { Text = "Name:" };
  195. interactiveTab.Add (lblName);
  196. var tbName = new TextField { X = Pos.Right (lblName), Width = 10 };
  197. interactiveTab.Add (tbName);
  198. var lblAddr = new Label { Y = 1, Text = "Address:" };
  199. interactiveTab.Add (lblAddr);
  200. var tbAddr = new TextField { X = Pos.Right (lblAddr), Y = 1, Width = 10 };
  201. interactiveTab.Add (tbAddr);
  202. return interactiveTab;
  203. }
  204. private void Quit () { Application.RequestStop (); }
  205. private void SetTabsOnBottom ()
  206. {
  207. _miTabsOnBottom.Checked = !_miTabsOnBottom.Checked;
  208. _tabView.Style.TabsOnBottom = (bool)_miTabsOnBottom.Checked;
  209. _tabView.ApplyStyleChanges ();
  210. }
  211. private void ShowBorder ()
  212. {
  213. _miShowBorder.Checked = !_miShowBorder.Checked;
  214. _tabView.Style.ShowBorder = (bool)_miShowBorder.Checked;
  215. _tabView.ApplyStyleChanges ();
  216. }
  217. private void ShowTabViewBorder ()
  218. {
  219. _miShowTabViewBorder.Checked = !_miShowTabViewBorder.Checked;
  220. _tabView.BorderStyle = _miShowTabViewBorder.Checked == true
  221. ? _tabView.BorderStyle = LineStyle.Single
  222. : LineStyle.None;
  223. _tabView.ApplyStyleChanges ();
  224. }
  225. private void ShowTopLine ()
  226. {
  227. _miShowTopLine.Checked = !_miShowTopLine.Checked;
  228. _tabView.Style.ShowTopLine = (bool)_miShowTopLine.Checked;
  229. _tabView.ApplyStyleChanges ();
  230. }
  231. }