Przeglądaj źródła

Changes GetTextFormatterBoundsSize and GetBoundsTextFormatterSize as public.

BDisp 3 lat temu
rodzic
commit
5b35b0a842
2 zmienionych plików z 73 dodań i 20 usunięć
  1. 26 18
      Terminal.Gui/Core/View.cs
  2. 47 2
      UnitTests/ViewTests.cs

+ 26 - 18
Terminal.Gui/Core/View.cs

@@ -522,7 +522,7 @@ namespace Terminal.Gui {
 				if (x is Pos.PosAbsolute) {
 					frame = new Rect (x.Anchor (0), frame.Y, frame.Width, frame.Height);
 				}
-				TextFormatter.Size = GetBoundsSize ();
+				TextFormatter.Size = GetBoundsTextFormatterSize ();
 				SetNeedsDisplay (frame);
 			}
 		}
@@ -546,7 +546,7 @@ namespace Terminal.Gui {
 				if (y is Pos.PosAbsolute) {
 					frame = new Rect (frame.X, y.Anchor (0), frame.Width, frame.Height);
 				}
-				TextFormatter.Size = GetBoundsSize ();
+				TextFormatter.Size = GetBoundsTextFormatterSize ();
 				SetNeedsDisplay (frame);
 			}
 		}
@@ -580,7 +580,7 @@ namespace Terminal.Gui {
 				if (width is Dim.DimAbsolute) {
 					frame = new Rect (frame.X, frame.Y, width.Anchor (0), frame.Height);
 				}
-				TextFormatter.Size = GetBoundsSize ();
+				TextFormatter.Size = GetBoundsTextFormatterSize ();
 				SetNeedsDisplay (frame);
 			}
 		}
@@ -610,7 +610,7 @@ namespace Terminal.Gui {
 				if (height is Dim.DimAbsolute) {
 					frame = new Rect (frame.X, frame.Y, frame.Width, height.Anchor (0));
 				}
-				TextFormatter.Size = GetBoundsSize ();
+				TextFormatter.Size = GetBoundsTextFormatterSize ();
 				SetNeedsDisplay (frame);
 			}
 		}
@@ -637,14 +637,14 @@ namespace Terminal.Gui {
 					if (Width == null || (Width is Dim.DimAbsolute && Width.Anchor (0) < colWidth)) {
 						width = colWidth;
 						Bounds = new Rect (Bounds.X, Bounds.Y, colWidth, Bounds.Height);
-						TextFormatter.Size = GetBoundsSize ();
+						TextFormatter.Size = GetBoundsTextFormatterSize ();
 					}
 					break;
 				default:
 					if (Height == null || (Height is Dim.DimAbsolute && Height.Anchor (0) == 0)) {
 						height = 1;
 						Bounds = new Rect (Bounds.X, Bounds.Y, Bounds.Width, 1);
-						TextFormatter.Size = GetBoundsSize ();
+						TextFormatter.Size = GetBoundsTextFormatterSize ();
 					}
 					break;
 				}
@@ -2210,7 +2210,7 @@ namespace Terminal.Gui {
 			Rect oldBounds = Bounds;
 			OnLayoutStarted (new LayoutEventArgs () { OldBounds = oldBounds });
 
-			TextFormatter.Size = GetBoundsSize ();
+			TextFormatter.Size = GetBoundsTextFormatterSize ();
 
 
 			// Sort out the dependencies of the X, Y, Width, Height properties
@@ -2316,11 +2316,11 @@ namespace Terminal.Gui {
 				TextFormatter.Text = value;
 				var prevSize = frame.Size;
 				var canResize = ResizeView (autoSize);
-				var txtFmtSize = GetTextFormatterSize ();
+				var txtFmtSize = GetTextFormatterBoundsSize ();
 				if (canResize && txtFmtSize != Bounds.Size) {
 					Bounds = new Rect (new Point (Bounds.X, Bounds.Y), txtFmtSize);
 				} else if (!canResize && txtFmtSize != Bounds.Size) {
-					TextFormatter.Size = GetBoundsSize ();
+					TextFormatter.Size = GetBoundsTextFormatterSize ();
 				}
 				SetMinWidthHeight ();
 				SetNeedsLayout ();
@@ -2397,7 +2397,7 @@ namespace Terminal.Gui {
 						SetWidthHeight (b);
 					}
 
-					TextFormatter.Size = GetBoundsSize ();
+					TextFormatter.Size = GetBoundsTextFormatterSize ();
 					SetNeedsDisplay ();
 				}
 			}
@@ -2498,7 +2498,7 @@ namespace Terminal.Gui {
 			if (TextFormatter.Size != nBounds.Size) {
 				TextFormatter.Size = nBounds.Size;
 			}
-			var fmtSize = GetTextFormatterSize ();
+			var fmtSize = GetTextFormatterBoundsSize ();
 			if ((fmtSize != Bounds.Size || fmtSize != nBounds.Size)
 				&& (((width == null || width is Dim.DimAbsolute) && (Bounds.Width == 0
 				|| autoSize && Bounds.Width != nBounds.Width))
@@ -2526,7 +2526,7 @@ namespace Terminal.Gui {
 			}
 			if (aSize) {
 				Bounds = new Rect (Bounds.X, Bounds.Y, canSizeW ? rW : Bounds.Width, canSizeH ? rH : Bounds.Height);
-				TextFormatter.Size = GetBoundsSize ();
+				TextFormatter.Size = GetBoundsTextFormatterSize ();
 			}
 
 			return aSize;
@@ -2564,23 +2564,31 @@ namespace Terminal.Gui {
 				&& TextFormatter.Text?.Contains (HotKeySpecifier) == true ? 1 : 0));
 		}
 
-		Size GetTextFormatterSize ()
+		/// <summary>
+		/// Gets the bounds size from a <see cref="TextFormatter.Size"/>.
+		/// </summary>
+		/// <returns>The bounds size minus the <see cref="TextFormatter.HotKeySpecifier"/> length.</returns>
+		public Size GetTextFormatterBoundsSize ()
 		{
 			return new Size (TextFormatter.Size.Width - (TextFormatter.IsHorizontalDirection (TextDirection)
-				&& TextFormatter.Text.Contains (HotKeySpecifier) ? 1 : 0),
+				&& TextFormatter.Text.Contains (HotKeySpecifier) ? Math.Max (Rune.ColumnWidth (HotKeySpecifier), 0) : 0),
 				TextFormatter.Size.Height - (TextFormatter.IsVerticalDirection (TextDirection)
-				&& TextFormatter.Text.Contains (HotKeySpecifier) ? 1 : 0));
+				&& TextFormatter.Text.Contains (HotKeySpecifier) ? Math.Max (Rune.ColumnWidth (HotKeySpecifier), 0) : 0));
 		}
 
-		Size GetBoundsSize ()
+		/// <summary>
+		/// Gets the text formatter size from a <see cref="Bounds"/> size.
+		/// </summary>
+		/// <returns>The text formatter size more the <see cref="TextFormatter.HotKeySpecifier"/> length.</returns>
+		public Size GetBoundsTextFormatterSize ()
 		{
 			if (TextFormatter.Text == null)
 				return Bounds.Size;
 
 			return new Size (frame.Size.Width + (TextFormatter.IsHorizontalDirection (TextDirection)
-					&& TextFormatter.Text.Contains (HotKeySpecifier) ? 1 : 0),
+					&& TextFormatter.Text.Contains (HotKeySpecifier) ? Math.Max (Rune.ColumnWidth (HotKeySpecifier), 0) : 0),
 				frame.Size.Height + (TextFormatter.IsVerticalDirection (TextDirection)
-				&& TextFormatter.Text.Contains (HotKeySpecifier) ? 1 : 0));
+				&& TextFormatter.Text.Contains (HotKeySpecifier) ? Math.Max (Rune.ColumnWidth (HotKeySpecifier), 0) : 0));
 		}
 
 		/// <summary>

+ 47 - 2
UnitTests/ViewTests.cs

@@ -2753,8 +2753,8 @@ Y
 			Application.Begin (Application.Top);
 			((FakeDriver)Application.Driver).SetBufferSize (22, 22);
 
-			Assert.True(horizontalView.AutoSize);
-			Assert.True(verticalView.AutoSize);
+			Assert.True (horizontalView.AutoSize);
+			Assert.True (verticalView.AutoSize);
 			Assert.Equal (new Size (10, 1), horizontalView.TextFormatter.Size);
 			Assert.Equal (new Size (2, 9), verticalView.TextFormatter.Size);
 			Assert.Equal (new Rect (0, 0, 9, 1), horizontalView.Frame);
@@ -2867,5 +2867,50 @@ Y
 
 			GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
 		}
+
+		[Fact]
+		public void GetTextFormatterBoundsSize_GetBoundsTextFormatterSize_HotKeySpecifier ()
+		{
+			var text = "Say Hello 你";
+			var horizontalView = new View () { Text = text, AutoSize = true, HotKeySpecifier = '_' };
+			var verticalView = new View () {
+				Text = text,
+				AutoSize = true,
+				HotKeySpecifier = '_',
+				TextDirection = TextDirection.TopBottom_LeftRight
+			};
+
+			Assert.True (horizontalView.AutoSize);
+			Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
+			Assert.Equal (new Size (12, 1), horizontalView.GetTextFormatterBoundsSize ());
+			Assert.Equal (new Size (12, 1), horizontalView.GetBoundsTextFormatterSize ());
+			Assert.Equal (horizontalView.TextFormatter.Size, horizontalView.GetBoundsTextFormatterSize ());
+			Assert.Equal (horizontalView.Frame.Size, horizontalView.GetTextFormatterBoundsSize ());
+
+			Assert.True (verticalView.AutoSize);
+			Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
+			Assert.Equal (new Size (2, 11), verticalView.GetTextFormatterBoundsSize ());
+			Assert.Equal (new Size (2, 11), verticalView.GetBoundsTextFormatterSize ());
+			Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetBoundsTextFormatterSize ());
+			Assert.Equal (verticalView.Frame.Size, verticalView.GetTextFormatterBoundsSize ());
+
+			text = "Say He_llo 你";
+			horizontalView.Text = text;
+			verticalView.Text = text;
+
+			Assert.True (horizontalView.AutoSize);
+			Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
+			Assert.Equal (new Size (12, 1), horizontalView.GetTextFormatterBoundsSize ());
+			Assert.Equal (new Size (13, 1), horizontalView.GetBoundsTextFormatterSize ());
+			Assert.Equal (horizontalView.TextFormatter.Size, horizontalView.GetBoundsTextFormatterSize ());
+			Assert.Equal (horizontalView.Frame.Size, horizontalView.GetTextFormatterBoundsSize ());
+
+			Assert.True (verticalView.AutoSize);
+			Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
+			Assert.Equal (new Size (2, 11), verticalView.GetTextFormatterBoundsSize ());
+			Assert.Equal (new Size (2, 12), verticalView.GetBoundsTextFormatterSize ());
+			Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetBoundsTextFormatterSize ());
+			Assert.Equal (verticalView.Frame.Size, verticalView.GetTextFormatterBoundsSize ());
+		}
 	}
 }