فهرست منبع

Merge branch 'main' of tig:migueldeicaza/gui.cs

Charlie Kindel 4 سال پیش
والد
کامیت
15823105f2

+ 9 - 0
Terminal.Gui/Core/Application.cs

@@ -110,6 +110,15 @@ namespace Terminal.Gui {
 			}
 		}
 
+		/// <summary>
+		/// Alternative key to navigate forwards through all views. Ctrl+Tab is always used.
+		/// </summary>
+		public static Key AlternateForwardKey { get; set; } = Key.PageDown | Key.CtrlMask;
+		/// <summary>
+		/// Alternative key to navigate backwards through all views. Shift+Ctrl+Tab is always used.
+		/// </summary>
+		public static Key AlternateBackwardKey { get; set; } = Key.PageUp | Key.CtrlMask;
+
 		/// <summary>
 		/// The <see cref="MainLoop"/>  driver for the application
 		/// </summary>

+ 43 - 10
Terminal.Gui/Core/PosDim.cs

@@ -242,13 +242,12 @@ namespace Terminal.Gui {
 		/// <returns>The <see cref="Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
 		public static Pos operator + (Pos left, Pos right)
 		{
-			PosCombine newPos = new PosCombine (true, left, right);
-			if (posCombine?.ToString () != newPos.ToString ()) {
-				var view = left as PosView;
-				if (view != null) {
-					view.Target.SetNeedsLayout ();
-				}
+			if (left is PosAbsolute && right is PosAbsolute) {
+				posCombine = null;
+				return new PosAbsolute (left.Anchor (0) + right.Anchor (0));
 			}
+			PosCombine newPos = new PosCombine (true, left, right);
+			SetPosCombine (left, newPos);
 			return posCombine = newPos;
 		}
 
@@ -260,13 +259,23 @@ namespace Terminal.Gui {
 		/// <returns>The <see cref="Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
 		public static Pos operator - (Pos left, Pos right)
 		{
+			if (left is PosAbsolute && right is PosAbsolute) {
+				posCombine = null;
+				return new PosAbsolute (left.Anchor (0) - right.Anchor (0));
+			}
 			PosCombine newPos = new PosCombine (false, left, right);
+			SetPosCombine (left, newPos);
+			return posCombine = newPos;
+		}
+
+		static void SetPosCombine (Pos left, PosCombine newPos)
+		{
 			if (posCombine?.ToString () != newPos.ToString ()) {
 				var view = left as PosView;
-				if (view != null)
+				if (view != null) {
 					view.Target.SetNeedsLayout ();
+				}
 			}
-			return posCombine = newPos;
 		}
 
 		internal class PosView : Pos {
@@ -526,6 +535,8 @@ namespace Terminal.Gui {
 
 		}
 
+		static DimCombine dimCombine;
+
 		/// <summary>
 		/// Adds a <see cref="Terminal.Gui.Dim"/> to a <see cref="Terminal.Gui.Dim"/>, yielding a new <see cref="Dim"/>.
 		/// </summary>
@@ -534,7 +545,13 @@ namespace Terminal.Gui {
 		/// <returns>The <see cref="Dim"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
 		public static Dim operator + (Dim left, Dim right)
 		{
-			return new DimCombine (true, left, right);
+			if (left is DimAbsolute && right is DimAbsolute) {
+				dimCombine = null;
+				return new DimAbsolute (left.Anchor (0) + right.Anchor (0));
+			}
+			DimCombine newDim = new DimCombine (true, left, right);
+			SetDimCombine (left, newDim);
+			return dimCombine = newDim;
 		}
 
 		/// <summary>
@@ -545,7 +562,23 @@ namespace Terminal.Gui {
 		/// <returns>The <see cref="Dim"/> that is the <c>left</c> minus <c>right</c>.</returns>
 		public static Dim operator - (Dim left, Dim right)
 		{
-			return new DimCombine (false, left, right);
+			if (left is DimAbsolute && right is DimAbsolute) {
+				dimCombine = null;
+				return new DimAbsolute (left.Anchor (0) - right.Anchor (0));
+			}
+			DimCombine newDim = new DimCombine (false, left, right);
+			SetDimCombine (left, newDim);
+			return dimCombine = newDim;
+		}
+
+		static void SetDimCombine (Dim left, DimCombine newPos)
+		{
+			if (dimCombine?.ToString () != newPos.ToString ()) {
+				var view = left as DimView;
+				if (view != null) {
+					view.Target.SetNeedsLayout ();
+				}
+			}
 		}
 
 		internal class DimView : Dim {

+ 8 - 0
Terminal.Gui/Core/Toplevel.cs

@@ -238,10 +238,18 @@ namespace Terminal.Gui {
 				}
 				return true;
 			case Key.Tab | Key.CtrlMask:
+			case Key key when key == Application.AlternateForwardKey: // Needed on Unix
 				Application.Top.FocusNext ();
+				if (Application.Top.Focused == null) {
+					Application.Top.FocusNext ();
+				}
 				return true;
 			case Key.Tab | Key.ShiftMask | Key.CtrlMask:
+			case Key key when key == Application.AlternateBackwardKey: // Needed on Unix
 				Application.Top.FocusPrev ();
+				if (Application.Top.Focused == null) {
+					Application.Top.FocusPrev ();
+				}
 				return true;
 			case Key.L | Key.CtrlMask:
 				Application.Refresh ();

+ 1 - 1
Terminal.Gui/Views/TextView.cs

@@ -2017,7 +2017,7 @@ namespace Terminal.Gui {
 					StopSelecting ();
 				}
 				int nPageDnShift = Frame.Height - 1;
-				if (currentRow > 0 && currentRow < model.Count) {
+				if (currentRow >= 0 && currentRow < model.Count) {
 					if (columnTrack == -1)
 						columnTrack = currentColumn;
 					currentRow = (currentRow + nPageDnShift) > model.Count

+ 104 - 3
UnitTests/ApplicationTests.cs

@@ -4,7 +4,7 @@ using System.Threading;
 using System.Threading.Tasks;
 using Xunit;
 
-// Alais Console to MockConsole so we don't accidentally use Console
+// Alias Console to MockConsole so we don't accidentally use Console
 using Console = Terminal.Gui.FakeConsole;
 
 namespace Terminal.Gui.Core {
@@ -19,7 +19,7 @@ namespace Terminal.Gui.Core {
 		[Fact]
 		public void Init_Shutdown_Cleans_Up ()
 		{
-			// Verify inital state is per spec
+			// Verify initial state is per spec
 			Pre_Init_State ();
 
 			Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
@@ -175,7 +175,7 @@ namespace Terminal.Gui.Core {
 			// Setup Mock driver
 			Init ();
 
-			// Setup some fake kepresses (This)
+			// Setup some fake keypresses (This)
 			var input = "Tests";
 
 			// Put a control-q in at the end
@@ -268,5 +268,106 @@ namespace Terminal.Gui.Core {
 			Application.Shutdown ();
 			Assert.Null (SynchronizationContext.Current);
 		}
+
+		[Fact]
+		public void AlternateForwardKey_AlternateBackwardKey_Tests ()
+		{
+			Init ();
+
+			var top = Application.Top;
+			var w1 = new Window ();
+			var v1 = new TextField ();
+			var v2 = new TextView ();
+			w1.Add (v1, v2);
+
+			var w2 = new Window ();
+			var v3 = new CheckBox ();
+			var v4 = new Button ();
+			w2.Add (v3, v4);
+
+			top.Add (w1, w2);
+
+			Application.Iteration += () => {
+				Assert.True (v1.HasFocus);
+				// Using default keys.
+				top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
+					new KeyModifiers () { Ctrl = true }));
+				Assert.True (v2.HasFocus);
+				top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
+					new KeyModifiers () { Ctrl = true }));
+				Assert.True (v3.HasFocus);
+				top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
+					new KeyModifiers () { Ctrl = true }));
+				Assert.True (v4.HasFocus);
+				top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
+					new KeyModifiers () { Ctrl = true }));
+				Assert.True (v1.HasFocus);
+
+				top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
+					new KeyModifiers () { Shift = true, Ctrl = true }));
+				Assert.True (v4.HasFocus);
+				top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
+					new KeyModifiers () { Shift = true, Ctrl = true }));
+				Assert.True (v3.HasFocus);
+				top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
+					new KeyModifiers () { Shift = true, Ctrl = true }));
+				Assert.True (v2.HasFocus);
+				top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
+					new KeyModifiers () { Shift = true, Ctrl = true }));
+				Assert.True (v1.HasFocus);
+
+				top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
+					new KeyModifiers () { Ctrl = true }));
+				Assert.True (v2.HasFocus);
+				top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
+					new KeyModifiers () { Ctrl = true }));
+				Assert.True (v3.HasFocus);
+				top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
+					new KeyModifiers () { Ctrl = true }));
+				Assert.True (v4.HasFocus);
+				top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
+					new KeyModifiers () { Ctrl = true }));
+				Assert.True (v1.HasFocus);
+
+				top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
+					new KeyModifiers () { Ctrl = true }));
+				Assert.True (v4.HasFocus);
+				top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
+					new KeyModifiers () { Ctrl = true }));
+				Assert.True (v3.HasFocus);
+				top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
+					new KeyModifiers () { Ctrl = true }));
+				Assert.True (v2.HasFocus);
+				top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
+					new KeyModifiers () { Ctrl = true }));
+				Assert.True (v1.HasFocus);
+
+				// Using another's alternate keys.
+				Application.AlternateForwardKey = Key.F7;
+				Application.AlternateBackwardKey = Key.F6;
+
+				top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
+				Assert.True (v2.HasFocus);
+				top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
+				Assert.True (v3.HasFocus);
+				top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
+				Assert.True (v4.HasFocus);
+				top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
+				Assert.True (v1.HasFocus);
+
+				top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
+				Assert.True (v4.HasFocus);
+				top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
+				Assert.True (v3.HasFocus);
+				top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
+				Assert.True (v2.HasFocus);
+				top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
+				Assert.True (v1.HasFocus);
+
+				Application.RequestStop ();
+			};
+
+			Application.Run (top);
+		}
 	}
 }

+ 104 - 0
UnitTests/DimTests.cs

@@ -590,5 +590,109 @@ namespace Terminal.Gui.Core {
 			Assert.Throws<InvalidOperationException> (() => Application.Run ());
 			Application.Shutdown ();
 		}
+
+
+		[Fact]
+		public void Dim_Add_Operator ()
+		{
+
+			Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
+
+			var top = Application.Top;
+
+			var view = new View () { X = 0, Y = 0, Width = 20, Height = 0 };
+			var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
+			var count = 0;
+
+			field.KeyDown += (k) => {
+				if (k.KeyEvent.Key == Key.Enter) {
+					field.Text = $"Label {count}";
+					var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
+					view.Add (label);
+					Assert.Equal ($"Label {count}", label.Text);
+					Assert.Equal ($"Pos.Absolute({count})", label.Y.ToString ());
+
+					Assert.Equal ($"Dim.Absolute({count})", view.Height.ToString ());
+					view.Height += 1;
+					count++;
+					Assert.Equal ($"Dim.Absolute({count})", view.Height.ToString ());
+				}
+			};
+
+			Application.Iteration += () => {
+				while (count < 20) {
+					field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
+				}
+
+				Application.RequestStop ();
+			};
+
+			var win = new Window ();
+			win.Add (view);
+			win.Add (field);
+
+			top.Add (win);
+
+			Application.Run (top);
+
+			Assert.Equal (20, count);
+		}
+
+		[Fact]
+		public void Dim_Subtract_Operator ()
+		{
+
+			Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
+
+			var top = Application.Top;
+
+			var view = new View () { X = 0, Y = 0, Width = 20, Height = 0 };
+			var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
+			var count = 20;
+			var listLabels = new List<Label> ();
+
+			for (int i = 0; i < count; i++) {
+				field.Text = $"Label {i}";
+				var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
+				view.Add (label);
+				Assert.Equal ($"Label {i}", label.Text);
+				Assert.Equal ($"Pos.Absolute({i})", label.Y.ToString ());
+				listLabels.Add (label);
+
+				Assert.Equal ($"Dim.Absolute({i})", view.Height.ToString ());
+				view.Height += 1;
+				Assert.Equal ($"Dim.Absolute({i + 1})", view.Height.ToString ());
+			}
+
+			field.KeyDown += (k) => {
+				if (k.KeyEvent.Key == Key.Enter) {
+					Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
+					view.Remove (listLabels [count - 1]);
+
+					Assert.Equal ($"Dim.Absolute({count})", view.Height.ToString ());
+					view.Height -= 1;
+					count--;
+					Assert.Equal ($"Dim.Absolute({count})", view.Height.ToString ());
+				}
+			};
+
+			Application.Iteration += () => {
+				while (count > 0) {
+					field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
+				}
+
+				Application.RequestStop ();
+			};
+
+			var win = new Window ();
+			win.Add (view);
+			win.Add (field);
+
+			top.Add (win);
+
+			Application.Run (top);
+
+			Assert.Equal (0, count);
+		}
 	}
 }

+ 103 - 0
UnitTests/PosTests.cs

@@ -536,5 +536,108 @@ namespace Terminal.Gui.Core {
 			Assert.Throws<InvalidOperationException> (() => Application.Run ());
 			Application.Shutdown ();
 		}
+
+		[Fact]
+		public void Pos_Add_Operator ()
+		{
+
+			Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
+
+			var top = Application.Top;
+
+			var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
+			var field = new TextField () { X = 0, Y = 0, Width = 20 };
+			var count = 0;
+
+			field.KeyDown += (k) => {
+				if (k.KeyEvent.Key == Key.Enter) {
+					field.Text = $"Label {count}";
+					var label = new Label (field.Text) { X = 0, Y = field.Y, Width = 20 };
+					view.Add (label);
+					Assert.Equal ($"Label {count}", label.Text);
+					Assert.Equal ($"Pos.Absolute({count})", label.Y.ToString ());
+
+					Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
+					field.Y += 1;
+					count++;
+					Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
+				}
+			};
+
+			Application.Iteration += () => {
+				while (count < 20) {
+					field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
+				}
+
+				Application.RequestStop ();
+			};
+
+			var win = new Window ();
+			win.Add (view);
+			win.Add (field);
+
+			top.Add (win);
+
+			Application.Run (top);
+
+			Assert.Equal (20, count);
+		}
+
+		[Fact]
+		public void Pos_Subtract_Operator ()
+		{
+
+			Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
+
+			var top = Application.Top;
+
+			var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
+			var field = new TextField () { X = 0, Y = 0, Width = 20 };
+			var count = 20;
+			var listLabels = new List<Label> ();
+
+			for (int i = 0; i < count; i++) {
+				field.Text = $"Label {i}";
+				var label = new Label (field.Text) { X = 0, Y = field.Y, Width = 20 };
+				view.Add (label);
+				Assert.Equal ($"Label {i}", label.Text);
+				Assert.Equal ($"Pos.Absolute({i})", field.Y.ToString ());
+				listLabels.Add (label);
+
+				Assert.Equal ($"Pos.Absolute({i})", field.Y.ToString ());
+				field.Y += 1;
+				Assert.Equal ($"Pos.Absolute({i + 1})", field.Y.ToString ());
+			}
+
+			field.KeyDown += (k) => {
+				if (k.KeyEvent.Key == Key.Enter) {
+					Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
+					view.Remove (listLabels [count - 1]);
+
+					Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
+					field.Y -= 1;
+					count--;
+					Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
+				}
+			};
+
+			Application.Iteration += () => {
+				while (count > 0) {
+					field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
+				}
+
+				Application.RequestStop ();
+			};
+
+			var win = new Window ();
+			win.Add (view);
+			win.Add (field);
+
+			top.Add (win);
+
+			Application.Run (top);
+
+			Assert.Equal (0, count);
+		}
 	}
 }