From e66e8dc08a66051652a0530b83228a7f2cbc5f61 Mon Sep 17 00:00:00 2001 From: Thomas Nind <31306100+tznind@users.noreply.github.com> Date: Sat, 21 May 2022 06:25:01 +0100 Subject: [PATCH] Fix thin tabview (#1737) --- Terminal.Gui/Views/TabView.cs | 10 ++- UnitTests/TabViewTests.cs | 113 +++++++++++++++++++++++++++++++++- 2 files changed, 119 insertions(+), 4 deletions(-) diff --git a/Terminal.Gui/Views/TabView.cs b/Terminal.Gui/Views/TabView.cs index d99db86e4..eed2a5b84 100644 --- a/Terminal.Gui/Views/TabView.cs +++ b/Terminal.Gui/Views/TabView.cs @@ -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); diff --git a/UnitTests/TabViewTests.cs b/UnitTests/TabViewTests.cs index 673dfafeb..3656a6258 100644 --- a/UnitTests/TabViewTests.cs +++ b/UnitTests/TabViewTests.cs @@ -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 ();