TabView.cs 20 KB

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