Browse Source

Fixes changing focus using backtab (#243)

- Focus is now moved correctly when using tab and backtab
Martin Björkström 6 years ago
parent
commit
bacf7cb688
1 changed files with 28 additions and 10 deletions
  1. 28 10
      Terminal.Gui/Core.cs

+ 28 - 10
Terminal.Gui/Core.cs

@@ -219,8 +219,24 @@ namespace Terminal.Gui {
 	/// </para>
 	/// </para>
 	/// </remarks>
 	/// </remarks>
 	public class View : Responder, IEnumerable {
 	public class View : Responder, IEnumerable {
+		internal enum Direction {
+			Forward,
+			Backward
+		}
+
 		View container = null;
 		View container = null;
 		View focused = null;
 		View focused = null;
+		Direction focusDirection;
+
+		internal Direction FocusDirection {
+			get => SuperView?.FocusDirection ?? focusDirection;
+			set {
+				if (SuperView != null)
+					SuperView.FocusDirection = value;
+				else
+					focusDirection = value;
+			}
+		}
 
 
 		/// <summary>
 		/// <summary>
 		/// Points to the current driver in use by the view, it is a convenience property
 		/// Points to the current driver in use by the view, it is a convenience property
@@ -900,7 +916,10 @@ namespace Terminal.Gui {
 		public void EnsureFocus ()
 		public void EnsureFocus ()
 		{
 		{
 			if (focused == null)
 			if (focused == null)
-				FocusFirst ();
+				if (FocusDirection == Direction.Forward)
+					FocusFirst ();
+				else
+					FocusLast ();
 		}
 		}
 
 
 		/// <summary>
 		/// <summary>
@@ -926,8 +945,10 @@ namespace Terminal.Gui {
 		/// </summary>
 		/// </summary>
 		public void FocusLast ()
 		public void FocusLast ()
 		{
 		{
-			if (subviews == null)
+			if (subviews == null) {
+				SuperView?.SetFocus(this);
 				return;
 				return;
+			}
 
 
 			for (int i = subviews.Count; i > 0;) {
 			for (int i = subviews.Count; i > 0;) {
 				i--;
 				i--;
@@ -946,12 +967,13 @@ namespace Terminal.Gui {
 		/// <returns><c>true</c>, if previous was focused, <c>false</c> otherwise.</returns>
 		/// <returns><c>true</c>, if previous was focused, <c>false</c> otherwise.</returns>
 		public bool FocusPrev ()
 		public bool FocusPrev ()
 		{
 		{
+			FocusDirection = Direction.Backward;
 			if (subviews == null || subviews.Count == 0)
 			if (subviews == null || subviews.Count == 0)
 				return false;
 				return false;
 
 
 			if (focused == null) {
 			if (focused == null) {
 				FocusLast ();
 				FocusLast ();
-				return true;
+				return focused != null;
 			}
 			}
 			int focused_idx = -1;
 			int focused_idx = -1;
 			for (int i = subviews.Count; i > 0;) {
 			for (int i = subviews.Count; i > 0;) {
@@ -967,18 +989,13 @@ namespace Terminal.Gui {
 				if (w.CanFocus && focused_idx != -1) {
 				if (w.CanFocus && focused_idx != -1) {
 					focused.HasFocus = false;
 					focused.HasFocus = false;
 
 
-					if (w.CanFocus)
+					if (w != null && w.CanFocus)
 						w.FocusLast ();
 						w.FocusLast ();
 
 
 					SetFocus (w);
 					SetFocus (w);
-					return true;
-				}
-			}
-			if (focused_idx != -1) {
-				FocusLast ();
 				return true;
 				return true;
+				}
 			}
 			}
-
 			if (focused != null) {
 			if (focused != null) {
 				focused.HasFocus = false;
 				focused.HasFocus = false;
 				focused = null;
 				focused = null;
@@ -992,6 +1009,7 @@ namespace Terminal.Gui {
 		/// <returns><c>true</c>, if next was focused, <c>false</c> otherwise.</returns>
 		/// <returns><c>true</c>, if next was focused, <c>false</c> otherwise.</returns>
 		public bool FocusNext ()
 		public bool FocusNext ()
 		{
 		{
+			FocusDirection = Direction.Forward;
 			if (subviews == null || subviews.Count == 0)
 			if (subviews == null || subviews.Count == 0)
 				return false;
 				return false;