TabView.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Terminal.Gui;
  5. /// <summary>
  6. /// Control that hosts multiple sub views, presenting a single one at once.
  7. /// </summary>
  8. public class TabView : View {
  9. private Tab _selectedTab;
  10. /// <summary>
  11. /// The default <see cref="MaxTabTextWidth"/> to set on new <see cref="TabView"/> controls.
  12. /// </summary>
  13. public const uint DefaultMaxTabTextWidth = 30;
  14. /// <summary>
  15. /// This sub view is the 2 or 3 line control that represents the actual tabs themselves.
  16. /// </summary>
  17. TabRowView _tabsBar;
  18. /// <summary>
  19. /// This sub view is the main client area of the current tab. It hosts the <see cref="Tab.View"/>
  20. /// of the tab, the <see cref="SelectedTab"/>.
  21. /// </summary>
  22. View _contentView;
  23. private List<Tab> _tabs = new List<Tab> ();
  24. /// <summary>
  25. /// All tabs currently hosted by the control.
  26. /// </summary>
  27. /// <value></value>
  28. public IReadOnlyCollection<Tab> Tabs { get => _tabs.AsReadOnly (); }
  29. /// <summary>
  30. /// When there are too many tabs to render, this indicates the first
  31. /// tab to render on the screen.
  32. /// </summary>
  33. /// <value></value>
  34. public int TabScrollOffset {
  35. get => _tabScrollOffset;
  36. set {
  37. _tabScrollOffset = EnsureValidScrollOffsets (value);
  38. }
  39. }
  40. /// <summary>
  41. /// The maximum number of characters to render in a Tab header. This prevents one long tab
  42. /// from pushing out all the others.
  43. /// </summary>
  44. public uint MaxTabTextWidth { get; set; } = DefaultMaxTabTextWidth;
  45. /// <summary>
  46. /// Event for when <see cref="SelectedTab"/> changes.
  47. /// </summary>
  48. public event EventHandler<TabChangedEventArgs> SelectedTabChanged;
  49. /// <summary>
  50. /// Event fired when a <see cref="Tab"/> is clicked. Can be used to cancel navigation,
  51. /// show context menu (e.g. on right click) etc.
  52. /// </summary>
  53. public event EventHandler<TabMouseEventArgs> TabClicked;
  54. /// <summary>
  55. /// The currently selected member of <see cref="Tabs"/> chosen by the user.
  56. /// </summary>
  57. /// <value></value>
  58. public Tab SelectedTab {
  59. get => _selectedTab;
  60. set {
  61. UnSetCurrentTabs ();
  62. var old = _selectedTab;
  63. if (_selectedTab != null) {
  64. if (_selectedTab.View != null) {
  65. // remove old content
  66. _contentView.Remove (_selectedTab.View);
  67. }
  68. }
  69. _selectedTab = value;
  70. if (value != null) {
  71. // add new content
  72. if (_selectedTab.View != null) {
  73. _contentView.Add (_selectedTab.View);
  74. }
  75. }
  76. EnsureSelectedTabIsVisible ();
  77. if (old != value) {
  78. if (old?.HasFocus == true) {
  79. SelectedTab.SetFocus ();
  80. }
  81. OnSelectedTabChanged (old, value);
  82. }
  83. }
  84. }
  85. /// <summary>
  86. /// Render choices for how to display tabs. After making changes, call <see cref="ApplyStyleChanges()"/>.
  87. /// </summary>
  88. /// <value></value>
  89. public TabStyle Style { get; set; } = new TabStyle ();
  90. /// <summary>
  91. /// Initializes a <see cref="TabView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  92. /// </summary>
  93. public TabView () : base ()
  94. {
  95. CanFocus = true;
  96. _tabsBar = new TabRowView (this);
  97. _contentView = new View ();
  98. ApplyStyleChanges ();
  99. base.Add (_tabsBar);
  100. base.Add (_contentView);
  101. // Things this view knows how to do
  102. AddCommand (Command.Left, () => { SwitchTabBy (-1); return true; });
  103. AddCommand (Command.Right, () => { SwitchTabBy (1); return true; });
  104. AddCommand (Command.LeftHome, () => { TabScrollOffset = 0; SelectedTab = Tabs.FirstOrDefault (); return true; });
  105. AddCommand (Command.RightEnd, () => { TabScrollOffset = Tabs.Count - 1; SelectedTab = Tabs.LastOrDefault (); return true; });
  106. AddCommand (Command.NextView, () => { _contentView.SetFocus (); return true; });
  107. AddCommand (Command.PreviousView, () => { SuperView?.FocusPrev (); return true; });
  108. AddCommand (Command.PageDown, () => { TabScrollOffset += _tabLocations.Length; SelectedTab = Tabs.ElementAt (TabScrollOffset); return true; });
  109. AddCommand (Command.PageUp, () => { TabScrollOffset -= _tabLocations.Length; SelectedTab = Tabs.ElementAt (TabScrollOffset); return true; });
  110. // Default keybindings for this view
  111. KeyBindings.Add (KeyCode.CursorLeft, Command.Left);
  112. KeyBindings.Add (KeyCode.CursorRight, Command.Right);
  113. KeyBindings.Add (KeyCode.Home, Command.LeftHome);
  114. KeyBindings.Add (KeyCode.End, Command.RightEnd);
  115. KeyBindings.Add (KeyCode.CursorDown, Command.NextView);
  116. KeyBindings.Add (KeyCode.CursorUp, Command.PreviousView);
  117. KeyBindings.Add (KeyCode.PageDown, Command.PageDown);
  118. KeyBindings.Add (KeyCode.PageUp, Command.PageUp);
  119. }
  120. /// <summary>
  121. /// Updates the control to use the latest state settings in <see cref="Style"/>.
  122. /// This can change the size of the client area of the tab (for rendering the
  123. /// selected tab's content). This method includes a call
  124. /// to <see cref="View.SetNeedsDisplay()"/>.
  125. /// </summary>
  126. public void ApplyStyleChanges ()
  127. {
  128. _contentView.BorderStyle = Style.ShowBorder ? LineStyle.Single : LineStyle.None;
  129. _contentView.Width = Dim.Fill ();
  130. if (Style.TabsOnBottom) {
  131. // Tabs are along the bottom so just dodge the border
  132. if (Style.ShowBorder) {
  133. _contentView.Border.Thickness = new Thickness (1, 1, 1, 0);
  134. }
  135. _contentView.Y = 0;
  136. var tabHeight = GetTabHeight (false);
  137. // Fill client area leaving space at bottom for tabs
  138. _contentView.Height = Dim.Fill (tabHeight);
  139. _tabsBar.Height = tabHeight;
  140. _tabsBar.Y = Pos.Bottom (_contentView);
  141. } else {
  142. // Tabs are along the top
  143. if (Style.ShowBorder) {
  144. _contentView.Border.Thickness = new Thickness (1, 0, 1, 1);
  145. }
  146. _tabsBar.Y = 0;
  147. var tabHeight = GetTabHeight (true);
  148. //move content down to make space for tabs
  149. _contentView.Y = Pos.Bottom (_tabsBar);
  150. // Fill client area leaving space at bottom for border
  151. _contentView.Height = Dim.Fill ();
  152. // The top tab should be 2 or 3 rows high and on the top
  153. _tabsBar.Height = tabHeight;
  154. // Should be able to just use 0 but switching between top/bottom tabs repeatedly breaks in ValidatePosDim if just using the absolute value 0
  155. }
  156. if (IsInitialized) {
  157. LayoutSubviews ();
  158. }
  159. SetNeedsDisplay ();
  160. }
  161. ///<inheritdoc/>
  162. public override void OnDrawContent (Rect contentArea)
  163. {
  164. Driver.SetAttribute (GetNormalColor ());
  165. if (Tabs.Any ()) {
  166. var savedClip = ClipToBounds ();
  167. _tabsBar.OnDrawContent (contentArea);
  168. _contentView.SetNeedsDisplay ();
  169. _contentView.Draw ();
  170. Driver.Clip = savedClip;
  171. }
  172. }
  173. ///<inheritdoc/>
  174. public override void OnDrawContentComplete (Rect contentArea)
  175. {
  176. _tabsBar.OnDrawContentComplete (contentArea);
  177. }
  178. /// <summary>
  179. /// Disposes the control and all <see cref="Tabs"/>.
  180. /// </summary>
  181. /// <param name="disposing"></param>
  182. protected override void Dispose (bool disposing)
  183. {
  184. base.Dispose (disposing);
  185. // The selected tab will automatically be disposed but
  186. // any tabs not visible will need to be manually disposed
  187. foreach (var tab in Tabs) {
  188. if (!Equals (SelectedTab, tab)) {
  189. tab.View?.Dispose ();
  190. }
  191. }
  192. }
  193. /// <summary>
  194. /// Raises the <see cref="SelectedTabChanged"/> event.
  195. /// </summary>
  196. protected virtual void OnSelectedTabChanged (Tab oldTab, Tab newTab)
  197. {
  198. SelectedTabChanged?.Invoke (this, new TabChangedEventArgs (oldTab, newTab));
  199. }
  200. /// <summary>
  201. /// Changes the <see cref="SelectedTab"/> by the given <paramref name="amount"/>.
  202. /// Positive for right, negative for left. If no tab is currently selected then
  203. /// the first tab will become selected.
  204. /// </summary>
  205. /// <param name="amount"></param>
  206. public void SwitchTabBy (int amount)
  207. {
  208. if (Tabs.Count == 0) {
  209. return;
  210. }
  211. // if there is only one tab anyway or nothing is selected
  212. if (Tabs.Count == 1 || SelectedTab == null) {
  213. SelectedTab = Tabs.ElementAt (0);
  214. SetNeedsDisplay ();
  215. return;
  216. }
  217. var currentIdx = Tabs.IndexOf (SelectedTab);
  218. // Currently selected tab has vanished!
  219. if (currentIdx == -1) {
  220. SelectedTab = Tabs.ElementAt (0);
  221. SetNeedsDisplay ();
  222. return;
  223. }
  224. var newIdx = Math.Max (0, Math.Min (currentIdx + amount, Tabs.Count - 1));
  225. SelectedTab = _tabs [newIdx];
  226. SetNeedsDisplay ();
  227. EnsureSelectedTabIsVisible ();
  228. }
  229. /// <summary>
  230. /// Updates <see cref="TabScrollOffset"/> to be a valid index of <see cref="Tabs"/>.
  231. /// </summary>
  232. /// <param name="value">The value to validate.</param>
  233. /// <remarks>Changes will not be immediately visible in the display until you call <see cref="View.SetNeedsDisplay()"/>.</remarks>
  234. /// <returns>The valid <see cref="TabScrollOffset"/> for the given value.</returns>
  235. public int EnsureValidScrollOffsets (int value)
  236. {
  237. return Math.Max (Math.Min (value, Tabs.Count - 1), 0);
  238. }
  239. /// <summary>
  240. /// Updates <see cref="TabScrollOffset"/> to ensure that <see cref="SelectedTab"/> is visible.
  241. /// </summary>
  242. public void EnsureSelectedTabIsVisible ()
  243. {
  244. if (!IsInitialized || SelectedTab == null) {
  245. return;
  246. }
  247. // if current viewport does not include the selected tab
  248. if (!CalculateViewport (Bounds).Any (r => Equals (SelectedTab, r.Tab))) {
  249. // Set scroll offset so the first tab rendered is the
  250. TabScrollOffset = Math.Max (0, Tabs.IndexOf (SelectedTab));
  251. }
  252. }
  253. /// <summary>
  254. /// Returns the number of rows occupied by rendering the tabs, this depends
  255. /// on <see cref="TabStyle.ShowTopLine"/> and can be 0 (e.g. if
  256. /// <see cref="TabStyle.TabsOnBottom"/> and you ask for <paramref name="top"/>).
  257. /// </summary>
  258. /// <param name="top">True to measure the space required at the top of the control,
  259. /// false to measure space at the bottom.</param>.
  260. /// <returns></returns>
  261. private int GetTabHeight (bool top)
  262. {
  263. if (top && Style.TabsOnBottom) {
  264. return 0;
  265. }
  266. if (!top && !Style.TabsOnBottom) {
  267. return 0;
  268. }
  269. return Style.ShowTopLine ? 3 : 2;
  270. }
  271. private TabToRender [] _tabLocations;
  272. private int _tabScrollOffset;
  273. /// <summary>
  274. /// Returns which tabs to render at each x location.
  275. /// </summary>
  276. /// <returns></returns>
  277. private IEnumerable<TabToRender> CalculateViewport (Rect bounds)
  278. {
  279. UnSetCurrentTabs ();
  280. int i = 1;
  281. View prevTab = null;
  282. // Starting at the first or scrolled to tab
  283. foreach (var tab in Tabs.Skip (TabScrollOffset)) {
  284. if (prevTab != null) {
  285. tab.X = Pos.Right (prevTab);
  286. } else {
  287. tab.X = 0;
  288. }
  289. tab.Y = 0;
  290. // while there is space for the tab
  291. var tabTextWidth = tab.DisplayText.EnumerateRunes ().Sum (c => c.GetColumns ());
  292. string text = tab.DisplayText;
  293. // The maximum number of characters to use for the tab name as specified
  294. // by the user (MaxTabTextWidth). But not more than the width of the view
  295. // or we won't even be able to render a single tab!
  296. var maxWidth = Math.Max (0, Math.Min (bounds.Width - 3, MaxTabTextWidth));
  297. prevTab = tab;
  298. tab.Width = 2;
  299. tab.Height = Style.ShowTopLine ? 3 : 2;
  300. // if tab view is width <= 3 don't render any tabs
  301. if (maxWidth == 0) {
  302. tab.Visible = true;
  303. tab.MouseClick += Tab_MouseClick;
  304. yield return new TabToRender (i, tab, string.Empty, Equals (SelectedTab, tab), 0);
  305. break;
  306. }
  307. if (tabTextWidth > maxWidth) {
  308. text = tab.DisplayText.Substring (0, (int)maxWidth);
  309. tabTextWidth = (int)maxWidth;
  310. }
  311. tab.Width = Math.Max (tabTextWidth + 2, 1);
  312. tab.Height = Style.ShowTopLine ? 3 : 2;
  313. // if there is not enough space for this tab
  314. if (i + tabTextWidth >= bounds.Width) {
  315. tab.Visible = false;
  316. break;
  317. }
  318. // there is enough space!
  319. tab.Visible = true;
  320. tab.MouseClick += Tab_MouseClick;
  321. yield return new TabToRender (i, tab, text, Equals (SelectedTab, tab), tabTextWidth);
  322. i += tabTextWidth + 1;
  323. }
  324. }
  325. private void UnSetCurrentTabs ()
  326. {
  327. if (_tabLocations != null) {
  328. foreach (var tabToRender in _tabLocations) {
  329. tabToRender.Tab.MouseClick -= Tab_MouseClick;
  330. tabToRender.Tab.Visible = false;
  331. }
  332. _tabLocations = null;
  333. }
  334. }
  335. private void Tab_MouseClick (object sender, MouseEventEventArgs e)
  336. {
  337. e.Handled = _tabsBar.MouseEvent (e.MouseEvent);
  338. }
  339. /// <summary>
  340. /// Adds the given <paramref name="tab"/> to <see cref="Tabs"/>.
  341. /// </summary>
  342. /// <param name="tab"></param>
  343. /// <param name="andSelect">True to make the newly added Tab the <see cref="SelectedTab"/>.</param>
  344. public void AddTab (Tab tab, bool andSelect)
  345. {
  346. if (_tabs.Contains (tab)) {
  347. return;
  348. }
  349. _tabs.Add (tab);
  350. _tabsBar.Add (tab);
  351. if (SelectedTab == null || andSelect) {
  352. SelectedTab = tab;
  353. EnsureSelectedTabIsVisible ();
  354. tab.View?.SetFocus ();
  355. }
  356. SetNeedsDisplay ();
  357. }
  358. /// <summary>
  359. /// Removes the given <paramref name="tab"/> from <see cref="Tabs"/>.
  360. /// Caller is responsible for disposing the tab's hosted <see cref="Tab.View"/>
  361. /// if appropriate.
  362. /// </summary>
  363. /// <param name="tab"></param>
  364. public void RemoveTab (Tab tab)
  365. {
  366. if (tab == null || !_tabs.Contains (tab)) {
  367. return;
  368. }
  369. // what tab was selected before closing
  370. var idx = _tabs.IndexOf (tab);
  371. _tabs.Remove (tab);
  372. // if the currently selected tab is no longer a member of Tabs
  373. if (SelectedTab == null || !Tabs.Contains (SelectedTab)) {
  374. // select the tab closest to the one that disappeared
  375. var toSelect = Math.Max (idx - 1, 0);
  376. if (toSelect < Tabs.Count) {
  377. SelectedTab = Tabs.ElementAt (toSelect);
  378. } else {
  379. SelectedTab = Tabs.LastOrDefault ();
  380. }
  381. }
  382. EnsureSelectedTabIsVisible ();
  383. SetNeedsDisplay ();
  384. }
  385. private class TabToRender {
  386. public int X { get; set; }
  387. public Tab Tab { get; set; }
  388. /// <summary>
  389. /// True if the tab that is being rendered is the selected one.
  390. /// </summary>
  391. /// <value></value>
  392. public bool IsSelected { get; set; }
  393. public int Width { get; }
  394. public string TextToRender { get; }
  395. public TabToRender (int x, Tab tab, string textToRender, bool isSelected, int width)
  396. {
  397. X = x;
  398. Tab = tab;
  399. IsSelected = isSelected;
  400. Width = width;
  401. TextToRender = textToRender;
  402. }
  403. }
  404. private class TabRowView : View {
  405. readonly TabView _host;
  406. View _rightScrollIndicator;
  407. View _leftScrollIndicator;
  408. public TabRowView (TabView host)
  409. {
  410. this._host = host;
  411. CanFocus = true;
  412. Height = 1;
  413. Width = Dim.Fill ();
  414. _rightScrollIndicator = new View () { Id = "rightScrollIndicator", Width = 1, Height = 1, Visible = false, Text = CM.Glyphs.RightArrow.ToString () };
  415. _rightScrollIndicator.MouseClick += _host.Tab_MouseClick;
  416. _leftScrollIndicator = new View () { Id = "leftScrollIndicator", Width = 1, Height = 1, Visible = false, Text = CM.Glyphs.LeftArrow.ToString () };
  417. _leftScrollIndicator.MouseClick += _host.Tab_MouseClick;
  418. Add (_rightScrollIndicator, _leftScrollIndicator);
  419. }
  420. public override bool OnEnter (View view)
  421. {
  422. Driver.SetCursorVisibility (CursorVisibility.Invisible);
  423. return base.OnEnter (view);
  424. }
  425. public override void OnDrawContent (Rect contentArea)
  426. {
  427. _host._tabLocations = _host.CalculateViewport (Bounds).ToArray ();
  428. // clear any old text
  429. Clear ();
  430. RenderTabLine ();
  431. RenderUnderline ();
  432. Driver.SetAttribute (GetNormalColor ());
  433. }
  434. public override void OnDrawContentComplete (Rect contentArea)
  435. {
  436. if (_host._tabLocations == null) {
  437. return;
  438. }
  439. var tabLocations = _host._tabLocations;
  440. var selectedTab = -1;
  441. for (int i = 0; i < tabLocations.Length; i++) {
  442. View tab = tabLocations [i].Tab;
  443. var vts = tab.BoundsToScreen (tab.Bounds);
  444. LineCanvas lc = new LineCanvas ();
  445. var selectedOffset = _host.Style.ShowTopLine && tabLocations [i].IsSelected ? 0 : 1;
  446. if (tabLocations [i].IsSelected) {
  447. selectedTab = i;
  448. if (i == 0 && _host.TabScrollOffset == 0) {
  449. if (_host.Style.TabsOnBottom) {
  450. // Upper left vertical line
  451. lc.AddLine (new Point (vts.X - 1, vts.Y - 1), -1, Orientation.Vertical, tab.BorderStyle);
  452. } else {
  453. // Lower left vertical line
  454. lc.AddLine (new Point (vts.X - 1, vts.Bottom - selectedOffset), -1, Orientation.Vertical, tab.BorderStyle);
  455. }
  456. } else if (i > 0 && i <= tabLocations.Length - 1) {
  457. if (_host.Style.TabsOnBottom) {
  458. // URCorner
  459. lc.AddLine (new Point (vts.X - 1, vts.Y - 1), 1, Orientation.Vertical, tab.BorderStyle);
  460. lc.AddLine (new Point (vts.X - 1, vts.Y - 1), -1, Orientation.Horizontal, tab.BorderStyle);
  461. } else {
  462. // LRCorner
  463. lc.AddLine (new Point (vts.X - 1, vts.Bottom - selectedOffset), -1, Orientation.Vertical, tab.BorderStyle);
  464. lc.AddLine (new Point (vts.X - 1, vts.Bottom - selectedOffset), -1, Orientation.Horizontal, tab.BorderStyle);
  465. }
  466. if (_host.Style.ShowTopLine) {
  467. if (_host.Style.TabsOnBottom) {
  468. // Lower left tee
  469. lc.AddLine (new Point (vts.X - 1, vts.Bottom), -1, Orientation.Vertical, tab.BorderStyle);
  470. lc.AddLine (new Point (vts.X - 1, vts.Bottom), 0, Orientation.Horizontal, tab.BorderStyle);
  471. } else {
  472. // Upper left tee
  473. lc.AddLine (new Point (vts.X - 1, vts.Y - 1), 1, Orientation.Vertical, tab.BorderStyle);
  474. lc.AddLine (new Point (vts.X - 1, vts.Y - 1), 0, Orientation.Horizontal, tab.BorderStyle);
  475. }
  476. }
  477. }
  478. if (i < tabLocations.Length - 1) {
  479. if (_host.Style.ShowTopLine) {
  480. if (_host.Style.TabsOnBottom) {
  481. // Lower right tee
  482. lc.AddLine (new Point (vts.Right, vts.Bottom), -1, Orientation.Vertical, tab.BorderStyle);
  483. lc.AddLine (new Point (vts.Right, vts.Bottom), 0, Orientation.Horizontal, tab.BorderStyle);
  484. } else {
  485. // Upper right tee
  486. lc.AddLine (new Point (vts.Right, vts.Y - 1), 1, Orientation.Vertical, tab.BorderStyle);
  487. lc.AddLine (new Point (vts.Right, vts.Y - 1), 0, Orientation.Horizontal, tab.BorderStyle);
  488. }
  489. }
  490. }
  491. if (_host.Style.TabsOnBottom) {
  492. //URCorner
  493. lc.AddLine (new Point (vts.Right, vts.Y - 1), 1, Orientation.Vertical, tab.BorderStyle);
  494. lc.AddLine (new Point (vts.Right, vts.Y - 1), 1, Orientation.Horizontal, tab.BorderStyle);
  495. } else {
  496. //LLCorner
  497. lc.AddLine (new Point (vts.Right, vts.Bottom - selectedOffset), -1, Orientation.Vertical, tab.BorderStyle);
  498. lc.AddLine (new Point (vts.Right, vts.Bottom - selectedOffset), 1, Orientation.Horizontal, tab.BorderStyle);
  499. }
  500. } else if (selectedTab == -1) {
  501. if (i == 0 && string.IsNullOrEmpty (tab.Text)) {
  502. if (_host.Style.TabsOnBottom) {
  503. if (_host.Style.ShowTopLine) {
  504. // LLCorner
  505. lc.AddLine (new Point (vts.X - 1, vts.Bottom), -1, Orientation.Vertical, tab.BorderStyle);
  506. lc.AddLine (new Point (vts.X - 1, vts.Bottom), 1, Orientation.Horizontal, tab.BorderStyle);
  507. }
  508. // ULCorner
  509. lc.AddLine (new Point (vts.X - 1, vts.Y - 1), 1, Orientation.Vertical, tab.BorderStyle);
  510. lc.AddLine (new Point (vts.X - 1, vts.Y - 1), 1, Orientation.Horizontal, tab.BorderStyle);
  511. } else {
  512. if (_host.Style.ShowTopLine) {
  513. // ULCorner
  514. lc.AddLine (new Point (vts.X - 1, vts.Y - 1), 1, Orientation.Vertical, tab.BorderStyle);
  515. lc.AddLine (new Point (vts.X - 1, vts.Y - 1), 1, Orientation.Horizontal, tab.BorderStyle);
  516. }
  517. // LLCorner
  518. lc.AddLine (new Point (vts.X - 1, vts.Bottom), -1, Orientation.Vertical, tab.BorderStyle);
  519. lc.AddLine (new Point (vts.X - 1, vts.Bottom), 1, Orientation.Horizontal, tab.BorderStyle);
  520. }
  521. } else if (i > 0) {
  522. if (_host.Style.ShowTopLine || _host.Style.TabsOnBottom) {
  523. // Upper left tee
  524. lc.AddLine (new Point (vts.X - 1, vts.Y - 1), 1, Orientation.Vertical, tab.BorderStyle);
  525. lc.AddLine (new Point (vts.X - 1, vts.Y - 1), 0, Orientation.Horizontal, tab.BorderStyle);
  526. }
  527. // Lower left tee
  528. lc.AddLine (new Point (vts.X - 1, vts.Bottom), -1, Orientation.Vertical, tab.BorderStyle);
  529. lc.AddLine (new Point (vts.X - 1, vts.Bottom), 0, Orientation.Horizontal, tab.BorderStyle);
  530. }
  531. } else if (i < tabLocations.Length - 1) {
  532. if (_host.Style.ShowTopLine) {
  533. // Upper right tee
  534. lc.AddLine (new Point (vts.Right, vts.Y - 1), 1, Orientation.Vertical, tab.BorderStyle);
  535. lc.AddLine (new Point (vts.Right, vts.Y - 1), 0, Orientation.Horizontal, tab.BorderStyle);
  536. }
  537. if (_host.Style.ShowTopLine || !_host.Style.TabsOnBottom) {
  538. // Lower right tee
  539. lc.AddLine (new Point (vts.Right, vts.Bottom), -1, Orientation.Vertical, tab.BorderStyle);
  540. lc.AddLine (new Point (vts.Right, vts.Bottom), 0, Orientation.Horizontal, tab.BorderStyle);
  541. } else {
  542. // Upper right tee
  543. lc.AddLine (new Point (vts.Right, vts.Y - 1), 1, Orientation.Vertical, tab.BorderStyle);
  544. lc.AddLine (new Point (vts.Right, vts.Y - 1), 0, Orientation.Horizontal, tab.BorderStyle);
  545. }
  546. }
  547. if (i == 0 && i != selectedTab && _host.TabScrollOffset == 0 && _host.Style.ShowBorder) {
  548. if (_host.Style.TabsOnBottom) {
  549. // Upper left vertical line
  550. lc.AddLine (new Point (vts.X - 1, vts.Y - 1), 0, Orientation.Vertical, tab.BorderStyle);
  551. lc.AddLine (new Point (vts.X - 1, vts.Y - 1), 1, Orientation.Horizontal, tab.BorderStyle);
  552. } else {
  553. // Lower left vertical line
  554. lc.AddLine (new Point (vts.X - 1, vts.Bottom), 0, Orientation.Vertical, tab.BorderStyle);
  555. lc.AddLine (new Point (vts.X - 1, vts.Bottom), 1, Orientation.Horizontal, tab.BorderStyle);
  556. }
  557. }
  558. if (i == tabLocations.Length - 1 && i != selectedTab) {
  559. if (_host.Style.TabsOnBottom) {
  560. // Upper right tee
  561. lc.AddLine (new Point (vts.Right, vts.Y - 1), 1, Orientation.Vertical, tab.BorderStyle);
  562. lc.AddLine (new Point (vts.Right, vts.Y - 1), 0, Orientation.Horizontal, tab.BorderStyle);
  563. } else {
  564. // Lower right tee
  565. lc.AddLine (new Point (vts.Right, vts.Bottom), -1, Orientation.Vertical, tab.BorderStyle);
  566. lc.AddLine (new Point (vts.Right, vts.Bottom), 0, Orientation.Horizontal, tab.BorderStyle);
  567. }
  568. }
  569. if (i == tabLocations.Length - 1) {
  570. var arrowOffset = 1;
  571. var lastSelectedTab = !_host.Style.ShowTopLine && i == selectedTab ? 1 : _host.Style.TabsOnBottom ? 1 : 0;
  572. var tabsBarVts = BoundsToScreen (Bounds);
  573. var lineLength = tabsBarVts.Right - vts.Right;
  574. // Right horizontal line
  575. if (ShouldDrawRightScrollIndicator ()) {
  576. if (lineLength - arrowOffset > 0) {
  577. if (_host.Style.TabsOnBottom) {
  578. lc.AddLine (new Point (vts.Right, vts.Y - lastSelectedTab), lineLength - arrowOffset, Orientation.Horizontal, tab.BorderStyle);
  579. } else {
  580. lc.AddLine (new Point (vts.Right, vts.Bottom - lastSelectedTab), lineLength - arrowOffset, Orientation.Horizontal, tab.BorderStyle);
  581. }
  582. }
  583. } else {
  584. if (_host.Style.TabsOnBottom) {
  585. lc.AddLine (new Point (vts.Right, vts.Y - lastSelectedTab), lineLength, Orientation.Horizontal, tab.BorderStyle);
  586. } else {
  587. lc.AddLine (new Point (vts.Right, vts.Bottom - lastSelectedTab), lineLength, Orientation.Horizontal, tab.BorderStyle);
  588. }
  589. if (_host.Style.ShowBorder) {
  590. if (_host.Style.TabsOnBottom) {
  591. // More LRCorner
  592. lc.AddLine (new Point (tabsBarVts.Right - 1, vts.Y - lastSelectedTab), -1, Orientation.Vertical, tab.BorderStyle);
  593. } else {
  594. // More URCorner
  595. lc.AddLine (new Point (tabsBarVts.Right - 1, vts.Bottom - lastSelectedTab), 1, Orientation.Vertical, tab.BorderStyle);
  596. }
  597. }
  598. }
  599. }
  600. tab.LineCanvas.Merge (lc);
  601. tab.OnRenderLineCanvas ();
  602. }
  603. }
  604. /// <summary>
  605. /// Renders the line with the tab names in it.
  606. /// </summary>
  607. private void RenderTabLine ()
  608. {
  609. var tabLocations = _host._tabLocations;
  610. int y;
  611. if (_host.Style.TabsOnBottom) {
  612. y = 1;
  613. } else {
  614. y = _host.Style.ShowTopLine ? 1 : 0;
  615. }
  616. View selected = null;
  617. var topLine = _host.Style.ShowTopLine ? 1 : 0;
  618. var width = Bounds.Width;
  619. foreach (var toRender in tabLocations) {
  620. var tab = toRender.Tab;
  621. if (toRender.IsSelected) {
  622. selected = tab;
  623. if (_host.Style.TabsOnBottom) {
  624. tab.Border.Thickness = new Thickness (1, 0, 1, topLine);
  625. tab.Margin.Thickness = new Thickness (0, 1, 0, 0);
  626. } else {
  627. tab.Border.Thickness = new Thickness (1, topLine, 1, 0);
  628. tab.Margin.Thickness = new Thickness (0, 0, 0, topLine);
  629. }
  630. } else if (selected == null) {
  631. if (_host.Style.TabsOnBottom) {
  632. tab.Border.Thickness = new Thickness (1, 1, 0, topLine);
  633. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  634. } else {
  635. tab.Border.Thickness = new Thickness (1, topLine, 0, 1);
  636. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  637. }
  638. tab.Width = Math.Max (tab.Width.Anchor (0) - 1, 1);
  639. } else {
  640. if (_host.Style.TabsOnBottom) {
  641. tab.Border.Thickness = new Thickness (0, 1, 1, topLine);
  642. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  643. } else {
  644. tab.Border.Thickness = new Thickness (0, topLine, 1, 1);
  645. tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
  646. }
  647. tab.Width = Math.Max (tab.Width.Anchor (0) - 1, 1);
  648. }
  649. tab.Text = toRender.TextToRender;
  650. LayoutSubviews ();
  651. tab.OnDrawAdornments ();
  652. var prevAttr = Driver.GetAttribute ();
  653. // if tab is the selected one and focus is inside this control
  654. if (toRender.IsSelected && _host.HasFocus) {
  655. if (_host.Focused == this) {
  656. // if focus is the tab bar ourself then show that they can switch tabs
  657. prevAttr = ColorScheme.HotFocus;
  658. } else {
  659. // Focus is inside the tab
  660. prevAttr = ColorScheme.HotNormal;
  661. }
  662. }
  663. tab.TextFormatter.Draw (tab.BoundsToScreen (tab.Bounds), prevAttr, ColorScheme.HotNormal);
  664. tab.OnRenderLineCanvas ();
  665. Driver.SetAttribute (GetNormalColor ());
  666. }
  667. }
  668. /// <summary>
  669. /// Renders the line of the tab that adjoins the content of the tab.
  670. /// </summary>
  671. private void RenderUnderline ()
  672. {
  673. int y = GetUnderlineYPosition ();
  674. var selected = _host._tabLocations.FirstOrDefault (t => t.IsSelected);
  675. if (selected == null) {
  676. return;
  677. }
  678. // draw scroll indicators
  679. // if there are more tabs to the left not visible
  680. if (_host.TabScrollOffset > 0) {
  681. _leftScrollIndicator.X = 0;
  682. _leftScrollIndicator.Y = y;
  683. // indicate that
  684. _leftScrollIndicator.Visible = true;
  685. // Ensures this is clicked instead of the first tab
  686. BringSubviewToFront (_leftScrollIndicator);
  687. _leftScrollIndicator.Draw ();
  688. } else {
  689. _leftScrollIndicator.Visible = false;
  690. }
  691. // if there are more tabs to the right not visible
  692. if (ShouldDrawRightScrollIndicator ()) {
  693. _rightScrollIndicator.X = Bounds.Width - 1;
  694. _rightScrollIndicator.Y = y;
  695. // indicate that
  696. _rightScrollIndicator.Visible = true;
  697. // Ensures this is clicked instead of the last tab if under this
  698. BringSubviewToFront (_rightScrollIndicator);
  699. _rightScrollIndicator.Draw ();
  700. } else {
  701. _rightScrollIndicator.Visible = false;
  702. }
  703. }
  704. private bool ShouldDrawRightScrollIndicator ()
  705. {
  706. return _host._tabLocations.LastOrDefault ()?.Tab != _host.Tabs.LastOrDefault ();
  707. }
  708. private int GetUnderlineYPosition ()
  709. {
  710. if (_host.Style.TabsOnBottom) {
  711. return 0;
  712. } else {
  713. return _host.Style.ShowTopLine ? 2 : 1;
  714. }
  715. }
  716. public override bool MouseEvent (MouseEvent me)
  717. {
  718. var hit = me.View is Tab ? (Tab)me.View : null;
  719. bool isClick = me.Flags.HasFlag (MouseFlags.Button1Clicked) ||
  720. me.Flags.HasFlag (MouseFlags.Button2Clicked) ||
  721. me.Flags.HasFlag (MouseFlags.Button3Clicked);
  722. if (isClick) {
  723. _host.OnTabClicked (new TabMouseEventArgs (hit, me));
  724. // user canceled click
  725. if (me.Handled) {
  726. return true;
  727. }
  728. }
  729. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) &&
  730. !me.Flags.HasFlag (MouseFlags.Button1DoubleClicked) &&
  731. !me.Flags.HasFlag (MouseFlags.Button1TripleClicked))
  732. return false;
  733. if (!HasFocus && CanFocus) {
  734. SetFocus ();
  735. }
  736. if (me.Flags.HasFlag (MouseFlags.Button1Clicked) ||
  737. me.Flags.HasFlag (MouseFlags.Button1DoubleClicked) ||
  738. me.Flags.HasFlag (MouseFlags.Button1TripleClicked)) {
  739. int scrollIndicatorHit = 0;
  740. if (me.View != null && me.View.Id == "rightScrollIndicator") {
  741. scrollIndicatorHit = 1;
  742. } else if (me.View != null && me.View.Id == "leftScrollIndicator") {
  743. scrollIndicatorHit = -1;
  744. }
  745. if (scrollIndicatorHit != 0) {
  746. _host.SwitchTabBy (scrollIndicatorHit);
  747. SetNeedsDisplay ();
  748. return true;
  749. }
  750. if (hit != null) {
  751. _host.SelectedTab = hit;
  752. SetNeedsDisplay ();
  753. return true;
  754. }
  755. }
  756. return false;
  757. }
  758. }
  759. /// <summary>
  760. /// Raises the <see cref="TabClicked"/> event.
  761. /// </summary>
  762. /// <param name="tabMouseEventArgs"></param>
  763. protected virtual private void OnTabClicked (TabMouseEventArgs tabMouseEventArgs)
  764. {
  765. TabClicked?.Invoke (this, tabMouseEventArgs);
  766. }
  767. }