TabView.cs 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. using System.Diagnostics;
  2. namespace Terminal.Gui;
  3. /// <summary>Control that hosts multiple sub views, presenting a single one at once.</summary>
  4. public class TabView : View
  5. {
  6. /// <summary>The default <see cref="MaxTabTextWidth"/> to set on new <see cref="TabView"/> controls.</summary>
  7. public const uint DefaultMaxTabTextWidth = 30;
  8. /// <summary>
  9. /// This sub view is the main client area of the current tab. It hosts the <see cref="Tab.View"/> of the tab, the
  10. /// <see cref="SelectedTab"/>.
  11. /// </summary>
  12. private readonly View _contentView;
  13. private readonly List<Tab> _tabs = new ();
  14. /// <summary>This sub view is the 2 or 3 line control that represents the actual tabs themselves.</summary>
  15. private readonly TabRowView _tabsBar;
  16. private Tab _selectedTab;
  17. private TabToRender [] _tabLocations;
  18. private int _tabScrollOffset;
  19. /// <summary>Initializes a <see cref="TabView"/> class.</summary>
  20. public TabView ()
  21. {
  22. CanFocus = true;
  23. TabStop = TabBehavior.TabStop; // Because TabView has focusable subviews, it must be a TabGroup
  24. _tabsBar = new TabRowView (this);
  25. _contentView = new View ()
  26. {
  27. //Id = "TabView._contentView",
  28. };
  29. ApplyStyleChanges ();
  30. base.Add (_tabsBar);
  31. base.Add (_contentView);
  32. // Things this view knows how to do
  33. AddCommand (Command.Left, () => SwitchTabBy (-1));
  34. AddCommand (Command.Right, () => SwitchTabBy (1));
  35. AddCommand (
  36. Command.LeftStart,
  37. () =>
  38. {
  39. TabScrollOffset = 0;
  40. SelectedTab = Tabs.FirstOrDefault ();
  41. return true;
  42. }
  43. );
  44. AddCommand (
  45. Command.RightEnd,
  46. () =>
  47. {
  48. TabScrollOffset = Tabs.Count - 1;
  49. SelectedTab = Tabs.LastOrDefault ();
  50. return true;
  51. }
  52. );
  53. AddCommand (
  54. Command.PageDown,
  55. () =>
  56. {
  57. TabScrollOffset += _tabLocations.Length;
  58. SelectedTab = Tabs.ElementAt (TabScrollOffset);
  59. return true;
  60. }
  61. );
  62. AddCommand (
  63. Command.PageUp,
  64. () =>
  65. {
  66. TabScrollOffset -= _tabLocations.Length;
  67. SelectedTab = Tabs.ElementAt (TabScrollOffset);
  68. return true;
  69. }
  70. );
  71. // Default keybindings for this view
  72. KeyBindings.Add (Key.CursorLeft, Command.Left);
  73. KeyBindings.Add (Key.CursorRight, Command.Right);
  74. KeyBindings.Add (Key.Home, Command.LeftStart);
  75. KeyBindings.Add (Key.End, Command.RightEnd);
  76. KeyBindings.Add (Key.PageDown, Command.PageDown);
  77. KeyBindings.Add (Key.PageUp, Command.PageUp);
  78. }
  79. /// <summary>
  80. /// The maximum number of characters to render in a Tab header. This prevents one long tab from pushing out all
  81. /// the others.
  82. /// </summary>
  83. public uint MaxTabTextWidth { get; set; } = DefaultMaxTabTextWidth;
  84. /// <summary>The currently selected member of <see cref="Tabs"/> chosen by the user.</summary>
  85. /// <value></value>
  86. public Tab SelectedTab
  87. {
  88. get => _selectedTab;
  89. set
  90. {
  91. UnSetCurrentTabs ();
  92. Tab old = _selectedTab;
  93. if (_selectedTab is { })
  94. {
  95. if (_selectedTab.View is { })
  96. {
  97. // remove old content
  98. _contentView.Remove (_selectedTab.View);
  99. }
  100. }
  101. _selectedTab = value;
  102. if (value is { })
  103. {
  104. // add new content
  105. if (_selectedTab.View is { })
  106. {
  107. _contentView.Add (_selectedTab.View);
  108. // _contentView.Id = $"_contentView for {_selectedTab.DisplayText}";
  109. }
  110. }
  111. _contentView.CanFocus = _contentView.Subviews.Count (v => v.CanFocus) > 0;
  112. EnsureSelectedTabIsVisible ();
  113. if (old != value)
  114. {
  115. if (old?.HasFocus == true)
  116. {
  117. SelectedTab?.SetFocus ();
  118. }
  119. OnSelectedTabChanged (old, value);
  120. }
  121. SetLayoutNeeded ();
  122. }
  123. }
  124. private TabStyle _style = new ();
  125. /// <summary>Render choices for how to display tabs. After making changes, call <see cref="ApplyStyleChanges()"/>.</summary>
  126. /// <value></value>
  127. public TabStyle Style
  128. {
  129. get => _style;
  130. set
  131. {
  132. if (_style == value)
  133. {
  134. return;
  135. }
  136. _style = value;
  137. SetLayoutNeeded();
  138. }
  139. }
  140. /// <summary>All tabs currently hosted by the control.</summary>
  141. /// <value></value>
  142. public IReadOnlyCollection<Tab> Tabs => _tabs.AsReadOnly ();
  143. /// <summary>When there are too many tabs to render, this indicates the first tab to render on the screen.</summary>
  144. /// <value></value>
  145. public int TabScrollOffset
  146. {
  147. get => _tabScrollOffset;
  148. set
  149. {
  150. _tabScrollOffset = EnsureValidScrollOffsets (value);
  151. SetLayoutNeeded ();
  152. }
  153. }
  154. /// <summary>Adds the given <paramref name="tab"/> to <see cref="Tabs"/>.</summary>
  155. /// <param name="tab"></param>
  156. /// <param name="andSelect">True to make the newly added Tab the <see cref="SelectedTab"/>.</param>
  157. public void AddTab (Tab tab, bool andSelect)
  158. {
  159. if (_tabs.Contains (tab))
  160. {
  161. return;
  162. }
  163. _tabs.Add (tab);
  164. _tabsBar.Add (tab);
  165. if (SelectedTab is null || andSelect)
  166. {
  167. SelectedTab = tab;
  168. EnsureSelectedTabIsVisible ();
  169. tab.View?.SetFocus ();
  170. }
  171. SetLayoutNeeded ();
  172. }
  173. /// <summary>
  174. /// Updates the control to use the latest state settings in <see cref="Style"/>. This can change the size of the
  175. /// client area of the tab (for rendering the selected tab's content). This method includes a call to
  176. /// <see cref="View.SetNeedsDisplay()"/>.
  177. /// </summary>
  178. public void ApplyStyleChanges ()
  179. {
  180. _contentView.BorderStyle = Style.ShowBorder ? LineStyle.Single : LineStyle.None;
  181. _contentView.Width = Dim.Fill ();
  182. if (Style.TabsOnBottom)
  183. {
  184. // Tabs are along the bottom so just dodge the border
  185. if (Style.ShowBorder)
  186. {
  187. _contentView.Border.Thickness = new Thickness (1, 1, 1, 0);
  188. }
  189. _contentView.Y = 0;
  190. int tabHeight = GetTabHeight (false);
  191. // Fill client area leaving space at bottom for tabs
  192. _contentView.Height = Dim.Fill (tabHeight);
  193. _tabsBar.Height = tabHeight;
  194. _tabsBar.Y = Pos.Bottom (_contentView);
  195. }
  196. else
  197. {
  198. // Tabs are along the top
  199. if (Style.ShowBorder)
  200. {
  201. _contentView.Border.Thickness = new Thickness (1, 0, 1, 1);
  202. }
  203. _tabsBar.Y = 0;
  204. int tabHeight = GetTabHeight (true);
  205. //move content down to make space for tabs
  206. _contentView.Y = Pos.Bottom (_tabsBar) ;
  207. // Fill client area leaving space at bottom for border
  208. _contentView.Height = Dim.Fill ();
  209. // The top tab should be 2 or 3 rows high and on the top
  210. _tabsBar.Height = tabHeight;
  211. // Should be able to just use 0 but switching between top/bottom tabs repeatedly breaks in ValidatePosDim if just using the absolute value 0
  212. }
  213. SetLayoutNeeded();
  214. }
  215. /// <summary>Updates <see cref="TabScrollOffset"/> to ensure that <see cref="SelectedTab"/> is visible.</summary>
  216. public void EnsureSelectedTabIsVisible ()
  217. {
  218. if (!IsInitialized || SelectedTab is null)
  219. {
  220. return;
  221. }
  222. // if current viewport does not include the selected tab
  223. if (!CalculateViewport (Viewport).Any (r => Equals (SelectedTab, r.Tab)))
  224. {
  225. // Set scroll offset so the first tab rendered is the
  226. TabScrollOffset = Math.Max (0, Tabs.IndexOf (SelectedTab));
  227. }
  228. }
  229. /// <summary>Updates <see cref="TabScrollOffset"/> to be a valid index of <see cref="Tabs"/>.</summary>
  230. /// <param name="value">The value to validate.</param>
  231. /// <remarks>Changes will not be immediately visible in the display until you call <see cref="View.SetNeedsDisplay()"/>.</remarks>
  232. /// <returns>The valid <see cref="TabScrollOffset"/> for the given value.</returns>
  233. public int EnsureValidScrollOffsets (int value) { return Math.Max (Math.Min (value, Tabs.Count - 1), 0); }
  234. /// <inheritdoc/>
  235. public override void OnDrawContent (Rectangle viewport)
  236. {
  237. Driver?.SetAttribute (GetNormalColor ());
  238. if (Tabs.Any ())
  239. {
  240. Rectangle savedClip = SetClip ();
  241. _tabsBar.OnDrawContent (viewport);
  242. _contentView.SetNeedsDisplay ();
  243. _contentView.Draw ();
  244. if (Driver is { })
  245. {
  246. Driver.Clip = savedClip;
  247. }
  248. }
  249. }
  250. /// <inheritdoc/>
  251. public override void OnDrawContentComplete (Rectangle viewport) { _tabsBar.OnDrawContentComplete (viewport); }
  252. /// <summary>
  253. /// Removes the given <paramref name="tab"/> from <see cref="Tabs"/>. Caller is responsible for disposing the
  254. /// tab's hosted <see cref="Tab.View"/> if appropriate.
  255. /// </summary>
  256. /// <param name="tab"></param>
  257. public void RemoveTab (Tab tab)
  258. {
  259. if (tab is null || !_tabs.Contains (tab))
  260. {
  261. return;
  262. }
  263. // what tab was selected before closing
  264. int idx = _tabs.IndexOf (tab);
  265. _tabs.Remove (tab);
  266. // if the currently selected tab is no longer a member of Tabs
  267. if (SelectedTab is null || !Tabs.Contains (SelectedTab))
  268. {
  269. // select the tab closest to the one that disappeared
  270. int toSelect = Math.Max (idx - 1, 0);
  271. if (toSelect < Tabs.Count)
  272. {
  273. SelectedTab = Tabs.ElementAt (toSelect);
  274. }
  275. else
  276. {
  277. SelectedTab = Tabs.LastOrDefault ();
  278. }
  279. }
  280. EnsureSelectedTabIsVisible ();
  281. SetLayoutNeeded ();
  282. }
  283. /// <summary>Event for when <see cref="SelectedTab"/> changes.</summary>
  284. public event EventHandler<TabChangedEventArgs> SelectedTabChanged;
  285. /// <summary>
  286. /// Changes the <see cref="SelectedTab"/> by the given <paramref name="amount"/>. Positive for right, negative for
  287. /// left. If no tab is currently selected then the first tab will become selected.
  288. /// </summary>
  289. /// <param name="amount"></param>
  290. public bool SwitchTabBy (int amount)
  291. {
  292. if (Tabs.Count == 0)
  293. {
  294. return false;
  295. }
  296. // if there is only one tab anyway or nothing is selected
  297. if (Tabs.Count == 1 || SelectedTab is null)
  298. {
  299. SelectedTab = Tabs.ElementAt (0);
  300. return SelectedTab is { };
  301. }
  302. int currentIdx = Tabs.IndexOf (SelectedTab);
  303. // Currently selected tab has vanished!
  304. if (currentIdx == -1)
  305. {
  306. SelectedTab = Tabs.ElementAt (0);
  307. return true;
  308. }
  309. int newIdx = Math.Max (0, Math.Min (currentIdx + amount, Tabs.Count - 1));
  310. if (newIdx == currentIdx)
  311. {
  312. return false;
  313. }
  314. SelectedTab = _tabs [newIdx];
  315. EnsureSelectedTabIsVisible ();
  316. return true;
  317. }
  318. /// <summary>
  319. /// Event fired when a <see cref="Tab"/> is clicked. Can be used to cancel navigation, show context menu (e.g. on
  320. /// right click) etc.
  321. /// </summary>
  322. public event EventHandler<TabMouseEventArgs> TabClicked;
  323. /// <summary>Disposes the control and all <see cref="Tabs"/>.</summary>
  324. /// <param name="disposing"></param>
  325. protected override void Dispose (bool disposing)
  326. {
  327. base.Dispose (disposing);
  328. // The selected tab will automatically be disposed but
  329. // any tabs not visible will need to be manually disposed
  330. foreach (Tab tab in Tabs)
  331. {
  332. if (!Equals (SelectedTab, tab))
  333. {
  334. tab.View?.Dispose ();
  335. }
  336. }
  337. }
  338. /// <summary>Raises the <see cref="SelectedTabChanged"/> event.</summary>
  339. protected virtual void OnSelectedTabChanged (Tab oldTab, Tab newTab)
  340. {
  341. SelectedTabChanged?.Invoke (this, new TabChangedEventArgs (oldTab, newTab));
  342. }
  343. /// <summary>Returns which tabs to render at each x location.</summary>
  344. /// <returns></returns>
  345. private IEnumerable<TabToRender> CalculateViewport (Rectangle bounds)
  346. {
  347. UnSetCurrentTabs ();
  348. var i = 1;
  349. View prevTab = null;
  350. // Starting at the first or scrolled to tab
  351. foreach (Tab tab in Tabs.Skip (TabScrollOffset))
  352. {
  353. if (prevTab is { })
  354. {
  355. tab.X = Pos.Right (prevTab);
  356. }
  357. else
  358. {
  359. tab.X = 0;
  360. }
  361. tab.Y = 0;
  362. // while there is space for the tab
  363. int tabTextWidth = tab.DisplayText.EnumerateRunes ().Sum (c => c.GetColumns ());
  364. string text = tab.DisplayText;
  365. // The maximum number of characters to use for the tab name as specified
  366. // by the user (MaxTabTextWidth). But not more than the width of the view
  367. // or we won't even be able to render a single tab!
  368. long maxWidth = Math.Max (0, Math.Min (bounds.Width - 3, MaxTabTextWidth));
  369. prevTab = tab;
  370. tab.Width = 2;
  371. tab.Height = Style.ShowTopLine ? 3 : 2;
  372. // if tab view is width <= 3 don't render any tabs
  373. if (maxWidth == 0)
  374. {
  375. tab.Visible = true;
  376. tab.MouseClick += Tab_MouseClick;
  377. yield return new TabToRender (i, tab, string.Empty, Equals (SelectedTab, tab), 0);
  378. break;
  379. }
  380. if (tabTextWidth > maxWidth)
  381. {
  382. text = tab.DisplayText.Substring (0, (int)maxWidth);
  383. tabTextWidth = (int)maxWidth;
  384. }
  385. tab.Width = Math.Max (tabTextWidth + 2, 1);
  386. tab.Height = Style.ShowTopLine ? 3 : 2;
  387. // if there is not enough space for this tab
  388. if (i + tabTextWidth >= bounds.Width)
  389. {
  390. tab.Visible = false;
  391. break;
  392. }
  393. // there is enough space!
  394. tab.Visible = true;
  395. tab.MouseClick += Tab_MouseClick;
  396. yield return new TabToRender (i, tab, text, Equals (SelectedTab, tab), tabTextWidth);
  397. i += tabTextWidth + 1;
  398. }
  399. }
  400. /// <summary>
  401. /// Returns the number of rows occupied by rendering the tabs, this depends on <see cref="TabStyle.ShowTopLine"/>
  402. /// and can be 0 (e.g. if <see cref="TabStyle.TabsOnBottom"/> and you ask for <paramref name="top"/>).
  403. /// </summary>
  404. /// <param name="top">True to measure the space required at the top of the control, false to measure space at the bottom.</param>
  405. /// .
  406. /// <returns></returns>
  407. private int GetTabHeight (bool top)
  408. {
  409. if (top && Style.TabsOnBottom)
  410. {
  411. return 0;
  412. }
  413. if (!top && !Style.TabsOnBottom)
  414. {
  415. return 0;
  416. }
  417. return Style.ShowTopLine ? 3 : 2;
  418. }
  419. private void Tab_MouseClick (object sender, MouseEventArgs e)
  420. {
  421. e.Handled = _tabsBar.NewMouseEvent (e) == true;
  422. }
  423. private void UnSetCurrentTabs ()
  424. {
  425. if (_tabLocations is { })
  426. {
  427. foreach (TabToRender tabToRender in _tabLocations)
  428. {
  429. tabToRender.Tab.MouseClick -= Tab_MouseClick;
  430. tabToRender.Tab.Visible = false;
  431. }
  432. _tabLocations = null;
  433. }
  434. }
  435. /// <summary>Raises the <see cref="TabClicked"/> event.</summary>
  436. /// <param name="tabMouseEventArgs"></param>
  437. private protected virtual void OnTabClicked (TabMouseEventArgs tabMouseEventArgs) { TabClicked?.Invoke (this, tabMouseEventArgs); }
  438. private class TabRowView : View
  439. {
  440. private readonly TabView _host;
  441. private readonly View _leftScrollIndicator;
  442. private readonly View _rightScrollIndicator;
  443. public TabRowView (TabView host)
  444. {
  445. _host = host;
  446. Id = "tabRowView";
  447. CanFocus = true;
  448. Height = 1; // BUGBUG: Views should avoid setting Height as doing so implies Frame.Size == GetContentSize ().
  449. Width = Dim.Fill ();
  450. _rightScrollIndicator = new View
  451. {
  452. Id = "rightScrollIndicator",
  453. Width = 1,
  454. Height = 1,
  455. Visible = false,
  456. Text = Glyphs.RightArrow.ToString ()
  457. };
  458. _rightScrollIndicator.MouseClick += _host.Tab_MouseClick;
  459. _leftScrollIndicator = new View
  460. {
  461. Id = "leftScrollIndicator",
  462. Width = 1,
  463. Height = 1,
  464. Visible = false,
  465. Text = Glyphs.LeftArrow.ToString ()
  466. };
  467. _leftScrollIndicator.MouseClick += _host.Tab_MouseClick;
  468. Add (_rightScrollIndicator, _leftScrollIndicator);
  469. }
  470. protected override bool OnMouseEvent (MouseEventArgs me)
  471. {
  472. Tab hit = me.View is Tab ? (Tab)me.View : null;
  473. if (me.IsSingleClicked)
  474. {
  475. _host.OnTabClicked (new TabMouseEventArgs (hit, me));
  476. // user canceled click
  477. if (me.Handled)
  478. {
  479. return true;
  480. }
  481. }
  482. if (!me.IsSingleDoubleOrTripleClicked)
  483. {
  484. return false;
  485. }
  486. if (!HasFocus && CanFocus)
  487. {
  488. SetFocus ();
  489. }
  490. if (me.IsSingleDoubleOrTripleClicked)
  491. {
  492. var scrollIndicatorHit = 0;
  493. if (me.View is { } && me.View.Id == "rightScrollIndicator")
  494. {
  495. scrollIndicatorHit = 1;
  496. }
  497. else if (me.View is { } && me.View.Id == "leftScrollIndicator")
  498. {
  499. scrollIndicatorHit = -1;
  500. }
  501. if (scrollIndicatorHit != 0)
  502. {
  503. _host.SwitchTabBy (scrollIndicatorHit);
  504. SetLayoutNeeded ();
  505. return true;
  506. }
  507. if (hit is { })
  508. {
  509. _host.SelectedTab = hit;
  510. SetLayoutNeeded ();
  511. return true;
  512. }
  513. }
  514. return false;
  515. }
  516. public override void OnDrawContent (Rectangle viewport)
  517. {
  518. _host._tabLocations = _host.CalculateViewport (Viewport).ToArray ();
  519. // clear any old text
  520. Clear ();
  521. RenderTabLine ();
  522. RenderUnderline ();
  523. Driver?.SetAttribute (HasFocus ? GetFocusColor () : GetNormalColor ());
  524. }
  525. public override void OnDrawContentComplete (Rectangle viewport)
  526. {
  527. if (_host._tabLocations is null)
  528. {
  529. return;
  530. }
  531. TabToRender [] tabLocations = _host._tabLocations;
  532. int selectedTab = -1;
  533. for (var i = 0; i < tabLocations.Length; i++)
  534. {
  535. View tab = tabLocations [i].Tab;
  536. Rectangle vts = tab.ViewportToScreen (tab.Viewport);
  537. var lc = new LineCanvas ();
  538. int selectedOffset = _host.Style.ShowTopLine && tabLocations [i].IsSelected ? 0 : 1;
  539. if (tabLocations [i].IsSelected)
  540. {
  541. selectedTab = i;
  542. if (i == 0 && _host.TabScrollOffset == 0)
  543. {
  544. if (_host.Style.TabsOnBottom)
  545. {
  546. // Upper left vertical line
  547. lc.AddLine (
  548. new Point (vts.X - 1, vts.Y - 1),
  549. -1,
  550. Orientation.Vertical,
  551. tab.BorderStyle
  552. );
  553. }
  554. else
  555. {
  556. // Lower left vertical line
  557. lc.AddLine (
  558. new Point (vts.X - 1, vts.Bottom - selectedOffset),
  559. -1,
  560. Orientation.Vertical,
  561. tab.BorderStyle
  562. );
  563. }
  564. }
  565. else if (i > 0 && i <= tabLocations.Length - 1)
  566. {
  567. if (_host.Style.TabsOnBottom)
  568. {
  569. // URCorner
  570. lc.AddLine (
  571. new Point (vts.X - 1, vts.Y - 1),
  572. 1,
  573. Orientation.Vertical,
  574. tab.BorderStyle
  575. );
  576. lc.AddLine (
  577. new Point (vts.X - 1, vts.Y - 1),
  578. -1,
  579. Orientation.Horizontal,
  580. tab.BorderStyle
  581. );
  582. }
  583. else
  584. {
  585. // LRCorner
  586. lc.AddLine (
  587. new Point (vts.X - 1, vts.Bottom - selectedOffset),
  588. -1,
  589. Orientation.Vertical,
  590. tab.BorderStyle
  591. );
  592. lc.AddLine (
  593. new Point (vts.X - 1, vts.Bottom - selectedOffset),
  594. -1,
  595. Orientation.Horizontal,
  596. tab.BorderStyle
  597. );
  598. }
  599. if (_host.Style.ShowTopLine)
  600. {
  601. if (_host.Style.TabsOnBottom)
  602. {
  603. // Lower left tee
  604. lc.AddLine (
  605. new Point (vts.X - 1, vts.Bottom),
  606. -1,
  607. Orientation.Vertical,
  608. tab.BorderStyle
  609. );
  610. lc.AddLine (
  611. new Point (vts.X - 1, vts.Bottom),
  612. 0,
  613. Orientation.Horizontal,
  614. tab.BorderStyle
  615. );
  616. }
  617. else
  618. {
  619. // Upper left tee
  620. lc.AddLine (
  621. new Point (vts.X - 1, vts.Y - 1),
  622. 1,
  623. Orientation.Vertical,
  624. tab.BorderStyle
  625. );
  626. lc.AddLine (
  627. new Point (vts.X - 1, vts.Y - 1),
  628. 0,
  629. Orientation.Horizontal,
  630. tab.BorderStyle
  631. );
  632. }
  633. }
  634. }
  635. if (i < tabLocations.Length - 1)
  636. {
  637. if (_host.Style.ShowTopLine)
  638. {
  639. if (_host.Style.TabsOnBottom)
  640. {
  641. // Lower right tee
  642. lc.AddLine (
  643. new Point (vts.Right, vts.Bottom),
  644. -1,
  645. Orientation.Vertical,
  646. tab.BorderStyle
  647. );
  648. lc.AddLine (
  649. new Point (vts.Right, vts.Bottom),
  650. 0,
  651. Orientation.Horizontal,
  652. tab.BorderStyle
  653. );
  654. }
  655. else
  656. {
  657. // Upper right tee
  658. lc.AddLine (
  659. new Point (vts.Right, vts.Y - 1),
  660. 1,
  661. Orientation.Vertical,
  662. tab.BorderStyle
  663. );
  664. lc.AddLine (
  665. new Point (vts.Right, vts.Y - 1),
  666. 0,
  667. Orientation.Horizontal,
  668. tab.BorderStyle
  669. );
  670. }
  671. }
  672. }
  673. if (_host.Style.TabsOnBottom)
  674. {
  675. //URCorner
  676. lc.AddLine (
  677. new Point (vts.Right, vts.Y - 1),
  678. 1,
  679. Orientation.Vertical,
  680. tab.BorderStyle
  681. );
  682. lc.AddLine (
  683. new Point (vts.Right, vts.Y - 1),
  684. 1,
  685. Orientation.Horizontal,
  686. tab.BorderStyle
  687. );
  688. }
  689. else
  690. {
  691. //LLCorner
  692. lc.AddLine (
  693. new Point (vts.Right, vts.Bottom - selectedOffset),
  694. -1,
  695. Orientation.Vertical,
  696. tab.BorderStyle
  697. );
  698. lc.AddLine (
  699. new Point (vts.Right, vts.Bottom - selectedOffset),
  700. 1,
  701. Orientation.Horizontal,
  702. tab.BorderStyle
  703. );
  704. }
  705. }
  706. else if (selectedTab == -1)
  707. {
  708. if (i == 0 && string.IsNullOrEmpty (tab.Text))
  709. {
  710. if (_host.Style.TabsOnBottom)
  711. {
  712. if (_host.Style.ShowTopLine)
  713. {
  714. // LLCorner
  715. lc.AddLine (
  716. new Point (vts.X - 1, vts.Bottom),
  717. -1,
  718. Orientation.Vertical,
  719. tab.BorderStyle
  720. );
  721. lc.AddLine (
  722. new Point (vts.X - 1, vts.Bottom),
  723. 1,
  724. Orientation.Horizontal,
  725. tab.BorderStyle
  726. );
  727. }
  728. // ULCorner
  729. lc.AddLine (
  730. new Point (vts.X - 1, vts.Y - 1),
  731. 1,
  732. Orientation.Vertical,
  733. tab.BorderStyle
  734. );
  735. lc.AddLine (
  736. new Point (vts.X - 1, vts.Y - 1),
  737. 1,
  738. Orientation.Horizontal,
  739. tab.BorderStyle
  740. );
  741. }
  742. else
  743. {
  744. if (_host.Style.ShowTopLine)
  745. {
  746. // ULCorner
  747. lc.AddLine (
  748. new Point (vts.X - 1, vts.Y - 1),
  749. 1,
  750. Orientation.Vertical,
  751. tab.BorderStyle
  752. );
  753. lc.AddLine (
  754. new Point (vts.X - 1, vts.Y - 1),
  755. 1,
  756. Orientation.Horizontal,
  757. tab.BorderStyle
  758. );
  759. }
  760. // LLCorner
  761. lc.AddLine (
  762. new Point (vts.X - 1, vts.Bottom),
  763. -1,
  764. Orientation.Vertical,
  765. tab.BorderStyle
  766. );
  767. lc.AddLine (
  768. new Point (vts.X - 1, vts.Bottom),
  769. 1,
  770. Orientation.Horizontal,
  771. tab.BorderStyle
  772. );
  773. }
  774. }
  775. else if (i > 0)
  776. {
  777. if (_host.Style.ShowTopLine || _host.Style.TabsOnBottom)
  778. {
  779. // Upper left tee
  780. lc.AddLine (
  781. new Point (vts.X - 1, vts.Y - 1),
  782. 1,
  783. Orientation.Vertical,
  784. tab.BorderStyle
  785. );
  786. lc.AddLine (
  787. new Point (vts.X - 1, vts.Y - 1),
  788. 0,
  789. Orientation.Horizontal,
  790. tab.BorderStyle
  791. );
  792. }
  793. // Lower left tee
  794. lc.AddLine (
  795. new Point (vts.X - 1, vts.Bottom),
  796. -1,
  797. Orientation.Vertical,
  798. tab.BorderStyle
  799. );
  800. lc.AddLine (
  801. new Point (vts.X - 1, vts.Bottom),
  802. 0,
  803. Orientation.Horizontal,
  804. tab.BorderStyle
  805. );
  806. }
  807. }
  808. else if (i < tabLocations.Length - 1)
  809. {
  810. if (_host.Style.ShowTopLine)
  811. {
  812. // Upper right tee
  813. lc.AddLine (
  814. new Point (vts.Right, vts.Y - 1),
  815. 1,
  816. Orientation.Vertical,
  817. tab.BorderStyle
  818. );
  819. lc.AddLine (
  820. new Point (vts.Right, vts.Y - 1),
  821. 0,
  822. Orientation.Horizontal,
  823. tab.BorderStyle
  824. );
  825. }
  826. if (_host.Style.ShowTopLine || !_host.Style.TabsOnBottom)
  827. {
  828. // Lower right tee
  829. lc.AddLine (
  830. new Point (vts.Right, vts.Bottom),
  831. -1,
  832. Orientation.Vertical,
  833. tab.BorderStyle
  834. );
  835. lc.AddLine (
  836. new Point (vts.Right, vts.Bottom),
  837. 0,
  838. Orientation.Horizontal,
  839. tab.BorderStyle
  840. );
  841. }
  842. else
  843. {
  844. // Upper right tee
  845. lc.AddLine (
  846. new Point (vts.Right, vts.Y - 1),
  847. 1,
  848. Orientation.Vertical,
  849. tab.BorderStyle
  850. );
  851. lc.AddLine (
  852. new Point (vts.Right, vts.Y - 1),
  853. 0,
  854. Orientation.Horizontal,
  855. tab.BorderStyle
  856. );
  857. }
  858. }
  859. if (i == 0 && i != selectedTab && _host.TabScrollOffset == 0 && _host.Style.ShowBorder)
  860. {
  861. if (_host.Style.TabsOnBottom)
  862. {
  863. // Upper left vertical line
  864. lc.AddLine (
  865. new Point (vts.X - 1, vts.Y - 1),
  866. 0,
  867. Orientation.Vertical,
  868. tab.BorderStyle
  869. );
  870. lc.AddLine (
  871. new Point (vts.X - 1, vts.Y - 1),
  872. 1,
  873. Orientation.Horizontal,
  874. tab.BorderStyle
  875. );
  876. }
  877. else
  878. {
  879. // Lower left vertical line
  880. lc.AddLine (
  881. new Point (vts.X - 1, vts.Bottom),
  882. 0,
  883. Orientation.Vertical,
  884. tab.BorderStyle
  885. );
  886. lc.AddLine (
  887. new Point (vts.X - 1, vts.Bottom),
  888. 1,
  889. Orientation.Horizontal,
  890. tab.BorderStyle
  891. );
  892. }
  893. }
  894. if (i == tabLocations.Length - 1 && i != selectedTab)
  895. {
  896. if (_host.Style.TabsOnBottom)
  897. {
  898. // Upper right tee
  899. lc.AddLine (
  900. new Point (vts.Right, vts.Y - 1),
  901. 1,
  902. Orientation.Vertical,
  903. tab.BorderStyle
  904. );
  905. lc.AddLine (
  906. new Point (vts.Right, vts.Y - 1),
  907. 0,
  908. Orientation.Horizontal,
  909. tab.BorderStyle
  910. );
  911. }
  912. else
  913. {
  914. // Lower right tee
  915. lc.AddLine (
  916. new Point (vts.Right, vts.Bottom),
  917. -1,
  918. Orientation.Vertical,
  919. tab.BorderStyle
  920. );
  921. lc.AddLine (
  922. new Point (vts.Right, vts.Bottom),
  923. 0,
  924. Orientation.Horizontal,
  925. tab.BorderStyle
  926. );
  927. }
  928. }
  929. if (i == tabLocations.Length - 1)
  930. {
  931. var arrowOffset = 1;
  932. int lastSelectedTab = !_host.Style.ShowTopLine && i == selectedTab ? 1 :
  933. _host.Style.TabsOnBottom ? 1 : 0;
  934. Rectangle tabsBarVts = ViewportToScreen (Viewport);
  935. int lineLength = tabsBarVts.Right - vts.Right;
  936. // Right horizontal line
  937. if (ShouldDrawRightScrollIndicator ())
  938. {
  939. if (lineLength - arrowOffset > 0)
  940. {
  941. if (_host.Style.TabsOnBottom)
  942. {
  943. lc.AddLine (
  944. new Point (vts.Right, vts.Y - lastSelectedTab),
  945. lineLength - arrowOffset,
  946. Orientation.Horizontal,
  947. tab.BorderStyle
  948. );
  949. }
  950. else
  951. {
  952. lc.AddLine (
  953. new Point (
  954. vts.Right,
  955. vts.Bottom - lastSelectedTab
  956. ),
  957. lineLength - arrowOffset,
  958. Orientation.Horizontal,
  959. tab.BorderStyle
  960. );
  961. }
  962. }
  963. }
  964. else
  965. {
  966. if (_host.Style.TabsOnBottom)
  967. {
  968. lc.AddLine (
  969. new Point (vts.Right, vts.Y - lastSelectedTab),
  970. lineLength,
  971. Orientation.Horizontal,
  972. tab.BorderStyle
  973. );
  974. }
  975. else
  976. {
  977. lc.AddLine (
  978. new Point (vts.Right, vts.Bottom - lastSelectedTab),
  979. lineLength,
  980. Orientation.Horizontal,
  981. tab.BorderStyle
  982. );
  983. }
  984. if (_host.Style.ShowBorder)
  985. {
  986. if (_host.Style.TabsOnBottom)
  987. {
  988. // More LRCorner
  989. lc.AddLine (
  990. new Point (
  991. tabsBarVts.Right - 1,
  992. vts.Y - lastSelectedTab
  993. ),
  994. -1,
  995. Orientation.Vertical,
  996. tab.BorderStyle
  997. );
  998. }
  999. else
  1000. {
  1001. // More URCorner
  1002. lc.AddLine (
  1003. new Point (
  1004. tabsBarVts.Right - 1,
  1005. vts.Bottom - lastSelectedTab
  1006. ),
  1007. 1,
  1008. Orientation.Vertical,
  1009. tab.BorderStyle
  1010. );
  1011. }
  1012. }
  1013. }
  1014. }
  1015. tab.LineCanvas.Merge (lc);
  1016. tab.OnRenderLineCanvas ();
  1017. }
  1018. }
  1019. private int GetUnderlineYPosition ()
  1020. {
  1021. if (_host.Style.TabsOnBottom)
  1022. {
  1023. return 0;
  1024. }
  1025. return _host.Style.ShowTopLine ? 2 : 1;
  1026. }
  1027. /// <summary>Renders the line with the tab names in it.</summary>
  1028. private void RenderTabLine ()
  1029. {
  1030. TabToRender [] tabLocations = _host._tabLocations;
  1031. int y;
  1032. if (_host.Style.TabsOnBottom)
  1033. {
  1034. y = 1;
  1035. }
  1036. else
  1037. {
  1038. y = _host.Style.ShowTopLine ? 1 : 0;
  1039. }
  1040. View selected = null;
  1041. int topLine = _host.Style.ShowTopLine ? 1 : 0;
  1042. int width = Viewport.Width;
  1043. foreach (TabToRender toRender in tabLocations)
  1044. {
  1045. Tab tab = toRender.Tab;
  1046. if (toRender.IsSelected)
  1047. {
  1048. selected = tab;
  1049. if (_host.Style.TabsOnBottom)
  1050. {
  1051. tab.Border.Thickness = new Thickness (1, 0, 1, topLine);
  1052. tab.Margin.Thickness = new Thickness (0, 1, 0, 0);
  1053. }
  1054. else
  1055. {
  1056. tab.Border.Thickness = new Thickness (1, topLine, 1, 0);
  1057. tab.Margin.Thickness = new Thickness (0, 0, 0, topLine);
  1058. }
  1059. }
  1060. else if (selected is null)
  1061. {
  1062. if (_host.Style.TabsOnBottom)
  1063. {
  1064. tab.Border.Thickness = new Thickness (1, 1, 0, topLine);
  1065. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  1066. }
  1067. else
  1068. {
  1069. tab.Border.Thickness = new Thickness (1, topLine, 0, 1);
  1070. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  1071. }
  1072. tab.Width = Math.Max (tab.Width.GetAnchor (0) - 1, 1);
  1073. }
  1074. else
  1075. {
  1076. if (_host.Style.TabsOnBottom)
  1077. {
  1078. tab.Border.Thickness = new Thickness (0, 1, 1, topLine);
  1079. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  1080. }
  1081. else
  1082. {
  1083. tab.Border.Thickness = new Thickness (0, topLine, 1, 1);
  1084. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  1085. }
  1086. tab.Width = Math.Max (tab.Width.GetAnchor (0) - 1, 1);
  1087. }
  1088. tab.Text = toRender.TextToRender;
  1089. // BUGBUG: Layout should only be called from Mainloop iteration!
  1090. Layout ();
  1091. tab.OnDrawAdornments ();
  1092. Attribute prevAttr = Driver?.GetAttribute () ?? Attribute.Default;
  1093. // if tab is the selected one and focus is inside this control
  1094. if (toRender.IsSelected && _host.HasFocus)
  1095. {
  1096. if (_host.Focused == this)
  1097. {
  1098. // if focus is the tab bar itself then show that they can switch tabs
  1099. prevAttr = ColorScheme.HotFocus;
  1100. }
  1101. else
  1102. {
  1103. // Focus is inside the tab
  1104. prevAttr = ColorScheme.HotNormal;
  1105. }
  1106. }
  1107. tab.TextFormatter.Draw (
  1108. tab.ViewportToScreen (tab.Viewport),
  1109. prevAttr,
  1110. ColorScheme.HotNormal
  1111. );
  1112. tab.OnRenderLineCanvas ();
  1113. Driver?.SetAttribute (GetNormalColor ());
  1114. }
  1115. }
  1116. /// <summary>Renders the line of the tab that adjoins the content of the tab.</summary>
  1117. private void RenderUnderline ()
  1118. {
  1119. int y = GetUnderlineYPosition ();
  1120. TabToRender selected = _host._tabLocations.FirstOrDefault (t => t.IsSelected);
  1121. if (selected is null)
  1122. {
  1123. return;
  1124. }
  1125. // draw scroll indicators
  1126. // if there are more tabs to the left not visible
  1127. if (_host.TabScrollOffset > 0)
  1128. {
  1129. _leftScrollIndicator.X = 0;
  1130. _leftScrollIndicator.Y = y;
  1131. // indicate that
  1132. _leftScrollIndicator.Visible = true;
  1133. // Ensures this is clicked instead of the first tab
  1134. MoveSubviewToEnd (_leftScrollIndicator);
  1135. _leftScrollIndicator.Draw ();
  1136. }
  1137. else
  1138. {
  1139. _leftScrollIndicator.Visible = false;
  1140. }
  1141. // if there are more tabs to the right not visible
  1142. if (ShouldDrawRightScrollIndicator ())
  1143. {
  1144. _rightScrollIndicator.X = Viewport.Width - 1;
  1145. _rightScrollIndicator.Y = y;
  1146. // indicate that
  1147. _rightScrollIndicator.Visible = true;
  1148. // Ensures this is clicked instead of the last tab if under this
  1149. MoveSubviewToStart (_rightScrollIndicator);
  1150. _rightScrollIndicator.Draw ();
  1151. }
  1152. else
  1153. {
  1154. _rightScrollIndicator.Visible = false;
  1155. }
  1156. }
  1157. private bool ShouldDrawRightScrollIndicator () { return _host._tabLocations.LastOrDefault ()?.Tab != _host.Tabs.LastOrDefault (); }
  1158. }
  1159. private class TabToRender
  1160. {
  1161. public TabToRender (int x, Tab tab, string textToRender, bool isSelected, int width)
  1162. {
  1163. X = x;
  1164. Tab = tab;
  1165. IsSelected = isSelected;
  1166. Width = width;
  1167. TextToRender = textToRender;
  1168. }
  1169. /// <summary>True if the tab that is being rendered is the selected one.</summary>
  1170. /// <value></value>
  1171. public bool IsSelected { get; }
  1172. public Tab Tab { get; }
  1173. public string TextToRender { get; }
  1174. public int Width { get; }
  1175. public int X { get; set; }
  1176. }
  1177. }