Bläddra i källkod

Fixes #1750. Erroneous suppression of Button Text updates. (#1752)

* - Fix upstream issue 1750
- Unexplained breakage in odd test "Update_Only_On_Or_After_Initialize"

* Add workaround in Button.cs for erroneous text width caching in TextFormatter

* - Revert earlier attempted workaround for update issue

* Fix TextFormatter erroneous width caching issue when new runecount matched previous width in columns, add regression test, revert temporary workaround from Button.cs

* Add new unit test Update_Parameterless_Only_On_Or_After_Initialize
James A Sutherland 3 år sedan
förälder
incheckning
efb654e6d9

+ 1 - 1
Terminal.Gui/Core/TextFormatter.cs

@@ -136,7 +136,7 @@ namespace Terminal.Gui {
 			set {
 				text = value;
 
-				if (text.RuneCount > 0 && (Size.Width == 0 || Size.Height == 0 || Size.Width != text.RuneCount)) {
+				if (text.RuneCount > 0 && (Size.Width == 0 || Size.Height == 0 || Size.Width != text.ConsoleWidth)) {
 					// Provide a default size (width = length of longest line, height = 1)
 					// TODO: It might makes more sense for the default to be width = length of first line?
 					Size = new Size (TextFormatter.MaxWidth (Text, int.MaxValue), 1);

+ 3 - 6
Terminal.Gui/Views/Button.cs

@@ -137,8 +137,7 @@ namespace Terminal.Gui {
 				if (hotKey != hk) {
 					HotKey = hk;
 				}
-				if (IsInitialized)
-					Update ();
+				Update ();
 			}
 		}
 
@@ -150,8 +149,7 @@ namespace Terminal.Gui {
 			get => is_default;
 			set {
 				is_default = value;
-				if (IsInitialized)
-					Update ();
+				Update ();
 			}
 		}
 
@@ -188,8 +186,7 @@ namespace Terminal.Gui {
 			get => base.AutoSize;
 			set {
 				base.AutoSize = value;
-				if (IsInitialized)
-					Update ();
+				Update ();
 			}
 		}
 

+ 45 - 4
UnitTests/ButtonTests.cs

@@ -26,7 +26,7 @@ namespace Terminal.Gui.Views {
 			Assert.Equal (new Rect (0, 0, 4, 1), btn.Frame);
 			Assert.Equal (Key.Null, btn.HotKey);
 
-			btn = new Button ("Test", true);
+			btn = new Button ("ARGS", true) {Text="Test"};
 			Assert.Equal ("Test", btn.Text);
 			Application.Top.Add (btn);
 			btn.Redraw (btn.Bounds);
@@ -166,12 +166,15 @@ namespace Terminal.Gui.Views {
 		[Fact]
 		public void TestAssignTextToButton ()
 		{
-			View b = new Button ();
-			b.Text = "heya";
+			View b = new Button () {Text="heya"};
 			Assert.Equal ("heya", b.Text);
+			Assert.True (b.TextFormatter.Text.Contains ("heya"));
+			b.Text = "heyb";
+			Assert.Equal ("heyb", b.Text);
+			Assert.True (b.TextFormatter.Text.Contains ("heyb"));
 
 			// with cast
-			Assert.Equal ("heya", ((Button)b).Text);
+			Assert.Equal ("heyb", ((Button)b).Text);
 		}
 
 		[Fact]
@@ -220,6 +223,44 @@ namespace Terminal.Gui.Views {
 │      [ Say Hello 你 ]      │
 │                            │
 └────────────────────────────┘
+";
+
+			var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
+			Assert.Equal (new Rect (0, 0, 30, 5), pos);
+		}
+		
+		[Fact, AutoInitShutdown]
+		public void Update_Parameterless_Only_On_Or_After_Initialize ()
+		{
+			var btn = new Button () {
+				X = Pos.Center (),
+				Y = Pos.Center (),
+				Text = "Say Hello 你"
+			};
+			var win = new Window () {
+				Width = Dim.Fill (),
+				Height = Dim.Fill (),
+				Title = "Test Demo 你"
+			};
+			win.Add (btn);
+			Application.Top.Add (win);
+
+			Assert.False (btn.IsInitialized);
+
+			Application.Begin (Application.Top);
+			((FakeDriver)Application.Driver).SetBufferSize (30, 5);
+
+			Assert.True (btn.IsInitialized);
+			Assert.Equal ("Say Hello 你", btn.Text);
+			Assert.Equal ("[ Say Hello 你 ]", btn.TextFormatter.Text);
+			Assert.Equal (new Rect (0, 0, 16, 1), btn.Bounds);
+
+			var expected = @"
+┌ Test Demo 你 ──────────────┐
+│                            │
+│      [ Say Hello 你 ]      │
+│                            │
+└────────────────────────────┘
 ";
 
 			var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);

+ 9 - 0
UnitTests/TextFormatterTests.cs

@@ -65,6 +65,15 @@ namespace Terminal.Gui.Core {
 			Assert.NotEmpty (tf.Lines);
 		}
 
+		[Fact]
+		public void TestSize_TextChange ()
+		{
+			var tf = new TextFormatter () { Text = "你" };
+			Assert.Equal (2,tf.Size.Width);
+			tf.Text = "你你";
+			Assert.Equal (4, tf.Size.Width);
+		}
+
 		[Fact]
 		public void NeedsFormat_Sets ()
 		{