TabView.cs 49 KB

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