Browse Source

Fixing the FocusNearestView method.

BDisp 4 years ago
parent
commit
ea8d84f185
3 changed files with 42 additions and 42 deletions
  1. 16 21
      Terminal.Gui/Core/Toplevel.cs
  2. 1 1
      UICatalog/Scenarios/WindowsAndFrameViews.cs
  3. 25 20
      UnitTests/ViewTests.cs

+ 16 - 21
Terminal.Gui/Core/Toplevel.cs

@@ -217,11 +217,11 @@ namespace Terminal.Gui {
 				var old = GetDeepestFocusedSubview (Focused);
 				var old = GetDeepestFocusedSubview (Focused);
 				if (!FocusNext ())
 				if (!FocusNext ())
 					FocusNext ();
 					FocusNext ();
-				if (old != Focused) {
+				if (old != Focused && old != Focused?.Focused) {
 					old?.SetNeedsDisplay ();
 					old?.SetNeedsDisplay ();
 					Focused?.SetNeedsDisplay ();
 					Focused?.SetNeedsDisplay ();
 				} else {
 				} else {
-					FocusNearestView (GetToplevelSubviews (true));
+					FocusNearestView (SuperView?.Subviews, Direction.Forward);
 				}
 				}
 				return true;
 				return true;
 			case Key.CursorLeft:
 			case Key.CursorLeft:
@@ -230,11 +230,11 @@ namespace Terminal.Gui {
 				old = GetDeepestFocusedSubview (Focused);
 				old = GetDeepestFocusedSubview (Focused);
 				if (!FocusPrev ())
 				if (!FocusPrev ())
 					FocusPrev ();
 					FocusPrev ();
-				if (old != Focused) {
+				if (old != Focused && old != Focused?.Focused) {
 					old?.SetNeedsDisplay ();
 					old?.SetNeedsDisplay ();
 					Focused?.SetNeedsDisplay ();
 					Focused?.SetNeedsDisplay ();
 				} else {
 				} else {
-					FocusNearestView (GetToplevelSubviews (false));
+					FocusNearestView (SuperView?.Subviews?.Reverse(), Direction.Backward);
 				}
 				}
 				return true;
 				return true;
 
 
@@ -272,39 +272,34 @@ namespace Terminal.Gui {
 			return view;
 			return view;
 		}
 		}
 
 
-		IEnumerable<View> GetToplevelSubviews (bool isForward)
-		{
-			if (SuperView == null) {
-				return null;
-			}
-
-			IList<View> views = new List<View> ();
-
-			foreach (var v in SuperView.Subviews) {
-				views.Add (v);
-			}
-
-			return isForward ? views : views.Reverse ();
-		}
-
-		void FocusNearestView (IEnumerable<View> views)
+		void FocusNearestView (IEnumerable<View> views, Direction direction)
 		{
 		{
 			if (views == null) {
 			if (views == null) {
 				return;
 				return;
 			}
 			}
 
 
 			bool found = false;
 			bool found = false;
+			bool focusProcessed = false;
+			int idx = 0;
 
 
 			foreach (var v in views) {
 			foreach (var v in views) {
 				if (v == this) {
 				if (v == this) {
 					found = true;
 					found = true;
 				}
 				}
 				if (found && v != this) {
 				if (found && v != this) {
-					v.EnsureFocus ();
+					if (direction == Direction.Forward) {
+						SuperView?.FocusNext ();
+					} else {
+						SuperView?.FocusPrev ();
+					}
+					focusProcessed = true;
 					if (SuperView.Focused != null && SuperView.Focused != this) {
 					if (SuperView.Focused != null && SuperView.Focused != this) {
 						return;
 						return;
 					}
 					}
+				} else if (found && !focusProcessed && idx == views.Count () - 1) {
+					views.ToList () [0].SetFocus ();
 				}
 				}
+				idx++;
 			}
 			}
 		}
 		}
 
 

+ 1 - 1
UICatalog/Scenarios/WindowsAndFrameViews.cs

@@ -108,7 +108,7 @@ namespace UICatalog {
 				var frameView = new FrameView ("This is a Sub-FrameView") {
 				var frameView = new FrameView ("This is a Sub-FrameView") {
 					X = Pos.Percent (50),
 					X = Pos.Percent (50),
 					Y = 1,
 					Y = 1,
-					Width = Dim.Percent (100),
+					Width = Dim.Percent (100, true), // Or Dim.Percent (50)
 					Height = 5,
 					Height = 5,
 					ColorScheme = Colors.Base,
 					ColorScheme = Colors.Base,
 					Text = "The Text in the FrameView",
 					Text = "The Text in the FrameView",

+ 25 - 20
UnitTests/ViewTests.cs

@@ -1079,30 +1079,35 @@ namespace Terminal.Gui {
 		}
 		}
 
 
 		[Fact]
 		[Fact]
-		public void GetToplevelSubviews_Ensure_Order_List ()
+		public void FocusNearestView_Ensure_Focus_Ordered ()
 		{
 		{
 			var top = new Toplevel ();
 			var top = new Toplevel ();
 
 
-			for (int i = 0; i < 6; i++) {
-				var view = new View ($"View{i}");
-				top.Add (view);
-			}
-
-			IList<View> views = new List<View> ();
-
-			var idx = 0;
-			foreach (var v in top.Subviews) {
-				views.Add (v);
-				Assert.Equal ($"View{idx}", v.Text);
-				idx++;
-			}
+			var win = new Window ();
+			var winSubview = new View ("WindowSubview") {
+				CanFocus = true
+			};
+			win.Add (winSubview);
+			top.Add (win);
 
 
-			idx = top.Subviews.Count - 1;
-			foreach (var v in top.Subviews.Reverse ()) {
-				views.Add (v);
-				Assert.Equal ($"View{idx}", v.Text);
-				idx--;
-			}
+			var frm = new FrameView ();
+			var frmSubview = new View ("FrameSubview") {
+				CanFocus = true
+			};
+			frm.Add (frmSubview);
+			top.Add (frm);
+
+			top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
+			Assert.Equal ($"WindowSubview", top.MostFocused.Text);
+			top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
+			Assert.Equal ("FrameSubview", top.MostFocused.Text);
+			top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
+			Assert.Equal ($"WindowSubview", top.MostFocused.Text);
+
+			top.ProcessKey (new KeyEvent (Key.BackTab, new KeyModifiers ()));
+			Assert.Equal ("FrameSubview", top.MostFocused.Text);
+			top.ProcessKey (new KeyEvent (Key.BackTab, new KeyModifiers ()));
+			Assert.Equal ($"WindowSubview", top.MostFocused.Text);
 		}
 		}
 	}
 	}
 }
 }