Fix thin tabview (#1737)

This commit is contained in:
Thomas Nind
2022-05-21 06:25:01 +01:00
committed by GitHub
parent 7ed5f5b2b2
commit e66e8dc08a
2 changed files with 119 additions and 4 deletions

View File

@@ -338,7 +338,15 @@ namespace Terminal.Gui {
var tabTextWidth = tab.Text.Sum (c => Rune.ColumnWidth (c));
string text = tab.Text.ToString ();
var maxWidth = MaxTabTextWidth;
// 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!
var maxWidth = Math.Max(0,Math.Min(bounds.Width-3, MaxTabTextWidth));
// if tab view is width <= 3 don't render any tabs
if (maxWidth == 0)
yield break;
if (tabTextWidth > maxWidth) {
text = tab.Text.ToString ().Substring (0, (int)maxWidth);

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
@@ -6,20 +6,30 @@ using System.Threading.Tasks;
using Terminal.Gui;
using Xunit;
using System.Globalization;
using Xunit.Abstractions;
namespace Terminal.Gui.Views {
public class TabViewTests {
readonly ITestOutputHelper output;
public TabViewTests (ITestOutputHelper output)
{
this.output = output;
}
private TabView GetTabView ()
{
return GetTabView (out _, out _);
}
private TabView GetTabView (out TabView.Tab tab1, out TabView.Tab tab2)
private TabView GetTabView (out TabView.Tab tab1, out TabView.Tab tab2, bool initFakeDriver = true)
{
InitFakeDriver ();
if(initFakeDriver)
InitFakeDriver ();
var tv = new TabView ();
tv.ColorScheme = new ColorScheme ();
tv.AddTab (tab1 = new TabView.Tab ("Tab1", new TextField ("hi")), false);
tv.AddTab (tab2 = new TabView.Tab ("Tab2", new Label ("hi2")), false);
return tv;
@@ -235,6 +245,103 @@ namespace Terminal.Gui.Views {
}
[Fact,AutoInitShutdown]
public void TestThinTabView_WithLongNames ()
{
var tv = GetTabView (out var tab1, out var tab2,false);
tv.Width = 10;
tv.Height = 5;
// Ensures that the tab bar subview gets the bounds of the parent TabView
tv.LayoutSubviews ();
// Test two tab names that fit
tab1.Text = "12";
tab2.Text = "13";
tv.Redraw (tv.Bounds);
GraphViewTests.AssertDriverContentsAre (@"
┌──┐
│12│13
│ └─────┐
│hi │
└────────┘", output);
// Test first tab name too long
tab1.Text = "12345678910";
tab2.Text = "13";
tv.Redraw (tv.Bounds);
GraphViewTests.AssertDriverContentsAre (@"
┌───────┐
│1234567│
│ └►
│hi │
└────────┘", output);
//switch to tab2
tv.SelectedTab = tab2;
tv.Redraw (tv.Bounds);
GraphViewTests.AssertDriverContentsAre (@"
┌──┐
│13│
◄ └─────┐
│hi2 │
└────────┘", output);
// now make both tabs too long
tab1.Text = "12345678910";
tab2.Text = "abcdefghijklmnopq";
tv.Redraw (tv.Bounds);
GraphViewTests.AssertDriverContentsAre (@"
┌───────┐
│abcdefg│
◄ └┐
│hi2 │
└────────┘", output);
}
[Fact, AutoInitShutdown]
public void TestTabView_Width4 ()
{
var tv = GetTabView (out _, out _, false);
tv.Width = 4;
tv.Height = 5;
tv.LayoutSubviews ();
tv.Redraw (tv.Bounds);
GraphViewTests.AssertDriverContentsAre (@"
┌─┐
│T│
│ └►
│hi│
└──┘", output);
}
[Fact, AutoInitShutdown]
public void TestTabView_Width3 ()
{
var tv = GetTabView (out _, out _, false);
tv.Width = 3;
tv.Height = 5;
tv.LayoutSubviews ();
tv.Redraw (tv.Bounds);
GraphViewTests.AssertDriverContentsAre (@"
┌─┐
│hi
└─┘", output);
}
private void InitFakeDriver ()
{
var driver = new FakeDriver ();