TabView.cs 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  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 is { } && (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 || !_contentView.CanFocus)
  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. tab.Border!.MouseClick += Tab_MouseClick!;
  380. yield return new (tab, string.Empty, Equals (SelectedTab, tab));
  381. break;
  382. }
  383. if (tabTextWidth > maxWidth)
  384. {
  385. text = tab.Text = tab.DisplayText.Substring (0, (int)maxWidth);
  386. tabTextWidth = (int)maxWidth;
  387. }
  388. else
  389. {
  390. tab.Text = text;
  391. }
  392. tab.Width = Math.Max (tabTextWidth + 2, 1);
  393. tab.Height = Style.ShowTopLine ? 3 : 2;
  394. // if there is not enough space for this tab
  395. if (i + tabTextWidth >= bounds.Width)
  396. {
  397. tab.Visible = false;
  398. break;
  399. }
  400. // there is enough space!
  401. tab.Visible = true;
  402. tab.MouseClick += Tab_MouseClick!;
  403. tab.Border!.MouseClick += Tab_MouseClick!;
  404. yield return new (tab, text, Equals (SelectedTab, tab));
  405. prevTab = tab;
  406. i += tabTextWidth + 1;
  407. }
  408. if (_selectedTabHasFocus)
  409. {
  410. SelectedTab?.SetFocus ();
  411. }
  412. }
  413. /// <summary>
  414. /// Returns the number of rows occupied by rendering the tabs, this depends on <see cref="TabStyle.ShowTopLine"/>
  415. /// and can be 0 (e.g. if <see cref="TabStyle.TabsOnBottom"/> and you ask for <paramref name="top"/>).
  416. /// </summary>
  417. /// <param name="top">True to measure the space required at the top of the control, false to measure space at the bottom.</param>
  418. /// .
  419. /// <returns></returns>
  420. private int GetTabHeight (bool top)
  421. {
  422. if (top && Style.TabsOnBottom)
  423. {
  424. return 0;
  425. }
  426. if (!top && !Style.TabsOnBottom)
  427. {
  428. return 0;
  429. }
  430. return Style.ShowTopLine ? 3 : 2;
  431. }
  432. private void Tab_MouseClick (object sender, MouseEventArgs e)
  433. {
  434. e.Handled = _tabsBar.NewMouseEvent (e) == true;
  435. }
  436. private void UnSetCurrentTabs ()
  437. {
  438. if (_tabLocations is null)
  439. {
  440. // Ensures unset any visible tab prior to TabScrollOffset
  441. for (int i = 0; i < TabScrollOffset; i++)
  442. {
  443. Tab tab = Tabs.ElementAt (i);
  444. if (tab.Visible)
  445. {
  446. tab.MouseClick -= Tab_MouseClick!;
  447. tab.Border!.MouseClick -= Tab_MouseClick!;
  448. tab.Visible = false;
  449. }
  450. }
  451. }
  452. else if (_tabLocations is { })
  453. {
  454. foreach (TabToRender tabToRender in _tabLocations)
  455. {
  456. tabToRender.Tab.MouseClick -= Tab_MouseClick!;
  457. tabToRender.Tab.Border!.MouseClick -= Tab_MouseClick!;
  458. tabToRender.Tab.Visible = false;
  459. }
  460. _tabLocations = null;
  461. }
  462. }
  463. /// <summary>Raises the <see cref="TabClicked"/> event.</summary>
  464. /// <param name="tabMouseEventArgs"></param>
  465. private protected virtual void OnTabClicked (TabMouseEventArgs tabMouseEventArgs) { TabClicked?.Invoke (this, tabMouseEventArgs); }
  466. private class TabRowView : View
  467. {
  468. private readonly TabView _host;
  469. private readonly View _leftScrollIndicator;
  470. private readonly View _rightScrollIndicator;
  471. public TabRowView (TabView host)
  472. {
  473. _host = host;
  474. Id = "tabRowView";
  475. CanFocus = true;
  476. Width = Dim.Fill ();
  477. _rightScrollIndicator = new View
  478. {
  479. Id = "rightScrollIndicator",
  480. Width = 1,
  481. Height = 1,
  482. Visible = false,
  483. Text = Glyphs.RightArrow.ToString ()
  484. };
  485. _rightScrollIndicator.MouseClick += _host.Tab_MouseClick!;
  486. _leftScrollIndicator = new View
  487. {
  488. Id = "leftScrollIndicator",
  489. Width = 1,
  490. Height = 1,
  491. Visible = false,
  492. Text = Glyphs.LeftArrow.ToString ()
  493. };
  494. _leftScrollIndicator.MouseClick += _host.Tab_MouseClick!;
  495. Add (_rightScrollIndicator, _leftScrollIndicator);
  496. }
  497. protected override bool OnMouseEvent (MouseEventArgs me)
  498. {
  499. View? parent = me.View is Adornment adornment ? adornment.Parent : me.View;
  500. Tab? hit = parent as Tab;
  501. if (me.IsSingleClicked)
  502. {
  503. _host.OnTabClicked (new TabMouseEventArgs (hit, me));
  504. // user canceled click
  505. if (me.Handled)
  506. {
  507. return true;
  508. }
  509. if (parent == _host.SelectedTab)
  510. {
  511. _host.SelectedTab?.SetFocus ();
  512. }
  513. }
  514. if (!me.IsSingleDoubleOrTripleClicked)
  515. {
  516. return false;
  517. }
  518. if (!HasFocus && CanFocus)
  519. {
  520. SetFocus ();
  521. }
  522. if (me.IsSingleDoubleOrTripleClicked)
  523. {
  524. var scrollIndicatorHit = 0;
  525. if (me.View is { Id: "rightScrollIndicator" })
  526. {
  527. scrollIndicatorHit = 1;
  528. }
  529. else if (me.View is { Id: "leftScrollIndicator" })
  530. {
  531. scrollIndicatorHit = -1;
  532. }
  533. if (scrollIndicatorHit != 0)
  534. {
  535. _host.SwitchTabBy (scrollIndicatorHit);
  536. SetNeedsLayout ();
  537. return true;
  538. }
  539. if (hit is { })
  540. {
  541. _host.SelectedTab = hit;
  542. SetNeedsLayout ();
  543. return true;
  544. }
  545. }
  546. return false;
  547. }
  548. /// <inheritdoc />
  549. protected override void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? focusedView)
  550. {
  551. if (_host.SelectedTab is { HasFocus: false, CanFocus: true } && focusedView == this)
  552. {
  553. _host.SelectedTab?.SetFocus ();
  554. return;
  555. }
  556. base.OnHasFocusChanged (newHasFocus, previousFocusedView, focusedView);
  557. }
  558. /// <inheritdoc />
  559. protected override void OnSubviewLayout (LayoutEventArgs args)
  560. {
  561. _host._tabLocations = _host.CalculateViewport (Viewport).ToArray ();
  562. RenderTabLine ();
  563. RenderUnderline ();
  564. base.OnSubviewLayout (args);
  565. }
  566. /// <inheritdoc />
  567. protected override bool OnRenderingLineCanvas ()
  568. {
  569. RenderTabLineCanvas ();
  570. return false;
  571. }
  572. private void RenderTabLineCanvas ()
  573. {
  574. if (_host._tabLocations is null)
  575. {
  576. return;
  577. }
  578. TabToRender [] tabLocations = _host._tabLocations;
  579. int selectedTab = -1;
  580. var lc = new LineCanvas ();
  581. for (var i = 0; i < tabLocations.Length; i++)
  582. {
  583. View tab = tabLocations [i].Tab;
  584. Rectangle vts = tab.ViewportToScreen (tab.Viewport);
  585. int selectedOffset = _host.Style.ShowTopLine && tabLocations [i].IsSelected ? 0 : 1;
  586. if (tabLocations [i].IsSelected)
  587. {
  588. selectedTab = i;
  589. if (i == 0 && _host.TabScrollOffset == 0)
  590. {
  591. if (_host.Style.TabsOnBottom)
  592. {
  593. // Upper left vertical line
  594. lc.AddLine (
  595. new Point (vts.X - 1, vts.Y - 1),
  596. -1,
  597. Orientation.Vertical,
  598. tab.BorderStyle
  599. );
  600. }
  601. else
  602. {
  603. // Lower left vertical line
  604. lc.AddLine (
  605. new Point (vts.X - 1, vts.Bottom - selectedOffset),
  606. -1,
  607. Orientation.Vertical,
  608. tab.BorderStyle
  609. );
  610. }
  611. }
  612. else if (i > 0 && i <= tabLocations.Length - 1)
  613. {
  614. if (_host.Style.TabsOnBottom)
  615. {
  616. // URCorner
  617. lc.AddLine (
  618. new Point (vts.X - 1, vts.Y - 1),
  619. 1,
  620. Orientation.Vertical,
  621. tab.BorderStyle
  622. );
  623. lc.AddLine (
  624. new Point (vts.X - 1, vts.Y - 1),
  625. -1,
  626. Orientation.Horizontal,
  627. tab.BorderStyle
  628. );
  629. }
  630. else
  631. {
  632. // LRCorner
  633. lc.AddLine (
  634. new Point (vts.X - 1, vts.Bottom - selectedOffset),
  635. -1,
  636. Orientation.Vertical,
  637. tab.BorderStyle
  638. );
  639. lc.AddLine (
  640. new Point (vts.X - 1, vts.Bottom - selectedOffset),
  641. -1,
  642. Orientation.Horizontal,
  643. tab.BorderStyle
  644. );
  645. }
  646. if (_host.Style.ShowTopLine)
  647. {
  648. if (_host.Style.TabsOnBottom)
  649. {
  650. // Lower left tee
  651. lc.AddLine (
  652. new Point (vts.X - 1, vts.Bottom),
  653. -1,
  654. Orientation.Vertical,
  655. tab.BorderStyle
  656. );
  657. lc.AddLine (
  658. new Point (vts.X - 1, vts.Bottom),
  659. 0,
  660. Orientation.Horizontal,
  661. tab.BorderStyle
  662. );
  663. }
  664. else
  665. {
  666. // Upper left tee
  667. lc.AddLine (
  668. new Point (vts.X - 1, vts.Y - 1),
  669. 1,
  670. Orientation.Vertical,
  671. tab.BorderStyle
  672. );
  673. lc.AddLine (
  674. new Point (vts.X - 1, vts.Y - 1),
  675. 0,
  676. Orientation.Horizontal,
  677. tab.BorderStyle
  678. );
  679. }
  680. }
  681. }
  682. if (i < tabLocations.Length - 1)
  683. {
  684. if (_host.Style.ShowTopLine)
  685. {
  686. if (_host.Style.TabsOnBottom)
  687. {
  688. // Lower right tee
  689. lc.AddLine (
  690. new Point (vts.Right, vts.Bottom),
  691. -1,
  692. Orientation.Vertical,
  693. tab.BorderStyle
  694. );
  695. lc.AddLine (
  696. new Point (vts.Right, vts.Bottom),
  697. 0,
  698. Orientation.Horizontal,
  699. tab.BorderStyle
  700. );
  701. }
  702. else
  703. {
  704. // Upper right tee
  705. lc.AddLine (
  706. new Point (vts.Right, vts.Y - 1),
  707. 1,
  708. Orientation.Vertical,
  709. tab.BorderStyle
  710. );
  711. lc.AddLine (
  712. new Point (vts.Right, vts.Y - 1),
  713. 0,
  714. Orientation.Horizontal,
  715. tab.BorderStyle
  716. );
  717. }
  718. }
  719. }
  720. if (_host.Style.TabsOnBottom)
  721. {
  722. //URCorner
  723. lc.AddLine (
  724. new Point (vts.Right, vts.Y - 1),
  725. 1,
  726. Orientation.Vertical,
  727. tab.BorderStyle
  728. );
  729. lc.AddLine (
  730. new Point (vts.Right, vts.Y - 1),
  731. 1,
  732. Orientation.Horizontal,
  733. tab.BorderStyle
  734. );
  735. }
  736. else
  737. {
  738. //LLCorner
  739. lc.AddLine (
  740. new Point (vts.Right, vts.Bottom - selectedOffset),
  741. -1,
  742. Orientation.Vertical,
  743. tab.BorderStyle
  744. );
  745. lc.AddLine (
  746. new Point (vts.Right, vts.Bottom - selectedOffset),
  747. 1,
  748. Orientation.Horizontal,
  749. tab.BorderStyle
  750. );
  751. }
  752. }
  753. else if (selectedTab == -1)
  754. {
  755. if (i == 0 && string.IsNullOrEmpty (tab.Text))
  756. {
  757. if (_host.Style.TabsOnBottom)
  758. {
  759. if (_host.Style.ShowTopLine)
  760. {
  761. // LLCorner
  762. lc.AddLine (
  763. new Point (vts.X - 1, vts.Bottom),
  764. -1,
  765. Orientation.Vertical,
  766. tab.BorderStyle
  767. );
  768. lc.AddLine (
  769. new Point (vts.X - 1, vts.Bottom),
  770. 1,
  771. Orientation.Horizontal,
  772. tab.BorderStyle
  773. );
  774. }
  775. // ULCorner
  776. lc.AddLine (
  777. new Point (vts.X - 1, vts.Y - 1),
  778. 1,
  779. Orientation.Vertical,
  780. tab.BorderStyle
  781. );
  782. lc.AddLine (
  783. new Point (vts.X - 1, vts.Y - 1),
  784. 1,
  785. Orientation.Horizontal,
  786. tab.BorderStyle
  787. );
  788. }
  789. else
  790. {
  791. if (_host.Style.ShowTopLine)
  792. {
  793. // ULCorner
  794. lc.AddLine (
  795. new Point (vts.X - 1, vts.Y - 1),
  796. 1,
  797. Orientation.Vertical,
  798. tab.BorderStyle
  799. );
  800. lc.AddLine (
  801. new Point (vts.X - 1, vts.Y - 1),
  802. 1,
  803. Orientation.Horizontal,
  804. tab.BorderStyle
  805. );
  806. }
  807. // LLCorner
  808. lc.AddLine (
  809. new Point (vts.X - 1, vts.Bottom),
  810. -1,
  811. Orientation.Vertical,
  812. tab.BorderStyle
  813. );
  814. lc.AddLine (
  815. new Point (vts.X - 1, vts.Bottom),
  816. 1,
  817. Orientation.Horizontal,
  818. tab.BorderStyle
  819. );
  820. }
  821. }
  822. else if (i > 0)
  823. {
  824. if (_host.Style.ShowTopLine || _host.Style.TabsOnBottom)
  825. {
  826. // Upper left tee
  827. lc.AddLine (
  828. new Point (vts.X - 1, vts.Y - 1),
  829. 1,
  830. Orientation.Vertical,
  831. tab.BorderStyle
  832. );
  833. lc.AddLine (
  834. new Point (vts.X - 1, vts.Y - 1),
  835. 0,
  836. Orientation.Horizontal,
  837. tab.BorderStyle
  838. );
  839. }
  840. // Lower left tee
  841. lc.AddLine (
  842. new Point (vts.X - 1, vts.Bottom),
  843. -1,
  844. Orientation.Vertical,
  845. tab.BorderStyle
  846. );
  847. lc.AddLine (
  848. new Point (vts.X - 1, vts.Bottom),
  849. 0,
  850. Orientation.Horizontal,
  851. tab.BorderStyle
  852. );
  853. }
  854. }
  855. else if (i < tabLocations.Length - 1)
  856. {
  857. if (_host.Style.ShowTopLine)
  858. {
  859. // Upper right tee
  860. lc.AddLine (
  861. new Point (vts.Right, vts.Y - 1),
  862. 1,
  863. Orientation.Vertical,
  864. tab.BorderStyle
  865. );
  866. lc.AddLine (
  867. new Point (vts.Right, vts.Y - 1),
  868. 0,
  869. Orientation.Horizontal,
  870. tab.BorderStyle
  871. );
  872. }
  873. if (_host.Style.ShowTopLine || !_host.Style.TabsOnBottom)
  874. {
  875. // Lower right tee
  876. lc.AddLine (
  877. new Point (vts.Right, vts.Bottom),
  878. -1,
  879. Orientation.Vertical,
  880. tab.BorderStyle
  881. );
  882. lc.AddLine (
  883. new Point (vts.Right, vts.Bottom),
  884. 0,
  885. Orientation.Horizontal,
  886. tab.BorderStyle
  887. );
  888. }
  889. else
  890. {
  891. // Upper right tee
  892. lc.AddLine (
  893. new Point (vts.Right, vts.Y - 1),
  894. 1,
  895. Orientation.Vertical,
  896. tab.BorderStyle
  897. );
  898. lc.AddLine (
  899. new Point (vts.Right, vts.Y - 1),
  900. 0,
  901. Orientation.Horizontal,
  902. tab.BorderStyle
  903. );
  904. }
  905. }
  906. if (i == 0 && i != selectedTab && _host is { TabScrollOffset: 0, Style.ShowBorder: true })
  907. {
  908. if (_host.Style.TabsOnBottom)
  909. {
  910. // Upper left vertical line
  911. lc.AddLine (
  912. new Point (vts.X - 1, vts.Y - 1),
  913. 0,
  914. Orientation.Vertical,
  915. tab.BorderStyle
  916. );
  917. lc.AddLine (
  918. new Point (vts.X - 1, vts.Y - 1),
  919. 1,
  920. Orientation.Horizontal,
  921. tab.BorderStyle
  922. );
  923. }
  924. else
  925. {
  926. // Lower left vertical line
  927. lc.AddLine (
  928. new Point (vts.X - 1, vts.Bottom),
  929. 0,
  930. Orientation.Vertical,
  931. tab.BorderStyle
  932. );
  933. lc.AddLine (
  934. new Point (vts.X - 1, vts.Bottom),
  935. 1,
  936. Orientation.Horizontal,
  937. tab.BorderStyle
  938. );
  939. }
  940. }
  941. if (i == tabLocations.Length - 1 && i != selectedTab)
  942. {
  943. if (_host.Style.TabsOnBottom)
  944. {
  945. // Upper right tee
  946. lc.AddLine (
  947. new Point (vts.Right, vts.Y - 1),
  948. 1,
  949. Orientation.Vertical,
  950. tab.BorderStyle
  951. );
  952. lc.AddLine (
  953. new Point (vts.Right, vts.Y - 1),
  954. 0,
  955. Orientation.Horizontal,
  956. tab.BorderStyle
  957. );
  958. }
  959. else
  960. {
  961. // Lower right tee
  962. lc.AddLine (
  963. new Point (vts.Right, vts.Bottom),
  964. -1,
  965. Orientation.Vertical,
  966. tab.BorderStyle
  967. );
  968. lc.AddLine (
  969. new Point (vts.Right, vts.Bottom),
  970. 0,
  971. Orientation.Horizontal,
  972. tab.BorderStyle
  973. );
  974. }
  975. }
  976. if (i == tabLocations.Length - 1)
  977. {
  978. var arrowOffset = 1;
  979. int lastSelectedTab = !_host.Style.ShowTopLine && i == selectedTab ? 1 :
  980. _host.Style.TabsOnBottom ? 1 : 0;
  981. Rectangle tabsBarVts = ViewportToScreen (Viewport);
  982. int lineLength = tabsBarVts.Right - vts.Right;
  983. // Right horizontal line
  984. if (ShouldDrawRightScrollIndicator ())
  985. {
  986. if (lineLength - arrowOffset > 0)
  987. {
  988. if (_host.Style.TabsOnBottom)
  989. {
  990. lc.AddLine (
  991. new Point (vts.Right, vts.Y - lastSelectedTab),
  992. lineLength - arrowOffset,
  993. Orientation.Horizontal,
  994. tab.BorderStyle
  995. );
  996. }
  997. else
  998. {
  999. lc.AddLine (
  1000. new Point (
  1001. vts.Right,
  1002. vts.Bottom - lastSelectedTab
  1003. ),
  1004. lineLength - arrowOffset,
  1005. Orientation.Horizontal,
  1006. tab.BorderStyle
  1007. );
  1008. }
  1009. }
  1010. }
  1011. else
  1012. {
  1013. // Right corner
  1014. if (_host.Style.TabsOnBottom)
  1015. {
  1016. lc.AddLine (
  1017. new Point (vts.Right, vts.Y - lastSelectedTab),
  1018. lineLength,
  1019. Orientation.Horizontal,
  1020. tab.BorderStyle
  1021. );
  1022. }
  1023. else
  1024. {
  1025. lc.AddLine (
  1026. new Point (vts.Right, vts.Bottom - lastSelectedTab),
  1027. lineLength,
  1028. Orientation.Horizontal,
  1029. tab.BorderStyle
  1030. );
  1031. }
  1032. if (_host.Style.ShowBorder)
  1033. {
  1034. if (_host.Style.TabsOnBottom)
  1035. {
  1036. // More LRCorner
  1037. lc.AddLine (
  1038. new Point (
  1039. tabsBarVts.Right - 1,
  1040. vts.Y - lastSelectedTab
  1041. ),
  1042. -1,
  1043. Orientation.Vertical,
  1044. tab.BorderStyle
  1045. );
  1046. }
  1047. else
  1048. {
  1049. // More URCorner
  1050. lc.AddLine (
  1051. new Point (
  1052. tabsBarVts.Right - 1,
  1053. vts.Bottom - lastSelectedTab
  1054. ),
  1055. 1,
  1056. Orientation.Vertical,
  1057. tab.BorderStyle
  1058. );
  1059. }
  1060. }
  1061. }
  1062. }
  1063. }
  1064. _host.LineCanvas.Merge (lc);
  1065. }
  1066. private int GetUnderlineYPosition ()
  1067. {
  1068. if (_host.Style.TabsOnBottom)
  1069. {
  1070. return 0;
  1071. }
  1072. return _host.Style.ShowTopLine ? 2 : 1;
  1073. }
  1074. /// <summary>Renders the line with the tab names in it.</summary>
  1075. private void RenderTabLine ()
  1076. {
  1077. if (_host._tabLocations is null)
  1078. {
  1079. return;
  1080. }
  1081. View? selected = null;
  1082. int topLine = _host.Style.ShowTopLine ? 1 : 0;
  1083. foreach (TabToRender toRender in _host._tabLocations)
  1084. {
  1085. Tab tab = toRender.Tab;
  1086. if (toRender.IsSelected)
  1087. {
  1088. selected = tab;
  1089. if (_host.Style.TabsOnBottom)
  1090. {
  1091. tab.Border!.Thickness = new (1, 0, 1, topLine);
  1092. tab.Margin!.Thickness = new (0, 1, 0, 0);
  1093. }
  1094. else
  1095. {
  1096. tab.Border!.Thickness = new (1, topLine, 1, 0);
  1097. tab.Margin!.Thickness = new (0, 0, 0, topLine);
  1098. }
  1099. }
  1100. else if (selected is null)
  1101. {
  1102. if (_host.Style.TabsOnBottom)
  1103. {
  1104. tab.Border!.Thickness = new (1, 1, 1, topLine);
  1105. tab.Margin!.Thickness = new (0, 0, 0, 0);
  1106. }
  1107. else
  1108. {
  1109. tab.Border!.Thickness = new (1, topLine, 1, 1);
  1110. tab.Margin!.Thickness = new (0, 0, 0, 0);
  1111. }
  1112. }
  1113. else
  1114. {
  1115. if (_host.Style.TabsOnBottom)
  1116. {
  1117. tab.Border!.Thickness = new (1, 1, 1, topLine);
  1118. tab.Margin!.Thickness = new (0, 0, 0, 0);
  1119. }
  1120. else
  1121. {
  1122. tab.Border!.Thickness = new (1, topLine, 1, 1);
  1123. tab.Margin!.Thickness = new (0, 0, 0, 0);
  1124. }
  1125. }
  1126. // Ensures updating TextFormatter constrains
  1127. tab.TextFormatter.ConstrainToWidth = tab.GetContentSize ().Width;
  1128. tab.TextFormatter.ConstrainToHeight = tab.GetContentSize ().Height;
  1129. }
  1130. }
  1131. /// <summary>Renders the line of the tab that adjoins the content of the tab.</summary>
  1132. private void RenderUnderline ()
  1133. {
  1134. int y = GetUnderlineYPosition ();
  1135. TabToRender? selected = _host._tabLocations?.FirstOrDefault (t => t.IsSelected);
  1136. if (selected is null)
  1137. {
  1138. return;
  1139. }
  1140. // draw scroll indicators
  1141. // if there are more tabs to the left not visible
  1142. if (_host.TabScrollOffset > 0)
  1143. {
  1144. _leftScrollIndicator.X = 0;
  1145. _leftScrollIndicator.Y = y;
  1146. // indicate that
  1147. _leftScrollIndicator.Visible = true;
  1148. // Ensures this is clicked instead of the first tab
  1149. MoveSubviewToEnd (_leftScrollIndicator);
  1150. }
  1151. else
  1152. {
  1153. _leftScrollIndicator.Visible = false;
  1154. }
  1155. // if there are more tabs to the right not visible
  1156. if (ShouldDrawRightScrollIndicator ())
  1157. {
  1158. _rightScrollIndicator.X = Viewport.Width - 1;
  1159. _rightScrollIndicator.Y = y;
  1160. // indicate that
  1161. _rightScrollIndicator.Visible = true;
  1162. // Ensures this is clicked instead of the last tab if under this
  1163. MoveSubviewToStart (_rightScrollIndicator);
  1164. }
  1165. else
  1166. {
  1167. _rightScrollIndicator.Visible = false;
  1168. }
  1169. }
  1170. private bool ShouldDrawRightScrollIndicator () { return _host._tabLocations!.LastOrDefault ()?.Tab != _host.Tabs.LastOrDefault (); }
  1171. }
  1172. private class TabToRender
  1173. {
  1174. public TabToRender (Tab tab, string textToRender, bool isSelected)
  1175. {
  1176. Tab = tab;
  1177. IsSelected = isSelected;
  1178. TextToRender = textToRender;
  1179. }
  1180. /// <summary>True if the tab that is being rendered is the selected one.</summary>
  1181. /// <value></value>
  1182. public bool IsSelected { get; }
  1183. public Tab Tab { get; }
  1184. public string TextToRender { get; }
  1185. }
  1186. }