Explorar el Código

Fixes #731. Pos and Dim validation.

BDisp hace 5 años
padre
commit
5efa8d5388
Se han modificado 4 ficheros con 103 adiciones y 1 borrados
  1. 29 0
      Terminal.Gui/Core/View.cs
  2. 6 1
      Terminal.Gui/Views/StatusBar.cs
  3. 34 0
      UnitTests/DimTests.cs
  4. 34 0
      UnitTests/PosTests.cs

+ 29 - 0
Terminal.Gui/Core/View.cs

@@ -412,6 +412,10 @@ namespace Terminal.Gui {
 		public Pos X {
 			get => x;
 			set {
+				if (!ValidatePosDim (x, value)) {
+					throw new ArgumentException ();
+				}
+
 				x = value;
 				SetNeedsLayout ();
 				SetNeedsDisplay (frame);
@@ -428,6 +432,10 @@ namespace Terminal.Gui {
 		public Pos Y {
 			get => y;
 			set {
+				if (!ValidatePosDim (y, value)) {
+					throw new ArgumentException ();
+				}
+
 				y = value;
 				SetNeedsLayout ();
 				SetNeedsDisplay (frame);
@@ -446,6 +454,10 @@ namespace Terminal.Gui {
 		public Dim Width {
 			get => width;
 			set {
+				if (!ValidatePosDim (width, value)) {
+					throw new ArgumentException ();
+				}
+
 				width = value;
 				SetNeedsLayout ();
 				SetNeedsDisplay (frame);
@@ -460,12 +472,29 @@ namespace Terminal.Gui {
 		public Dim Height {
 			get => height;
 			set {
+				if (!ValidatePosDim (height, value)) {
+					throw new ArgumentException ();
+				}
+
 				height = value;
 				SetNeedsLayout ();
 				SetNeedsDisplay (frame);
 			}
 		}
 
+		bool ValidatePosDim (object oldvalue, object newValue)
+		{
+			if (!IsInitialized || layoutStyle == LayoutStyle.Absolute || oldvalue == null || oldvalue.GetType () == newValue.GetType () || this is Toplevel) {
+				return true;
+			}
+			if (layoutStyle == LayoutStyle.Computed) {
+				if (oldvalue.GetType () != newValue.GetType () && !(newValue is Pos.PosAbsolute || newValue is Dim.DimAbsolute)) {
+					return true;
+				}
+			}
+			return false;
+		}
+
 		/// <summary>
 		/// Returns the container for this view, or null if this view has not been added to a container.
 		/// </summary>

+ 6 - 1
Terminal.Gui/Views/StatusBar.cs

@@ -88,13 +88,18 @@ namespace Terminal.Gui {
 			CanFocus = false;
 			ColorScheme = Colors.Menu;
 			X = 0;
-			Y = SuperView != null ? SuperView.Frame.Height - 1 : Pos.AnchorEnd (1);
 			Width = Dim.Fill ();
 			Height = 1;
 
+			Initialized += StatusBar_Initialized;
 			Application.Resized += Application_Resized ();
 		}
 
+		private void StatusBar_Initialized (object sender, EventArgs e)
+		{
+			Y = SuperView.Frame.Height - 1;
+		}
+
 		private Action<Application.ResizedEventArgs> Application_Resized ()
 		{
 			return delegate {

+ 34 - 0
UnitTests/DimTests.cs

@@ -233,6 +233,40 @@ namespace Terminal.Gui {
 			Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
 		}
 
+		[Fact]
+		public void Dim_Validation_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type ()
+		{
+			Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
+
+			var t = Application.Top;
+
+			var w = new Window ("w") {
+				Width = Dim.Fill (0),
+				Height = Dim.Sized (10)
+			};
+			var v = new View ("v") {
+				Width = Dim.Width (w) - 2,
+				Height = Dim.Percent (10)
+			};
+			w.Add (v);
+			t.Add (w);
+
+			t.Ready += () => {
+				w.Width = 2;
+				Assert.Equal (2, w.Width = 2);
+				w.Height = 2;
+				Assert.Equal (2, w.Height);
+				Assert.Throws<ArgumentException> (() => v.Width = 2);
+				Assert.Throws<ArgumentException> (() => v.Height = 2);
+			};
+
+			Application.Iteration += () => Application.RequestStop ();
+
+			Application.Run ();
+			Application.Shutdown ();
+
+		}
+
 		// TODO: Test operators
 	}
 }

+ 34 - 0
UnitTests/PosTests.cs

@@ -372,6 +372,40 @@ namespace Terminal.Gui {
 			Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
 		}
 
+		[Fact]
+		public void Pos_Validation_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type ()
+		{
+			Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
+
+			var t = Application.Top;
+
+			var w = new Window ("w") {
+				X = Pos.Left (t) + 2,
+				Y = Pos.At (2)
+			};
+			var v = new View ("v") {
+				X = Pos.Center (),
+				Y = Pos.Percent (10)
+			};
+			w.Add (v);
+			t.Add (w);
+
+			t.Ready += () => {
+				w.X = 2;
+				Assert.Equal (2, w.X = 2);
+				w.Y = 2;
+				Assert.Equal (2, w.Y);
+				Assert.Throws<ArgumentException> (() => v.X = 2);
+				Assert.Throws<ArgumentException> (() => v.Y = 2);
+			};
+
+			Application.Iteration += () => Application.RequestStop ();
+
+			Application.Run ();
+			Application.Shutdown ();
+
+		}
+
 		// TODO: Test PosCombine