TabView.cs 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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 = ClipToViewport ();
  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.OnMouseEvent (e.MouseEvent); }
  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 (viewport);
  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. public override bool OnEnter (View view)
  1030. {
  1031. Driver.SetCursorVisibility (CursorVisibility.Invisible);
  1032. return base.OnEnter (view);
  1033. }
  1034. private int GetUnderlineYPosition ()
  1035. {
  1036. if (_host.Style.TabsOnBottom)
  1037. {
  1038. return 0;
  1039. }
  1040. return _host.Style.ShowTopLine ? 2 : 1;
  1041. }
  1042. /// <summary>Renders the line with the tab names in it.</summary>
  1043. private void RenderTabLine ()
  1044. {
  1045. TabToRender [] tabLocations = _host._tabLocations;
  1046. int y;
  1047. if (_host.Style.TabsOnBottom)
  1048. {
  1049. y = 1;
  1050. }
  1051. else
  1052. {
  1053. y = _host.Style.ShowTopLine ? 1 : 0;
  1054. }
  1055. View selected = null;
  1056. int topLine = _host.Style.ShowTopLine ? 1 : 0;
  1057. int width = Viewport.Width;
  1058. foreach (TabToRender toRender in tabLocations)
  1059. {
  1060. Tab tab = toRender.Tab;
  1061. if (toRender.IsSelected)
  1062. {
  1063. selected = tab;
  1064. if (_host.Style.TabsOnBottom)
  1065. {
  1066. tab.Border.Thickness = new Thickness (1, 0, 1, topLine);
  1067. tab.Margin.Thickness = new Thickness (0, 1, 0, 0);
  1068. }
  1069. else
  1070. {
  1071. tab.Border.Thickness = new Thickness (1, topLine, 1, 0);
  1072. tab.Margin.Thickness = new Thickness (0, 0, 0, topLine);
  1073. }
  1074. }
  1075. else if (selected is null)
  1076. {
  1077. if (_host.Style.TabsOnBottom)
  1078. {
  1079. tab.Border.Thickness = new Thickness (1, 1, 0, topLine);
  1080. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  1081. }
  1082. else
  1083. {
  1084. tab.Border.Thickness = new Thickness (1, topLine, 0, 1);
  1085. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  1086. }
  1087. tab.Width = Math.Max (tab.Width.Anchor (0) - 1, 1);
  1088. }
  1089. else
  1090. {
  1091. if (_host.Style.TabsOnBottom)
  1092. {
  1093. tab.Border.Thickness = new Thickness (0, 1, 1, topLine);
  1094. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  1095. }
  1096. else
  1097. {
  1098. tab.Border.Thickness = new Thickness (0, topLine, 1, 1);
  1099. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  1100. }
  1101. tab.Width = Math.Max (tab.Width.Anchor (0) - 1, 1);
  1102. }
  1103. tab.Text = toRender.TextToRender;
  1104. LayoutSubviews ();
  1105. tab.OnDrawAdornments ();
  1106. Attribute prevAttr = Driver.GetAttribute ();
  1107. // if tab is the selected one and focus is inside this control
  1108. if (toRender.IsSelected && _host.HasFocus)
  1109. {
  1110. if (_host.Focused == this)
  1111. {
  1112. // if focus is the tab bar ourself then show that they can switch tabs
  1113. prevAttr = ColorScheme.HotFocus;
  1114. }
  1115. else
  1116. {
  1117. // Focus is inside the tab
  1118. prevAttr = ColorScheme.HotNormal;
  1119. }
  1120. }
  1121. tab.TextFormatter.Draw (
  1122. tab.ViewportToScreen (tab.Viewport),
  1123. prevAttr,
  1124. ColorScheme.HotNormal
  1125. );
  1126. tab.OnRenderLineCanvas ();
  1127. Driver.SetAttribute (GetNormalColor ());
  1128. }
  1129. }
  1130. /// <summary>Renders the line of the tab that adjoins the content of the tab.</summary>
  1131. private void RenderUnderline ()
  1132. {
  1133. int y = GetUnderlineYPosition ();
  1134. TabToRender selected = _host._tabLocations.FirstOrDefault (t => t.IsSelected);
  1135. if (selected is null)
  1136. {
  1137. return;
  1138. }
  1139. // draw scroll indicators
  1140. // if there are more tabs to the left not visible
  1141. if (_host.TabScrollOffset > 0)
  1142. {
  1143. _leftScrollIndicator.X = 0;
  1144. _leftScrollIndicator.Y = y;
  1145. // indicate that
  1146. _leftScrollIndicator.Visible = true;
  1147. // Ensures this is clicked instead of the first tab
  1148. BringSubviewToFront (_leftScrollIndicator);
  1149. _leftScrollIndicator.Draw ();
  1150. }
  1151. else
  1152. {
  1153. _leftScrollIndicator.Visible = false;
  1154. }
  1155. // if there are more tabs to the right not visible
  1156. if (ShouldDrawRightScrollIndicator ())
  1157. {
  1158. _rightScrollIndicator.X = Viewport.Width - 1;
  1159. _rightScrollIndicator.Y = y;
  1160. // indicate that
  1161. _rightScrollIndicator.Visible = true;
  1162. // Ensures this is clicked instead of the last tab if under this
  1163. BringSubviewToFront (_rightScrollIndicator);
  1164. _rightScrollIndicator.Draw ();
  1165. }
  1166. else
  1167. {
  1168. _rightScrollIndicator.Visible = false;
  1169. }
  1170. }
  1171. private bool ShouldDrawRightScrollIndicator () { return _host._tabLocations.LastOrDefault ()?.Tab != _host.Tabs.LastOrDefault (); }
  1172. }
  1173. private class TabToRender
  1174. {
  1175. public TabToRender (int x, Tab tab, string textToRender, bool isSelected, int width)
  1176. {
  1177. X = x;
  1178. Tab = tab;
  1179. IsSelected = isSelected;
  1180. Width = width;
  1181. TextToRender = textToRender;
  1182. }
  1183. /// <summary>True if the tab that is being rendered is the selected one.</summary>
  1184. /// <value></value>
  1185. public bool IsSelected { get; }
  1186. public Tab Tab { get; }
  1187. public string TextToRender { get; }
  1188. public int Width { get; }
  1189. public int X { get; set; }
  1190. }
  1191. }