12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397 |
- namespace Terminal.Gui;
- /// <summary>Control that hosts multiple sub views, presenting a single one at once.</summary>
- public class TabView : View
- {
- /// <summary>The default <see cref="MaxTabTextWidth"/> to set on new <see cref="TabView"/> controls.</summary>
- public const uint DefaultMaxTabTextWidth = 30;
- /// <summary>
- /// This sub view is the main client area of the current tab. It hosts the <see cref="Tab.View"/> of the tab, the
- /// <see cref="SelectedTab"/>.
- /// </summary>
- private readonly View _contentView;
- private readonly List<Tab> _tabs = new ();
- /// <summary>This sub view is the 2 or 3 line control that represents the actual tabs themselves.</summary>
- private readonly TabRowView _tabsBar;
- private Tab _selectedTab;
- private TabToRender [] _tabLocations;
- private int _tabScrollOffset;
- /// <summary>Initializes a <see cref="TabView"/> class.</summary>
- public TabView ()
- {
- CanFocus = true;
- TabStop = TabBehavior.TabGroup;
- _tabsBar = new TabRowView (this);
- _contentView = new View ();
- ApplyStyleChanges ();
- base.Add (_tabsBar);
- base.Add (_contentView);
- // Things this view knows how to do
- AddCommand (
- Command.Left,
- () =>
- {
- SwitchTabBy (-1);
- return true;
- }
- );
- AddCommand (
- Command.Right,
- () =>
- {
- SwitchTabBy (1);
- return true;
- }
- );
- AddCommand (
- Command.LeftHome,
- () =>
- {
- TabScrollOffset = 0;
- SelectedTab = Tabs.FirstOrDefault ();
- return true;
- }
- );
- AddCommand (
- Command.RightEnd,
- () =>
- {
- TabScrollOffset = Tabs.Count - 1;
- SelectedTab = Tabs.LastOrDefault ();
- return true;
- }
- );
- AddCommand (
- Command.NextView,
- () =>
- {
- if (_contentView is { HasFocus: false })
- {
- _contentView.SetFocus ();
- return true;
- }
- return false;
- }
- );
- AddCommand (
- Command.PreviousView,
- () =>
- {
- SuperView?.AdvanceFocus (NavigationDirection.Backward);
- return true;
- }
- );
- AddCommand (
- Command.PageDown,
- () =>
- {
- TabScrollOffset += _tabLocations.Length;
- SelectedTab = Tabs.ElementAt (TabScrollOffset);
- return true;
- }
- );
- AddCommand (
- Command.PageUp,
- () =>
- {
- TabScrollOffset -= _tabLocations.Length;
- SelectedTab = Tabs.ElementAt (TabScrollOffset);
- return true;
- }
- );
- // Default keybindings for this view
- KeyBindings.Add (Key.CursorLeft, Command.Left);
- KeyBindings.Add (Key.CursorRight, Command.Right);
- KeyBindings.Add (Key.Home, Command.LeftHome);
- KeyBindings.Add (Key.End, Command.RightEnd);
- KeyBindings.Add (Key.CursorDown, Command.NextView);
- KeyBindings.Add (Key.CursorUp, Command.PreviousView);
- KeyBindings.Add (Key.PageDown, Command.PageDown);
- KeyBindings.Add (Key.PageUp, Command.PageUp);
- }
- /// <summary>
- /// The maximum number of characters to render in a Tab header. This prevents one long tab from pushing out all
- /// the others.
- /// </summary>
- public uint MaxTabTextWidth { get; set; } = DefaultMaxTabTextWidth;
- /// <summary>The currently selected member of <see cref="Tabs"/> chosen by the user.</summary>
- /// <value></value>
- public Tab SelectedTab
- {
- get => _selectedTab;
- set
- {
- UnSetCurrentTabs ();
- Tab old = _selectedTab;
- if (_selectedTab is { })
- {
- if (_selectedTab.View is { })
- {
- // remove old content
- _contentView.Remove (_selectedTab.View);
- }
- }
- _selectedTab = value;
- if (value is { })
- {
- // add new content
- if (_selectedTab.View is { })
- {
- _contentView.Add (_selectedTab.View);
- }
- }
- EnsureSelectedTabIsVisible ();
- if (old != value)
- {
- if (old?.HasFocus == true)
- {
- SelectedTab?.SetFocus ();
- }
- OnSelectedTabChanged (old, value);
- }
- }
- }
- /// <summary>Render choices for how to display tabs. After making changes, call <see cref="ApplyStyleChanges()"/>.</summary>
- /// <value></value>
- public TabStyle Style { get; set; } = new ();
- /// <summary>All tabs currently hosted by the control.</summary>
- /// <value></value>
- public IReadOnlyCollection<Tab> Tabs => _tabs.AsReadOnly ();
- /// <summary>When there are too many tabs to render, this indicates the first tab to render on the screen.</summary>
- /// <value></value>
- public int TabScrollOffset
- {
- get => _tabScrollOffset;
- set => _tabScrollOffset = EnsureValidScrollOffsets (value);
- }
- /// <summary>Adds the given <paramref name="tab"/> to <see cref="Tabs"/>.</summary>
- /// <param name="tab"></param>
- /// <param name="andSelect">True to make the newly added Tab the <see cref="SelectedTab"/>.</param>
- public void AddTab (Tab tab, bool andSelect)
- {
- if (_tabs.Contains (tab))
- {
- return;
- }
- _tabs.Add (tab);
- _tabsBar.Add (tab);
- if (SelectedTab is null || andSelect)
- {
- SelectedTab = tab;
- EnsureSelectedTabIsVisible ();
- tab.View?.SetFocus ();
- }
- SetNeedsDisplay ();
- }
- /// <summary>
- /// Updates the control to use the latest state settings in <see cref="Style"/>. This can change the size of the
- /// client area of the tab (for rendering the selected tab's content). This method includes a call to
- /// <see cref="View.SetNeedsDisplay()"/>.
- /// </summary>
- public void ApplyStyleChanges ()
- {
- _contentView.BorderStyle = Style.ShowBorder ? LineStyle.Single : LineStyle.None;
- _contentView.Width = Dim.Fill ();
- if (Style.TabsOnBottom)
- {
- // Tabs are along the bottom so just dodge the border
- if (Style.ShowBorder)
- {
- _contentView.Border.Thickness = new Thickness (1, 1, 1, 0);
- }
- _contentView.Y = 0;
- int tabHeight = GetTabHeight (false);
- // Fill client area leaving space at bottom for tabs
- _contentView.Height = Dim.Fill (tabHeight);
- _tabsBar.Height = tabHeight;
- _tabsBar.Y = Pos.Bottom (_contentView);
- }
- else
- {
- // Tabs are along the top
- if (Style.ShowBorder)
- {
- _contentView.Border.Thickness = new Thickness (1, 0, 1, 1);
- }
- _tabsBar.Y = 0;
- int tabHeight = GetTabHeight (true);
- //move content down to make space for tabs
- _contentView.Y = Pos.Bottom (_tabsBar);
- // Fill client area leaving space at bottom for border
- _contentView.Height = Dim.Fill ();
- // The top tab should be 2 or 3 rows high and on the top
- _tabsBar.Height = tabHeight;
- // Should be able to just use 0 but switching between top/bottom tabs repeatedly breaks in ValidatePosDim if just using the absolute value 0
- }
- if (IsInitialized)
- {
- LayoutSubviews ();
- }
- SetNeedsDisplay ();
- }
- /// <summary>Updates <see cref="TabScrollOffset"/> to ensure that <see cref="SelectedTab"/> is visible.</summary>
- public void EnsureSelectedTabIsVisible ()
- {
- if (!IsInitialized || SelectedTab is null)
- {
- return;
- }
- // if current viewport does not include the selected tab
- if (!CalculateViewport (Viewport).Any (r => Equals (SelectedTab, r.Tab)))
- {
- // Set scroll offset so the first tab rendered is the
- TabScrollOffset = Math.Max (0, Tabs.IndexOf (SelectedTab));
- }
- }
- /// <summary>Updates <see cref="TabScrollOffset"/> to be a valid index of <see cref="Tabs"/>.</summary>
- /// <param name="value">The value to validate.</param>
- /// <remarks>Changes will not be immediately visible in the display until you call <see cref="View.SetNeedsDisplay()"/>.</remarks>
- /// <returns>The valid <see cref="TabScrollOffset"/> for the given value.</returns>
- public int EnsureValidScrollOffsets (int value) { return Math.Max (Math.Min (value, Tabs.Count - 1), 0); }
- /// <inheritdoc/>
- public override void OnDrawContent (Rectangle viewport)
- {
- Driver.SetAttribute (GetNormalColor ());
- if (Tabs.Any ())
- {
- Rectangle savedClip = SetClip ();
- _tabsBar.OnDrawContent (viewport);
- _contentView.SetNeedsDisplay ();
- _contentView.Draw ();
- Driver.Clip = savedClip;
- }
- }
- /// <inheritdoc/>
- public override void OnDrawContentComplete (Rectangle viewport) { _tabsBar.OnDrawContentComplete (viewport); }
- /// <summary>
- /// Removes the given <paramref name="tab"/> from <see cref="Tabs"/>. Caller is responsible for disposing the
- /// tab's hosted <see cref="Tab.View"/> if appropriate.
- /// </summary>
- /// <param name="tab"></param>
- public void RemoveTab (Tab tab)
- {
- if (tab is null || !_tabs.Contains (tab))
- {
- return;
- }
- // what tab was selected before closing
- int idx = _tabs.IndexOf (tab);
- _tabs.Remove (tab);
- // if the currently selected tab is no longer a member of Tabs
- if (SelectedTab is null || !Tabs.Contains (SelectedTab))
- {
- // select the tab closest to the one that disappeared
- int toSelect = Math.Max (idx - 1, 0);
- if (toSelect < Tabs.Count)
- {
- SelectedTab = Tabs.ElementAt (toSelect);
- }
- else
- {
- SelectedTab = Tabs.LastOrDefault ();
- }
- }
- EnsureSelectedTabIsVisible ();
- SetNeedsDisplay ();
- }
- /// <summary>Event for when <see cref="SelectedTab"/> changes.</summary>
- public event EventHandler<TabChangedEventArgs> SelectedTabChanged;
- /// <summary>
- /// Changes the <see cref="SelectedTab"/> by the given <paramref name="amount"/>. Positive for right, negative for
- /// left. If no tab is currently selected then the first tab will become selected.
- /// </summary>
- /// <param name="amount"></param>
- public void SwitchTabBy (int amount)
- {
- if (Tabs.Count == 0)
- {
- return;
- }
- // if there is only one tab anyway or nothing is selected
- if (Tabs.Count == 1 || SelectedTab is null)
- {
- SelectedTab = Tabs.ElementAt (0);
- SetNeedsDisplay ();
- return;
- }
- int currentIdx = Tabs.IndexOf (SelectedTab);
- // Currently selected tab has vanished!
- if (currentIdx == -1)
- {
- SelectedTab = Tabs.ElementAt (0);
- SetNeedsDisplay ();
- return;
- }
- int newIdx = Math.Max (0, Math.Min (currentIdx + amount, Tabs.Count - 1));
- SelectedTab = _tabs [newIdx];
- SetNeedsDisplay ();
- EnsureSelectedTabIsVisible ();
- }
- /// <summary>
- /// Event fired when a <see cref="Tab"/> is clicked. Can be used to cancel navigation, show context menu (e.g. on
- /// right click) etc.
- /// </summary>
- public event EventHandler<TabMouseEventArgs> TabClicked;
- /// <summary>Disposes the control and all <see cref="Tabs"/>.</summary>
- /// <param name="disposing"></param>
- protected override void Dispose (bool disposing)
- {
- base.Dispose (disposing);
- // The selected tab will automatically be disposed but
- // any tabs not visible will need to be manually disposed
- foreach (Tab tab in Tabs)
- {
- if (!Equals (SelectedTab, tab))
- {
- tab.View?.Dispose ();
- }
- }
- }
- /// <summary>Raises the <see cref="SelectedTabChanged"/> event.</summary>
- protected virtual void OnSelectedTabChanged (Tab oldTab, Tab newTab) { SelectedTabChanged?.Invoke (this, new TabChangedEventArgs (oldTab, newTab)); }
- /// <summary>Returns which tabs to render at each x location.</summary>
- /// <returns></returns>
- private IEnumerable<TabToRender> CalculateViewport (Rectangle bounds)
- {
- UnSetCurrentTabs ();
- var i = 1;
- View prevTab = null;
- // Starting at the first or scrolled to tab
- foreach (Tab tab in Tabs.Skip (TabScrollOffset))
- {
- if (prevTab is { })
- {
- tab.X = Pos.Right (prevTab);
- }
- else
- {
- tab.X = 0;
- }
- tab.Y = 0;
- // while there is space for the tab
- int tabTextWidth = tab.DisplayText.EnumerateRunes ().Sum (c => c.GetColumns ());
- string text = tab.DisplayText;
- // The maximum number of characters to use for the tab name as specified
- // by the user (MaxTabTextWidth). But not more than the width of the view
- // or we won't even be able to render a single tab!
- long maxWidth = Math.Max (0, Math.Min (bounds.Width - 3, MaxTabTextWidth));
- prevTab = tab;
- tab.Width = 2;
- tab.Height = Style.ShowTopLine ? 3 : 2;
- // if tab view is width <= 3 don't render any tabs
- if (maxWidth == 0)
- {
- tab.Visible = true;
- tab.MouseClick += Tab_MouseClick;
- yield return new TabToRender (i, tab, string.Empty, Equals (SelectedTab, tab), 0);
- break;
- }
- if (tabTextWidth > maxWidth)
- {
- text = tab.DisplayText.Substring (0, (int)maxWidth);
- tabTextWidth = (int)maxWidth;
- }
- tab.Width = Math.Max (tabTextWidth + 2, 1);
- tab.Height = Style.ShowTopLine ? 3 : 2;
- // if there is not enough space for this tab
- if (i + tabTextWidth >= bounds.Width)
- {
- tab.Visible = false;
- break;
- }
- // there is enough space!
- tab.Visible = true;
- tab.MouseClick += Tab_MouseClick;
- yield return new TabToRender (i, tab, text, Equals (SelectedTab, tab), tabTextWidth);
- i += tabTextWidth + 1;
- }
- }
- /// <summary>
- /// Returns the number of rows occupied by rendering the tabs, this depends on <see cref="TabStyle.ShowTopLine"/>
- /// and can be 0 (e.g. if <see cref="TabStyle.TabsOnBottom"/> and you ask for <paramref name="top"/>).
- /// </summary>
- /// <param name="top">True to measure the space required at the top of the control, false to measure space at the bottom.</param>
- /// .
- /// <returns></returns>
- private int GetTabHeight (bool top)
- {
- if (top && Style.TabsOnBottom)
- {
- return 0;
- }
- if (!top && !Style.TabsOnBottom)
- {
- return 0;
- }
- return Style.ShowTopLine ? 3 : 2;
- }
- private void Tab_MouseClick (object sender, MouseEventEventArgs e) { e.Handled = _tabsBar.NewMouseEvent (e.MouseEvent) == true; }
- private void UnSetCurrentTabs ()
- {
- if (_tabLocations is { })
- {
- foreach (TabToRender tabToRender in _tabLocations)
- {
- tabToRender.Tab.MouseClick -= Tab_MouseClick;
- tabToRender.Tab.Visible = false;
- }
- _tabLocations = null;
- }
- }
- /// <summary>Raises the <see cref="TabClicked"/> event.</summary>
- /// <param name="tabMouseEventArgs"></param>
- private protected virtual void OnTabClicked (TabMouseEventArgs tabMouseEventArgs) { TabClicked?.Invoke (this, tabMouseEventArgs); }
- private class TabRowView : View
- {
- private readonly TabView _host;
- private readonly View _leftScrollIndicator;
- private readonly View _rightScrollIndicator;
- public TabRowView (TabView host)
- {
- _host = host;
- CanFocus = true;
- TabStop = TabBehavior.TabGroup;
- Height = 1; // BUGBUG: Views should avoid setting Height as doing so implies Frame.Size == GetContentSize ().
- Width = Dim.Fill ();
- _rightScrollIndicator = new View
- {
- Id = "rightScrollIndicator",
- Width = 1,
- Height = 1,
- Visible = false,
- Text = Glyphs.RightArrow.ToString ()
- };
- _rightScrollIndicator.MouseClick += _host.Tab_MouseClick;
- _leftScrollIndicator = new View
- {
- Id = "leftScrollIndicator",
- Width = 1,
- Height = 1,
- Visible = false,
- Text = Glyphs.LeftArrow.ToString ()
- };
- _leftScrollIndicator.MouseClick += _host.Tab_MouseClick;
- Add (_rightScrollIndicator, _leftScrollIndicator);
- }
- protected internal override bool OnMouseEvent (MouseEvent me)
- {
- Tab hit = me.View is Tab ? (Tab)me.View : null;
- bool isClick = me.Flags.HasFlag (MouseFlags.Button1Clicked)
- || me.Flags.HasFlag (MouseFlags.Button2Clicked)
- || me.Flags.HasFlag (MouseFlags.Button3Clicked);
- if (isClick)
- {
- _host.OnTabClicked (new TabMouseEventArgs (hit, me));
- // user canceled click
- if (me.Handled)
- {
- return true;
- }
- }
- if (!me.Flags.HasFlag (MouseFlags.Button1Clicked)
- && !me.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
- && !me.Flags.HasFlag (MouseFlags.Button1TripleClicked))
- {
- return false;
- }
- if (!HasFocus && CanFocus)
- {
- SetFocus ();
- }
- if (me.Flags.HasFlag (MouseFlags.Button1Clicked)
- || me.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
- || me.Flags.HasFlag (MouseFlags.Button1TripleClicked))
- {
- var scrollIndicatorHit = 0;
- if (me.View is { } && me.View.Id == "rightScrollIndicator")
- {
- scrollIndicatorHit = 1;
- }
- else if (me.View is { } && me.View.Id == "leftScrollIndicator")
- {
- scrollIndicatorHit = -1;
- }
- if (scrollIndicatorHit != 0)
- {
- _host.SwitchTabBy (scrollIndicatorHit);
- SetNeedsDisplay ();
- return true;
- }
- if (hit is { })
- {
- _host.SelectedTab = hit;
- SetNeedsDisplay ();
- return true;
- }
- }
- return false;
- }
- public override void OnDrawContent (Rectangle viewport)
- {
- _host._tabLocations = _host.CalculateViewport (Viewport).ToArray ();
- // clear any old text
- Clear ();
- RenderTabLine ();
- RenderUnderline ();
- Driver.SetAttribute (HasFocus ? GetFocusColor () : GetNormalColor ());
- }
- public override void OnDrawContentComplete (Rectangle viewport)
- {
- if (_host._tabLocations is null)
- {
- return;
- }
- TabToRender [] tabLocations = _host._tabLocations;
- int selectedTab = -1;
- for (var i = 0; i < tabLocations.Length; i++)
- {
- View tab = tabLocations [i].Tab;
- Rectangle vts = tab.ViewportToScreen (tab.Viewport);
- var lc = new LineCanvas ();
- int selectedOffset = _host.Style.ShowTopLine && tabLocations [i].IsSelected ? 0 : 1;
- if (tabLocations [i].IsSelected)
- {
- selectedTab = i;
- if (i == 0 && _host.TabScrollOffset == 0)
- {
- if (_host.Style.TabsOnBottom)
- {
- // Upper left vertical line
- lc.AddLine (
- new Point (vts.X - 1, vts.Y - 1),
- -1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- }
- else
- {
- // Lower left vertical line
- lc.AddLine (
- new Point (vts.X - 1, vts.Bottom - selectedOffset),
- -1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- }
- }
- else if (i > 0 && i <= tabLocations.Length - 1)
- {
- if (_host.Style.TabsOnBottom)
- {
- // URCorner
- lc.AddLine (
- new Point (vts.X - 1, vts.Y - 1),
- 1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.X - 1, vts.Y - 1),
- -1,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- else
- {
- // LRCorner
- lc.AddLine (
- new Point (vts.X - 1, vts.Bottom - selectedOffset),
- -1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.X - 1, vts.Bottom - selectedOffset),
- -1,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- if (_host.Style.ShowTopLine)
- {
- if (_host.Style.TabsOnBottom)
- {
- // Lower left tee
- lc.AddLine (
- new Point (vts.X - 1, vts.Bottom),
- -1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.X - 1, vts.Bottom),
- 0,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- else
- {
- // Upper left tee
- lc.AddLine (
- new Point (vts.X - 1, vts.Y - 1),
- 1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.X - 1, vts.Y - 1),
- 0,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- }
- }
- if (i < tabLocations.Length - 1)
- {
- if (_host.Style.ShowTopLine)
- {
- if (_host.Style.TabsOnBottom)
- {
- // Lower right tee
- lc.AddLine (
- new Point (vts.Right, vts.Bottom),
- -1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.Right, vts.Bottom),
- 0,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- else
- {
- // Upper right tee
- lc.AddLine (
- new Point (vts.Right, vts.Y - 1),
- 1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.Right, vts.Y - 1),
- 0,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- }
- }
- if (_host.Style.TabsOnBottom)
- {
- //URCorner
- lc.AddLine (
- new Point (vts.Right, vts.Y - 1),
- 1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.Right, vts.Y - 1),
- 1,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- else
- {
- //LLCorner
- lc.AddLine (
- new Point (vts.Right, vts.Bottom - selectedOffset),
- -1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.Right, vts.Bottom - selectedOffset),
- 1,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- }
- else if (selectedTab == -1)
- {
- if (i == 0 && string.IsNullOrEmpty (tab.Text))
- {
- if (_host.Style.TabsOnBottom)
- {
- if (_host.Style.ShowTopLine)
- {
- // LLCorner
- lc.AddLine (
- new Point (vts.X - 1, vts.Bottom),
- -1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.X - 1, vts.Bottom),
- 1,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- // ULCorner
- lc.AddLine (
- new Point (vts.X - 1, vts.Y - 1),
- 1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.X - 1, vts.Y - 1),
- 1,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- else
- {
- if (_host.Style.ShowTopLine)
- {
- // ULCorner
- lc.AddLine (
- new Point (vts.X - 1, vts.Y - 1),
- 1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.X - 1, vts.Y - 1),
- 1,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- // LLCorner
- lc.AddLine (
- new Point (vts.X - 1, vts.Bottom),
- -1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.X - 1, vts.Bottom),
- 1,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- }
- else if (i > 0)
- {
- if (_host.Style.ShowTopLine || _host.Style.TabsOnBottom)
- {
- // Upper left tee
- lc.AddLine (
- new Point (vts.X - 1, vts.Y - 1),
- 1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.X - 1, vts.Y - 1),
- 0,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- // Lower left tee
- lc.AddLine (
- new Point (vts.X - 1, vts.Bottom),
- -1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.X - 1, vts.Bottom),
- 0,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- }
- else if (i < tabLocations.Length - 1)
- {
- if (_host.Style.ShowTopLine)
- {
- // Upper right tee
- lc.AddLine (
- new Point (vts.Right, vts.Y - 1),
- 1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.Right, vts.Y - 1),
- 0,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- if (_host.Style.ShowTopLine || !_host.Style.TabsOnBottom)
- {
- // Lower right tee
- lc.AddLine (
- new Point (vts.Right, vts.Bottom),
- -1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.Right, vts.Bottom),
- 0,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- else
- {
- // Upper right tee
- lc.AddLine (
- new Point (vts.Right, vts.Y - 1),
- 1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.Right, vts.Y - 1),
- 0,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- }
- if (i == 0 && i != selectedTab && _host.TabScrollOffset == 0 && _host.Style.ShowBorder)
- {
- if (_host.Style.TabsOnBottom)
- {
- // Upper left vertical line
- lc.AddLine (
- new Point (vts.X - 1, vts.Y - 1),
- 0,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.X - 1, vts.Y - 1),
- 1,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- else
- {
- // Lower left vertical line
- lc.AddLine (
- new Point (vts.X - 1, vts.Bottom),
- 0,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.X - 1, vts.Bottom),
- 1,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- }
- if (i == tabLocations.Length - 1 && i != selectedTab)
- {
- if (_host.Style.TabsOnBottom)
- {
- // Upper right tee
- lc.AddLine (
- new Point (vts.Right, vts.Y - 1),
- 1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.Right, vts.Y - 1),
- 0,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- else
- {
- // Lower right tee
- lc.AddLine (
- new Point (vts.Right, vts.Bottom),
- -1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- lc.AddLine (
- new Point (vts.Right, vts.Bottom),
- 0,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- }
- if (i == tabLocations.Length - 1)
- {
- var arrowOffset = 1;
- int lastSelectedTab = !_host.Style.ShowTopLine && i == selectedTab ? 1 :
- _host.Style.TabsOnBottom ? 1 : 0;
- Rectangle tabsBarVts = ViewportToScreen (Viewport);
- int lineLength = tabsBarVts.Right - vts.Right;
- // Right horizontal line
- if (ShouldDrawRightScrollIndicator ())
- {
- if (lineLength - arrowOffset > 0)
- {
- if (_host.Style.TabsOnBottom)
- {
- lc.AddLine (
- new Point (vts.Right, vts.Y - lastSelectedTab),
- lineLength - arrowOffset,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- else
- {
- lc.AddLine (
- new Point (
- vts.Right,
- vts.Bottom - lastSelectedTab
- ),
- lineLength - arrowOffset,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- }
- }
- else
- {
- if (_host.Style.TabsOnBottom)
- {
- lc.AddLine (
- new Point (vts.Right, vts.Y - lastSelectedTab),
- lineLength,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- else
- {
- lc.AddLine (
- new Point (vts.Right, vts.Bottom - lastSelectedTab),
- lineLength,
- Orientation.Horizontal,
- tab.BorderStyle
- );
- }
- if (_host.Style.ShowBorder)
- {
- if (_host.Style.TabsOnBottom)
- {
- // More LRCorner
- lc.AddLine (
- new Point (
- tabsBarVts.Right - 1,
- vts.Y - lastSelectedTab
- ),
- -1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- }
- else
- {
- // More URCorner
- lc.AddLine (
- new Point (
- tabsBarVts.Right - 1,
- vts.Bottom - lastSelectedTab
- ),
- 1,
- Orientation.Vertical,
- tab.BorderStyle
- );
- }
- }
- }
- }
- tab.LineCanvas.Merge (lc);
- tab.OnRenderLineCanvas ();
- }
- }
- private int GetUnderlineYPosition ()
- {
- if (_host.Style.TabsOnBottom)
- {
- return 0;
- }
- return _host.Style.ShowTopLine ? 2 : 1;
- }
- /// <summary>Renders the line with the tab names in it.</summary>
- private void RenderTabLine ()
- {
- TabToRender [] tabLocations = _host._tabLocations;
- int y;
- if (_host.Style.TabsOnBottom)
- {
- y = 1;
- }
- else
- {
- y = _host.Style.ShowTopLine ? 1 : 0;
- }
- View selected = null;
- int topLine = _host.Style.ShowTopLine ? 1 : 0;
- int width = Viewport.Width;
- foreach (TabToRender toRender in tabLocations)
- {
- Tab tab = toRender.Tab;
- if (toRender.IsSelected)
- {
- selected = tab;
- if (_host.Style.TabsOnBottom)
- {
- tab.Border.Thickness = new Thickness (1, 0, 1, topLine);
- tab.Margin.Thickness = new Thickness (0, 1, 0, 0);
- }
- else
- {
- tab.Border.Thickness = new Thickness (1, topLine, 1, 0);
- tab.Margin.Thickness = new Thickness (0, 0, 0, topLine);
- }
- }
- else if (selected is null)
- {
- if (_host.Style.TabsOnBottom)
- {
- tab.Border.Thickness = new Thickness (1, 1, 0, topLine);
- tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
- }
- else
- {
- tab.Border.Thickness = new Thickness (1, topLine, 0, 1);
- tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
- }
- tab.Width = Math.Max (tab.Width.GetAnchor (0) - 1, 1);
- }
- else
- {
- if (_host.Style.TabsOnBottom)
- {
- tab.Border.Thickness = new Thickness (0, 1, 1, topLine);
- tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
- }
- else
- {
- tab.Border.Thickness = new Thickness (0, topLine, 1, 1);
- tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
- }
- tab.Width = Math.Max (tab.Width.GetAnchor (0) - 1, 1);
- }
- tab.Text = toRender.TextToRender;
- LayoutSubviews ();
- tab.OnDrawAdornments ();
- Attribute prevAttr = Driver.GetAttribute ();
- // if tab is the selected one and focus is inside this control
- if (toRender.IsSelected && _host.HasFocus)
- {
- if (_host.Focused == this)
- {
- // if focus is the tab bar itself then show that they can switch tabs
- prevAttr = ColorScheme.HotFocus;
- }
- else
- {
- // Focus is inside the tab
- prevAttr = ColorScheme.HotNormal;
- }
- }
- tab.TextFormatter.Draw (
- tab.ViewportToScreen (tab.Viewport),
- prevAttr,
- ColorScheme.HotNormal
- );
- tab.OnRenderLineCanvas ();
- Driver.SetAttribute (GetNormalColor ());
- }
- }
- /// <summary>Renders the line of the tab that adjoins the content of the tab.</summary>
- private void RenderUnderline ()
- {
- int y = GetUnderlineYPosition ();
- TabToRender selected = _host._tabLocations.FirstOrDefault (t => t.IsSelected);
- if (selected is null)
- {
- return;
- }
- // draw scroll indicators
- // if there are more tabs to the left not visible
- if (_host.TabScrollOffset > 0)
- {
- _leftScrollIndicator.X = 0;
- _leftScrollIndicator.Y = y;
- // indicate that
- _leftScrollIndicator.Visible = true;
- // Ensures this is clicked instead of the first tab
- BringSubviewToFront (_leftScrollIndicator);
- _leftScrollIndicator.Draw ();
- }
- else
- {
- _leftScrollIndicator.Visible = false;
- }
- // if there are more tabs to the right not visible
- if (ShouldDrawRightScrollIndicator ())
- {
- _rightScrollIndicator.X = Viewport.Width - 1;
- _rightScrollIndicator.Y = y;
- // indicate that
- _rightScrollIndicator.Visible = true;
- // Ensures this is clicked instead of the last tab if under this
- BringSubviewToFront (_rightScrollIndicator);
- _rightScrollIndicator.Draw ();
- }
- else
- {
- _rightScrollIndicator.Visible = false;
- }
- }
- private bool ShouldDrawRightScrollIndicator () { return _host._tabLocations.LastOrDefault ()?.Tab != _host.Tabs.LastOrDefault (); }
- }
- private class TabToRender
- {
- public TabToRender (int x, Tab tab, string textToRender, bool isSelected, int width)
- {
- X = x;
- Tab = tab;
- IsSelected = isSelected;
- Width = width;
- TextToRender = textToRender;
- }
- /// <summary>True if the tab that is being rendered is the selected one.</summary>
- /// <value></value>
- public bool IsSelected { get; }
- public Tab Tab { get; }
- public string TextToRender { get; }
- public int Width { get; }
- public int X { get; set; }
- }
- }
|