TabView.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. namespace Terminal.Gui {
  7. /// <summary>
  8. /// Control that hosts multiple sub views, presenting a single one at once
  9. /// </summary>
  10. public class TabView : View {
  11. private Tab selectedTab;
  12. /// <summary>
  13. /// The default <see cref="MaxTabTextWidth"/> to set on new <see cref="TabView"/> controls
  14. /// </summary>
  15. public const uint DefaultMaxTabTextWidth = 30;
  16. /// <summary>
  17. /// This sub view is the 2 or 3 line control that represents the actual tabs themselves
  18. /// </summary>
  19. TabRowView tabsBar;
  20. /// <summary>
  21. /// This sub view is the main client area of the current tab. It hosts the <see cref="Tab.View"/>
  22. /// of the tab, the <see cref="SelectedTab"/>
  23. /// </summary>
  24. View contentView;
  25. private List<Tab> tabs = new List<Tab> ();
  26. /// <summary>
  27. /// All tabs currently hosted by the control
  28. /// </summary>
  29. /// <value></value>
  30. public IReadOnlyCollection<Tab> Tabs { get => tabs.AsReadOnly (); }
  31. /// <summary>
  32. /// When there are too many tabs to render, this indicates the first
  33. /// tab to render on the screen.
  34. /// </summary>
  35. /// <value></value>
  36. public int TabScrollOffset { get; set; }
  37. /// <summary>
  38. /// The maximum number of characters to render in a Tab header. This prevents one long tab
  39. /// from pushing out all the others.
  40. /// </summary>
  41. public uint MaxTabTextWidth { get; set; } = DefaultMaxTabTextWidth;
  42. /// <summary>
  43. /// Event for when <see cref="SelectedTab"/> changes
  44. /// </summary>
  45. public event EventHandler<TabChangedEventArgs> SelectedTabChanged;
  46. /// <summary>
  47. /// The currently selected member of <see cref="Tabs"/> chosen by the user
  48. /// </summary>
  49. /// <value></value>
  50. public Tab SelectedTab {
  51. get => selectedTab;
  52. set {
  53. var old = selectedTab;
  54. if (selectedTab != null) {
  55. if (selectedTab.View != null) {
  56. // remove old content
  57. contentView.Remove (selectedTab.View);
  58. }
  59. }
  60. selectedTab = value;
  61. if (value != null) {
  62. // add new content
  63. if (selectedTab.View != null) {
  64. contentView.Add (selectedTab.View);
  65. }
  66. }
  67. EnsureSelectedTabIsVisible ();
  68. if (old != value) {
  69. OnSelectedTabChanged (old, value);
  70. }
  71. }
  72. }
  73. /// <summary>
  74. /// Render choices for how to display tabs. After making changes, call <see cref="ApplyStyleChanges()"/>
  75. /// </summary>
  76. /// <value></value>
  77. public TabStyle Style { get; set; } = new TabStyle ();
  78. /// <summary>
  79. /// Initialzies a <see cref="TabView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  80. /// </summary>
  81. public TabView () : base ()
  82. {
  83. CanFocus = true;
  84. contentView = new View ();
  85. tabsBar = new TabRowView (this);
  86. ApplyStyleChanges ();
  87. base.Add (tabsBar);
  88. base.Add (contentView);
  89. }
  90. /// <summary>
  91. /// Updates the control to use the latest state settings in <see cref="Style"/>.
  92. /// This can change the size of the client area of the tab (for rendering the
  93. /// selected tab's content). This method includes a call
  94. /// to <see cref="View.SetNeedsDisplay()"/>
  95. /// </summary>
  96. public void ApplyStyleChanges ()
  97. {
  98. contentView.X = Style.ShowBorder ? 1 : 0;
  99. contentView.Width = Dim.Fill (Style.ShowBorder ? 1 : 0);
  100. if (Style.TabsOnBottom) {
  101. // Tabs are along the bottom so just dodge the border
  102. contentView.Y = Style.ShowBorder ? 1 : 0;
  103. // Fill client area leaving space at bottom for tabs
  104. contentView.Height = Dim.Fill (GetTabHeight (false));
  105. var tabHeight = GetTabHeight (false);
  106. tabsBar.Height = tabHeight;
  107. tabsBar.Y = Pos.Percent (100) - tabHeight;
  108. } else {
  109. // Tabs are along the top
  110. var tabHeight = GetTabHeight (true);
  111. //move content down to make space for tabs
  112. contentView.Y = tabHeight;
  113. // Fill client area leaving space at bottom for border
  114. contentView.Height = Dim.Fill (Style.ShowBorder ? 1 : 0);
  115. // The top tab should be 2 or 3 rows high and on the top
  116. tabsBar.Height = tabHeight;
  117. // Should be able to just use 0 but switching between top/bottom tabs repeatedly breaks in ValidatePosDim if just using the absolute value 0
  118. tabsBar.Y = Pos.Percent (0);
  119. }
  120. SetNeedsDisplay ();
  121. }
  122. ///<inheritdoc/>
  123. public override void Redraw (Rect bounds)
  124. {
  125. Move (0, 0);
  126. Driver.SetAttribute (ColorScheme.Normal);
  127. if (Style.ShowBorder) {
  128. // How muc space do we need to leave at the bottom to show the tabs
  129. int spaceAtBottom = Math.Max (0, GetTabHeight (false) - 1);
  130. int startAtY = Math.Max (0, GetTabHeight (true) - 1);
  131. DrawFrame (new Rect (0, startAtY, bounds.Width,
  132. bounds.Height - spaceAtBottom - startAtY), 0, true);
  133. }
  134. if (Tabs.Any ()) {
  135. tabsBar.Redraw (tabsBar.Bounds);
  136. contentView.Redraw (contentView.Bounds);
  137. }
  138. }
  139. /// <summary>
  140. /// Disposes the control and all <see cref="Tabs"/>
  141. /// </summary>
  142. /// <param name="disposing"></param>
  143. protected override void Dispose (bool disposing)
  144. {
  145. base.Dispose (disposing);
  146. // The selected tab will automatically be disposed but
  147. // any tabs not visible will need to be manually disposed
  148. foreach (var tab in Tabs) {
  149. if (!Equals (SelectedTab, tab)) {
  150. tab.View?.Dispose ();
  151. }
  152. }
  153. }
  154. /// <summary>
  155. /// Raises the <see cref="SelectedTabChanged"/> event
  156. /// </summary>
  157. protected virtual void OnSelectedTabChanged (Tab oldTab, Tab newTab)
  158. {
  159. SelectedTabChanged?.Invoke (this, new TabChangedEventArgs (oldTab, newTab));
  160. }
  161. /// <inheritdoc/>
  162. public override bool ProcessKey (KeyEvent keyEvent)
  163. {
  164. if (HasFocus && CanFocus && Focused == tabsBar) {
  165. switch (keyEvent.Key) {
  166. case Key.CursorLeft:
  167. SwitchTabBy (-1);
  168. return true;
  169. case Key.CursorRight:
  170. SwitchTabBy (1);
  171. return true;
  172. case Key.Home:
  173. SelectedTab = Tabs.FirstOrDefault ();
  174. return true;
  175. case Key.End:
  176. SelectedTab = Tabs.LastOrDefault ();
  177. return true;
  178. }
  179. }
  180. return base.ProcessKey (keyEvent);
  181. }
  182. /// <summary>
  183. /// Changes the <see cref="SelectedTab"/> by the given <paramref name="amount"/>.
  184. /// Positive for right, negative for left. If no tab is currently selected then
  185. /// the first tab will become selected
  186. /// </summary>
  187. /// <param name="amount"></param>
  188. public void SwitchTabBy (int amount)
  189. {
  190. if (Tabs.Count == 0) {
  191. return;
  192. }
  193. // if there is only one tab anyway or nothing is selected
  194. if (Tabs.Count == 1 || SelectedTab == null) {
  195. SelectedTab = Tabs.ElementAt (0);
  196. SetNeedsDisplay ();
  197. return;
  198. }
  199. var currentIdx = Tabs.IndexOf (SelectedTab);
  200. // Currently selected tab has vanished!
  201. if (currentIdx == -1) {
  202. SelectedTab = Tabs.ElementAt (0);
  203. SetNeedsDisplay ();
  204. return;
  205. }
  206. var newIdx = Math.Max (0, Math.Min (currentIdx + amount, Tabs.Count - 1));
  207. SelectedTab = tabs [newIdx];
  208. SetNeedsDisplay ();
  209. EnsureSelectedTabIsVisible ();
  210. }
  211. /// <summary>
  212. /// Updates <see cref="TabScrollOffset"/> to be a valid index of <see cref="Tabs"/>
  213. /// </summary>
  214. /// <remarks>Changes will not be immediately visible in the display until you call <see cref="View.SetNeedsDisplay()"/></remarks>
  215. public void EnsureValidScrollOffsets ()
  216. {
  217. TabScrollOffset = Math.Max (Math.Min (TabScrollOffset, Tabs.Count - 1), 0);
  218. }
  219. /// <summary>
  220. /// Updates <see cref="TabScrollOffset"/> to ensure that <see cref="SelectedTab"/> is visible
  221. /// </summary>
  222. public void EnsureSelectedTabIsVisible ()
  223. {
  224. if (SelectedTab == null) {
  225. return;
  226. }
  227. // if current viewport does not include the selected tab
  228. if (!CalculateViewport (Bounds).Any (r => Equals (SelectedTab, r.Tab))) {
  229. // Set scroll offset so the first tab rendered is the
  230. TabScrollOffset = Math.Max (0, Tabs.IndexOf (SelectedTab));
  231. }
  232. }
  233. /// <summary>
  234. /// Returns the number of rows occupied by rendering the tabs, this depends
  235. /// on <see cref="TabStyle.ShowTopLine"/> and can be 0 (e.g. if
  236. /// <see cref="TabStyle.TabsOnBottom"/> and you ask for <paramref name="top"/>).
  237. /// </summary>
  238. /// <param name="top">True to measure the space required at the top of the control,
  239. /// false to measure space at the bottom</param>
  240. /// <returns></returns>
  241. private int GetTabHeight (bool top)
  242. {
  243. if (top && Style.TabsOnBottom) {
  244. return 0;
  245. }
  246. if (!top && !Style.TabsOnBottom) {
  247. return 0;
  248. }
  249. return Style.ShowTopLine ? 3 : 2;
  250. }
  251. /// <summary>
  252. /// Returns which tabs to render at each x location
  253. /// </summary>
  254. /// <returns></returns>
  255. private IEnumerable<TabToRender> CalculateViewport (Rect bounds)
  256. {
  257. int i = 1;
  258. // Starting at the first or scrolled to tab
  259. foreach (var tab in Tabs.Skip (TabScrollOffset)) {
  260. // while there is space for the tab
  261. var tabTextWidth = tab.Text.Sum (c => Rune.ColumnWidth (c));
  262. string text = tab.Text.ToString ();
  263. var maxWidth = MaxTabTextWidth;
  264. if (tabTextWidth > maxWidth) {
  265. text = tab.Text.ToString ().Substring (0, (int)maxWidth);
  266. tabTextWidth = (int)maxWidth;
  267. }
  268. // if there is not enough space for this tab
  269. if (i + tabTextWidth >= bounds.Width) {
  270. break;
  271. }
  272. // there is enough space!
  273. yield return new TabToRender (i, tab, text, Equals (SelectedTab, tab), tabTextWidth);
  274. i += tabTextWidth + 1;
  275. }
  276. }
  277. /// <summary>
  278. /// Adds the given <paramref name="tab"/> to <see cref="Tabs"/>
  279. /// </summary>
  280. /// <param name="tab"></param>
  281. /// <param name="andSelect">True to make the newly added Tab the <see cref="SelectedTab"/></param>
  282. public void AddTab (Tab tab, bool andSelect)
  283. {
  284. if (tabs.Contains (tab)) {
  285. return;
  286. }
  287. tabs.Add (tab);
  288. if (SelectedTab == null || andSelect) {
  289. SelectedTab = tab;
  290. EnsureSelectedTabIsVisible ();
  291. tab.View?.SetFocus ();
  292. }
  293. SetNeedsDisplay ();
  294. }
  295. /// <summary>
  296. /// Removes the given <paramref name="tab"/> from <see cref="Tabs"/>.
  297. /// Caller is responsible for disposing the tab's hosted <see cref="Tab.View"/>
  298. /// if appropriate.
  299. /// </summary>
  300. /// <param name="tab"></param>
  301. public void RemoveTab (Tab tab)
  302. {
  303. if (tab == null || !tabs.Contains (tab)) {
  304. return;
  305. }
  306. // what tab was selected before closing
  307. var idx = tabs.IndexOf (tab);
  308. tabs.Remove (tab);
  309. // if the currently selected tab is no longer a member of Tabs
  310. if (SelectedTab == null || !Tabs.Contains (SelectedTab)) {
  311. // select the tab closest to the one that disapeared
  312. var toSelect = Math.Max (idx - 1, 0);
  313. if (toSelect < Tabs.Count) {
  314. SelectedTab = Tabs.ElementAt (toSelect);
  315. } else {
  316. SelectedTab = Tabs.LastOrDefault ();
  317. }
  318. }
  319. EnsureSelectedTabIsVisible ();
  320. SetNeedsDisplay ();
  321. }
  322. #region Nested Types
  323. private class TabToRender {
  324. public int X { get; set; }
  325. public Tab Tab { get; set; }
  326. /// <summary>
  327. /// True if the tab that is being rendered is the selected one
  328. /// </summary>
  329. /// <value></value>
  330. public bool IsSelected { get; set; }
  331. public int Width { get; }
  332. public string TextToRender { get; }
  333. public TabToRender (int x, Tab tab, string textToRender, bool isSelected, int width)
  334. {
  335. X = x;
  336. Tab = tab;
  337. IsSelected = isSelected;
  338. Width = width;
  339. TextToRender = textToRender;
  340. }
  341. }
  342. private class TabRowView : View {
  343. readonly TabView host;
  344. public TabRowView (TabView host)
  345. {
  346. this.host = host;
  347. CanFocus = true;
  348. Height = 1;
  349. Width = Dim.Fill ();
  350. }
  351. /// <summary>
  352. /// Positions the cursor at the start of the currently selected tab
  353. /// </summary>
  354. public override void PositionCursor ()
  355. {
  356. base.PositionCursor ();
  357. var selected = host.CalculateViewport (Bounds).FirstOrDefault (t => Equals (host.SelectedTab, t.Tab));
  358. if (selected == null) {
  359. return;
  360. }
  361. int y;
  362. if (host.Style.TabsOnBottom) {
  363. y = 1;
  364. } else {
  365. y = host.Style.ShowTopLine ? 1 : 0;
  366. }
  367. Move (selected.X, y);
  368. }
  369. public override void Redraw (Rect bounds)
  370. {
  371. base.Redraw (bounds);
  372. var tabLocations = host.CalculateViewport (bounds).ToArray ();
  373. var width = bounds.Width;
  374. Driver.SetAttribute (ColorScheme.Normal);
  375. if (host.Style.ShowTopLine) {
  376. RenderOverline (tabLocations, width);
  377. }
  378. RenderTabLine (tabLocations, width);
  379. RenderUnderline (tabLocations, width);
  380. Driver.SetAttribute (ColorScheme.Normal);
  381. }
  382. /// <summary>
  383. /// Renders the line of the tabs that does not adjoin the content
  384. /// </summary>
  385. /// <param name="tabLocations"></param>
  386. /// <param name="width"></param>
  387. private void RenderOverline (TabToRender [] tabLocations, int width)
  388. {
  389. // if tabs are on the bottom draw the side of the tab that doesn't border the content area at the bottom otherwise the top
  390. int y = host.Style.TabsOnBottom ? 2 : 0;
  391. Move (0, y);
  392. var selected = tabLocations.FirstOrDefault (t => t.IsSelected);
  393. // Clear out everything
  394. Driver.AddStr (new string (' ', width));
  395. // Nothing is selected... odd but we are done
  396. if (selected == null) {
  397. return;
  398. }
  399. Move (selected.X - 1, y);
  400. Driver.AddRune (host.Style.TabsOnBottom ? Driver.LLCorner : Driver.ULCorner);
  401. for (int i = 0; i < selected.Width; i++) {
  402. if (selected.X + i > width) {
  403. // we ran out of space horizontally
  404. return;
  405. }
  406. Driver.AddRune (Driver.HLine);
  407. }
  408. // Add the end of the selected tab
  409. Driver.AddRune (host.Style.TabsOnBottom ? Driver.LRCorner : Driver.URCorner);
  410. }
  411. /// <summary>
  412. /// Renders the line with the tab names in it
  413. /// </summary>
  414. /// <param name="tabLocations"></param>
  415. /// <param name="width"></param>
  416. private void RenderTabLine (TabToRender [] tabLocations, int width)
  417. {
  418. int y;
  419. if (host.Style.TabsOnBottom) {
  420. y = 1;
  421. } else {
  422. y = host.Style.ShowTopLine ? 1 : 0;
  423. }
  424. // clear any old text
  425. Move (0, y);
  426. Driver.AddStr (new string (' ', width));
  427. foreach (var toRender in tabLocations) {
  428. if (toRender.IsSelected) {
  429. Move (toRender.X - 1, y);
  430. Driver.AddRune (Driver.VLine);
  431. }
  432. Move (toRender.X, y);
  433. // if tab is the selected one and focus is inside this control
  434. if (toRender.IsSelected && host.HasFocus) {
  435. if (host.Focused == this) {
  436. // if focus is the tab bar ourself then show that they can switch tabs
  437. Driver.SetAttribute (ColorScheme.HotFocus);
  438. } else {
  439. // Focus is inside the tab
  440. Driver.SetAttribute (ColorScheme.HotNormal);
  441. }
  442. }
  443. Driver.AddStr (toRender.TextToRender);
  444. Driver.SetAttribute (ColorScheme.Normal);
  445. if (toRender.IsSelected) {
  446. Driver.AddRune (Driver.VLine);
  447. }
  448. }
  449. }
  450. /// <summary>
  451. /// Renders the line of the tab that adjoins the content of the tab
  452. /// </summary>
  453. /// <param name="tabLocations"></param>
  454. /// <param name="width"></param>
  455. private void RenderUnderline (TabToRender [] tabLocations, int width)
  456. {
  457. int y = GetUnderlineYPosition ();
  458. Move (0, y);
  459. // If host has no border then we need to draw the solid line first (then we draw gaps over the top)
  460. if (!host.Style.ShowBorder) {
  461. for (int x = 0; x < width; x++) {
  462. Driver.AddRune (Driver.HLine);
  463. }
  464. }
  465. var selected = tabLocations.FirstOrDefault (t => t.IsSelected);
  466. if (selected == null) {
  467. return;
  468. }
  469. Move (selected.X - 1, y);
  470. Driver.AddRune (selected.X == 1 ? Driver.VLine :
  471. (host.Style.TabsOnBottom ? Driver.URCorner : Driver.LRCorner));
  472. Driver.AddStr (new string (' ', selected.Width));
  473. Driver.AddRune (selected.X + selected.Width == width - 1 ?
  474. Driver.VLine :
  475. (host.Style.TabsOnBottom ? Driver.ULCorner : Driver.LLCorner));
  476. // draw scroll indicators
  477. // if there are more tabs to the left not visible
  478. if (host.TabScrollOffset > 0) {
  479. Move (0, y);
  480. // indicate that
  481. Driver.AddRune (Driver.LeftArrow);
  482. }
  483. // if there are mmore tabs to the right not visible
  484. if (ShouldDrawRightScrollIndicator (tabLocations)) {
  485. Move (width - 1, y);
  486. // indicate that
  487. Driver.AddRune (Driver.RightArrow);
  488. }
  489. }
  490. private bool ShouldDrawRightScrollIndicator (TabToRender [] tabLocations)
  491. {
  492. return tabLocations.LastOrDefault ()?.Tab != host.Tabs.LastOrDefault ();
  493. }
  494. private int GetUnderlineYPosition ()
  495. {
  496. if (host.Style.TabsOnBottom) {
  497. return 0;
  498. } else {
  499. return host.Style.ShowTopLine ? 2 : 1;
  500. }
  501. }
  502. public override bool MouseEvent (MouseEvent me)
  503. {
  504. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) &&
  505. !me.Flags.HasFlag (MouseFlags.Button1DoubleClicked) &&
  506. !me.Flags.HasFlag (MouseFlags.Button1TripleClicked))
  507. return false;
  508. if (!HasFocus && CanFocus) {
  509. SetFocus ();
  510. }
  511. if (me.Flags.HasFlag (MouseFlags.Button1Clicked) ||
  512. me.Flags.HasFlag (MouseFlags.Button1DoubleClicked) ||
  513. me.Flags.HasFlag (MouseFlags.Button1TripleClicked)) {
  514. var scrollIndicatorHit = ScreenToScrollIndicator (me.X, me.Y);
  515. if (scrollIndicatorHit != 0) {
  516. host.SwitchTabBy (scrollIndicatorHit);
  517. SetNeedsDisplay ();
  518. return true;
  519. }
  520. var hit = ScreenToTab (me.X, me.Y);
  521. if (hit != null) {
  522. host.SelectedTab = hit;
  523. SetNeedsDisplay ();
  524. return true;
  525. }
  526. }
  527. return false;
  528. }
  529. /// <summary>
  530. /// Calculates whether scroll indicators are visible and if so whether the click
  531. /// was on one of them.
  532. /// </summary>
  533. /// <param name="x"></param>
  534. /// <param name="y"></param>
  535. /// <returns>-1 for click in scroll left, 1 for scroll right or 0 for no hit</returns>
  536. private int ScreenToScrollIndicator (int x, int y)
  537. {
  538. // scroll indicator is showing
  539. if (host.TabScrollOffset > 0 && x == 0) {
  540. return y == GetUnderlineYPosition () ? -1 : 0;
  541. }
  542. // scroll indicator is showing
  543. if (x == Bounds.Width - 1 && ShouldDrawRightScrollIndicator (host.CalculateViewport (Bounds).ToArray ())) {
  544. return y == GetUnderlineYPosition () ? 1 : 0;
  545. }
  546. return 0;
  547. }
  548. /// <summary>
  549. /// Translates the client coordinates of a click into a tab when the click is on top of a tab
  550. /// </summary>
  551. /// <param name="x"></param>
  552. /// <param name="y"></param>
  553. /// <returns></returns>
  554. public Tab ScreenToTab (int x, int y)
  555. {
  556. var tabs = host.CalculateViewport (Bounds);
  557. return tabs.LastOrDefault (t => x >= t.X && x < t.X + t.Width)?.Tab;
  558. }
  559. }
  560. /// <summary>
  561. /// A single tab in a <see cref="TabView"/>
  562. /// </summary>
  563. public class Tab {
  564. private ustring text;
  565. /// <summary>
  566. /// The text to display in a <see cref="TabView"/>
  567. /// </summary>
  568. /// <value></value>
  569. public ustring Text { get => text ?? "Unamed"; set => text = value; }
  570. /// <summary>
  571. /// The control to display when the tab is selected
  572. /// </summary>
  573. /// <value></value>
  574. public View View { get; set; }
  575. /// <summary>
  576. /// Creates a new unamed tab with no controls inside
  577. /// </summary>
  578. public Tab ()
  579. {
  580. }
  581. /// <summary>
  582. /// Creates a new tab with the given text hosting a view
  583. /// </summary>
  584. /// <param name="text"></param>
  585. /// <param name="view"></param>
  586. public Tab (string text, View view)
  587. {
  588. this.Text = text;
  589. this.View = view;
  590. }
  591. }
  592. /// <summary>
  593. /// Describes render stylistic selections of a <see cref="TabView"/>
  594. /// </summary>
  595. public class TabStyle {
  596. /// <summary>
  597. /// True to show the top lip of tabs. False to directly begin with tab text during
  598. /// rendering. When true header line occupies 3 rows, when false only 2.
  599. /// Defaults to true.
  600. ///
  601. /// <para>When <see cref="TabsOnBottom"/> is enabled this instead applies to the
  602. /// bottommost line of the control</para>
  603. /// </summary>
  604. public bool ShowTopLine { get; set; } = true;
  605. /// <summary>
  606. /// True to show a solid box around the edge of the control. Defaults to true.
  607. /// </summary>
  608. public bool ShowBorder { get; set; } = true;
  609. /// <summary>
  610. /// True to render tabs at the bottom of the view instead of the top
  611. /// </summary>
  612. public bool TabsOnBottom { get; set; } = false;
  613. }
  614. /// <summary>
  615. /// Describes a change in <see cref="TabView.SelectedTab"/>
  616. /// </summary>
  617. public class TabChangedEventArgs : EventArgs {
  618. /// <summary>
  619. /// The previously selected tab. May be null
  620. /// </summary>
  621. public Tab OldTab { get; }
  622. /// <summary>
  623. /// The currently selected tab. May be null
  624. /// </summary>
  625. public Tab NewTab { get; }
  626. /// <summary>
  627. /// Documents a tab change
  628. /// </summary>
  629. /// <param name="oldTab"></param>
  630. /// <param name="newTab"></param>
  631. public TabChangedEventArgs (Tab oldTab, Tab newTab)
  632. {
  633. OldTab = oldTab;
  634. NewTab = newTab;
  635. }
  636. }
  637. #endregion
  638. }
  639. }