TabView.cs 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  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. RenderTabLine ();
  538. RenderUnderline ();
  539. SetAttribute (HasFocus ? GetFocusColor () : GetNormalColor ());
  540. return true;
  541. }
  542. /// <inheritdoc />
  543. protected override bool OnDrawingSubviews ()
  544. {
  545. // RenderTabLine ();
  546. return false;
  547. }
  548. protected override void OnDrawComplete ()
  549. {
  550. if (_host._tabLocations is null)
  551. {
  552. return;
  553. }
  554. TabToRender [] tabLocations = _host._tabLocations;
  555. int selectedTab = -1;
  556. for (var i = 0; i < tabLocations.Length; i++)
  557. {
  558. View tab = tabLocations [i].Tab;
  559. Rectangle vts = tab.ViewportToScreen (tab.Viewport);
  560. var lc = new LineCanvas ();
  561. int selectedOffset = _host.Style.ShowTopLine && tabLocations [i].IsSelected ? 0 : 1;
  562. if (tabLocations [i].IsSelected)
  563. {
  564. selectedTab = i;
  565. if (i == 0 && _host.TabScrollOffset == 0)
  566. {
  567. if (_host.Style.TabsOnBottom)
  568. {
  569. // Upper left vertical line
  570. lc.AddLine (
  571. new Point (vts.X - 1, vts.Y - 1),
  572. -1,
  573. Orientation.Vertical,
  574. tab.BorderStyle
  575. );
  576. }
  577. else
  578. {
  579. // Lower left vertical line
  580. lc.AddLine (
  581. new Point (vts.X - 1, vts.Bottom - selectedOffset),
  582. -1,
  583. Orientation.Vertical,
  584. tab.BorderStyle
  585. );
  586. }
  587. }
  588. else if (i > 0 && i <= tabLocations.Length - 1)
  589. {
  590. if (_host.Style.TabsOnBottom)
  591. {
  592. // URCorner
  593. lc.AddLine (
  594. new Point (vts.X - 1, vts.Y - 1),
  595. 1,
  596. Orientation.Vertical,
  597. tab.BorderStyle
  598. );
  599. lc.AddLine (
  600. new Point (vts.X - 1, vts.Y - 1),
  601. -1,
  602. Orientation.Horizontal,
  603. tab.BorderStyle
  604. );
  605. }
  606. else
  607. {
  608. // LRCorner
  609. lc.AddLine (
  610. new Point (vts.X - 1, vts.Bottom - selectedOffset),
  611. -1,
  612. Orientation.Vertical,
  613. tab.BorderStyle
  614. );
  615. lc.AddLine (
  616. new Point (vts.X - 1, vts.Bottom - selectedOffset),
  617. -1,
  618. Orientation.Horizontal,
  619. tab.BorderStyle
  620. );
  621. }
  622. if (_host.Style.ShowTopLine)
  623. {
  624. if (_host.Style.TabsOnBottom)
  625. {
  626. // Lower left tee
  627. lc.AddLine (
  628. new Point (vts.X - 1, vts.Bottom),
  629. -1,
  630. Orientation.Vertical,
  631. tab.BorderStyle
  632. );
  633. lc.AddLine (
  634. new Point (vts.X - 1, vts.Bottom),
  635. 0,
  636. Orientation.Horizontal,
  637. tab.BorderStyle
  638. );
  639. }
  640. else
  641. {
  642. // Upper left tee
  643. lc.AddLine (
  644. new Point (vts.X - 1, vts.Y - 1),
  645. 1,
  646. Orientation.Vertical,
  647. tab.BorderStyle
  648. );
  649. lc.AddLine (
  650. new Point (vts.X - 1, vts.Y - 1),
  651. 0,
  652. Orientation.Horizontal,
  653. tab.BorderStyle
  654. );
  655. }
  656. }
  657. }
  658. if (i < tabLocations.Length - 1)
  659. {
  660. if (_host.Style.ShowTopLine)
  661. {
  662. if (_host.Style.TabsOnBottom)
  663. {
  664. // Lower right tee
  665. lc.AddLine (
  666. new Point (vts.Right, vts.Bottom),
  667. -1,
  668. Orientation.Vertical,
  669. tab.BorderStyle
  670. );
  671. lc.AddLine (
  672. new Point (vts.Right, vts.Bottom),
  673. 0,
  674. Orientation.Horizontal,
  675. tab.BorderStyle
  676. );
  677. }
  678. else
  679. {
  680. // Upper right tee
  681. lc.AddLine (
  682. new Point (vts.Right, vts.Y - 1),
  683. 1,
  684. Orientation.Vertical,
  685. tab.BorderStyle
  686. );
  687. lc.AddLine (
  688. new Point (vts.Right, vts.Y - 1),
  689. 0,
  690. Orientation.Horizontal,
  691. tab.BorderStyle
  692. );
  693. }
  694. }
  695. }
  696. if (_host.Style.TabsOnBottom)
  697. {
  698. //URCorner
  699. lc.AddLine (
  700. new Point (vts.Right, vts.Y - 1),
  701. 1,
  702. Orientation.Vertical,
  703. tab.BorderStyle
  704. );
  705. lc.AddLine (
  706. new Point (vts.Right, vts.Y - 1),
  707. 1,
  708. Orientation.Horizontal,
  709. tab.BorderStyle
  710. );
  711. }
  712. else
  713. {
  714. //LLCorner
  715. lc.AddLine (
  716. new Point (vts.Right, vts.Bottom - selectedOffset),
  717. -1,
  718. Orientation.Vertical,
  719. tab.BorderStyle
  720. );
  721. lc.AddLine (
  722. new Point (vts.Right, vts.Bottom - selectedOffset),
  723. 1,
  724. Orientation.Horizontal,
  725. tab.BorderStyle
  726. );
  727. }
  728. }
  729. else if (selectedTab == -1)
  730. {
  731. if (i == 0 && string.IsNullOrEmpty (tab.Text))
  732. {
  733. if (_host.Style.TabsOnBottom)
  734. {
  735. if (_host.Style.ShowTopLine)
  736. {
  737. // LLCorner
  738. lc.AddLine (
  739. new Point (vts.X - 1, vts.Bottom),
  740. -1,
  741. Orientation.Vertical,
  742. tab.BorderStyle
  743. );
  744. lc.AddLine (
  745. new Point (vts.X - 1, vts.Bottom),
  746. 1,
  747. Orientation.Horizontal,
  748. tab.BorderStyle
  749. );
  750. }
  751. // ULCorner
  752. lc.AddLine (
  753. new Point (vts.X - 1, vts.Y - 1),
  754. 1,
  755. Orientation.Vertical,
  756. tab.BorderStyle
  757. );
  758. lc.AddLine (
  759. new Point (vts.X - 1, vts.Y - 1),
  760. 1,
  761. Orientation.Horizontal,
  762. tab.BorderStyle
  763. );
  764. }
  765. else
  766. {
  767. if (_host.Style.ShowTopLine)
  768. {
  769. // ULCorner
  770. lc.AddLine (
  771. new Point (vts.X - 1, vts.Y - 1),
  772. 1,
  773. Orientation.Vertical,
  774. tab.BorderStyle
  775. );
  776. lc.AddLine (
  777. new Point (vts.X - 1, vts.Y - 1),
  778. 1,
  779. Orientation.Horizontal,
  780. tab.BorderStyle
  781. );
  782. }
  783. // LLCorner
  784. lc.AddLine (
  785. new Point (vts.X - 1, vts.Bottom),
  786. -1,
  787. Orientation.Vertical,
  788. tab.BorderStyle
  789. );
  790. lc.AddLine (
  791. new Point (vts.X - 1, vts.Bottom),
  792. 1,
  793. Orientation.Horizontal,
  794. tab.BorderStyle
  795. );
  796. }
  797. }
  798. else if (i > 0)
  799. {
  800. if (_host.Style.ShowTopLine || _host.Style.TabsOnBottom)
  801. {
  802. // Upper left tee
  803. lc.AddLine (
  804. new Point (vts.X - 1, vts.Y - 1),
  805. 1,
  806. Orientation.Vertical,
  807. tab.BorderStyle
  808. );
  809. lc.AddLine (
  810. new Point (vts.X - 1, vts.Y - 1),
  811. 0,
  812. Orientation.Horizontal,
  813. tab.BorderStyle
  814. );
  815. }
  816. // Lower left tee
  817. lc.AddLine (
  818. new Point (vts.X - 1, vts.Bottom),
  819. -1,
  820. Orientation.Vertical,
  821. tab.BorderStyle
  822. );
  823. lc.AddLine (
  824. new Point (vts.X - 1, vts.Bottom),
  825. 0,
  826. Orientation.Horizontal,
  827. tab.BorderStyle
  828. );
  829. }
  830. }
  831. else if (i < tabLocations.Length - 1)
  832. {
  833. if (_host.Style.ShowTopLine)
  834. {
  835. // Upper right tee
  836. lc.AddLine (
  837. new Point (vts.Right, vts.Y - 1),
  838. 1,
  839. Orientation.Vertical,
  840. tab.BorderStyle
  841. );
  842. lc.AddLine (
  843. new Point (vts.Right, vts.Y - 1),
  844. 0,
  845. Orientation.Horizontal,
  846. tab.BorderStyle
  847. );
  848. }
  849. if (_host.Style.ShowTopLine || !_host.Style.TabsOnBottom)
  850. {
  851. // Lower right tee
  852. lc.AddLine (
  853. new Point (vts.Right, vts.Bottom),
  854. -1,
  855. Orientation.Vertical,
  856. tab.BorderStyle
  857. );
  858. lc.AddLine (
  859. new Point (vts.Right, vts.Bottom),
  860. 0,
  861. Orientation.Horizontal,
  862. tab.BorderStyle
  863. );
  864. }
  865. else
  866. {
  867. // Upper right tee
  868. lc.AddLine (
  869. new Point (vts.Right, vts.Y - 1),
  870. 1,
  871. Orientation.Vertical,
  872. tab.BorderStyle
  873. );
  874. lc.AddLine (
  875. new Point (vts.Right, vts.Y - 1),
  876. 0,
  877. Orientation.Horizontal,
  878. tab.BorderStyle
  879. );
  880. }
  881. }
  882. if (i == 0 && i != selectedTab && _host.TabScrollOffset == 0 && _host.Style.ShowBorder)
  883. {
  884. if (_host.Style.TabsOnBottom)
  885. {
  886. // Upper left vertical line
  887. lc.AddLine (
  888. new Point (vts.X - 1, vts.Y - 1),
  889. 0,
  890. Orientation.Vertical,
  891. tab.BorderStyle
  892. );
  893. lc.AddLine (
  894. new Point (vts.X - 1, vts.Y - 1),
  895. 1,
  896. Orientation.Horizontal,
  897. tab.BorderStyle
  898. );
  899. }
  900. else
  901. {
  902. // Lower left vertical line
  903. lc.AddLine (
  904. new Point (vts.X - 1, vts.Bottom),
  905. 0,
  906. Orientation.Vertical,
  907. tab.BorderStyle
  908. );
  909. lc.AddLine (
  910. new Point (vts.X - 1, vts.Bottom),
  911. 1,
  912. Orientation.Horizontal,
  913. tab.BorderStyle
  914. );
  915. }
  916. }
  917. if (i == tabLocations.Length - 1 && i != selectedTab)
  918. {
  919. if (_host.Style.TabsOnBottom)
  920. {
  921. // Upper right tee
  922. lc.AddLine (
  923. new Point (vts.Right, vts.Y - 1),
  924. 1,
  925. Orientation.Vertical,
  926. tab.BorderStyle
  927. );
  928. lc.AddLine (
  929. new Point (vts.Right, vts.Y - 1),
  930. 0,
  931. Orientation.Horizontal,
  932. tab.BorderStyle
  933. );
  934. }
  935. else
  936. {
  937. // Lower right tee
  938. lc.AddLine (
  939. new Point (vts.Right, vts.Bottom),
  940. -1,
  941. Orientation.Vertical,
  942. tab.BorderStyle
  943. );
  944. lc.AddLine (
  945. new Point (vts.Right, vts.Bottom),
  946. 0,
  947. Orientation.Horizontal,
  948. tab.BorderStyle
  949. );
  950. }
  951. }
  952. if (i == tabLocations.Length - 1)
  953. {
  954. var arrowOffset = 1;
  955. int lastSelectedTab = !_host.Style.ShowTopLine && i == selectedTab ? 1 :
  956. _host.Style.TabsOnBottom ? 1 : 0;
  957. Rectangle tabsBarVts = ViewportToScreen (Viewport);
  958. int lineLength = tabsBarVts.Right - vts.Right;
  959. // Right horizontal line
  960. if (ShouldDrawRightScrollIndicator ())
  961. {
  962. if (lineLength - arrowOffset > 0)
  963. {
  964. if (_host.Style.TabsOnBottom)
  965. {
  966. lc.AddLine (
  967. new Point (vts.Right, vts.Y - lastSelectedTab),
  968. lineLength - arrowOffset,
  969. Orientation.Horizontal,
  970. tab.BorderStyle
  971. );
  972. }
  973. else
  974. {
  975. lc.AddLine (
  976. new Point (
  977. vts.Right,
  978. vts.Bottom - lastSelectedTab
  979. ),
  980. lineLength - arrowOffset,
  981. Orientation.Horizontal,
  982. tab.BorderStyle
  983. );
  984. }
  985. }
  986. }
  987. else
  988. {
  989. if (_host.Style.TabsOnBottom)
  990. {
  991. lc.AddLine (
  992. new Point (vts.Right, vts.Y - lastSelectedTab),
  993. lineLength,
  994. Orientation.Horizontal,
  995. tab.BorderStyle
  996. );
  997. }
  998. else
  999. {
  1000. lc.AddLine (
  1001. new Point (vts.Right, vts.Bottom - lastSelectedTab),
  1002. lineLength,
  1003. Orientation.Horizontal,
  1004. tab.BorderStyle
  1005. );
  1006. }
  1007. if (_host.Style.ShowBorder)
  1008. {
  1009. if (_host.Style.TabsOnBottom)
  1010. {
  1011. // More LRCorner
  1012. lc.AddLine (
  1013. new Point (
  1014. tabsBarVts.Right - 1,
  1015. vts.Y - lastSelectedTab
  1016. ),
  1017. -1,
  1018. Orientation.Vertical,
  1019. tab.BorderStyle
  1020. );
  1021. }
  1022. else
  1023. {
  1024. // More URCorner
  1025. lc.AddLine (
  1026. new Point (
  1027. tabsBarVts.Right - 1,
  1028. vts.Bottom - lastSelectedTab
  1029. ),
  1030. 1,
  1031. Orientation.Vertical,
  1032. tab.BorderStyle
  1033. );
  1034. }
  1035. }
  1036. }
  1037. }
  1038. tab.LineCanvas.Merge (lc);
  1039. tab.RenderLineCanvas ();
  1040. // RenderUnderline ();
  1041. }
  1042. }
  1043. private int GetUnderlineYPosition ()
  1044. {
  1045. if (_host.Style.TabsOnBottom)
  1046. {
  1047. return 0;
  1048. }
  1049. return _host.Style.ShowTopLine ? 2 : 1;
  1050. }
  1051. /// <summary>Renders the line with the tab names in it.</summary>
  1052. private void RenderTabLine ()
  1053. {
  1054. TabToRender []? tabLocations = _host._tabLocations;
  1055. if (tabLocations is null)
  1056. {
  1057. return;
  1058. }
  1059. View? selected = null;
  1060. int topLine = _host.Style.ShowTopLine ? 1 : 0;
  1061. foreach (TabToRender toRender in tabLocations)
  1062. {
  1063. Tab tab = toRender.Tab;
  1064. if (toRender.IsSelected)
  1065. {
  1066. selected = tab;
  1067. if (_host.Style.TabsOnBottom)
  1068. {
  1069. tab.Border.Thickness = new Thickness (1, 0, 1, topLine);
  1070. tab.Margin.Thickness = new Thickness (0, 1, 0, 0);
  1071. }
  1072. else
  1073. {
  1074. tab.Border.Thickness = new Thickness (1, topLine, 1, 0);
  1075. tab.Margin.Thickness = new Thickness (0, 0, 0, topLine);
  1076. }
  1077. }
  1078. else if (selected is null)
  1079. {
  1080. if (_host.Style.TabsOnBottom)
  1081. {
  1082. tab.Border.Thickness = new Thickness (1, 1, 0, topLine);
  1083. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  1084. }
  1085. else
  1086. {
  1087. tab.Border.Thickness = new Thickness (1, topLine, 0, 1);
  1088. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  1089. }
  1090. tab.Width = Math.Max (tab.Width!.GetAnchor (0) - 1, 1);
  1091. }
  1092. else
  1093. {
  1094. if (_host.Style.TabsOnBottom)
  1095. {
  1096. tab.Border.Thickness = new Thickness (0, 1, 1, topLine);
  1097. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  1098. }
  1099. else
  1100. {
  1101. tab.Border.Thickness = new Thickness (0, topLine, 1, 1);
  1102. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  1103. }
  1104. tab.Width = Math.Max (tab.Width!.GetAnchor (0) - 1, 1);
  1105. }
  1106. tab.Text = toRender.TextToRender;
  1107. // BUGBUG: Layout should only be called from Mainloop iteration!
  1108. Layout ();
  1109. tab.DrawBorderAndPadding ();
  1110. Attribute prevAttr = Driver?.GetAttribute () ?? Attribute.Default;
  1111. // if tab is the selected one and focus is inside this control
  1112. if (toRender.IsSelected && _host.HasFocus)
  1113. {
  1114. if (_host.Focused == this)
  1115. {
  1116. // if focus is the tab bar itself then show that they can switch tabs
  1117. prevAttr = ColorScheme.HotFocus;
  1118. }
  1119. else
  1120. {
  1121. // Focus is inside the tab
  1122. prevAttr = ColorScheme.HotNormal;
  1123. }
  1124. }
  1125. tab.TextFormatter.Draw (
  1126. tab.ViewportToScreen (tab.Viewport),
  1127. prevAttr,
  1128. ColorScheme.HotNormal
  1129. );
  1130. tab.DrawBorderAndPadding ();
  1131. SetAttribute (GetNormalColor ());
  1132. }
  1133. }
  1134. /// <summary>Renders the line of the tab that adjoins the content of the tab.</summary>
  1135. private void RenderUnderline ()
  1136. {
  1137. int y = GetUnderlineYPosition ();
  1138. TabToRender? selected = _host._tabLocations?.FirstOrDefault (t => t.IsSelected);
  1139. if (selected is null)
  1140. {
  1141. return;
  1142. }
  1143. // draw scroll indicators
  1144. // if there are more tabs to the left not visible
  1145. if (_host.TabScrollOffset > 0)
  1146. {
  1147. _leftScrollIndicator.X = 0;
  1148. _leftScrollIndicator.Y = y;
  1149. // indicate that
  1150. _leftScrollIndicator.Visible = true;
  1151. // Ensures this is clicked instead of the first tab
  1152. MoveSubviewToEnd (_leftScrollIndicator);
  1153. _leftScrollIndicator.Draw ();
  1154. }
  1155. else
  1156. {
  1157. _leftScrollIndicator.Visible = false;
  1158. }
  1159. // if there are more tabs to the right not visible
  1160. if (ShouldDrawRightScrollIndicator ())
  1161. {
  1162. _rightScrollIndicator.X = Viewport.Width - 1;
  1163. _rightScrollIndicator.Y = y;
  1164. // indicate that
  1165. _rightScrollIndicator.Visible = true;
  1166. // Ensures this is clicked instead of the last tab if under this
  1167. MoveSubviewToStart (_rightScrollIndicator);
  1168. _rightScrollIndicator.Draw ();
  1169. }
  1170. else
  1171. {
  1172. _rightScrollIndicator.Visible = false;
  1173. }
  1174. }
  1175. private bool ShouldDrawRightScrollIndicator () { return _host._tabLocations!.LastOrDefault ()?.Tab != _host.Tabs.LastOrDefault (); }
  1176. }
  1177. private class TabToRender
  1178. {
  1179. public TabToRender (Tab tab, string textToRender, bool isSelected)
  1180. {
  1181. Tab = tab;
  1182. IsSelected = isSelected;
  1183. TextToRender = textToRender;
  1184. }
  1185. /// <summary>True if the tab that is being rendered is the selected one.</summary>
  1186. /// <value></value>
  1187. public bool IsSelected { get; }
  1188. public Tab Tab { get; }
  1189. public string TextToRender { get; }
  1190. }
  1191. }