Browse Source

Moving common methods to the TextModel class.

BDisp 4 years ago
parent
commit
fe311ea54b
2 changed files with 36 additions and 34 deletions
  1. 4 30
      Terminal.Gui/Views/TextField.cs
  2. 32 4
      Terminal.Gui/Views/TextView.cs

+ 4 - 30
Terminal.Gui/Views/TextField.cs

@@ -195,7 +195,7 @@ namespace Terminal.Gui {
 				if (idx == point)
 					break;
 				var cols = Rune.ColumnWidth (text [idx]);
-				col = SetCol (col, Frame.Width - 1, cols);
+				col = TextModel.SetCol (col, Frame.Width - 1, cols);
 			}
 			Move (col, 0);
 		}
@@ -227,7 +227,7 @@ namespace Terminal.Gui {
 				if (col + cols <= width) {
 					Driver.AddRune ((Rune)(Secret ? '*' : rune));
 				}
-				col = SetCol (col, width, cols);
+				col = TextModel.SetCol (col, width, cols);
 				if (idx + 1 < tcount && col + Rune.ColumnWidth (text [idx + 1]) > width) {
 					break;
 				}
@@ -241,15 +241,6 @@ namespace Terminal.Gui {
 			PositionCursor ();
 		}
 
-		internal static int SetCol (int col, int width, int cols)
-		{
-			if (col + cols <= width) {
-				col += cols;
-			}
-
-			return col;
-		}
-
 		// Returns the size and length in a range of the string.
 		(int size, int length) DisplaySize (List<Rune> t, int start = -1, int end = -1, bool checkNextRune = true)
 		{
@@ -779,7 +770,7 @@ namespace Terminal.Gui {
 		{
 			// We could also set the cursor position.
 			int x;
-			var pX = GetPointFromX (text, first, ev.X);
+			var pX = TextModel.GetColFromX (text, first, ev.X);
 			if (text.Count == 0) {
 				x = pX - ev.OfX;
 			} else {
@@ -792,7 +783,7 @@ namespace Terminal.Gui {
 		{
 			int pX = x;
 			if (getX) {
-				pX = GetPointFromX (text, first, x);
+				pX = TextModel.GetColFromX (text, first, x);
 			}
 			if (first + pX > text.Count) {
 				point = text.Count;
@@ -805,23 +796,6 @@ namespace Terminal.Gui {
 			return point;
 		}
 
-		internal static int GetPointFromX (List<Rune> t, int start, int x)
-		{
-			if (x < 0) {
-				return x;
-			}
-			int size = start;
-			var pX = x + start;
-			for (int i = start; i < t.Count; i++) {
-				var r = t [i];
-				size += Rune.ColumnWidth (r);
-				if (i == pX || (size > pX)) {
-					return i - start;
-				}
-			}
-			return t.Count - start;
-		}
-
 		void PrepareSelection (int x, int direction = 0)
 		{
 			x = x + first < 0 ? 0 : x;

+ 32 - 4
Terminal.Gui/Views/TextView.cs

@@ -171,6 +171,34 @@ namespace Terminal.Gui {
 		{
 			lines.RemoveAt (pos);
 		}
+
+		internal static int SetCol (int col, int width, int cols)
+		{
+			if (col + cols <= width) {
+				col += cols;
+			}
+
+			return col;
+		}
+
+		internal static int GetColFromX (List<Rune> t, int start, int x)
+		{
+			if (x < 0) {
+				return x;
+			}
+			int size = start;
+			var pX = x + start;
+			for (int i = start; i < t.Count; i++) {
+				var r = t [i];
+				size += Rune.ColumnWidth (r);
+				if (i == pX || (size > pX)) {
+					return i - start;
+				}
+			}
+			return t.Count - start;
+		}
+
+
 	}
 
 	/// <summary>
@@ -590,7 +618,7 @@ namespace Terminal.Gui {
 					if (!SpecialRune (rune)) {
 						AddRune (col, row, rune);
 					}
-					col = TextField.SetCol (col, bounds.Right, cols);
+					col = TextModel.SetCol (col, bounds.Right, cols);
 				}
 			}
 			PositionCursor ();
@@ -658,7 +686,7 @@ namespace Terminal.Gui {
 
 			var line = GetCurrentLine ();
 
-			// Optmize single line
+			// Optimize single line
 			if (lines.Count == 1) {
 				line.InsertRange (currentColumn, lines [0]);
 				currentColumn += lines [0].Count;
@@ -683,7 +711,7 @@ namespace Terminal.Gui {
 			var lastp = last.Count;
 			last.InsertRange (last.Count, rest);
 
-			// Now adjjust column and row positions
+			// Now adjust column and row positions
 			currentRow += lines.Count - 1;
 			currentColumn = lastp;
 			if (currentRow - topRow > Frame.Height) {
@@ -1275,7 +1303,7 @@ namespace Terminal.Gui {
 						currentRow = ev.Y + topRow;
 					}
 					var r = GetCurrentLine ();
-					var idx = TextField.GetPointFromX (r, leftColumn, ev.X);
+					var idx = TextModel.GetColFromX (r, leftColumn, ev.X);
 					if (idx - leftColumn >= r.Count) {
 						currentColumn = r.Count - leftColumn;
 					} else {