瀏覽代碼

Ensures that the ScrollBarView don't have two vertical or horizontal ScrollBarView.

BDisp 4 年之前
父節點
當前提交
146d23422b
共有 2 個文件被更改,包括 36 次插入1 次删除
  1. 10 1
      Terminal.Gui/Views/ScrollBarView.cs
  2. 26 0
      UnitTests/ScrollBarViewTests.cs

+ 10 - 1
Terminal.Gui/Views/ScrollBarView.cs

@@ -29,6 +29,7 @@ namespace Terminal.Gui {
 		bool autoHideScrollBars = true;
 		Dim originalHostWidth, originalHostHeight;
 		bool hosted;
+		ScrollBarView otherScrollBarView;
 		bool showBothScrollIndicator => OtherScrollBarView != null && OtherScrollBarView.showScrollIndicator && showScrollIndicator;
 
 		/// <summary>
@@ -156,7 +157,15 @@ namespace Terminal.Gui {
 		/// <summary>
 		/// Represent a vertical or horizontal ScrollBarView other than this.
 		/// </summary>
-		public ScrollBarView OtherScrollBarView { get; set; }
+		public ScrollBarView OtherScrollBarView {
+			get => otherScrollBarView;
+			set {
+				if (value.IsVertical && vertical || !value.IsVertical && !vertical) {
+					throw new ArgumentException ($"There is already a {(vertical ? "vertical" : "horizontal")} ScrollBarView.");
+				}
+				otherScrollBarView = value;
+			}
+		}
 
 		/// <summary>
 		/// Gets or sets the visibility for the vertical or horizontal scroll indicator.

+ 26 - 0
UnitTests/ScrollBarViewTests.cs

@@ -104,6 +104,32 @@ namespace Terminal.Gui {
 				() => new ScrollBarView (new View (), false));
 		}
 
+		[Fact]
+		public void Hosting_Two_Vertical_ScrollBarView_Throws_ArgumentException ()
+		{
+			var top = new Toplevel ();
+			var host = new View ();
+			top.Add (host);
+			var v = new ScrollBarView (host, true);
+			var h = new ScrollBarView (host, true);
+
+			Assert.Throws<ArgumentException> (null, () => v.OtherScrollBarView = h);
+			Assert.Throws<ArgumentException> (null, () => h.OtherScrollBarView = v);
+		}
+
+		[Fact]
+		public void Hosting_Two_Horizontal_ScrollBarView_Throws_ArgumentException ()
+		{
+			var top = new Toplevel ();
+			var host = new View ();
+			top.Add (host);
+			var v = new ScrollBarView (host, false);
+			var h = new ScrollBarView (host, false);
+
+			Assert.Throws<ArgumentException> (null, () => v.OtherScrollBarView = h);
+			Assert.Throws<ArgumentException> (null, () => h.OtherScrollBarView = v);
+		}
+
 		[Fact]
 		public void Hosting_A_View_To_A_ScrollBarView ()
 		{