TabView.cs 23 KB

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