TabView.cs 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  1. using System.Diagnostics;
  2. namespace Terminal.Gui;
  3. /// <summary>Control that hosts multiple sub views, presenting a single one at once.</summary>
  4. public class TabView : View
  5. {
  6. /// <summary>The default <see cref="MaxTabTextWidth"/> to set on new <see cref="TabView"/> controls.</summary>
  7. public const uint DefaultMaxTabTextWidth = 30;
  8. /// <summary>
  9. /// This sub view is the main client area of the current tab. It hosts the <see cref="Tab.View"/> of the tab, the
  10. /// <see cref="SelectedTab"/>.
  11. /// </summary>
  12. private readonly View _contentView;
  13. private readonly List<Tab> _tabs = new ();
  14. /// <summary>This sub view is the 2 or 3 line control that represents the actual tabs themselves.</summary>
  15. private readonly TabRowView _tabsBar;
  16. private Tab _selectedTab;
  17. private TabToRender [] _tabLocations;
  18. private int _tabScrollOffset;
  19. /// <summary>Initializes a <see cref="TabView"/> class.</summary>
  20. public TabView ()
  21. {
  22. CanFocus = true;
  23. TabStop = TabBehavior.TabStop; // Because TabView has focusable subviews, it must be a TabGroup
  24. _tabsBar = new TabRowView (this);
  25. _contentView = new View ()
  26. {
  27. //Id = "TabView._contentView",
  28. };
  29. ApplyStyleChanges ();
  30. base.Add (_tabsBar);
  31. base.Add (_contentView);
  32. // Things this view knows how to do
  33. AddCommand (Command.Left, () => SwitchTabBy (-1));
  34. AddCommand (Command.Right, () => SwitchTabBy (1));
  35. AddCommand (
  36. Command.LeftStart,
  37. () =>
  38. {
  39. TabScrollOffset = 0;
  40. SelectedTab = Tabs.FirstOrDefault ();
  41. return true;
  42. }
  43. );
  44. AddCommand (
  45. Command.RightEnd,
  46. () =>
  47. {
  48. TabScrollOffset = Tabs.Count - 1;
  49. SelectedTab = Tabs.LastOrDefault ();
  50. return true;
  51. }
  52. );
  53. AddCommand (
  54. Command.PageDown,
  55. () =>
  56. {
  57. TabScrollOffset += _tabLocations.Length;
  58. SelectedTab = Tabs.ElementAt (TabScrollOffset);
  59. return true;
  60. }
  61. );
  62. AddCommand (
  63. Command.PageUp,
  64. () =>
  65. {
  66. TabScrollOffset -= _tabLocations.Length;
  67. SelectedTab = Tabs.ElementAt (TabScrollOffset);
  68. return true;
  69. }
  70. );
  71. // Default keybindings for this view
  72. KeyBindings.Add (Key.CursorLeft, Command.Left);
  73. KeyBindings.Add (Key.CursorRight, Command.Right);
  74. KeyBindings.Add (Key.Home, Command.LeftStart);
  75. KeyBindings.Add (Key.End, Command.RightEnd);
  76. KeyBindings.Add (Key.PageDown, Command.PageDown);
  77. KeyBindings.Add (Key.PageUp, Command.PageUp);
  78. }
  79. /// <summary>
  80. /// The maximum number of characters to render in a Tab header. This prevents one long tab from pushing out all
  81. /// the others.
  82. /// </summary>
  83. public uint MaxTabTextWidth { get; set; } = DefaultMaxTabTextWidth;
  84. /// <summary>The currently selected member of <see cref="Tabs"/> chosen by the user.</summary>
  85. /// <value></value>
  86. public Tab SelectedTab
  87. {
  88. get => _selectedTab;
  89. set
  90. {
  91. UnSetCurrentTabs ();
  92. Tab old = _selectedTab;
  93. if (_selectedTab is { })
  94. {
  95. if (_selectedTab.View is { })
  96. {
  97. _selectedTab.View.CanFocusChanged -= ContentViewCanFocus;
  98. // remove old content
  99. _contentView.Remove (_selectedTab.View);
  100. }
  101. }
  102. _selectedTab = value;
  103. if (value is { })
  104. {
  105. // add new content
  106. if (_selectedTab.View is { })
  107. {
  108. _selectedTab.View.CanFocusChanged += ContentViewCanFocus;
  109. _contentView.Add (_selectedTab.View);
  110. // _contentView.Id = $"_contentView for {_selectedTab.DisplayText}";
  111. }
  112. }
  113. ContentViewCanFocus (null, null);
  114. EnsureSelectedTabIsVisible ();
  115. if (old != value)
  116. {
  117. if (old?.HasFocus == true)
  118. {
  119. SelectedTab?.SetFocus ();
  120. }
  121. OnSelectedTabChanged (old, value);
  122. }
  123. SetNeedsLayout ();
  124. }
  125. }
  126. private void ContentViewCanFocus (object sender, EventArgs eventArgs)
  127. {
  128. _contentView.CanFocus = _contentView.Subviews.Count (v => v.CanFocus) > 0;
  129. }
  130. private TabStyle _style = new ();
  131. /// <summary>Render choices for how to display tabs. After making changes, call <see cref="ApplyStyleChanges()"/>.</summary>
  132. /// <value></value>
  133. public TabStyle Style
  134. {
  135. get => _style;
  136. set
  137. {
  138. if (_style == value)
  139. {
  140. return;
  141. }
  142. _style = value;
  143. SetNeedsLayout ();
  144. }
  145. }
  146. /// <summary>All tabs currently hosted by the control.</summary>
  147. /// <value></value>
  148. public IReadOnlyCollection<Tab> Tabs => _tabs.AsReadOnly ();
  149. /// <summary>When there are too many tabs to render, this indicates the first tab to render on the screen.</summary>
  150. /// <value></value>
  151. public int TabScrollOffset
  152. {
  153. get => _tabScrollOffset;
  154. set
  155. {
  156. _tabScrollOffset = EnsureValidScrollOffsets (value);
  157. SetNeedsLayout ();
  158. }
  159. }
  160. /// <summary>Adds the given <paramref name="tab"/> to <see cref="Tabs"/>.</summary>
  161. /// <param name="tab"></param>
  162. /// <param name="andSelect">True to make the newly added Tab the <see cref="SelectedTab"/>.</param>
  163. public void AddTab (Tab tab, bool andSelect)
  164. {
  165. if (_tabs.Contains (tab))
  166. {
  167. return;
  168. }
  169. _tabs.Add (tab);
  170. _tabsBar.Add (tab);
  171. if (SelectedTab is null || andSelect)
  172. {
  173. SelectedTab = tab;
  174. EnsureSelectedTabIsVisible ();
  175. tab.View?.SetFocus ();
  176. }
  177. SetNeedsLayout ();
  178. }
  179. /// <summary>
  180. /// Updates the control to use the latest state settings in <see cref="Style"/>. This can change the size of the
  181. /// client area of the tab (for rendering the selected tab's content). This method includes a call to
  182. /// <see cref="View.SetNeedsDisplay()"/>.
  183. /// </summary>
  184. public void ApplyStyleChanges ()
  185. {
  186. _contentView.BorderStyle = Style.ShowBorder ? LineStyle.Single : LineStyle.None;
  187. _contentView.Width = Dim.Fill ();
  188. if (Style.TabsOnBottom)
  189. {
  190. // Tabs are along the bottom so just dodge the border
  191. if (Style.ShowBorder)
  192. {
  193. _contentView.Border.Thickness = new Thickness (1, 1, 1, 0);
  194. }
  195. _contentView.Y = 0;
  196. int tabHeight = GetTabHeight (false);
  197. // Fill client area leaving space at bottom for tabs
  198. _contentView.Height = Dim.Fill (tabHeight);
  199. _tabsBar.Height = tabHeight;
  200. _tabsBar.Y = Pos.Bottom (_contentView);
  201. }
  202. else
  203. {
  204. // Tabs are along the top
  205. if (Style.ShowBorder)
  206. {
  207. _contentView.Border.Thickness = new Thickness (1, 0, 1, 1);
  208. }
  209. _tabsBar.Y = 0;
  210. int tabHeight = GetTabHeight (true);
  211. //move content down to make space for tabs
  212. _contentView.Y = Pos.Bottom (_tabsBar);
  213. // Fill client area leaving space at bottom for border
  214. _contentView.Height = Dim.Fill ();
  215. // The top tab should be 2 or 3 rows high and on the top
  216. _tabsBar.Height = tabHeight;
  217. // Should be able to just use 0 but switching between top/bottom tabs repeatedly breaks in ValidatePosDim if just using the absolute value 0
  218. }
  219. SetNeedsLayout ();
  220. }
  221. /// <summary>Updates <see cref="TabScrollOffset"/> to ensure that <see cref="SelectedTab"/> is visible.</summary>
  222. public void EnsureSelectedTabIsVisible ()
  223. {
  224. if (!IsInitialized || SelectedTab is null)
  225. {
  226. return;
  227. }
  228. // if current viewport does not include the selected tab
  229. if (!CalculateViewport (Viewport).Any (r => Equals (SelectedTab, r.Tab)))
  230. {
  231. // Set scroll offset so the first tab rendered is the
  232. TabScrollOffset = Math.Max (0, Tabs.IndexOf (SelectedTab));
  233. }
  234. }
  235. /// <summary>Updates <see cref="TabScrollOffset"/> to be a valid index of <see cref="Tabs"/>.</summary>
  236. /// <param name="value">The value to validate.</param>
  237. /// <remarks>Changes will not be immediately visible in the display until you call <see cref="View.SetNeedsDisplay()"/>.</remarks>
  238. /// <returns>The valid <see cref="TabScrollOffset"/> for the given value.</returns>
  239. public int EnsureValidScrollOffsets (int value) { return Math.Max (Math.Min (value, Tabs.Count - 1), 0); }
  240. /// <inheritdoc />
  241. protected override void OnHasFocusChanged (bool newHasFocus, View previousFocusedView, View focusedVew)
  242. {
  243. if (SelectedTab is { } && !_contentView.CanFocus && focusedVew == this)
  244. {
  245. SelectedTab?.SetFocus ();
  246. return;
  247. }
  248. base.OnHasFocusChanged (newHasFocus, previousFocusedView, focusedVew);
  249. }
  250. /// <inheritdoc/>
  251. protected override bool OnDrawingContent (Rectangle viewport)
  252. {
  253. if (Tabs.Any ())
  254. {
  255. Rectangle savedClip = SetClip ();
  256. _tabsBar.Draw ();
  257. _contentView.SetNeedsDisplay ();
  258. _contentView.Draw ();
  259. if (Driver is { })
  260. {
  261. Driver.Clip = savedClip;
  262. }
  263. }
  264. return true;
  265. }
  266. /// <summary>
  267. /// Removes the given <paramref name="tab"/> from <see cref="Tabs"/>. Caller is responsible for disposing the
  268. /// tab's hosted <see cref="Tab.View"/> if appropriate.
  269. /// </summary>
  270. /// <param name="tab"></param>
  271. public void RemoveTab (Tab tab)
  272. {
  273. if (tab is null || !_tabs.Contains (tab))
  274. {
  275. return;
  276. }
  277. // what tab was selected before closing
  278. int idx = _tabs.IndexOf (tab);
  279. _tabs.Remove (tab);
  280. // if the currently selected tab is no longer a member of Tabs
  281. if (SelectedTab is null || !Tabs.Contains (SelectedTab))
  282. {
  283. // select the tab closest to the one that disappeared
  284. int toSelect = Math.Max (idx - 1, 0);
  285. if (toSelect < Tabs.Count)
  286. {
  287. SelectedTab = Tabs.ElementAt (toSelect);
  288. }
  289. else
  290. {
  291. SelectedTab = Tabs.LastOrDefault ();
  292. }
  293. }
  294. EnsureSelectedTabIsVisible ();
  295. SetNeedsLayout ();
  296. }
  297. /// <summary>Event for when <see cref="SelectedTab"/> changes.</summary>
  298. public event EventHandler<TabChangedEventArgs> SelectedTabChanged;
  299. /// <summary>
  300. /// Changes the <see cref="SelectedTab"/> by the given <paramref name="amount"/>. Positive for right, negative for
  301. /// left. If no tab is currently selected then the first tab will become selected.
  302. /// </summary>
  303. /// <param name="amount"></param>
  304. public bool SwitchTabBy (int amount)
  305. {
  306. if (Tabs.Count == 0)
  307. {
  308. return false;
  309. }
  310. // if there is only one tab anyway or nothing is selected
  311. if (Tabs.Count == 1 || SelectedTab is null)
  312. {
  313. SelectedTab = Tabs.ElementAt (0);
  314. return SelectedTab is { };
  315. }
  316. int currentIdx = Tabs.IndexOf (SelectedTab);
  317. // Currently selected tab has vanished!
  318. if (currentIdx == -1)
  319. {
  320. SelectedTab = Tabs.ElementAt (0);
  321. return true;
  322. }
  323. int newIdx = Math.Max (0, Math.Min (currentIdx + amount, Tabs.Count - 1));
  324. if (newIdx == currentIdx)
  325. {
  326. return false;
  327. }
  328. SelectedTab = _tabs [newIdx];
  329. EnsureSelectedTabIsVisible ();
  330. return true;
  331. }
  332. /// <summary>
  333. /// Event fired when a <see cref="Tab"/> is clicked. Can be used to cancel navigation, show context menu (e.g. on
  334. /// right click) etc.
  335. /// </summary>
  336. public event EventHandler<TabMouseEventArgs> TabClicked;
  337. /// <summary>Disposes the control and all <see cref="Tabs"/>.</summary>
  338. /// <param name="disposing"></param>
  339. protected override void Dispose (bool disposing)
  340. {
  341. base.Dispose (disposing);
  342. // The selected tab will automatically be disposed but
  343. // any tabs not visible will need to be manually disposed
  344. foreach (Tab tab in Tabs)
  345. {
  346. if (!Equals (SelectedTab, tab))
  347. {
  348. tab.View?.Dispose ();
  349. }
  350. }
  351. }
  352. /// <summary>Raises the <see cref="SelectedTabChanged"/> event.</summary>
  353. protected virtual void OnSelectedTabChanged (Tab oldTab, Tab newTab)
  354. {
  355. SelectedTabChanged?.Invoke (this, new TabChangedEventArgs (oldTab, newTab));
  356. }
  357. /// <summary>Returns which tabs to render at each x location.</summary>
  358. /// <returns></returns>
  359. private IEnumerable<TabToRender> CalculateViewport (Rectangle bounds)
  360. {
  361. UnSetCurrentTabs ();
  362. var i = 1;
  363. View prevTab = null;
  364. // Starting at the first or scrolled to tab
  365. foreach (Tab tab in Tabs.Skip (TabScrollOffset))
  366. {
  367. if (prevTab is { })
  368. {
  369. tab.X = Pos.Right (prevTab);
  370. }
  371. else
  372. {
  373. tab.X = 0;
  374. }
  375. tab.Y = 0;
  376. // while there is space for the tab
  377. int tabTextWidth = tab.DisplayText.EnumerateRunes ().Sum (c => c.GetColumns ());
  378. string text = tab.DisplayText;
  379. // The maximum number of characters to use for the tab name as specified
  380. // by the user (MaxTabTextWidth). But not more than the width of the view
  381. // or we won't even be able to render a single tab!
  382. long maxWidth = Math.Max (0, Math.Min (bounds.Width - 3, MaxTabTextWidth));
  383. prevTab = tab;
  384. tab.Width = 2;
  385. tab.Height = Style.ShowTopLine ? 3 : 2;
  386. // if tab view is width <= 3 don't render any tabs
  387. if (maxWidth == 0)
  388. {
  389. tab.Visible = true;
  390. tab.MouseClick += Tab_MouseClick;
  391. yield return new TabToRender (i, tab, string.Empty, Equals (SelectedTab, tab), 0);
  392. break;
  393. }
  394. if (tabTextWidth > maxWidth)
  395. {
  396. text = tab.DisplayText.Substring (0, (int)maxWidth);
  397. tabTextWidth = (int)maxWidth;
  398. }
  399. tab.Width = Math.Max (tabTextWidth + 2, 1);
  400. tab.Height = Style.ShowTopLine ? 3 : 2;
  401. // if there is not enough space for this tab
  402. if (i + tabTextWidth >= bounds.Width)
  403. {
  404. tab.Visible = false;
  405. break;
  406. }
  407. // there is enough space!
  408. tab.Visible = true;
  409. tab.MouseClick += Tab_MouseClick;
  410. yield return new TabToRender (i, tab, text, Equals (SelectedTab, tab), tabTextWidth);
  411. i += tabTextWidth + 1;
  412. }
  413. }
  414. /// <summary>
  415. /// Returns the number of rows occupied by rendering the tabs, this depends on <see cref="TabStyle.ShowTopLine"/>
  416. /// and can be 0 (e.g. if <see cref="TabStyle.TabsOnBottom"/> and you ask for <paramref name="top"/>).
  417. /// </summary>
  418. /// <param name="top">True to measure the space required at the top of the control, false to measure space at the bottom.</param>
  419. /// .
  420. /// <returns></returns>
  421. private int GetTabHeight (bool top)
  422. {
  423. if (top && Style.TabsOnBottom)
  424. {
  425. return 0;
  426. }
  427. if (!top && !Style.TabsOnBottom)
  428. {
  429. return 0;
  430. }
  431. return Style.ShowTopLine ? 3 : 2;
  432. }
  433. private void Tab_MouseClick (object sender, MouseEventArgs e)
  434. {
  435. e.Handled = _tabsBar.NewMouseEvent (e) == true;
  436. }
  437. private void UnSetCurrentTabs ()
  438. {
  439. if (_tabLocations is { })
  440. {
  441. foreach (TabToRender tabToRender in _tabLocations)
  442. {
  443. tabToRender.Tab.MouseClick -= Tab_MouseClick;
  444. tabToRender.Tab.Visible = false;
  445. }
  446. _tabLocations = null;
  447. }
  448. }
  449. /// <summary>Raises the <see cref="TabClicked"/> event.</summary>
  450. /// <param name="tabMouseEventArgs"></param>
  451. private protected virtual void OnTabClicked (TabMouseEventArgs tabMouseEventArgs) { TabClicked?.Invoke (this, tabMouseEventArgs); }
  452. private class TabRowView : View
  453. {
  454. private readonly TabView _host;
  455. private readonly View _leftScrollIndicator;
  456. private readonly View _rightScrollIndicator;
  457. public TabRowView (TabView host)
  458. {
  459. _host = host;
  460. Id = "tabRowView";
  461. CanFocus = true;
  462. Height = 1; // BUGBUG: Views should avoid setting Height as doing so implies Frame.Size == GetContentSize ().
  463. Width = Dim.Fill ();
  464. _rightScrollIndicator = new View
  465. {
  466. Id = "rightScrollIndicator",
  467. Width = 1,
  468. Height = 1,
  469. Visible = false,
  470. Text = Glyphs.RightArrow.ToString ()
  471. };
  472. _rightScrollIndicator.MouseClick += _host.Tab_MouseClick;
  473. _leftScrollIndicator = new View
  474. {
  475. Id = "leftScrollIndicator",
  476. Width = 1,
  477. Height = 1,
  478. Visible = false,
  479. Text = Glyphs.LeftArrow.ToString ()
  480. };
  481. _leftScrollIndicator.MouseClick += _host.Tab_MouseClick;
  482. Add (_rightScrollIndicator, _leftScrollIndicator);
  483. }
  484. protected override bool OnMouseEvent (MouseEventArgs me)
  485. {
  486. Tab hit = me.View is Tab ? (Tab)me.View : null;
  487. if (me.IsSingleClicked)
  488. {
  489. _host.OnTabClicked (new TabMouseEventArgs (hit, me));
  490. // user canceled click
  491. if (me.Handled)
  492. {
  493. return true;
  494. }
  495. }
  496. if (!me.IsSingleDoubleOrTripleClicked)
  497. {
  498. return false;
  499. }
  500. if (!HasFocus && CanFocus)
  501. {
  502. SetFocus ();
  503. }
  504. if (me.IsSingleDoubleOrTripleClicked)
  505. {
  506. var scrollIndicatorHit = 0;
  507. if (me.View is { } && me.View.Id == "rightScrollIndicator")
  508. {
  509. scrollIndicatorHit = 1;
  510. }
  511. else if (me.View is { } && me.View.Id == "leftScrollIndicator")
  512. {
  513. scrollIndicatorHit = -1;
  514. }
  515. if (scrollIndicatorHit != 0)
  516. {
  517. _host.SwitchTabBy (scrollIndicatorHit);
  518. SetNeedsLayout ();
  519. return true;
  520. }
  521. if (hit is { })
  522. {
  523. _host.SelectedTab = hit;
  524. SetNeedsLayout ();
  525. return true;
  526. }
  527. }
  528. return false;
  529. }
  530. /// <inheritdoc />
  531. protected override bool OnClearingViewport (Rectangle viewport)
  532. {
  533. // clear any old text
  534. ClearViewport ();
  535. return true;
  536. }
  537. protected override bool OnDrawingContent (Rectangle viewport)
  538. {
  539. _host._tabLocations = _host.CalculateViewport (Viewport).ToArray ();
  540. 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. int width = Viewport.Width;
  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 (int x, Tab tab, string textToRender, bool isSelected, int width)
  1178. {
  1179. X = x;
  1180. Tab = tab;
  1181. IsSelected = isSelected;
  1182. Width = width;
  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. public int Width { get; }
  1191. public int X { get; set; }
  1192. }
  1193. }