TabView.cs 49 KB

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