TabView.cs 23 KB

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