瀏覽代碼

Fix thin tabview (#1737)

Thomas Nind 3 年之前
父節點
當前提交
e66e8dc08a
共有 2 個文件被更改,包括 119 次插入4 次删除
  1. 9 1
      Terminal.Gui/Views/TabView.cs
  2. 110 3
      UnitTests/TabViewTests.cs

+ 9 - 1
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);

+ 110 - 3
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 ();