TabView.cs 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  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. Driver.Clip = savedClip;
  245. }
  246. }
  247. /// <inheritdoc/>
  248. public override void OnDrawContentComplete (Rectangle viewport) { _tabsBar.OnDrawContentComplete (viewport); }
  249. /// <summary>
  250. /// Removes the given <paramref name="tab"/> from <see cref="Tabs"/>. Caller is responsible for disposing the
  251. /// tab's hosted <see cref="Tab.View"/> if appropriate.
  252. /// </summary>
  253. /// <param name="tab"></param>
  254. public void RemoveTab (Tab tab)
  255. {
  256. if (tab is null || !_tabs.Contains (tab))
  257. {
  258. return;
  259. }
  260. // what tab was selected before closing
  261. int idx = _tabs.IndexOf (tab);
  262. _tabs.Remove (tab);
  263. // if the currently selected tab is no longer a member of Tabs
  264. if (SelectedTab is null || !Tabs.Contains (SelectedTab))
  265. {
  266. // select the tab closest to the one that disappeared
  267. int toSelect = Math.Max (idx - 1, 0);
  268. if (toSelect < Tabs.Count)
  269. {
  270. SelectedTab = Tabs.ElementAt (toSelect);
  271. }
  272. else
  273. {
  274. SelectedTab = Tabs.LastOrDefault ();
  275. }
  276. }
  277. EnsureSelectedTabIsVisible ();
  278. SetLayoutNeeded ();
  279. }
  280. /// <summary>Event for when <see cref="SelectedTab"/> changes.</summary>
  281. public event EventHandler<TabChangedEventArgs> SelectedTabChanged;
  282. /// <summary>
  283. /// Changes the <see cref="SelectedTab"/> by the given <paramref name="amount"/>. Positive for right, negative for
  284. /// left. If no tab is currently selected then the first tab will become selected.
  285. /// </summary>
  286. /// <param name="amount"></param>
  287. public bool SwitchTabBy (int amount)
  288. {
  289. if (Tabs.Count == 0)
  290. {
  291. return false;
  292. }
  293. // if there is only one tab anyway or nothing is selected
  294. if (Tabs.Count == 1 || SelectedTab is null)
  295. {
  296. SelectedTab = Tabs.ElementAt (0);
  297. return SelectedTab is { };
  298. }
  299. int currentIdx = Tabs.IndexOf (SelectedTab);
  300. // Currently selected tab has vanished!
  301. if (currentIdx == -1)
  302. {
  303. SelectedTab = Tabs.ElementAt (0);
  304. return true;
  305. }
  306. int newIdx = Math.Max (0, Math.Min (currentIdx + amount, Tabs.Count - 1));
  307. if (newIdx == currentIdx)
  308. {
  309. return false;
  310. }
  311. SelectedTab = _tabs [newIdx];
  312. EnsureSelectedTabIsVisible ();
  313. return true;
  314. }
  315. /// <summary>
  316. /// Event fired when a <see cref="Tab"/> is clicked. Can be used to cancel navigation, show context menu (e.g. on
  317. /// right click) etc.
  318. /// </summary>
  319. public event EventHandler<TabMouseEventArgs> TabClicked;
  320. /// <summary>Disposes the control and all <see cref="Tabs"/>.</summary>
  321. /// <param name="disposing"></param>
  322. protected override void Dispose (bool disposing)
  323. {
  324. base.Dispose (disposing);
  325. // The selected tab will automatically be disposed but
  326. // any tabs not visible will need to be manually disposed
  327. foreach (Tab tab in Tabs)
  328. {
  329. if (!Equals (SelectedTab, tab))
  330. {
  331. tab.View?.Dispose ();
  332. }
  333. }
  334. }
  335. /// <summary>Raises the <see cref="SelectedTabChanged"/> event.</summary>
  336. protected virtual void OnSelectedTabChanged (Tab oldTab, Tab newTab)
  337. {
  338. SelectedTabChanged?.Invoke (this, new TabChangedEventArgs (oldTab, newTab));
  339. }
  340. /// <summary>Returns which tabs to render at each x location.</summary>
  341. /// <returns></returns>
  342. private IEnumerable<TabToRender> CalculateViewport (Rectangle bounds)
  343. {
  344. UnSetCurrentTabs ();
  345. var i = 1;
  346. View prevTab = null;
  347. // Starting at the first or scrolled to tab
  348. foreach (Tab tab in Tabs.Skip (TabScrollOffset))
  349. {
  350. if (prevTab is { })
  351. {
  352. tab.X = Pos.Right (prevTab);
  353. }
  354. else
  355. {
  356. tab.X = 0;
  357. }
  358. tab.Y = 0;
  359. // while there is space for the tab
  360. int tabTextWidth = tab.DisplayText.EnumerateRunes ().Sum (c => c.GetColumns ());
  361. string text = tab.DisplayText;
  362. // The maximum number of characters to use for the tab name as specified
  363. // by the user (MaxTabTextWidth). But not more than the width of the view
  364. // or we won't even be able to render a single tab!
  365. long maxWidth = Math.Max (0, Math.Min (bounds.Width - 3, MaxTabTextWidth));
  366. prevTab = tab;
  367. tab.Width = 2;
  368. tab.Height = Style.ShowTopLine ? 3 : 2;
  369. // if tab view is width <= 3 don't render any tabs
  370. if (maxWidth == 0)
  371. {
  372. tab.Visible = true;
  373. tab.MouseClick += Tab_MouseClick;
  374. yield return new TabToRender (i, tab, string.Empty, Equals (SelectedTab, tab), 0);
  375. break;
  376. }
  377. if (tabTextWidth > maxWidth)
  378. {
  379. text = tab.DisplayText.Substring (0, (int)maxWidth);
  380. tabTextWidth = (int)maxWidth;
  381. }
  382. tab.Width = Math.Max (tabTextWidth + 2, 1);
  383. tab.Height = Style.ShowTopLine ? 3 : 2;
  384. // if there is not enough space for this tab
  385. if (i + tabTextWidth >= bounds.Width)
  386. {
  387. tab.Visible = false;
  388. break;
  389. }
  390. // there is enough space!
  391. tab.Visible = true;
  392. tab.MouseClick += Tab_MouseClick;
  393. yield return new TabToRender (i, tab, text, Equals (SelectedTab, tab), tabTextWidth);
  394. i += tabTextWidth + 1;
  395. }
  396. }
  397. /// <summary>
  398. /// Returns the number of rows occupied by rendering the tabs, this depends on <see cref="TabStyle.ShowTopLine"/>
  399. /// and can be 0 (e.g. if <see cref="TabStyle.TabsOnBottom"/> and you ask for <paramref name="top"/>).
  400. /// </summary>
  401. /// <param name="top">True to measure the space required at the top of the control, false to measure space at the bottom.</param>
  402. /// .
  403. /// <returns></returns>
  404. private int GetTabHeight (bool top)
  405. {
  406. if (top && Style.TabsOnBottom)
  407. {
  408. return 0;
  409. }
  410. if (!top && !Style.TabsOnBottom)
  411. {
  412. return 0;
  413. }
  414. return Style.ShowTopLine ? 3 : 2;
  415. }
  416. private void Tab_MouseClick (object sender, MouseEventArgs e)
  417. {
  418. e.Handled = _tabsBar.NewMouseEvent (e) == true;
  419. }
  420. private void UnSetCurrentTabs ()
  421. {
  422. if (_tabLocations is { })
  423. {
  424. foreach (TabToRender tabToRender in _tabLocations)
  425. {
  426. tabToRender.Tab.MouseClick -= Tab_MouseClick;
  427. tabToRender.Tab.Visible = false;
  428. }
  429. _tabLocations = null;
  430. }
  431. }
  432. /// <summary>Raises the <see cref="TabClicked"/> event.</summary>
  433. /// <param name="tabMouseEventArgs"></param>
  434. private protected virtual void OnTabClicked (TabMouseEventArgs tabMouseEventArgs) { TabClicked?.Invoke (this, tabMouseEventArgs); }
  435. private class TabRowView : View
  436. {
  437. private readonly TabView _host;
  438. private readonly View _leftScrollIndicator;
  439. private readonly View _rightScrollIndicator;
  440. public TabRowView (TabView host)
  441. {
  442. _host = host;
  443. Id = "tabRowView";
  444. CanFocus = true;
  445. Height = 1; // BUGBUG: Views should avoid setting Height as doing so implies Frame.Size == GetContentSize ().
  446. Width = Dim.Fill ();
  447. _rightScrollIndicator = new View
  448. {
  449. Id = "rightScrollIndicator",
  450. Width = 1,
  451. Height = 1,
  452. Visible = false,
  453. Text = Glyphs.RightArrow.ToString ()
  454. };
  455. _rightScrollIndicator.MouseClick += _host.Tab_MouseClick;
  456. _leftScrollIndicator = new View
  457. {
  458. Id = "leftScrollIndicator",
  459. Width = 1,
  460. Height = 1,
  461. Visible = false,
  462. Text = Glyphs.LeftArrow.ToString ()
  463. };
  464. _leftScrollIndicator.MouseClick += _host.Tab_MouseClick;
  465. Add (_rightScrollIndicator, _leftScrollIndicator);
  466. }
  467. protected override bool OnMouseEvent (MouseEventArgs me)
  468. {
  469. Tab hit = me.View is Tab ? (Tab)me.View : null;
  470. if (me.IsSingleClicked)
  471. {
  472. _host.OnTabClicked (new TabMouseEventArgs (hit, me));
  473. // user canceled click
  474. if (me.Handled)
  475. {
  476. return true;
  477. }
  478. }
  479. if (!me.IsSingleDoubleOrTripleClicked)
  480. {
  481. return false;
  482. }
  483. if (!HasFocus && CanFocus)
  484. {
  485. SetFocus ();
  486. }
  487. if (me.IsSingleDoubleOrTripleClicked)
  488. {
  489. var scrollIndicatorHit = 0;
  490. if (me.View is { } && me.View.Id == "rightScrollIndicator")
  491. {
  492. scrollIndicatorHit = 1;
  493. }
  494. else if (me.View is { } && me.View.Id == "leftScrollIndicator")
  495. {
  496. scrollIndicatorHit = -1;
  497. }
  498. if (scrollIndicatorHit != 0)
  499. {
  500. _host.SwitchTabBy (scrollIndicatorHit);
  501. SetLayoutNeeded ();
  502. return true;
  503. }
  504. if (hit is { })
  505. {
  506. _host.SelectedTab = hit;
  507. SetLayoutNeeded ();
  508. return true;
  509. }
  510. }
  511. return false;
  512. }
  513. public override void OnDrawContent (Rectangle viewport)
  514. {
  515. _host._tabLocations = _host.CalculateViewport (Viewport).ToArray ();
  516. // clear any old text
  517. Clear ();
  518. RenderTabLine ();
  519. RenderUnderline ();
  520. Driver.SetAttribute (HasFocus ? GetFocusColor () : GetNormalColor ());
  521. }
  522. public override void OnDrawContentComplete (Rectangle viewport)
  523. {
  524. if (_host._tabLocations is null)
  525. {
  526. return;
  527. }
  528. TabToRender [] tabLocations = _host._tabLocations;
  529. int selectedTab = -1;
  530. for (var i = 0; i < tabLocations.Length; i++)
  531. {
  532. View tab = tabLocations [i].Tab;
  533. Rectangle vts = tab.ViewportToScreen (tab.Viewport);
  534. var lc = new LineCanvas ();
  535. int selectedOffset = _host.Style.ShowTopLine && tabLocations [i].IsSelected ? 0 : 1;
  536. if (tabLocations [i].IsSelected)
  537. {
  538. selectedTab = i;
  539. if (i == 0 && _host.TabScrollOffset == 0)
  540. {
  541. if (_host.Style.TabsOnBottom)
  542. {
  543. // Upper left vertical line
  544. lc.AddLine (
  545. new Point (vts.X - 1, vts.Y - 1),
  546. -1,
  547. Orientation.Vertical,
  548. tab.BorderStyle
  549. );
  550. }
  551. else
  552. {
  553. // Lower left vertical line
  554. lc.AddLine (
  555. new Point (vts.X - 1, vts.Bottom - selectedOffset),
  556. -1,
  557. Orientation.Vertical,
  558. tab.BorderStyle
  559. );
  560. }
  561. }
  562. else if (i > 0 && i <= tabLocations.Length - 1)
  563. {
  564. if (_host.Style.TabsOnBottom)
  565. {
  566. // URCorner
  567. lc.AddLine (
  568. new Point (vts.X - 1, vts.Y - 1),
  569. 1,
  570. Orientation.Vertical,
  571. tab.BorderStyle
  572. );
  573. lc.AddLine (
  574. new Point (vts.X - 1, vts.Y - 1),
  575. -1,
  576. Orientation.Horizontal,
  577. tab.BorderStyle
  578. );
  579. }
  580. else
  581. {
  582. // LRCorner
  583. lc.AddLine (
  584. new Point (vts.X - 1, vts.Bottom - selectedOffset),
  585. -1,
  586. Orientation.Vertical,
  587. tab.BorderStyle
  588. );
  589. lc.AddLine (
  590. new Point (vts.X - 1, vts.Bottom - selectedOffset),
  591. -1,
  592. Orientation.Horizontal,
  593. tab.BorderStyle
  594. );
  595. }
  596. if (_host.Style.ShowTopLine)
  597. {
  598. if (_host.Style.TabsOnBottom)
  599. {
  600. // Lower left tee
  601. lc.AddLine (
  602. new Point (vts.X - 1, vts.Bottom),
  603. -1,
  604. Orientation.Vertical,
  605. tab.BorderStyle
  606. );
  607. lc.AddLine (
  608. new Point (vts.X - 1, vts.Bottom),
  609. 0,
  610. Orientation.Horizontal,
  611. tab.BorderStyle
  612. );
  613. }
  614. else
  615. {
  616. // Upper left tee
  617. lc.AddLine (
  618. new Point (vts.X - 1, vts.Y - 1),
  619. 1,
  620. Orientation.Vertical,
  621. tab.BorderStyle
  622. );
  623. lc.AddLine (
  624. new Point (vts.X - 1, vts.Y - 1),
  625. 0,
  626. Orientation.Horizontal,
  627. tab.BorderStyle
  628. );
  629. }
  630. }
  631. }
  632. if (i < tabLocations.Length - 1)
  633. {
  634. if (_host.Style.ShowTopLine)
  635. {
  636. if (_host.Style.TabsOnBottom)
  637. {
  638. // Lower right tee
  639. lc.AddLine (
  640. new Point (vts.Right, vts.Bottom),
  641. -1,
  642. Orientation.Vertical,
  643. tab.BorderStyle
  644. );
  645. lc.AddLine (
  646. new Point (vts.Right, vts.Bottom),
  647. 0,
  648. Orientation.Horizontal,
  649. tab.BorderStyle
  650. );
  651. }
  652. else
  653. {
  654. // Upper right tee
  655. lc.AddLine (
  656. new Point (vts.Right, vts.Y - 1),
  657. 1,
  658. Orientation.Vertical,
  659. tab.BorderStyle
  660. );
  661. lc.AddLine (
  662. new Point (vts.Right, vts.Y - 1),
  663. 0,
  664. Orientation.Horizontal,
  665. tab.BorderStyle
  666. );
  667. }
  668. }
  669. }
  670. if (_host.Style.TabsOnBottom)
  671. {
  672. //URCorner
  673. lc.AddLine (
  674. new Point (vts.Right, vts.Y - 1),
  675. 1,
  676. Orientation.Vertical,
  677. tab.BorderStyle
  678. );
  679. lc.AddLine (
  680. new Point (vts.Right, vts.Y - 1),
  681. 1,
  682. Orientation.Horizontal,
  683. tab.BorderStyle
  684. );
  685. }
  686. else
  687. {
  688. //LLCorner
  689. lc.AddLine (
  690. new Point (vts.Right, vts.Bottom - selectedOffset),
  691. -1,
  692. Orientation.Vertical,
  693. tab.BorderStyle
  694. );
  695. lc.AddLine (
  696. new Point (vts.Right, vts.Bottom - selectedOffset),
  697. 1,
  698. Orientation.Horizontal,
  699. tab.BorderStyle
  700. );
  701. }
  702. }
  703. else if (selectedTab == -1)
  704. {
  705. if (i == 0 && string.IsNullOrEmpty (tab.Text))
  706. {
  707. if (_host.Style.TabsOnBottom)
  708. {
  709. if (_host.Style.ShowTopLine)
  710. {
  711. // LLCorner
  712. lc.AddLine (
  713. new Point (vts.X - 1, vts.Bottom),
  714. -1,
  715. Orientation.Vertical,
  716. tab.BorderStyle
  717. );
  718. lc.AddLine (
  719. new Point (vts.X - 1, vts.Bottom),
  720. 1,
  721. Orientation.Horizontal,
  722. tab.BorderStyle
  723. );
  724. }
  725. // ULCorner
  726. lc.AddLine (
  727. new Point (vts.X - 1, vts.Y - 1),
  728. 1,
  729. Orientation.Vertical,
  730. tab.BorderStyle
  731. );
  732. lc.AddLine (
  733. new Point (vts.X - 1, vts.Y - 1),
  734. 1,
  735. Orientation.Horizontal,
  736. tab.BorderStyle
  737. );
  738. }
  739. else
  740. {
  741. if (_host.Style.ShowTopLine)
  742. {
  743. // ULCorner
  744. lc.AddLine (
  745. new Point (vts.X - 1, vts.Y - 1),
  746. 1,
  747. Orientation.Vertical,
  748. tab.BorderStyle
  749. );
  750. lc.AddLine (
  751. new Point (vts.X - 1, vts.Y - 1),
  752. 1,
  753. Orientation.Horizontal,
  754. tab.BorderStyle
  755. );
  756. }
  757. // LLCorner
  758. lc.AddLine (
  759. new Point (vts.X - 1, vts.Bottom),
  760. -1,
  761. Orientation.Vertical,
  762. tab.BorderStyle
  763. );
  764. lc.AddLine (
  765. new Point (vts.X - 1, vts.Bottom),
  766. 1,
  767. Orientation.Horizontal,
  768. tab.BorderStyle
  769. );
  770. }
  771. }
  772. else if (i > 0)
  773. {
  774. if (_host.Style.ShowTopLine || _host.Style.TabsOnBottom)
  775. {
  776. // Upper left tee
  777. lc.AddLine (
  778. new Point (vts.X - 1, vts.Y - 1),
  779. 1,
  780. Orientation.Vertical,
  781. tab.BorderStyle
  782. );
  783. lc.AddLine (
  784. new Point (vts.X - 1, vts.Y - 1),
  785. 0,
  786. Orientation.Horizontal,
  787. tab.BorderStyle
  788. );
  789. }
  790. // Lower left tee
  791. lc.AddLine (
  792. new Point (vts.X - 1, vts.Bottom),
  793. -1,
  794. Orientation.Vertical,
  795. tab.BorderStyle
  796. );
  797. lc.AddLine (
  798. new Point (vts.X - 1, vts.Bottom),
  799. 0,
  800. Orientation.Horizontal,
  801. tab.BorderStyle
  802. );
  803. }
  804. }
  805. else if (i < tabLocations.Length - 1)
  806. {
  807. if (_host.Style.ShowTopLine)
  808. {
  809. // Upper right tee
  810. lc.AddLine (
  811. new Point (vts.Right, vts.Y - 1),
  812. 1,
  813. Orientation.Vertical,
  814. tab.BorderStyle
  815. );
  816. lc.AddLine (
  817. new Point (vts.Right, vts.Y - 1),
  818. 0,
  819. Orientation.Horizontal,
  820. tab.BorderStyle
  821. );
  822. }
  823. if (_host.Style.ShowTopLine || !_host.Style.TabsOnBottom)
  824. {
  825. // Lower right tee
  826. lc.AddLine (
  827. new Point (vts.Right, vts.Bottom),
  828. -1,
  829. Orientation.Vertical,
  830. tab.BorderStyle
  831. );
  832. lc.AddLine (
  833. new Point (vts.Right, vts.Bottom),
  834. 0,
  835. Orientation.Horizontal,
  836. tab.BorderStyle
  837. );
  838. }
  839. else
  840. {
  841. // Upper right tee
  842. lc.AddLine (
  843. new Point (vts.Right, vts.Y - 1),
  844. 1,
  845. Orientation.Vertical,
  846. tab.BorderStyle
  847. );
  848. lc.AddLine (
  849. new Point (vts.Right, vts.Y - 1),
  850. 0,
  851. Orientation.Horizontal,
  852. tab.BorderStyle
  853. );
  854. }
  855. }
  856. if (i == 0 && i != selectedTab && _host.TabScrollOffset == 0 && _host.Style.ShowBorder)
  857. {
  858. if (_host.Style.TabsOnBottom)
  859. {
  860. // Upper left vertical line
  861. lc.AddLine (
  862. new Point (vts.X - 1, vts.Y - 1),
  863. 0,
  864. Orientation.Vertical,
  865. tab.BorderStyle
  866. );
  867. lc.AddLine (
  868. new Point (vts.X - 1, vts.Y - 1),
  869. 1,
  870. Orientation.Horizontal,
  871. tab.BorderStyle
  872. );
  873. }
  874. else
  875. {
  876. // Lower left vertical line
  877. lc.AddLine (
  878. new Point (vts.X - 1, vts.Bottom),
  879. 0,
  880. Orientation.Vertical,
  881. tab.BorderStyle
  882. );
  883. lc.AddLine (
  884. new Point (vts.X - 1, vts.Bottom),
  885. 1,
  886. Orientation.Horizontal,
  887. tab.BorderStyle
  888. );
  889. }
  890. }
  891. if (i == tabLocations.Length - 1 && i != selectedTab)
  892. {
  893. if (_host.Style.TabsOnBottom)
  894. {
  895. // Upper right tee
  896. lc.AddLine (
  897. new Point (vts.Right, vts.Y - 1),
  898. 1,
  899. Orientation.Vertical,
  900. tab.BorderStyle
  901. );
  902. lc.AddLine (
  903. new Point (vts.Right, vts.Y - 1),
  904. 0,
  905. Orientation.Horizontal,
  906. tab.BorderStyle
  907. );
  908. }
  909. else
  910. {
  911. // Lower right tee
  912. lc.AddLine (
  913. new Point (vts.Right, vts.Bottom),
  914. -1,
  915. Orientation.Vertical,
  916. tab.BorderStyle
  917. );
  918. lc.AddLine (
  919. new Point (vts.Right, vts.Bottom),
  920. 0,
  921. Orientation.Horizontal,
  922. tab.BorderStyle
  923. );
  924. }
  925. }
  926. if (i == tabLocations.Length - 1)
  927. {
  928. var arrowOffset = 1;
  929. int lastSelectedTab = !_host.Style.ShowTopLine && i == selectedTab ? 1 :
  930. _host.Style.TabsOnBottom ? 1 : 0;
  931. Rectangle tabsBarVts = ViewportToScreen (Viewport);
  932. int lineLength = tabsBarVts.Right - vts.Right;
  933. // Right horizontal line
  934. if (ShouldDrawRightScrollIndicator ())
  935. {
  936. if (lineLength - arrowOffset > 0)
  937. {
  938. if (_host.Style.TabsOnBottom)
  939. {
  940. lc.AddLine (
  941. new Point (vts.Right, vts.Y - lastSelectedTab),
  942. lineLength - arrowOffset,
  943. Orientation.Horizontal,
  944. tab.BorderStyle
  945. );
  946. }
  947. else
  948. {
  949. lc.AddLine (
  950. new Point (
  951. vts.Right,
  952. vts.Bottom - lastSelectedTab
  953. ),
  954. lineLength - arrowOffset,
  955. Orientation.Horizontal,
  956. tab.BorderStyle
  957. );
  958. }
  959. }
  960. }
  961. else
  962. {
  963. if (_host.Style.TabsOnBottom)
  964. {
  965. lc.AddLine (
  966. new Point (vts.Right, vts.Y - lastSelectedTab),
  967. lineLength,
  968. Orientation.Horizontal,
  969. tab.BorderStyle
  970. );
  971. }
  972. else
  973. {
  974. lc.AddLine (
  975. new Point (vts.Right, vts.Bottom - lastSelectedTab),
  976. lineLength,
  977. Orientation.Horizontal,
  978. tab.BorderStyle
  979. );
  980. }
  981. if (_host.Style.ShowBorder)
  982. {
  983. if (_host.Style.TabsOnBottom)
  984. {
  985. // More LRCorner
  986. lc.AddLine (
  987. new Point (
  988. tabsBarVts.Right - 1,
  989. vts.Y - lastSelectedTab
  990. ),
  991. -1,
  992. Orientation.Vertical,
  993. tab.BorderStyle
  994. );
  995. }
  996. else
  997. {
  998. // More URCorner
  999. lc.AddLine (
  1000. new Point (
  1001. tabsBarVts.Right - 1,
  1002. vts.Bottom - lastSelectedTab
  1003. ),
  1004. 1,
  1005. Orientation.Vertical,
  1006. tab.BorderStyle
  1007. );
  1008. }
  1009. }
  1010. }
  1011. }
  1012. tab.LineCanvas.Merge (lc);
  1013. tab.OnRenderLineCanvas ();
  1014. }
  1015. }
  1016. private int GetUnderlineYPosition ()
  1017. {
  1018. if (_host.Style.TabsOnBottom)
  1019. {
  1020. return 0;
  1021. }
  1022. return _host.Style.ShowTopLine ? 2 : 1;
  1023. }
  1024. /// <summary>Renders the line with the tab names in it.</summary>
  1025. private void RenderTabLine ()
  1026. {
  1027. TabToRender [] tabLocations = _host._tabLocations;
  1028. int y;
  1029. if (_host.Style.TabsOnBottom)
  1030. {
  1031. y = 1;
  1032. }
  1033. else
  1034. {
  1035. y = _host.Style.ShowTopLine ? 1 : 0;
  1036. }
  1037. View selected = null;
  1038. int topLine = _host.Style.ShowTopLine ? 1 : 0;
  1039. int width = Viewport.Width;
  1040. foreach (TabToRender toRender in tabLocations)
  1041. {
  1042. Tab tab = toRender.Tab;
  1043. if (toRender.IsSelected)
  1044. {
  1045. selected = tab;
  1046. if (_host.Style.TabsOnBottom)
  1047. {
  1048. tab.Border.Thickness = new Thickness (1, 0, 1, topLine);
  1049. tab.Margin.Thickness = new Thickness (0, 1, 0, 0);
  1050. }
  1051. else
  1052. {
  1053. tab.Border.Thickness = new Thickness (1, topLine, 1, 0);
  1054. tab.Margin.Thickness = new Thickness (0, 0, 0, topLine);
  1055. }
  1056. }
  1057. else if (selected is null)
  1058. {
  1059. if (_host.Style.TabsOnBottom)
  1060. {
  1061. tab.Border.Thickness = new Thickness (1, 1, 0, topLine);
  1062. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  1063. }
  1064. else
  1065. {
  1066. tab.Border.Thickness = new Thickness (1, topLine, 0, 1);
  1067. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  1068. }
  1069. tab.Width = Math.Max (tab.Width.GetAnchor (0) - 1, 1);
  1070. }
  1071. else
  1072. {
  1073. if (_host.Style.TabsOnBottom)
  1074. {
  1075. tab.Border.Thickness = new Thickness (0, 1, 1, topLine);
  1076. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  1077. }
  1078. else
  1079. {
  1080. tab.Border.Thickness = new Thickness (0, topLine, 1, 1);
  1081. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  1082. }
  1083. tab.Width = Math.Max (tab.Width.GetAnchor (0) - 1, 1);
  1084. }
  1085. tab.Text = toRender.TextToRender;
  1086. // BUGBUG: Layout should only be called from Mainloop iteration!
  1087. Layout ();
  1088. tab.OnDrawAdornments ();
  1089. Attribute prevAttr = Driver.GetAttribute ();
  1090. // if tab is the selected one and focus is inside this control
  1091. if (toRender.IsSelected && _host.HasFocus)
  1092. {
  1093. if (_host.Focused == this)
  1094. {
  1095. // if focus is the tab bar itself then show that they can switch tabs
  1096. prevAttr = ColorScheme.HotFocus;
  1097. }
  1098. else
  1099. {
  1100. // Focus is inside the tab
  1101. prevAttr = ColorScheme.HotNormal;
  1102. }
  1103. }
  1104. tab.TextFormatter.Draw (
  1105. tab.ViewportToScreen (tab.Viewport),
  1106. prevAttr,
  1107. ColorScheme.HotNormal
  1108. );
  1109. tab.OnRenderLineCanvas ();
  1110. Driver.SetAttribute (GetNormalColor ());
  1111. }
  1112. }
  1113. /// <summary>Renders the line of the tab that adjoins the content of the tab.</summary>
  1114. private void RenderUnderline ()
  1115. {
  1116. int y = GetUnderlineYPosition ();
  1117. TabToRender selected = _host._tabLocations.FirstOrDefault (t => t.IsSelected);
  1118. if (selected is null)
  1119. {
  1120. return;
  1121. }
  1122. // draw scroll indicators
  1123. // if there are more tabs to the left not visible
  1124. if (_host.TabScrollOffset > 0)
  1125. {
  1126. _leftScrollIndicator.X = 0;
  1127. _leftScrollIndicator.Y = y;
  1128. // indicate that
  1129. _leftScrollIndicator.Visible = true;
  1130. // Ensures this is clicked instead of the first tab
  1131. MoveSubviewToEnd (_leftScrollIndicator);
  1132. _leftScrollIndicator.Draw ();
  1133. }
  1134. else
  1135. {
  1136. _leftScrollIndicator.Visible = false;
  1137. }
  1138. // if there are more tabs to the right not visible
  1139. if (ShouldDrawRightScrollIndicator ())
  1140. {
  1141. _rightScrollIndicator.X = Viewport.Width - 1;
  1142. _rightScrollIndicator.Y = y;
  1143. // indicate that
  1144. _rightScrollIndicator.Visible = true;
  1145. // Ensures this is clicked instead of the last tab if under this
  1146. MoveSubviewToStart (_rightScrollIndicator);
  1147. _rightScrollIndicator.Draw ();
  1148. }
  1149. else
  1150. {
  1151. _rightScrollIndicator.Visible = false;
  1152. }
  1153. }
  1154. private bool ShouldDrawRightScrollIndicator () { return _host._tabLocations.LastOrDefault ()?.Tab != _host.Tabs.LastOrDefault (); }
  1155. }
  1156. private class TabToRender
  1157. {
  1158. public TabToRender (int x, Tab tab, string textToRender, bool isSelected, int width)
  1159. {
  1160. X = x;
  1161. Tab = tab;
  1162. IsSelected = isSelected;
  1163. Width = width;
  1164. TextToRender = textToRender;
  1165. }
  1166. /// <summary>True if the tab that is being rendered is the selected one.</summary>
  1167. /// <value></value>
  1168. public bool IsSelected { get; }
  1169. public Tab Tab { get; }
  1170. public string TextToRender { get; }
  1171. public int Width { get; }
  1172. public int X { get; set; }
  1173. }
  1174. }