TabView.cs 49 KB

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