Browse Source

Fixes #742. Added a TabIndex to the View.

BDisp 5 years ago
parent
commit
f7a2c9e35f
1 changed files with 28 additions and 12 deletions
  1. 28 12
      Terminal.Gui/Core/View.cs

+ 28 - 12
Terminal.Gui/Core/View.cs

@@ -200,6 +200,15 @@ namespace Terminal.Gui {
 		// to make the same mistakes our users make when they poke at the Subviews.
 		internal IList<View> InternalSubviews => subviews ?? empty;
 
+		// This is null, and allocated on demand.
+		List<View> tabIndex;
+
+		/// <summary>
+		/// This returns a tab index list of the subviews contained by this view.
+		/// </summary>
+		/// <value>The tabIndex.</value>
+		public IList<View> TabIndex => tabIndex == null ? empty : tabIndex.AsReadOnly ();
+
 		internal Rect NeedDisplay { get; private set; } = Rect.Empty;
 
 		// The frame for the object. Superview relative.
@@ -558,9 +567,14 @@ namespace Terminal.Gui {
 		{
 			if (view == null)
 				return;
-			if (subviews == null)
+			if (subviews == null) {
 				subviews = new List<View> ();
+			}
+			if (tabIndex == null) {
+				tabIndex = new List<View> ();
+			}
 			subviews.Add (view);
+			tabIndex.Add (view);
 			view.container = this;
 			OnAdded (view);
 			if (view.CanFocus)
@@ -594,6 +608,7 @@ namespace Terminal.Gui {
 
 			while (subviews.Count > 0) {
 				Remove (subviews [0]);
+				Remove (tabIndex [0]);
 			}
 		}
 
@@ -611,6 +626,7 @@ namespace Terminal.Gui {
 			SetNeedsDisplay ();
 			var touched = view.Frame;
 			subviews.Remove (view);
+			tabIndex.Remove (view);
 			view.container = null;
 			OnRemoved (view);
 
@@ -1301,12 +1317,12 @@ namespace Terminal.Gui {
 		/// </summary>
 		public void FocusFirst ()
 		{
-			if (subviews == null) {
+			if (tabIndex == null) {
 				SuperView?.SetFocus (this);
 				return;
 			}
 
-			foreach (var view in subviews) {
+			foreach (var view in tabIndex) {
 				if (view.CanFocus) {
 					SetFocus (view);
 					return;
@@ -1319,15 +1335,15 @@ namespace Terminal.Gui {
 		/// </summary>
 		public void FocusLast ()
 		{
-			if (subviews == null) {
+			if (tabIndex == null) {
 				SuperView?.SetFocus (this);
 				return;
 			}
 
-			for (int i = subviews.Count; i > 0;) {
+			for (int i = tabIndex.Count; i > 0;) {
 				i--;
 
-				View v = subviews [i];
+				View v = tabIndex [i];
 				if (v.CanFocus) {
 					SetFocus (v);
 					return;
@@ -1342,7 +1358,7 @@ namespace Terminal.Gui {
 		public bool FocusPrev ()
 		{
 			FocusDirection = Direction.Backward;
-			if (subviews == null || subviews.Count == 0)
+			if (tabIndex == null || tabIndex.Count == 0)
 				return false;
 
 			if (focused == null) {
@@ -1350,9 +1366,9 @@ namespace Terminal.Gui {
 				return focused != null;
 			}
 			int focused_idx = -1;
-			for (int i = subviews.Count; i > 0;) {
+			for (int i = tabIndex.Count; i > 0;) {
 				i--;
-				View w = subviews [i];
+				View w = tabIndex [i];
 
 				if (w.HasFocus) {
 					if (w.FocusPrev ())
@@ -1384,17 +1400,17 @@ namespace Terminal.Gui {
 		public bool FocusNext ()
 		{
 			FocusDirection = Direction.Forward;
-			if (subviews == null || subviews.Count == 0)
+			if (tabIndex == null || tabIndex.Count == 0)
 				return false;
 
 			if (focused == null) {
 				FocusFirst ();
 				return focused != null;
 			}
-			int n = subviews.Count;
+			int n = tabIndex.Count;
 			int focused_idx = -1;
 			for (int i = 0; i < n; i++) {
-				View w = subviews [i];
+				View w = tabIndex [i];
 
 				if (w.HasFocus) {
 					if (w.FocusNext ())