TabView.cs 19 KB

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