TabView.cs 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>Control that hosts multiple sub views, presenting a single one at once.</summary>
  4. public class TabView : View
  5. {
  6. /// <summary>The default <see cref="MaxTabTextWidth"/> to set on new <see cref="TabView"/> controls.</summary>
  7. public const uint DefaultMaxTabTextWidth = 30;
  8. /// <summary>
  9. /// This sub view is the main client area of the current tab. It hosts the <see cref="Tab.View"/> of the tab, the
  10. /// <see cref="SelectedTab"/>.
  11. /// </summary>
  12. private readonly View _contentView;
  13. private readonly List<Tab> _tabs = new ();
  14. /// <summary>This sub view is the 2 or 3 line control that represents the actual tabs themselves.</summary>
  15. private readonly TabRowView _tabsBar;
  16. private Tab? _selectedTab;
  17. private TabToRender []? _tabLocations;
  18. private int _tabScrollOffset;
  19. /// <summary>Initializes a <see cref="TabView"/> class.</summary>
  20. public TabView ()
  21. {
  22. CanFocus = true;
  23. TabStop = TabBehavior.TabStop; // Because TabView has focusable subviews, it must be a TabGroup
  24. _tabsBar = new TabRowView (this);
  25. _contentView = new View ()
  26. {
  27. //Id = "TabView._contentView",
  28. };
  29. ApplyStyleChanges ();
  30. base.Add (_tabsBar);
  31. base.Add (_contentView);
  32. // Things this view knows how to do
  33. AddCommand (Command.Left, () => SwitchTabBy (-1));
  34. AddCommand (Command.Right, () => SwitchTabBy (1));
  35. AddCommand (
  36. Command.LeftStart,
  37. () =>
  38. {
  39. TabScrollOffset = 0;
  40. SelectedTab = Tabs.FirstOrDefault ()!;
  41. return true;
  42. }
  43. );
  44. AddCommand (
  45. Command.RightEnd,
  46. () =>
  47. {
  48. TabScrollOffset = Tabs.Count - 1;
  49. SelectedTab = Tabs.LastOrDefault ()!;
  50. return true;
  51. }
  52. );
  53. AddCommand (
  54. Command.PageDown,
  55. () =>
  56. {
  57. TabScrollOffset += _tabLocations!.Length;
  58. SelectedTab = Tabs.ElementAt (TabScrollOffset);
  59. return true;
  60. }
  61. );
  62. AddCommand (
  63. Command.PageUp,
  64. () =>
  65. {
  66. TabScrollOffset -= _tabLocations!.Length;
  67. SelectedTab = Tabs.ElementAt (TabScrollOffset);
  68. return true;
  69. }
  70. );
  71. // Default keybindings for this view
  72. KeyBindings.Add (Key.CursorLeft, Command.Left);
  73. KeyBindings.Add (Key.CursorRight, Command.Right);
  74. KeyBindings.Add (Key.Home, Command.LeftStart);
  75. KeyBindings.Add (Key.End, Command.RightEnd);
  76. KeyBindings.Add (Key.PageDown, Command.PageDown);
  77. KeyBindings.Add (Key.PageUp, Command.PageUp);
  78. }
  79. /// <summary>
  80. /// The maximum number of characters to render in a Tab header. This prevents one long tab from pushing out all
  81. /// the others.
  82. /// </summary>
  83. public uint MaxTabTextWidth { get; set; } = DefaultMaxTabTextWidth;
  84. // This is needed to hold initial value because it may change during the setter process
  85. private bool _selectedTabHasFocus;
  86. /// <summary>The currently selected member of <see cref="Tabs"/> chosen by the user.</summary>
  87. /// <value></value>
  88. public Tab? SelectedTab
  89. {
  90. get => _selectedTab;
  91. set
  92. {
  93. Tab? old = _selectedTab;
  94. _selectedTabHasFocus = old?.HasFocus == true || !_contentView.CanFocus;
  95. if (_selectedTab is { })
  96. {
  97. if (_selectedTab.View is { })
  98. {
  99. _selectedTab.View.CanFocusChanged -= ContentViewCanFocus!;
  100. // remove old content
  101. _contentView.Remove (_selectedTab.View);
  102. }
  103. }
  104. _selectedTab = value;
  105. // add new content
  106. if (_selectedTab?.View != null)
  107. {
  108. _selectedTab.View.CanFocusChanged += ContentViewCanFocus!;
  109. _contentView.Add (_selectedTab.View);
  110. // _contentView.Id = $"_contentView for {_selectedTab.DisplayText}";
  111. }
  112. ContentViewCanFocus (null!, null!);
  113. EnsureSelectedTabIsVisible ();
  114. if (old != _selectedTab)
  115. {
  116. if (_selectedTabHasFocus)
  117. {
  118. SelectedTab?.SetFocus ();
  119. }
  120. OnSelectedTabChanged (old!, _selectedTab!);
  121. }
  122. SetNeedsLayout ();
  123. }
  124. }
  125. private void ContentViewCanFocus (object sender, EventArgs eventArgs)
  126. {
  127. _contentView.CanFocus = _contentView.Subviews.Count (v => v.CanFocus) > 0;
  128. }
  129. private TabStyle _style = new ();
  130. /// <summary>Render choices for how to display tabs. After making changes, call <see cref="ApplyStyleChanges()"/>.</summary>
  131. /// <value></value>
  132. public TabStyle Style
  133. {
  134. get => _style;
  135. set
  136. {
  137. if (_style == value)
  138. {
  139. return;
  140. }
  141. _style = value;
  142. SetNeedsLayout ();
  143. }
  144. }
  145. /// <summary>All tabs currently hosted by the control.</summary>
  146. /// <value></value>
  147. public IReadOnlyCollection<Tab> Tabs => _tabs.AsReadOnly ();
  148. /// <summary>When there are too many tabs to render, this indicates the first tab to render on the screen.</summary>
  149. /// <value></value>
  150. public int TabScrollOffset
  151. {
  152. get => _tabScrollOffset;
  153. set
  154. {
  155. _tabScrollOffset = EnsureValidScrollOffsets (value);
  156. SetNeedsLayout ();
  157. }
  158. }
  159. /// <summary>Adds the given <paramref name="tab"/> to <see cref="Tabs"/>.</summary>
  160. /// <param name="tab"></param>
  161. /// <param name="andSelect">True to make the newly added Tab the <see cref="SelectedTab"/>.</param>
  162. public void AddTab (Tab tab, bool andSelect)
  163. {
  164. if (_tabs.Contains (tab))
  165. {
  166. return;
  167. }
  168. _tabs.Add (tab);
  169. _tabsBar.Add (tab);
  170. if (SelectedTab is null || andSelect)
  171. {
  172. SelectedTab = tab;
  173. EnsureSelectedTabIsVisible ();
  174. tab.View?.SetFocus ();
  175. }
  176. SetNeedsLayout ();
  177. }
  178. /// <summary>
  179. /// Updates the control to use the latest state settings in <see cref="Style"/>. This can change the size of the
  180. /// client area of the tab (for rendering the selected tab's content). This method includes a call to
  181. /// <see cref="View.SetNeedsDraw()"/>.
  182. /// </summary>
  183. public void ApplyStyleChanges ()
  184. {
  185. _contentView.BorderStyle = Style.ShowBorder ? LineStyle.Single : LineStyle.None;
  186. _contentView.Width = Dim.Fill ();
  187. if (Style.TabsOnBottom)
  188. {
  189. // Tabs are along the bottom so just dodge the border
  190. if (Style.ShowBorder)
  191. {
  192. _contentView.Border.Thickness = new Thickness (1, 1, 1, 0);
  193. }
  194. _contentView.Y = 0;
  195. int tabHeight = GetTabHeight (false);
  196. // Fill client area leaving space at bottom for tabs
  197. _contentView.Height = Dim.Fill (tabHeight);
  198. _tabsBar.Height = tabHeight;
  199. _tabsBar.Y = Pos.Bottom (_contentView);
  200. }
  201. else
  202. {
  203. // Tabs are along the top
  204. if (Style.ShowBorder)
  205. {
  206. _contentView.Border.Thickness = new Thickness (1, 0, 1, 1);
  207. }
  208. _tabsBar.Y = 0;
  209. int tabHeight = GetTabHeight (true);
  210. //move content down to make space for tabs
  211. _contentView.Y = Pos.Bottom (_tabsBar);
  212. // Fill client area leaving space at bottom for border
  213. _contentView.Height = Dim.Fill ();
  214. // The top tab should be 2 or 3 rows high and on the top
  215. _tabsBar.Height = tabHeight;
  216. // Should be able to just use 0 but switching between top/bottom tabs repeatedly breaks in ValidatePosDim if just using the absolute value 0
  217. }
  218. SetNeedsLayout ();
  219. }
  220. /// <inheritdoc />
  221. protected override void OnViewportChanged (DrawEventArgs e)
  222. {
  223. _tabLocations = CalculateViewport (Viewport).ToArray ();
  224. base.OnViewportChanged (e);
  225. }
  226. /// <summary>Updates <see cref="TabScrollOffset"/> to ensure that <see cref="SelectedTab"/> is visible.</summary>
  227. public void EnsureSelectedTabIsVisible ()
  228. {
  229. if (!IsInitialized || SelectedTab is null)
  230. {
  231. return;
  232. }
  233. // if current viewport does not include the selected tab
  234. if (!CalculateViewport (Viewport).Any (r => Equals (SelectedTab, r.Tab)))
  235. {
  236. // Set scroll offset so the first tab rendered is the
  237. TabScrollOffset = Math.Max (0, Tabs.IndexOf (SelectedTab));
  238. }
  239. }
  240. /// <summary>Updates <see cref="TabScrollOffset"/> to be a valid index of <see cref="Tabs"/>.</summary>
  241. /// <param name="value">The value to validate.</param>
  242. /// <remarks>Changes will not be immediately visible in the display until you call <see cref="View.SetNeedsDraw()"/>.</remarks>
  243. /// <returns>The valid <see cref="TabScrollOffset"/> for the given value.</returns>
  244. public int EnsureValidScrollOffsets (int value) { return Math.Max (Math.Min (value, Tabs.Count - 1), 0); }
  245. /// <inheritdoc />
  246. protected override void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? focusedView)
  247. {
  248. if (SelectedTab is { HasFocus: false } && !_contentView.CanFocus && focusedView == this)
  249. {
  250. SelectedTab?.SetFocus ();
  251. return;
  252. }
  253. base.OnHasFocusChanged (newHasFocus, previousFocusedView, focusedView);
  254. }
  255. /// <summary>
  256. /// Removes the given <paramref name="tab"/> from <see cref="Tabs"/>. Caller is responsible for disposing the
  257. /// tab's hosted <see cref="Tab.View"/> if appropriate.
  258. /// </summary>
  259. /// <param name="tab"></param>
  260. public void RemoveTab (Tab? tab)
  261. {
  262. if (tab is null || !_tabs.Contains (tab))
  263. {
  264. return;
  265. }
  266. // what tab was selected before closing
  267. int idx = _tabs.IndexOf (tab);
  268. _tabs.Remove (tab);
  269. // if the currently selected tab is no longer a member of Tabs
  270. if (SelectedTab is null || !Tabs.Contains (SelectedTab))
  271. {
  272. // select the tab closest to the one that disappeared
  273. int toSelect = Math.Max (idx - 1, 0);
  274. if (toSelect < Tabs.Count)
  275. {
  276. SelectedTab = Tabs.ElementAt (toSelect);
  277. }
  278. else
  279. {
  280. SelectedTab = Tabs.LastOrDefault ();
  281. }
  282. }
  283. EnsureSelectedTabIsVisible ();
  284. SetNeedsLayout ();
  285. }
  286. /// <summary>Event for when <see cref="SelectedTab"/> changes.</summary>
  287. public event EventHandler<TabChangedEventArgs>? SelectedTabChanged;
  288. /// <summary>
  289. /// Changes the <see cref="SelectedTab"/> by the given <paramref name="amount"/>. Positive for right, negative for
  290. /// left. If no tab is currently selected then the first tab will become selected.
  291. /// </summary>
  292. /// <param name="amount"></param>
  293. public bool SwitchTabBy (int amount)
  294. {
  295. if (Tabs.Count == 0)
  296. {
  297. return false;
  298. }
  299. // if there is only one tab anyway or nothing is selected
  300. if (Tabs.Count == 1 || SelectedTab is null)
  301. {
  302. SelectedTab = Tabs.ElementAt (0);
  303. return SelectedTab is { };
  304. }
  305. int currentIdx = Tabs.IndexOf (SelectedTab);
  306. // Currently selected tab has vanished!
  307. if (currentIdx == -1)
  308. {
  309. SelectedTab = Tabs.ElementAt (0);
  310. return true;
  311. }
  312. int newIdx = Math.Max (0, Math.Min (currentIdx + amount, Tabs.Count - 1));
  313. if (newIdx == currentIdx)
  314. {
  315. return false;
  316. }
  317. SelectedTab = _tabs [newIdx];
  318. EnsureSelectedTabIsVisible ();
  319. return true;
  320. }
  321. /// <summary>
  322. /// Event fired when a <see cref="Tab"/> is clicked. Can be used to cancel navigation, show context menu (e.g. on
  323. /// right click) etc.
  324. /// </summary>
  325. public event EventHandler<TabMouseEventArgs>? TabClicked;
  326. /// <summary>Disposes the control and all <see cref="Tabs"/>.</summary>
  327. /// <param name="disposing"></param>
  328. protected override void Dispose (bool disposing)
  329. {
  330. base.Dispose (disposing);
  331. // The selected tab will automatically be disposed but
  332. // any tabs not visible will need to be manually disposed
  333. foreach (Tab tab in Tabs)
  334. {
  335. if (!Equals (SelectedTab, tab))
  336. {
  337. tab.View?.Dispose ();
  338. }
  339. }
  340. }
  341. /// <summary>Raises the <see cref="SelectedTabChanged"/> event.</summary>
  342. protected virtual void OnSelectedTabChanged (Tab oldTab, Tab newTab)
  343. {
  344. SelectedTabChanged?.Invoke (this, new TabChangedEventArgs (oldTab, newTab));
  345. }
  346. /// <summary>Returns which tabs to render at each x location.</summary>
  347. /// <returns></returns>
  348. private IEnumerable<TabToRender> CalculateViewport (Rectangle bounds)
  349. {
  350. UnSetCurrentTabs ();
  351. var i = 1;
  352. View? prevTab = null;
  353. // Starting at the first or scrolled to tab
  354. foreach (Tab tab in Tabs.Skip (TabScrollOffset))
  355. {
  356. if (prevTab is { })
  357. {
  358. tab.X = Pos.Right (prevTab) - 1;
  359. }
  360. else
  361. {
  362. tab.X = 0;
  363. }
  364. tab.Y = 0;
  365. // while there is space for the tab
  366. int tabTextWidth = tab.DisplayText.EnumerateRunes ().Sum (c => c.GetColumns ());
  367. string text = tab.DisplayText;
  368. // The maximum number of characters to use for the tab name as specified
  369. // by the user (MaxTabTextWidth). But not more than the width of the view
  370. // or we won't even be able to render a single tab!
  371. long maxWidth = Math.Max (0, Math.Min (bounds.Width - 3, MaxTabTextWidth));
  372. tab.Width = 2;
  373. tab.Height = Style.ShowTopLine ? 3 : 2;
  374. // if tab view is width <= 3 don't render any tabs
  375. if (maxWidth == 0)
  376. {
  377. tab.Visible = true;
  378. tab.MouseClick += Tab_MouseClick!;
  379. yield return new (tab, string.Empty, Equals (SelectedTab, tab));
  380. break;
  381. }
  382. if (tabTextWidth > maxWidth)
  383. {
  384. text = tab.Text = tab.DisplayText.Substring (0, (int)maxWidth);
  385. tabTextWidth = (int)maxWidth;
  386. }
  387. else
  388. {
  389. tab.Text = text;
  390. }
  391. tab.Width = Math.Max (tabTextWidth + 2, 1);
  392. tab.Height = Style.ShowTopLine ? 3 : 2;
  393. // if there is not enough space for this tab
  394. if (i + tabTextWidth >= bounds.Width)
  395. {
  396. tab.Visible = false;
  397. break;
  398. }
  399. // there is enough space!
  400. tab.Visible = true;
  401. tab.MouseClick += Tab_MouseClick!;
  402. tab.Border!.MouseClick += Tab_MouseClick!;
  403. yield return new (tab, text, Equals (SelectedTab, tab));
  404. prevTab = tab;
  405. i += tabTextWidth + 1;
  406. }
  407. if (_selectedTabHasFocus)
  408. {
  409. SelectedTab?.SetFocus ();
  410. }
  411. }
  412. /// <summary>
  413. /// Returns the number of rows occupied by rendering the tabs, this depends on <see cref="TabStyle.ShowTopLine"/>
  414. /// and can be 0 (e.g. if <see cref="TabStyle.TabsOnBottom"/> and you ask for <paramref name="top"/>).
  415. /// </summary>
  416. /// <param name="top">True to measure the space required at the top of the control, false to measure space at the bottom.</param>
  417. /// .
  418. /// <returns></returns>
  419. private int GetTabHeight (bool top)
  420. {
  421. if (top && Style.TabsOnBottom)
  422. {
  423. return 0;
  424. }
  425. if (!top && !Style.TabsOnBottom)
  426. {
  427. return 0;
  428. }
  429. return Style.ShowTopLine ? 3 : 2;
  430. }
  431. private void Tab_MouseClick (object sender, MouseEventArgs e)
  432. {
  433. e.Handled = _tabsBar.NewMouseEvent (e) == true;
  434. }
  435. private void UnSetCurrentTabs ()
  436. {
  437. if (_tabLocations is null)
  438. {
  439. // Ensures unset any visible tab prior to TabScrollOffset
  440. for (int i = 0; i < TabScrollOffset; i++)
  441. {
  442. Tab tab = Tabs.ElementAt (i);
  443. if (tab.Visible)
  444. {
  445. tab.MouseClick -= Tab_MouseClick!;
  446. tab.Visible = false;
  447. }
  448. }
  449. }
  450. else if (_tabLocations is { })
  451. {
  452. foreach (TabToRender tabToRender in _tabLocations)
  453. {
  454. tabToRender.Tab.MouseClick -= Tab_MouseClick!;
  455. tabToRender.Tab.Visible = false;
  456. }
  457. _tabLocations = null;
  458. }
  459. }
  460. /// <summary>Raises the <see cref="TabClicked"/> event.</summary>
  461. /// <param name="tabMouseEventArgs"></param>
  462. private protected virtual void OnTabClicked (TabMouseEventArgs tabMouseEventArgs) { TabClicked?.Invoke (this, tabMouseEventArgs); }
  463. private class TabRowView : View
  464. {
  465. private readonly TabView _host;
  466. private readonly View _leftScrollIndicator;
  467. private readonly View _rightScrollIndicator;
  468. public TabRowView (TabView host)
  469. {
  470. _host = host;
  471. Id = "tabRowView";
  472. CanFocus = true;
  473. Width = Dim.Fill ();
  474. _rightScrollIndicator = new View
  475. {
  476. Id = "rightScrollIndicator",
  477. Width = 1,
  478. Height = 1,
  479. Visible = false,
  480. Text = Glyphs.RightArrow.ToString ()
  481. };
  482. _rightScrollIndicator.MouseClick += _host.Tab_MouseClick!;
  483. _leftScrollIndicator = new View
  484. {
  485. Id = "leftScrollIndicator",
  486. Width = 1,
  487. Height = 1,
  488. Visible = false,
  489. Text = Glyphs.LeftArrow.ToString ()
  490. };
  491. _leftScrollIndicator.MouseClick += _host.Tab_MouseClick!;
  492. Add (_rightScrollIndicator, _leftScrollIndicator);
  493. }
  494. protected override bool OnMouseEvent (MouseEventArgs me)
  495. {
  496. View? parent = me.View is Adornment adornment ? adornment.Parent : me.View;
  497. Tab? hit = parent as Tab;
  498. if (me.IsSingleClicked)
  499. {
  500. _host.OnTabClicked (new TabMouseEventArgs (hit, me));
  501. // user canceled click
  502. if (me.Handled)
  503. {
  504. return true;
  505. }
  506. if (parent == _host.SelectedTab)
  507. {
  508. _host.SelectedTab?.SetFocus ();
  509. }
  510. }
  511. if (!me.IsSingleDoubleOrTripleClicked)
  512. {
  513. return false;
  514. }
  515. if (!HasFocus && CanFocus)
  516. {
  517. SetFocus ();
  518. }
  519. if (me.IsSingleDoubleOrTripleClicked)
  520. {
  521. var scrollIndicatorHit = 0;
  522. if (me.View is { Id: "rightScrollIndicator" })
  523. {
  524. scrollIndicatorHit = 1;
  525. }
  526. else if (me.View is { Id: "leftScrollIndicator" })
  527. {
  528. scrollIndicatorHit = -1;
  529. }
  530. if (scrollIndicatorHit != 0)
  531. {
  532. _host.SwitchTabBy (scrollIndicatorHit);
  533. SetNeedsLayout ();
  534. return true;
  535. }
  536. if (hit is { })
  537. {
  538. _host.SelectedTab = hit;
  539. SetNeedsLayout ();
  540. return true;
  541. }
  542. }
  543. return false;
  544. }
  545. /// <inheritdoc />
  546. protected override void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? focusedView)
  547. {
  548. if (_host.SelectedTab is { HasFocus: false, CanFocus: true } && focusedView == this)
  549. {
  550. _host.SelectedTab?.SetFocus ();
  551. return;
  552. }
  553. base.OnHasFocusChanged (newHasFocus, previousFocusedView, focusedView);
  554. }
  555. /// <inheritdoc />
  556. protected override void OnSubviewLayout (LayoutEventArgs args)
  557. {
  558. _host._tabLocations = _host.CalculateViewport (Viewport).ToArray ();
  559. RenderTabLine ();
  560. RenderUnderline ();
  561. base.OnSubviewLayout (args);
  562. }
  563. /// <inheritdoc />
  564. protected override bool OnRenderingLineCanvas ()
  565. {
  566. RenderTabLineCanvas ();
  567. return false;
  568. }
  569. private void RenderTabLineCanvas ()
  570. {
  571. if (_host._tabLocations is null)
  572. {
  573. return;
  574. }
  575. TabToRender [] tabLocations = _host._tabLocations;
  576. int selectedTab = -1;
  577. var lc = new LineCanvas ();
  578. for (var i = 0; i < tabLocations.Length; i++)
  579. {
  580. View tab = tabLocations [i].Tab;
  581. Rectangle vts = tab.ViewportToScreen (tab.Viewport);
  582. int selectedOffset = _host.Style.ShowTopLine && tabLocations [i].IsSelected ? 0 : 1;
  583. if (tabLocations [i].IsSelected)
  584. {
  585. selectedTab = i;
  586. if (i == 0 && _host.TabScrollOffset == 0)
  587. {
  588. if (_host.Style.TabsOnBottom)
  589. {
  590. // Upper left vertical line
  591. lc.AddLine (
  592. new Point (vts.X - 1, vts.Y - 1),
  593. -1,
  594. Orientation.Vertical,
  595. tab.BorderStyle
  596. );
  597. }
  598. else
  599. {
  600. // Lower left vertical line
  601. lc.AddLine (
  602. new Point (vts.X - 1, vts.Bottom - selectedOffset),
  603. -1,
  604. Orientation.Vertical,
  605. tab.BorderStyle
  606. );
  607. }
  608. }
  609. else if (i > 0 && i <= tabLocations.Length - 1)
  610. {
  611. if (_host.Style.TabsOnBottom)
  612. {
  613. // URCorner
  614. lc.AddLine (
  615. new Point (vts.X - 1, vts.Y - 1),
  616. 1,
  617. Orientation.Vertical,
  618. tab.BorderStyle
  619. );
  620. lc.AddLine (
  621. new Point (vts.X - 1, vts.Y - 1),
  622. -1,
  623. Orientation.Horizontal,
  624. tab.BorderStyle
  625. );
  626. }
  627. else
  628. {
  629. // LRCorner
  630. lc.AddLine (
  631. new Point (vts.X - 1, vts.Bottom - selectedOffset),
  632. -1,
  633. Orientation.Vertical,
  634. tab.BorderStyle
  635. );
  636. lc.AddLine (
  637. new Point (vts.X - 1, vts.Bottom - selectedOffset),
  638. -1,
  639. Orientation.Horizontal,
  640. tab.BorderStyle
  641. );
  642. }
  643. if (_host.Style.ShowTopLine)
  644. {
  645. if (_host.Style.TabsOnBottom)
  646. {
  647. // Lower left tee
  648. lc.AddLine (
  649. new Point (vts.X - 1, vts.Bottom),
  650. -1,
  651. Orientation.Vertical,
  652. tab.BorderStyle
  653. );
  654. lc.AddLine (
  655. new Point (vts.X - 1, vts.Bottom),
  656. 0,
  657. Orientation.Horizontal,
  658. tab.BorderStyle
  659. );
  660. }
  661. else
  662. {
  663. // Upper left tee
  664. lc.AddLine (
  665. new Point (vts.X - 1, vts.Y - 1),
  666. 1,
  667. Orientation.Vertical,
  668. tab.BorderStyle
  669. );
  670. lc.AddLine (
  671. new Point (vts.X - 1, vts.Y - 1),
  672. 0,
  673. Orientation.Horizontal,
  674. tab.BorderStyle
  675. );
  676. }
  677. }
  678. }
  679. if (i < tabLocations.Length - 1)
  680. {
  681. if (_host.Style.ShowTopLine)
  682. {
  683. if (_host.Style.TabsOnBottom)
  684. {
  685. // Lower right tee
  686. lc.AddLine (
  687. new Point (vts.Right, vts.Bottom),
  688. -1,
  689. Orientation.Vertical,
  690. tab.BorderStyle
  691. );
  692. lc.AddLine (
  693. new Point (vts.Right, vts.Bottom),
  694. 0,
  695. Orientation.Horizontal,
  696. tab.BorderStyle
  697. );
  698. }
  699. else
  700. {
  701. // Upper right tee
  702. lc.AddLine (
  703. new Point (vts.Right, vts.Y - 1),
  704. 1,
  705. Orientation.Vertical,
  706. tab.BorderStyle
  707. );
  708. lc.AddLine (
  709. new Point (vts.Right, vts.Y - 1),
  710. 0,
  711. Orientation.Horizontal,
  712. tab.BorderStyle
  713. );
  714. }
  715. }
  716. }
  717. if (_host.Style.TabsOnBottom)
  718. {
  719. //URCorner
  720. lc.AddLine (
  721. new Point (vts.Right, vts.Y - 1),
  722. 1,
  723. Orientation.Vertical,
  724. tab.BorderStyle
  725. );
  726. lc.AddLine (
  727. new Point (vts.Right, vts.Y - 1),
  728. 1,
  729. Orientation.Horizontal,
  730. tab.BorderStyle
  731. );
  732. }
  733. else
  734. {
  735. //LLCorner
  736. lc.AddLine (
  737. new Point (vts.Right, vts.Bottom - selectedOffset),
  738. -1,
  739. Orientation.Vertical,
  740. tab.BorderStyle
  741. );
  742. lc.AddLine (
  743. new Point (vts.Right, vts.Bottom - selectedOffset),
  744. 1,
  745. Orientation.Horizontal,
  746. tab.BorderStyle
  747. );
  748. }
  749. }
  750. else if (selectedTab == -1)
  751. {
  752. if (i == 0 && string.IsNullOrEmpty (tab.Text))
  753. {
  754. if (_host.Style.TabsOnBottom)
  755. {
  756. if (_host.Style.ShowTopLine)
  757. {
  758. // LLCorner
  759. lc.AddLine (
  760. new Point (vts.X - 1, vts.Bottom),
  761. -1,
  762. Orientation.Vertical,
  763. tab.BorderStyle
  764. );
  765. lc.AddLine (
  766. new Point (vts.X - 1, vts.Bottom),
  767. 1,
  768. Orientation.Horizontal,
  769. tab.BorderStyle
  770. );
  771. }
  772. // ULCorner
  773. lc.AddLine (
  774. new Point (vts.X - 1, vts.Y - 1),
  775. 1,
  776. Orientation.Vertical,
  777. tab.BorderStyle
  778. );
  779. lc.AddLine (
  780. new Point (vts.X - 1, vts.Y - 1),
  781. 1,
  782. Orientation.Horizontal,
  783. tab.BorderStyle
  784. );
  785. }
  786. else
  787. {
  788. if (_host.Style.ShowTopLine)
  789. {
  790. // ULCorner
  791. lc.AddLine (
  792. new Point (vts.X - 1, vts.Y - 1),
  793. 1,
  794. Orientation.Vertical,
  795. tab.BorderStyle
  796. );
  797. lc.AddLine (
  798. new Point (vts.X - 1, vts.Y - 1),
  799. 1,
  800. Orientation.Horizontal,
  801. tab.BorderStyle
  802. );
  803. }
  804. // LLCorner
  805. lc.AddLine (
  806. new Point (vts.X - 1, vts.Bottom),
  807. -1,
  808. Orientation.Vertical,
  809. tab.BorderStyle
  810. );
  811. lc.AddLine (
  812. new Point (vts.X - 1, vts.Bottom),
  813. 1,
  814. Orientation.Horizontal,
  815. tab.BorderStyle
  816. );
  817. }
  818. }
  819. else if (i > 0)
  820. {
  821. if (_host.Style.ShowTopLine || _host.Style.TabsOnBottom)
  822. {
  823. // Upper left tee
  824. lc.AddLine (
  825. new Point (vts.X - 1, vts.Y - 1),
  826. 1,
  827. Orientation.Vertical,
  828. tab.BorderStyle
  829. );
  830. lc.AddLine (
  831. new Point (vts.X - 1, vts.Y - 1),
  832. 0,
  833. Orientation.Horizontal,
  834. tab.BorderStyle
  835. );
  836. }
  837. // Lower left tee
  838. lc.AddLine (
  839. new Point (vts.X - 1, vts.Bottom),
  840. -1,
  841. Orientation.Vertical,
  842. tab.BorderStyle
  843. );
  844. lc.AddLine (
  845. new Point (vts.X - 1, vts.Bottom),
  846. 0,
  847. Orientation.Horizontal,
  848. tab.BorderStyle
  849. );
  850. }
  851. }
  852. else if (i < tabLocations.Length - 1)
  853. {
  854. if (_host.Style.ShowTopLine)
  855. {
  856. // Upper right tee
  857. lc.AddLine (
  858. new Point (vts.Right, vts.Y - 1),
  859. 1,
  860. Orientation.Vertical,
  861. tab.BorderStyle
  862. );
  863. lc.AddLine (
  864. new Point (vts.Right, vts.Y - 1),
  865. 0,
  866. Orientation.Horizontal,
  867. tab.BorderStyle
  868. );
  869. }
  870. if (_host.Style.ShowTopLine || !_host.Style.TabsOnBottom)
  871. {
  872. // Lower right tee
  873. lc.AddLine (
  874. new Point (vts.Right, vts.Bottom),
  875. -1,
  876. Orientation.Vertical,
  877. tab.BorderStyle
  878. );
  879. lc.AddLine (
  880. new Point (vts.Right, vts.Bottom),
  881. 0,
  882. Orientation.Horizontal,
  883. tab.BorderStyle
  884. );
  885. }
  886. else
  887. {
  888. // Upper right tee
  889. lc.AddLine (
  890. new Point (vts.Right, vts.Y - 1),
  891. 1,
  892. Orientation.Vertical,
  893. tab.BorderStyle
  894. );
  895. lc.AddLine (
  896. new Point (vts.Right, vts.Y - 1),
  897. 0,
  898. Orientation.Horizontal,
  899. tab.BorderStyle
  900. );
  901. }
  902. }
  903. if (i == 0 && i != selectedTab && _host is { TabScrollOffset: 0, Style.ShowBorder: true })
  904. {
  905. if (_host.Style.TabsOnBottom)
  906. {
  907. // Upper left vertical line
  908. lc.AddLine (
  909. new Point (vts.X - 1, vts.Y - 1),
  910. 0,
  911. Orientation.Vertical,
  912. tab.BorderStyle
  913. );
  914. lc.AddLine (
  915. new Point (vts.X - 1, vts.Y - 1),
  916. 1,
  917. Orientation.Horizontal,
  918. tab.BorderStyle
  919. );
  920. }
  921. else
  922. {
  923. // Lower left vertical line
  924. lc.AddLine (
  925. new Point (vts.X - 1, vts.Bottom),
  926. 0,
  927. Orientation.Vertical,
  928. tab.BorderStyle
  929. );
  930. lc.AddLine (
  931. new Point (vts.X - 1, vts.Bottom),
  932. 1,
  933. Orientation.Horizontal,
  934. tab.BorderStyle
  935. );
  936. }
  937. }
  938. if (i == tabLocations.Length - 1 && i != selectedTab)
  939. {
  940. if (_host.Style.TabsOnBottom)
  941. {
  942. // Upper right tee
  943. lc.AddLine (
  944. new Point (vts.Right, vts.Y - 1),
  945. 1,
  946. Orientation.Vertical,
  947. tab.BorderStyle
  948. );
  949. lc.AddLine (
  950. new Point (vts.Right, vts.Y - 1),
  951. 0,
  952. Orientation.Horizontal,
  953. tab.BorderStyle
  954. );
  955. }
  956. else
  957. {
  958. // Lower right tee
  959. lc.AddLine (
  960. new Point (vts.Right, vts.Bottom),
  961. -1,
  962. Orientation.Vertical,
  963. tab.BorderStyle
  964. );
  965. lc.AddLine (
  966. new Point (vts.Right, vts.Bottom),
  967. 0,
  968. Orientation.Horizontal,
  969. tab.BorderStyle
  970. );
  971. }
  972. }
  973. if (i == tabLocations.Length - 1)
  974. {
  975. var arrowOffset = 1;
  976. int lastSelectedTab = !_host.Style.ShowTopLine && i == selectedTab ? 1 :
  977. _host.Style.TabsOnBottom ? 1 : 0;
  978. Rectangle tabsBarVts = ViewportToScreen (Viewport);
  979. int lineLength = tabsBarVts.Right - vts.Right;
  980. // Right horizontal line
  981. if (ShouldDrawRightScrollIndicator ())
  982. {
  983. if (lineLength - arrowOffset > 0)
  984. {
  985. if (_host.Style.TabsOnBottom)
  986. {
  987. lc.AddLine (
  988. new Point (vts.Right, vts.Y - lastSelectedTab),
  989. lineLength - arrowOffset,
  990. Orientation.Horizontal,
  991. tab.BorderStyle
  992. );
  993. }
  994. else
  995. {
  996. lc.AddLine (
  997. new Point (
  998. vts.Right,
  999. vts.Bottom - lastSelectedTab
  1000. ),
  1001. lineLength - arrowOffset,
  1002. Orientation.Horizontal,
  1003. tab.BorderStyle
  1004. );
  1005. }
  1006. }
  1007. }
  1008. else
  1009. {
  1010. // Right corner
  1011. if (_host.Style.TabsOnBottom)
  1012. {
  1013. lc.AddLine (
  1014. new Point (vts.Right, vts.Y - lastSelectedTab),
  1015. lineLength,
  1016. Orientation.Horizontal,
  1017. tab.BorderStyle
  1018. );
  1019. }
  1020. else
  1021. {
  1022. lc.AddLine (
  1023. new Point (vts.Right, vts.Bottom - lastSelectedTab),
  1024. lineLength,
  1025. Orientation.Horizontal,
  1026. tab.BorderStyle
  1027. );
  1028. }
  1029. if (_host.Style.ShowBorder)
  1030. {
  1031. if (_host.Style.TabsOnBottom)
  1032. {
  1033. // More LRCorner
  1034. lc.AddLine (
  1035. new Point (
  1036. tabsBarVts.Right - 1,
  1037. vts.Y - lastSelectedTab
  1038. ),
  1039. -1,
  1040. Orientation.Vertical,
  1041. tab.BorderStyle
  1042. );
  1043. }
  1044. else
  1045. {
  1046. // More URCorner
  1047. lc.AddLine (
  1048. new Point (
  1049. tabsBarVts.Right - 1,
  1050. vts.Bottom - lastSelectedTab
  1051. ),
  1052. 1,
  1053. Orientation.Vertical,
  1054. tab.BorderStyle
  1055. );
  1056. }
  1057. }
  1058. }
  1059. }
  1060. }
  1061. _host.LineCanvas.Merge (lc);
  1062. }
  1063. private int GetUnderlineYPosition ()
  1064. {
  1065. if (_host.Style.TabsOnBottom)
  1066. {
  1067. return 0;
  1068. }
  1069. return _host.Style.ShowTopLine ? 2 : 1;
  1070. }
  1071. /// <summary>Renders the line with the tab names in it.</summary>
  1072. private void RenderTabLine ()
  1073. {
  1074. if (_host._tabLocations is null)
  1075. {
  1076. return;
  1077. }
  1078. View? selected = null;
  1079. int topLine = _host.Style.ShowTopLine ? 1 : 0;
  1080. foreach (TabToRender toRender in _host._tabLocations)
  1081. {
  1082. Tab tab = toRender.Tab;
  1083. if (toRender.IsSelected)
  1084. {
  1085. selected = tab;
  1086. if (_host.Style.TabsOnBottom)
  1087. {
  1088. tab.Border!.Thickness = new (1, 0, 1, topLine);
  1089. tab.Margin!.Thickness = new (0, 1, 0, 0);
  1090. }
  1091. else
  1092. {
  1093. tab.Border!.Thickness = new (1, topLine, 1, 0);
  1094. tab.Margin!.Thickness = new (0, 0, 0, topLine);
  1095. }
  1096. }
  1097. else if (selected is null)
  1098. {
  1099. if (_host.Style.TabsOnBottom)
  1100. {
  1101. tab.Border!.Thickness = new (1, 1, 1, topLine);
  1102. tab.Margin!.Thickness = new (0, 0, 0, 0);
  1103. }
  1104. else
  1105. {
  1106. tab.Border!.Thickness = new (1, topLine, 1, 1);
  1107. tab.Margin!.Thickness = new (0, 0, 0, 0);
  1108. }
  1109. }
  1110. else
  1111. {
  1112. if (_host.Style.TabsOnBottom)
  1113. {
  1114. tab.Border!.Thickness = new (1, 1, 1, topLine);
  1115. tab.Margin!.Thickness = new (0, 0, 0, 0);
  1116. }
  1117. else
  1118. {
  1119. tab.Border!.Thickness = new (1, topLine, 1, 1);
  1120. tab.Margin!.Thickness = new (0, 0, 0, 0);
  1121. }
  1122. }
  1123. // Ensures updating TextFormatter constrains
  1124. tab.TextFormatter.ConstrainToWidth = tab.GetContentSize ().Width;
  1125. tab.TextFormatter.ConstrainToHeight = tab.GetContentSize ().Height;
  1126. }
  1127. }
  1128. /// <summary>Renders the line of the tab that adjoins the content of the tab.</summary>
  1129. private void RenderUnderline ()
  1130. {
  1131. int y = GetUnderlineYPosition ();
  1132. TabToRender? selected = _host._tabLocations?.FirstOrDefault (t => t.IsSelected);
  1133. if (selected is null)
  1134. {
  1135. return;
  1136. }
  1137. // draw scroll indicators
  1138. // if there are more tabs to the left not visible
  1139. if (_host.TabScrollOffset > 0)
  1140. {
  1141. _leftScrollIndicator.X = 0;
  1142. _leftScrollIndicator.Y = y;
  1143. // indicate that
  1144. _leftScrollIndicator.Visible = true;
  1145. // Ensures this is clicked instead of the first tab
  1146. MoveSubviewToEnd (_leftScrollIndicator);
  1147. }
  1148. else
  1149. {
  1150. _leftScrollIndicator.Visible = false;
  1151. }
  1152. // if there are more tabs to the right not visible
  1153. if (ShouldDrawRightScrollIndicator ())
  1154. {
  1155. _rightScrollIndicator.X = Viewport.Width - 1;
  1156. _rightScrollIndicator.Y = y;
  1157. // indicate that
  1158. _rightScrollIndicator.Visible = true;
  1159. // Ensures this is clicked instead of the last tab if under this
  1160. MoveSubviewToStart (_rightScrollIndicator);
  1161. }
  1162. else
  1163. {
  1164. _rightScrollIndicator.Visible = false;
  1165. }
  1166. }
  1167. private bool ShouldDrawRightScrollIndicator () { return _host._tabLocations!.LastOrDefault ()?.Tab != _host.Tabs.LastOrDefault (); }
  1168. }
  1169. private class TabToRender
  1170. {
  1171. public TabToRender (Tab tab, string textToRender, bool isSelected)
  1172. {
  1173. Tab = tab;
  1174. IsSelected = isSelected;
  1175. TextToRender = textToRender;
  1176. }
  1177. /// <summary>True if the tab that is being rendered is the selected one.</summary>
  1178. /// <value></value>
  1179. public bool IsSelected { get; }
  1180. public Tab Tab { get; }
  1181. public string TextToRender { get; }
  1182. }
  1183. }