2
0

TabViewExample.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #nullable enable
  2. using System.Linq;
  3. using System.Text;
  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 CheckBox? _miShowBorderCheckBox;
  11. private CheckBox? _miShowTabViewBorderCheckBox;
  12. private CheckBox? _miShowTopLineCheckBox;
  13. private CheckBox? _miTabsOnBottomCheckBox;
  14. private TabView? _tabView;
  15. public override void Main ()
  16. {
  17. Application.Init ();
  18. Window appWindow = new ()
  19. {
  20. BorderStyle = LineStyle.None
  21. };
  22. // MenuBar
  23. MenuBar menu = new ();
  24. _tabView = new ()
  25. {
  26. Title = "_Tab View",
  27. X = 0,
  28. Y = Pos.Bottom (menu),
  29. Width = 60,
  30. Height = 20,
  31. BorderStyle = LineStyle.Single
  32. };
  33. _tabView.AddTab (new () { DisplayText = "Tab_1", View = new Label { Text = "hodor!" } }, false);
  34. _tabView.AddTab (new () { DisplayText = "Tab_2", View = new TextField { Text = "durdur", Width = 10 } }, false);
  35. _tabView.AddTab (new () { DisplayText = "_Interactive Tab", View = GetInteractiveTab () }, false);
  36. _tabView.AddTab (new () { DisplayText = "Big Text", View = GetBigTextFileTab () }, false);
  37. _tabView.AddTab (
  38. new ()
  39. {
  40. DisplayText =
  41. "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",
  42. View = new Label
  43. {
  44. Text =
  45. "This tab has a very long name which should be truncated. See TabView.MaxTabTextWidth"
  46. }
  47. },
  48. false
  49. );
  50. _tabView.AddTab (
  51. new ()
  52. {
  53. DisplayText = "Les Mise" + '\u0301' + "rables",
  54. View = new Label { Text = "This tab name is unicode" }
  55. },
  56. false
  57. );
  58. _tabView.AddTab (
  59. new ()
  60. {
  61. DisplayText = "Les Mise" + '\u0328' + '\u0301' + "rables",
  62. View = new Label
  63. {
  64. Text =
  65. "This tab name has two combining marks. Only one will show due to Issue #2616."
  66. }
  67. },
  68. false
  69. );
  70. for (var i = 0; i < 100; i++)
  71. {
  72. _tabView.AddTab (
  73. new () { DisplayText = $"Tab{i}", View = new Label { Text = $"Welcome to tab {i}" } },
  74. false
  75. );
  76. }
  77. _tabView.SelectedTab = _tabView.Tabs.First ();
  78. View frameRight = new ()
  79. {
  80. X = Pos.Right (_tabView),
  81. Y = Pos.Top (_tabView),
  82. Width = Dim.Fill (),
  83. Height = Dim.Fill (1),
  84. Title = "_About",
  85. BorderStyle = LineStyle.Single,
  86. TabStop = TabBehavior.TabStop,
  87. CanFocus = true
  88. };
  89. frameRight.Add (
  90. new TextView
  91. {
  92. Text =
  93. "This demos the tabs control\nSwitch between tabs using cursor keys.\nThis TextView has AllowsTab = false, so tab should nav too.",
  94. Width = Dim.Fill (),
  95. Height = Dim.Fill (),
  96. AllowsTab = false
  97. }
  98. );
  99. View frameBelow = new ()
  100. {
  101. X = 0,
  102. Y = Pos.Bottom (_tabView),
  103. Width = _tabView.Width,
  104. Height = Dim.Fill (1),
  105. Title = "B_ottom Frame",
  106. BorderStyle = LineStyle.Single,
  107. TabStop = TabBehavior.TabStop,
  108. CanFocus = true
  109. };
  110. frameBelow.Add (
  111. new TextView
  112. {
  113. Text =
  114. "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.",
  115. Width = Dim.Fill (),
  116. Height = Dim.Fill ()
  117. }
  118. );
  119. // StatusBar
  120. StatusBar statusBar = new (
  121. [
  122. new (Application.QuitKey, "Quit", Quit)
  123. ]
  124. );
  125. // Setup menu checkboxes
  126. _miShowTopLineCheckBox = new ()
  127. {
  128. Title = "_Show Top Line",
  129. CheckedState = CheckState.Checked
  130. };
  131. _miShowTopLineCheckBox.CheckedStateChanged += (s, e) => ShowTopLine ();
  132. _miShowBorderCheckBox = new ()
  133. {
  134. Title = "_Show Border",
  135. CheckedState = CheckState.Checked
  136. };
  137. _miShowBorderCheckBox.CheckedStateChanged += (s, e) => ShowBorder ();
  138. _miTabsOnBottomCheckBox = new ()
  139. {
  140. Title = "_Tabs On Bottom"
  141. };
  142. _miTabsOnBottomCheckBox.CheckedStateChanged += (s, e) => SetTabsOnBottom ();
  143. _miShowTabViewBorderCheckBox = new ()
  144. {
  145. Title = "_Show TabView Border",
  146. CheckedState = CheckState.Checked
  147. };
  148. _miShowTabViewBorderCheckBox.CheckedStateChanged += (s, e) => ShowTabViewBorder ();
  149. menu.Add (
  150. new MenuBarItem (
  151. "_File",
  152. [
  153. new MenuItem
  154. {
  155. Title = "_Add Blank Tab",
  156. Action = AddBlankTab
  157. },
  158. new MenuItem
  159. {
  160. Title = "_Clear SelectedTab",
  161. Action = () =>
  162. {
  163. if (_tabView is { })
  164. {
  165. _tabView.SelectedTab = null;
  166. }
  167. }
  168. },
  169. new MenuItem
  170. {
  171. Title = "_Quit",
  172. Action = Quit
  173. }
  174. ]
  175. )
  176. );
  177. menu.Add (
  178. new MenuBarItem (
  179. "_View",
  180. [
  181. new MenuItem
  182. {
  183. CommandView = _miShowTopLineCheckBox
  184. },
  185. new MenuItem
  186. {
  187. CommandView = _miShowBorderCheckBox
  188. },
  189. new MenuItem
  190. {
  191. CommandView = _miTabsOnBottomCheckBox
  192. },
  193. new MenuItem
  194. {
  195. CommandView = _miShowTabViewBorderCheckBox
  196. }
  197. ]
  198. )
  199. );
  200. appWindow.Add (menu, _tabView, frameRight, frameBelow, statusBar);
  201. Application.Run (appWindow);
  202. appWindow.Dispose ();
  203. Application.Shutdown ();
  204. }
  205. private void AddBlankTab () { _tabView?.AddTab (new (), false); }
  206. private View GetBigTextFileTab ()
  207. {
  208. TextView text = new () { Width = Dim.Fill (), Height = Dim.Fill () };
  209. StringBuilder sb = new ();
  210. for (var y = 0; y < 300; y++)
  211. {
  212. for (var x = 0; x < 500; x++)
  213. {
  214. sb.Append ((x + y) % 2 == 0 ? '1' : '0');
  215. }
  216. sb.AppendLine ();
  217. }
  218. text.Text = sb.ToString ();
  219. return text;
  220. }
  221. private View GetInteractiveTab ()
  222. {
  223. View interactiveTab = new ()
  224. {
  225. Width = Dim.Fill (),
  226. Height = Dim.Fill (),
  227. CanFocus = true
  228. };
  229. Label lblName = new () { Text = "Name:" };
  230. interactiveTab.Add (lblName);
  231. TextField tbName = new () { X = Pos.Right (lblName), Width = 10 };
  232. interactiveTab.Add (tbName);
  233. Label lblAddr = new () { Y = 1, Text = "Address:" };
  234. interactiveTab.Add (lblAddr);
  235. TextField tbAddr = new () { X = Pos.Right (lblAddr), Y = 1, Width = 10 };
  236. interactiveTab.Add (tbAddr);
  237. return interactiveTab;
  238. }
  239. private void Quit () { Application.RequestStop (); }
  240. private void SetTabsOnBottom ()
  241. {
  242. if (_tabView is null || _miTabsOnBottomCheckBox is null)
  243. {
  244. return;
  245. }
  246. _tabView.Style.TabsOnBottom = _miTabsOnBottomCheckBox.CheckedState == CheckState.Checked;
  247. _tabView.ApplyStyleChanges ();
  248. }
  249. private void ShowBorder ()
  250. {
  251. if (_tabView is null || _miShowBorderCheckBox is null)
  252. {
  253. return;
  254. }
  255. _tabView.Style.ShowBorder = _miShowBorderCheckBox.CheckedState == CheckState.Checked;
  256. _tabView.ApplyStyleChanges ();
  257. }
  258. private void ShowTabViewBorder ()
  259. {
  260. if (_tabView is null || _miShowTabViewBorderCheckBox is null)
  261. {
  262. return;
  263. }
  264. _tabView.BorderStyle = _miShowTabViewBorderCheckBox.CheckedState == CheckState.Checked
  265. ? LineStyle.Single
  266. : LineStyle.None;
  267. _tabView.ApplyStyleChanges ();
  268. }
  269. private void ShowTopLine ()
  270. {
  271. if (_tabView is null || _miShowTopLineCheckBox is null)
  272. {
  273. return;
  274. }
  275. _tabView.Style.ShowTopLine = _miShowTopLineCheckBox.CheckedState == CheckState.Checked;
  276. _tabView.ApplyStyleChanges ();
  277. }
  278. }