TabView.cs 49 KB

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