123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793 |
- #nullable enable
- namespace Terminal.Gui;
- internal class TabRowView : View
- {
- private readonly TabView _host;
- private readonly View _leftScrollIndicator;
- private readonly View _rightScrollIndicator;
- public TabRowView (TabView host)
- {
- _host = host;
- Id = "tabRowView";
- CanFocus = true;
- 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 override bool OnMouseEvent (MouseEventArgs me)
- {
- View? parent = me.View is Adornment adornment ? adornment.Parent : me.View;
- Tab? hit = parent as Tab;
- if (me.IsSingleClicked)
- {
- _host.OnTabClicked (new TabMouseEventArgs (hit!, me));
- // user canceled click
- if (me.Handled)
- {
- return true;
- }
- if (parent == _host.SelectedTab)
- {
- _host.SelectedTab?.SetFocus ();
- }
- }
- if (!me.IsSingleDoubleOrTripleClicked)
- {
- return false;
- }
- if (!HasFocus && CanFocus)
- {
- SetFocus ();
- }
- if (me.IsSingleDoubleOrTripleClicked)
- {
- var scrollIndicatorHit = 0;
- if (me.View is { Id: "rightScrollIndicator" })
- {
- scrollIndicatorHit = 1;
- }
- else if (me.View is { Id: "leftScrollIndicator" })
- {
- scrollIndicatorHit = -1;
- }
- if (scrollIndicatorHit != 0)
- {
- _host.SwitchTabBy (scrollIndicatorHit);
- return true;
- }
- if (hit is { })
- {
- _host.SelectedTab = hit;
- return true;
- }
- }
- return false;
- }
- /// <inheritdoc />
- protected override void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? focusedView)
- {
- if (_host.SelectedTab is { HasFocus: false, CanFocus: true } && focusedView == this)
- {
- _host.SelectedTab?.SetFocus ();
- return;
- }
- base.OnHasFocusChanged (newHasFocus, previousFocusedView, focusedView);
- }
- /// <inheritdoc/>
- protected override void OnSubviewLayout (LayoutEventArgs args)
- {
- _host._tabLocations = _host.CalculateViewport (Viewport).ToArray ();
- RenderTabLine ();
- RenderUnderline ();
- base.OnSubviewLayout (args);
- }
- /// <inheritdoc />
- protected override bool OnRenderingLineCanvas ()
- {
- RenderTabLineCanvas ();
- return false;
- }
- private void RenderTabLineCanvas ()
- {
- if (_host._tabLocations is null)
- {
- return;
- }
- Tab [] tabLocations = _host._tabLocations;
- int selectedTab = -1;
- var lc = new LineCanvas ();
- for (var i = 0; i < tabLocations.Length; i++)
- {
- View tab = tabLocations [i];
- Rectangle vts = tab.ViewportToScreen (tab.Viewport);
- int selectedOffset = _host.Style.ShowTopLine && tabLocations [i] == _host.SelectedTab ? 0 : 1;
- if (tabLocations [i] == _host.SelectedTab)
- {
- 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 is { TabScrollOffset: 0, Style.ShowBorder: true })
- {
- 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
- {
- // Right corner
- 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
- );
- }
- }
- }
- }
- }
- _host.LineCanvas.Merge (lc);
- }
- 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 ()
- {
- if (_host._tabLocations is null)
- {
- return;
- }
- View? selected = null;
- int topLine = _host.Style.ShowTopLine ? 1 : 0;
- foreach (Tab toRender in _host._tabLocations)
- {
- Tab tab = toRender;
- if (toRender == _host.SelectedTab)
- {
- selected = tab;
- if (_host.Style.TabsOnBottom)
- {
- tab.Border!.Thickness = new (1, 0, 1, topLine);
- tab.Margin!.Thickness = new (0, 1, 0, 0);
- }
- else
- {
- tab.Border!.Thickness = new (1, topLine, 1, 0);
- tab.Margin!.Thickness = new (0, 0, 0, topLine);
- }
- }
- else if (selected is null)
- {
- if (_host.Style.TabsOnBottom)
- {
- tab.Border!.Thickness = new (1, 1, 1, topLine);
- tab.Margin!.Thickness = new (0, 0, 0, 0);
- }
- else
- {
- tab.Border!.Thickness = new (1, topLine, 1, 1);
- tab.Margin!.Thickness = new (0, 0, 0, 0);
- }
- }
- else
- {
- if (_host.Style.TabsOnBottom)
- {
- tab.Border!.Thickness = new (1, 1, 1, topLine);
- tab.Margin!.Thickness = new (0, 0, 0, 0);
- }
- else
- {
- tab.Border!.Thickness = new (1, topLine, 1, 1);
- tab.Margin!.Thickness = new (0, 0, 0, 0);
- }
- }
- // Ensures updating TextFormatter constrains
- tab.TextFormatter.ConstrainToWidth = tab.GetContentSize ().Width;
- tab.TextFormatter.ConstrainToHeight = tab.GetContentSize ().Height;
- }
- }
- /// <summary>Renders the line of the tab that adjoins the content of the tab.</summary>
- private void RenderUnderline ()
- {
- int y = GetUnderlineYPosition ();
- Tab? selected = _host._tabLocations?.FirstOrDefault (t => t == _host.SelectedTab);
- 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
- MoveSubviewToEnd (_leftScrollIndicator);
- }
- 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
- MoveSubviewToStart (_rightScrollIndicator);
- }
- else
- {
- _rightScrollIndicator.Visible = false;
- }
- }
- private bool ShouldDrawRightScrollIndicator () { return _host._tabLocations!.LastOrDefault () != _host.Tabs.LastOrDefault (); }
- }
|