TabView.cs 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  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 focusedView)
  242. {
  243. if (SelectedTab is { } && !_contentView.CanFocus && focusedView == this)
  244. {
  245. SelectedTab?.SetFocus ();
  246. return;
  247. }
  248. base.OnHasFocusChanged (newHasFocus, previousFocusedView, focusedView);
  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. 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. View selected = null;
  1057. int topLine = _host.Style.ShowTopLine ? 1 : 0;
  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.GetAnchor (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.GetAnchor (0) - 1, 1);
  1102. }
  1103. tab.Text = toRender.TextToRender;
  1104. // BUGBUG: Layout should only be called from Mainloop iteration!
  1105. Layout ();
  1106. tab.DrawAdornments ();
  1107. Attribute prevAttr = Driver?.GetAttribute () ?? Attribute.Default;
  1108. // if tab is the selected one and focus is inside this control
  1109. if (toRender.IsSelected && _host.HasFocus)
  1110. {
  1111. if (_host.Focused == this)
  1112. {
  1113. // if focus is the tab bar itself then show that they can switch tabs
  1114. prevAttr = ColorScheme.HotFocus;
  1115. }
  1116. else
  1117. {
  1118. // Focus is inside the tab
  1119. prevAttr = ColorScheme.HotNormal;
  1120. }
  1121. }
  1122. tab.TextFormatter.Draw (
  1123. tab.ViewportToScreen (tab.Viewport),
  1124. prevAttr,
  1125. ColorScheme.HotNormal
  1126. );
  1127. tab.DrawAdornments ();
  1128. SetAttribute (GetNormalColor ());
  1129. }
  1130. }
  1131. /// <summary>Renders the line of the tab that adjoins the content of the tab.</summary>
  1132. private void RenderUnderline ()
  1133. {
  1134. int y = GetUnderlineYPosition ();
  1135. TabToRender selected = _host._tabLocations.FirstOrDefault (t => t.IsSelected);
  1136. if (selected is null)
  1137. {
  1138. return;
  1139. }
  1140. // draw scroll indicators
  1141. // if there are more tabs to the left not visible
  1142. if (_host.TabScrollOffset > 0)
  1143. {
  1144. _leftScrollIndicator.X = 0;
  1145. _leftScrollIndicator.Y = y;
  1146. // indicate that
  1147. _leftScrollIndicator.Visible = true;
  1148. // Ensures this is clicked instead of the first tab
  1149. MoveSubviewToEnd (_leftScrollIndicator);
  1150. _leftScrollIndicator.Draw ();
  1151. }
  1152. else
  1153. {
  1154. _leftScrollIndicator.Visible = false;
  1155. }
  1156. // if there are more tabs to the right not visible
  1157. if (ShouldDrawRightScrollIndicator ())
  1158. {
  1159. _rightScrollIndicator.X = Viewport.Width - 1;
  1160. _rightScrollIndicator.Y = y;
  1161. // indicate that
  1162. _rightScrollIndicator.Visible = true;
  1163. // Ensures this is clicked instead of the last tab if under this
  1164. MoveSubviewToStart (_rightScrollIndicator);
  1165. _rightScrollIndicator.Draw ();
  1166. }
  1167. else
  1168. {
  1169. _rightScrollIndicator.Visible = false;
  1170. }
  1171. }
  1172. private bool ShouldDrawRightScrollIndicator () { return _host._tabLocations.LastOrDefault ()?.Tab != _host.Tabs.LastOrDefault (); }
  1173. }
  1174. private class TabToRender
  1175. {
  1176. public TabToRender (int x, Tab tab, string textToRender, bool isSelected, int width)
  1177. {
  1178. X = x;
  1179. Tab = tab;
  1180. IsSelected = isSelected;
  1181. Width = width;
  1182. TextToRender = textToRender;
  1183. }
  1184. /// <summary>True if the tab that is being rendered is the selected one.</summary>
  1185. /// <value></value>
  1186. public bool IsSelected { get; }
  1187. public Tab Tab { get; }
  1188. public string TextToRender { get; }
  1189. public int Width { get; }
  1190. public int X { get; set; }
  1191. }
  1192. }