TabView.cs 49 KB

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