TabView.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. if (IsInitialized) {
  136. LayoutSubviews ();
  137. }
  138. SetNeedsDisplay ();
  139. }
  140. ///<inheritdoc/>
  141. public override void OnDrawContent (Rect contentArea)
  142. {
  143. Move (0, 0);
  144. Driver.SetAttribute (GetNormalColor ());
  145. if (Style.ShowBorder) {
  146. // How much space do we need to leave at the bottom to show the tabs
  147. int spaceAtBottom = Math.Max (0, GetTabHeight (false) - 1);
  148. int startAtY = Math.Max (0, GetTabHeight (true) - 1);
  149. Border.DrawFrame (new Rect (0, startAtY, Bounds.Width,
  150. Math.Max (Bounds.Height - spaceAtBottom - startAtY, 0)), false);
  151. }
  152. if (Tabs.Any ()) {
  153. tabsBar.OnDrawContent (contentArea);
  154. contentView.SetNeedsDisplay ();
  155. var savedClip = contentView.ClipToBounds ();
  156. contentView.Draw ();
  157. Driver.Clip = savedClip;
  158. }
  159. }
  160. /// <summary>
  161. /// Disposes the control and all <see cref="Tabs"/>
  162. /// </summary>
  163. /// <param name="disposing"></param>
  164. protected override void Dispose (bool disposing)
  165. {
  166. base.Dispose (disposing);
  167. // The selected tab will automatically be disposed but
  168. // any tabs not visible will need to be manually disposed
  169. foreach (var tab in Tabs) {
  170. if (!Equals (SelectedTab, tab)) {
  171. tab.View?.Dispose ();
  172. }
  173. }
  174. }
  175. /// <summary>
  176. /// Raises the <see cref="SelectedTabChanged"/> event
  177. /// </summary>
  178. protected virtual void OnSelectedTabChanged (Tab oldTab, Tab newTab)
  179. {
  180. SelectedTabChanged?.Invoke (this, new TabChangedEventArgs (oldTab, newTab));
  181. }
  182. /// <summary>
  183. /// Changes the <see cref="SelectedTab"/> by the given <paramref name="amount"/>.
  184. /// Positive for right, negative for left. If no tab is currently selected then
  185. /// the first tab will become selected
  186. /// </summary>
  187. /// <param name="amount"></param>
  188. public void SwitchTabBy (int amount)
  189. {
  190. if (Tabs.Count == 0) {
  191. return;
  192. }
  193. // if there is only one tab anyway or nothing is selected
  194. if (Tabs.Count == 1 || SelectedTab == null) {
  195. SelectedTab = Tabs.ElementAt (0);
  196. SetNeedsDisplay ();
  197. return;
  198. }
  199. var currentIdx = Tabs.IndexOf (SelectedTab);
  200. // Currently selected tab has vanished!
  201. if (currentIdx == -1) {
  202. SelectedTab = Tabs.ElementAt (0);
  203. SetNeedsDisplay ();
  204. return;
  205. }
  206. var newIdx = Math.Max (0, Math.Min (currentIdx + amount, Tabs.Count - 1));
  207. SelectedTab = tabs [newIdx];
  208. SetNeedsDisplay ();
  209. EnsureSelectedTabIsVisible ();
  210. }
  211. /// <summary>
  212. /// Updates <see cref="TabScrollOffset"/> to be a valid index of <see cref="Tabs"/>
  213. /// </summary>
  214. /// <remarks>Changes will not be immediately visible in the display until you call <see cref="View.SetNeedsDisplay()"/></remarks>
  215. public void EnsureValidScrollOffsets ()
  216. {
  217. TabScrollOffset = Math.Max (Math.Min (TabScrollOffset, Tabs.Count - 1), 0);
  218. }
  219. /// <summary>
  220. /// Updates <see cref="TabScrollOffset"/> to ensure that <see cref="SelectedTab"/> is visible
  221. /// </summary>
  222. public void EnsureSelectedTabIsVisible ()
  223. {
  224. if (!IsInitialized || SelectedTab == null) {
  225. return;
  226. }
  227. // if current viewport does not include the selected tab
  228. if (!CalculateViewport (Bounds).Any (r => Equals (SelectedTab, r.Tab))) {
  229. // Set scroll offset so the first tab rendered is the
  230. TabScrollOffset = Math.Max (0, Tabs.IndexOf (SelectedTab));
  231. }
  232. }
  233. /// <summary>
  234. /// Returns the number of rows occupied by rendering the tabs, this depends
  235. /// on <see cref="TabStyle.ShowTopLine"/> and can be 0 (e.g. if
  236. /// <see cref="TabStyle.TabsOnBottom"/> and you ask for <paramref name="top"/>).
  237. /// </summary>
  238. /// <param name="top">True to measure the space required at the top of the control,
  239. /// false to measure space at the bottom</param>
  240. /// <returns></returns>
  241. private int GetTabHeight (bool top)
  242. {
  243. if (top && Style.TabsOnBottom) {
  244. return 0;
  245. }
  246. if (!top && !Style.TabsOnBottom) {
  247. return 0;
  248. }
  249. return Style.ShowTopLine ? 3 : 2;
  250. }
  251. /// <summary>
  252. /// Returns which tabs to render at each x location
  253. /// </summary>
  254. /// <returns></returns>
  255. private IEnumerable<TabToRender> CalculateViewport (Rect bounds)
  256. {
  257. int i = 1;
  258. // Starting at the first or scrolled to tab
  259. foreach (var tab in Tabs.Skip (TabScrollOffset)) {
  260. // while there is space for the tab
  261. var tabTextWidth = tab.Text.EnumerateRunes ().Sum (c => c.GetColumns ());
  262. string text = tab.Text;
  263. // The maximum number of characters to use for the tab name as specified
  264. // by the user (MaxTabTextWidth). But not more than the width of the view
  265. // or we won't even be able to render a single tab!
  266. var maxWidth = Math.Max (0, Math.Min (bounds.Width - 3, MaxTabTextWidth));
  267. // if tab view is width <= 3 don't render any tabs
  268. if (maxWidth == 0) {
  269. yield return new TabToRender (i, tab, string.Empty, Equals (SelectedTab, tab), 0);
  270. break;
  271. }
  272. if (tabTextWidth > maxWidth) {
  273. text = tab.Text.Substring (0, (int)maxWidth);
  274. tabTextWidth = (int)maxWidth;
  275. }
  276. // if there is not enough space for this tab
  277. if (i + tabTextWidth >= bounds.Width) {
  278. break;
  279. }
  280. // there is enough space!
  281. yield return new TabToRender (i, tab, text, Equals (SelectedTab, tab), tabTextWidth);
  282. i += tabTextWidth + 1;
  283. }
  284. }
  285. /// <summary>
  286. /// Adds the given <paramref name="tab"/> to <see cref="Tabs"/>
  287. /// </summary>
  288. /// <param name="tab"></param>
  289. /// <param name="andSelect">True to make the newly added Tab the <see cref="SelectedTab"/></param>
  290. public void AddTab (Tab tab, bool andSelect)
  291. {
  292. if (tabs.Contains (tab)) {
  293. return;
  294. }
  295. tabs.Add (tab);
  296. if (SelectedTab == null || andSelect) {
  297. SelectedTab = tab;
  298. EnsureSelectedTabIsVisible ();
  299. tab.View?.SetFocus ();
  300. }
  301. SetNeedsDisplay ();
  302. }
  303. /// <summary>
  304. /// Removes the given <paramref name="tab"/> from <see cref="Tabs"/>.
  305. /// Caller is responsible for disposing the tab's hosted <see cref="Tab.View"/>
  306. /// if appropriate.
  307. /// </summary>
  308. /// <param name="tab"></param>
  309. public void RemoveTab (Tab tab)
  310. {
  311. if (tab == null || !tabs.Contains (tab)) {
  312. return;
  313. }
  314. // what tab was selected before closing
  315. var idx = tabs.IndexOf (tab);
  316. tabs.Remove (tab);
  317. // if the currently selected tab is no longer a member of Tabs
  318. if (SelectedTab == null || !Tabs.Contains (SelectedTab)) {
  319. // select the tab closest to the one that disappeared
  320. var toSelect = Math.Max (idx - 1, 0);
  321. if (toSelect < Tabs.Count) {
  322. SelectedTab = Tabs.ElementAt (toSelect);
  323. } else {
  324. SelectedTab = Tabs.LastOrDefault ();
  325. }
  326. }
  327. EnsureSelectedTabIsVisible ();
  328. SetNeedsDisplay ();
  329. }
  330. private class TabToRender {
  331. public int X { get; set; }
  332. public Tab Tab { get; set; }
  333. /// <summary>
  334. /// True if the tab that is being rendered is the selected one
  335. /// </summary>
  336. /// <value></value>
  337. public bool IsSelected { get; set; }
  338. public int Width { get; }
  339. public string TextToRender { get; }
  340. public TabToRender (int x, Tab tab, string textToRender, bool isSelected, int width)
  341. {
  342. X = x;
  343. Tab = tab;
  344. IsSelected = isSelected;
  345. Width = width;
  346. TextToRender = textToRender;
  347. }
  348. }
  349. private class TabRowView : View {
  350. readonly TabView host;
  351. public TabRowView (TabView host)
  352. {
  353. this.host = host;
  354. CanFocus = true;
  355. Height = 1;
  356. Width = Dim.Fill ();
  357. }
  358. public override bool OnEnter (View view)
  359. {
  360. Driver.SetCursorVisibility (CursorVisibility.Invisible);
  361. return base.OnEnter (view);
  362. }
  363. public override void OnDrawContent (Rect contentArea)
  364. {
  365. var tabLocations = host.CalculateViewport (Bounds).ToArray ();
  366. var width = Bounds.Width;
  367. Driver.SetAttribute (GetNormalColor ());
  368. if (host.Style.ShowTopLine) {
  369. RenderOverline (tabLocations, width);
  370. }
  371. RenderTabLine (tabLocations, width);
  372. RenderUnderline (tabLocations, width);
  373. Driver.SetAttribute (GetNormalColor ());
  374. }
  375. /// <summary>
  376. /// Renders the line of the tabs that does not adjoin the content
  377. /// </summary>
  378. /// <param name="tabLocations"></param>
  379. /// <param name="width"></param>
  380. private void RenderOverline (TabToRender [] tabLocations, int width)
  381. {
  382. // 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
  383. int y = host.Style.TabsOnBottom ? 2 : 0;
  384. Move (0, y);
  385. var selected = tabLocations.FirstOrDefault (t => t.IsSelected);
  386. // Clear out everything
  387. Driver.AddStr (new string (' ', width));
  388. // Nothing is selected... odd but we are done
  389. if (selected == null) {
  390. return;
  391. }
  392. Move (selected.X - 1, y);
  393. Driver.AddRune (host.Style.TabsOnBottom ? CM.Glyphs.LLCorner : CM.Glyphs.ULCorner);
  394. for (int i = 0; i < selected.Width; i++) {
  395. if (selected.X + i > width) {
  396. // we ran out of space horizontally
  397. return;
  398. }
  399. Driver.AddRune (CM.Glyphs.HLine);
  400. }
  401. // Add the end of the selected tab
  402. Driver.AddRune (host.Style.TabsOnBottom ? CM.Glyphs.LRCorner : CM.Glyphs.URCorner);
  403. }
  404. /// <summary>
  405. /// Renders the line with the tab names in it
  406. /// </summary>
  407. /// <param name="tabLocations"></param>
  408. /// <param name="width"></param>
  409. private void RenderTabLine (TabToRender [] tabLocations, int width)
  410. {
  411. int y;
  412. if (host.Style.TabsOnBottom) {
  413. y = 1;
  414. } else {
  415. y = host.Style.ShowTopLine ? 1 : 0;
  416. }
  417. // clear any old text
  418. Move (0, y);
  419. Driver.AddStr (new string (' ', width));
  420. foreach (var toRender in tabLocations) {
  421. if (toRender.IsSelected) {
  422. Move (toRender.X - 1, y);
  423. Driver.AddRune (CM.Glyphs.VLine);
  424. }
  425. Move (toRender.X, y);
  426. // if tab is the selected one and focus is inside this control
  427. if (toRender.IsSelected && host.HasFocus) {
  428. if (host.Focused == this) {
  429. // if focus is the tab bar ourself then show that they can switch tabs
  430. Driver.SetAttribute (ColorScheme.HotFocus);
  431. } else {
  432. // Focus is inside the tab
  433. Driver.SetAttribute (ColorScheme.HotNormal);
  434. }
  435. }
  436. Driver.AddStr (toRender.TextToRender);
  437. Driver.SetAttribute (GetNormalColor ());
  438. if (toRender.IsSelected) {
  439. Driver.AddRune (CM.Glyphs.VLine);
  440. }
  441. }
  442. }
  443. /// <summary>
  444. /// Renders the line of the tab that adjoins the content of the tab
  445. /// </summary>
  446. /// <param name="tabLocations"></param>
  447. /// <param name="width"></param>
  448. private void RenderUnderline (TabToRender [] tabLocations, int width)
  449. {
  450. int y = GetUnderlineYPosition ();
  451. Move (0, y);
  452. // If host has no border then we need to draw the solid line first (then we draw gaps over the top)
  453. if (!host.Style.ShowBorder) {
  454. for (int x = 0; x < width; x++) {
  455. Driver.AddRune (CM.Glyphs.HLine);
  456. }
  457. }
  458. var selected = tabLocations.FirstOrDefault (t => t.IsSelected);
  459. if (selected == null) {
  460. return;
  461. }
  462. Move (selected.X - 1, y);
  463. Driver.AddRune (selected.X == 1 ? CM.Glyphs.VLine :
  464. (host.Style.TabsOnBottom ? CM.Glyphs.URCorner : CM.Glyphs.LRCorner));
  465. Driver.AddStr (new string (' ', selected.Width));
  466. Driver.AddRune (selected.X + selected.Width == width - 1 ?
  467. CM.Glyphs.VLine :
  468. (host.Style.TabsOnBottom ? CM.Glyphs.ULCorner : CM.Glyphs.LLCorner));
  469. // draw scroll indicators
  470. // if there are more tabs to the left not visible
  471. if (host.TabScrollOffset > 0) {
  472. Move (0, y);
  473. // indicate that
  474. Driver.AddRune (CM.Glyphs.LeftArrow);
  475. }
  476. // if there are more tabs to the right not visible
  477. if (ShouldDrawRightScrollIndicator (tabLocations)) {
  478. Move (width - 1, y);
  479. // indicate that
  480. Driver.AddRune (CM.Glyphs.RightArrow);
  481. }
  482. }
  483. private bool ShouldDrawRightScrollIndicator (TabToRender [] tabLocations)
  484. {
  485. return tabLocations.LastOrDefault ()?.Tab != host.Tabs.LastOrDefault ();
  486. }
  487. private int GetUnderlineYPosition ()
  488. {
  489. if (host.Style.TabsOnBottom) {
  490. return 0;
  491. } else {
  492. return host.Style.ShowTopLine ? 2 : 1;
  493. }
  494. }
  495. public override bool MouseEvent (MouseEvent me)
  496. {
  497. var hit = ScreenToTab (me.X, me.Y);
  498. bool isClick = me.Flags.HasFlag (MouseFlags.Button1Clicked) ||
  499. me.Flags.HasFlag (MouseFlags.Button2Clicked) ||
  500. me.Flags.HasFlag (MouseFlags.Button3Clicked);
  501. if (isClick) {
  502. host.OnTabClicked (new TabMouseEventArgs (hit, me));
  503. // user canceled click
  504. if (me.Handled) {
  505. return true;
  506. }
  507. }
  508. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) &&
  509. !me.Flags.HasFlag (MouseFlags.Button1DoubleClicked) &&
  510. !me.Flags.HasFlag (MouseFlags.Button1TripleClicked))
  511. return false;
  512. if (!HasFocus && CanFocus) {
  513. SetFocus ();
  514. }
  515. if (me.Flags.HasFlag (MouseFlags.Button1Clicked) ||
  516. me.Flags.HasFlag (MouseFlags.Button1DoubleClicked) ||
  517. me.Flags.HasFlag (MouseFlags.Button1TripleClicked)) {
  518. var scrollIndicatorHit = ScreenToScrollIndicator (me.X, me.Y);
  519. if (scrollIndicatorHit != 0) {
  520. host.SwitchTabBy (scrollIndicatorHit);
  521. SetNeedsDisplay ();
  522. return true;
  523. }
  524. if (hit != null) {
  525. host.SelectedTab = hit;
  526. SetNeedsDisplay ();
  527. return true;
  528. }
  529. }
  530. return false;
  531. }
  532. /// <summary>
  533. /// Calculates whether scroll indicators are visible and if so whether the click
  534. /// was on one of them.
  535. /// </summary>
  536. /// <param name="x"></param>
  537. /// <param name="y"></param>
  538. /// <returns>-1 for click in scroll left, 1 for scroll right or 0 for no hit</returns>
  539. private int ScreenToScrollIndicator (int x, int y)
  540. {
  541. // scroll indicator is showing
  542. if (host.TabScrollOffset > 0 && x == 0) {
  543. return y == GetUnderlineYPosition () ? -1 : 0;
  544. }
  545. // scroll indicator is showing
  546. if (x == Bounds.Width - 1 && ShouldDrawRightScrollIndicator (host.CalculateViewport (Bounds).ToArray ())) {
  547. return y == GetUnderlineYPosition () ? 1 : 0;
  548. }
  549. return 0;
  550. }
  551. /// <summary>
  552. /// Translates the client coordinates of a click into a tab when the click is on top of a tab
  553. /// </summary>
  554. /// <param name="x"></param>
  555. /// <param name="y"></param>
  556. /// <returns></returns>
  557. public Tab ScreenToTab (int x, int y)
  558. {
  559. var tabs = host.CalculateViewport (Bounds);
  560. return tabs.LastOrDefault (t => x >= t.X && x < t.X + t.Width)?.Tab;
  561. }
  562. }
  563. /// <summary>
  564. /// Raises the <see cref="TabClicked"/> event.
  565. /// </summary>
  566. /// <param name="tabMouseEventArgs"></param>
  567. protected virtual private void OnTabClicked (TabMouseEventArgs tabMouseEventArgs)
  568. {
  569. TabClicked?.Invoke (this, tabMouseEventArgs);
  570. }
  571. }
  572. }