TabView.cs 21 KB

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