Ver código fonte

Merge pull request #824 from BDisp/setfocus-order

Fixes #750. Ensure the correct focus order after call SetFocus.
Charlie Kindel 5 anos atrás
pai
commit
e8ef6dca11

+ 17 - 9
Example/demo.cs

@@ -253,12 +253,7 @@ static class Demo {
 		});
 		ntop.Add (menu);
 
-		string fname = null;
-		foreach (var s in new [] { "/etc/passwd", "c:\\windows\\win.ini" })
-			if (System.IO.File.Exists (s)) {
-				fname = s;
-				break;
-			}
+		string fname = GetFileName ();
 
 		var win = new Window (fname ?? "Untitled") {
 			X = 0,
@@ -277,6 +272,18 @@ static class Demo {
 		Application.Run (ntop);
 	}
 
+	private static string GetFileName ()
+	{
+		string fname = null;
+		foreach (var s in new [] { "/etc/passwd", "c:\\windows\\win.ini" })
+			if (System.IO.File.Exists (s)) {
+				fname = s;
+				break;
+			}
+
+		return fname;
+	}
+
 	static bool Quit ()
 	{
 		var n = MessageBox.Query (50, 7, "Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
@@ -312,7 +319,8 @@ static class Demo {
 		});
 		ntop.Add (menu);
 
-		var win = new Window ("/etc/passwd") {
+		string fname = GetFileName ();
+		var win = new Window (fname) {
 			X = 0,
 			Y = 1,
 			Width = Dim.Fill (),
@@ -320,7 +328,7 @@ static class Demo {
 		};
 		ntop.Add (win);
 
-		var source = System.IO.File.OpenRead ("/etc/passwd");
+		var source = System.IO.File.OpenRead (fname);
 		var hex = new HexView (source) {
 			X = 0,
 			Y = 0,
@@ -647,7 +655,7 @@ static class Demo {
 		win.Add (test);
 		win.Add (ml);
 
-		var drag = new Label ("Drag: ") { X = 70, Y = 24 };
+		var drag = new Label ("Drag: ") { X = 70, Y = 22 };
 		var dragText = new TextField ("") {
 			X = Pos.Right (drag),
 			Y = Pos.Top (drag),

+ 21 - 11
Terminal.Gui/Core/View.cs

@@ -1049,13 +1049,13 @@ namespace Terminal.Gui {
 		{
 			if (hasFocus != value) {
 				hasFocus = value;
+				if (value) {
+					OnEnter (view);
+				} else {
+					OnLeave (view);
+				}
+				SetNeedsDisplay ();
 			}
-			if (value) {
-				OnEnter (view);
-			} else {
-				OnLeave (view);
-			}
-			SetNeedsDisplay ();
 
 			// Remove focus down the chain of subviews if focus is removed
 			if (!value && focused != null) {
@@ -1277,14 +1277,14 @@ namespace Terminal.Gui {
 		/// Causes the specified subview to have focus.
 		/// </summary>
 		/// <param name="view">View.</param>
-		public void SetFocus (View view)
+		void SetFocus (View view)
 		{
 			if (view == null)
 				return;
 			//Console.WriteLine ($"Request to focus {view}");
 			if (!view.CanFocus)
 				return;
-			if (focused == view)
+			if (focused?.hasFocus == true && focused == view)
 				return;
 
 			// Make sure that this view is a subview
@@ -1307,6 +1307,14 @@ namespace Terminal.Gui {
 			SuperView?.SetFocus (this);
 		}
 
+		/// <summary>
+		/// Causes the specified view and the entire parent hierarchy to have the focused order updated.
+		/// </summary>
+		public void SetFocus ()
+		{
+			SuperView?.SetFocus (this);
+		}
+
 		/// <summary>
 		/// Defines the event arguments for <see cref="KeyEvent"/>
 		/// </summary>
@@ -1420,11 +1428,13 @@ namespace Terminal.Gui {
 		/// </summary>
 		public void EnsureFocus ()
 		{
-			if (focused == null)
-				if (FocusDirection == Direction.Forward)
+			if (focused == null && subviews?.Count > 0) {
+				if (FocusDirection == Direction.Forward) {
 					FocusFirst ();
-				else
+				} else {
 					FocusLast ();
+				}
+			}
 		}
 
 		/// <summary>

+ 2 - 2
Terminal.Gui/Views/Button.cs

@@ -154,7 +154,7 @@ namespace Terminal.Gui {
 		bool CheckKey (KeyEvent key)
 		{
 			if (key.Key == (Key.AltMask | HotKey)) {
-				this.SuperView.SetFocus (this);
+				SetFocus ();
 				Clicked?.Invoke ();
 				return true;
 			}
@@ -210,7 +210,7 @@ namespace Terminal.Gui {
 				me.Flags == MouseFlags.Button1TripleClicked) {
 				if (CanFocus) {
 					if (!HasFocus) {
-						SuperView?.SetFocus (this);
+						SetFocus ();
 						SetNeedsDisplay ();
 					}
 					Clicked?.Invoke ();

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

@@ -150,7 +150,7 @@ namespace Terminal.Gui {
 			if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) || !CanFocus)
 				return false;
 
-			SuperView.SetFocus (this);
+			SetFocus ();
 			var previousChecked = Checked;
 			Checked = !Checked;
 			OnToggled (previousChecked);

+ 7 - 7
Terminal.Gui/Views/ComboBox.cs

@@ -196,7 +196,7 @@ namespace Terminal.Gui {
 				return true;
 			} else if (me.Flags == MouseFlags.Button1Pressed) {
 				if (!search.HasFocus) {
-					SetFocus (search);
+					search.SetFocus ();
 				}
 
 				return true;
@@ -210,7 +210,7 @@ namespace Terminal.Gui {
 			listview.SelectedItem = SelectedItem > -1 ? SelectedItem : 0;
 			if (SelectedItem > -1) {
 				listview.TabStop = true;
-				this.SetFocus (listview);
+				listview.SetFocus ();
 			}
 		}
 
@@ -218,7 +218,7 @@ namespace Terminal.Gui {
 		public override bool OnEnter (View view)
 		{
 			if (!search.HasFocus && !listview.HasFocus) {
-				SetFocus (search);
+				search.SetFocus ();
 			}
 
 			search.CursorPosition = search.Text.RuneCount;
@@ -301,7 +301,7 @@ namespace Terminal.Gui {
 			if (e.Key == Key.CursorDown && search.HasFocus) { // jump to list
 				if (searchset.Count > 0) {
 					listview.TabStop = true;
-					this.SetFocus (listview);
+					listview.SetFocus ();
 					SetValue (searchset [listview.SelectedItem]);
 					return true;
 				} else {
@@ -317,7 +317,7 @@ namespace Terminal.Gui {
 			if (e.Key == Key.CursorUp && listview.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) // jump back to search
 			{
 				search.CursorPosition = search.Text.RuneCount;
-				this.SetFocus (search);
+				search.SetFocus ();
 				return true;
 			}
 
@@ -350,7 +350,7 @@ namespace Terminal.Gui {
 			}
 
 			if (e.Key == Key.Esc) {
-				this.SetFocus (search);
+				search.SetFocus ();
 				search.Text = text = "";
 				OnSelectedChanged ();
 				return true;
@@ -431,7 +431,7 @@ namespace Terminal.Gui {
 			listview.Height = CalculatetHeight ();
 
 			if (Subviews.Count > 0) {
-				SetFocus (search);
+				search.SetFocus ();
 			}
 		}
 

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

@@ -357,7 +357,7 @@ namespace Terminal.Gui {
 			if (!ev.Flags.HasFlag (MouseFlags.Button1Clicked))
 				return false;
 			if (!HasFocus)
-				SuperView.SetFocus (this);
+				SetFocus ();
 
 			var point = ev.X;
 			if (point > FieldLen)

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

@@ -87,7 +87,7 @@ namespace Terminal.Gui {
 
 			if (mouseEvent.Flags == MouseFlags.Button1Clicked) {
 				if (!HasFocus && SuperView != null) {
-					SuperView.SetFocus (this);
+					SetFocus ();
 					SetNeedsDisplay ();
 				}
 

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

@@ -577,7 +577,7 @@ namespace Terminal.Gui {
 				return false;
 
 			if (!HasFocus)
-				SuperView.SetFocus (this);
+				SetFocus ();
 
 			if (source == null)
 				return false;

+ 11 - 11
Terminal.Gui/Views/Menu.cs

@@ -720,7 +720,7 @@ namespace Terminal.Gui {
 					selected = 0;
 					CanFocus = true;
 					lastFocused = SuperView.MostFocused;
-					SuperView.SetFocus (this);
+					SetFocus ();
 					SetNeedsDisplay ();
 					Application.GrabMouse (this);
 				} else if (!openedByHotKey) {
@@ -747,7 +747,7 @@ namespace Terminal.Gui {
 			selected = -1;
 			CanFocus = false;
 			if (lastFocused != null) {
-				lastFocused.SuperView?.SetFocus (lastFocused);
+				lastFocused.SetFocus ();
 			}
 			SetNeedsDisplay ();
 			Application.UngrabMouse ();
@@ -880,7 +880,7 @@ namespace Terminal.Gui {
 				openCurrentMenu.previousSubFocused = openMenu;
 
 				SuperView.Add (openMenu);
-				SuperView.SetFocus (openMenu);
+				openMenu.SetFocus ();
 				break;
 			default:
 				if (openSubMenu == null)
@@ -896,7 +896,7 @@ namespace Terminal.Gui {
 				}
 				selectedSub = openSubMenu.Count - 1;
 				if (selectedSub > -1 && SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
-					SuperView?.SetFocus (openCurrentMenu);
+					openCurrentMenu.SetFocus ();
 				}
 				break;
 			}
@@ -1007,7 +1007,7 @@ namespace Terminal.Gui {
 				}
 				SetNeedsDisplay ();
 				if (previousFocused != null && previousFocused is Menu && openMenu != null && previousFocused.ToString () != openCurrentMenu.ToString ())
-					previousFocused?.SuperView?.SetFocus (previousFocused);
+					previousFocused.SetFocus ();
 				openMenu?.Dispose ();
 				openMenu = null;
 				if (lastFocused is Menu || lastFocused is MenuBar) {
@@ -1020,10 +1020,10 @@ namespace Terminal.Gui {
 					if (!reopen) {
 						selected = -1;
 					}
-					LastFocused.SuperView?.SetFocus (LastFocused);
+					LastFocused.SetFocus ();
 				} else {
 					CanFocus = true;
-					SuperView.SetFocus (this);
+					SetFocus ();
 					PositionCursor ();
 				}
 				IsMenuOpen = false;
@@ -1033,7 +1033,7 @@ namespace Terminal.Gui {
 				selectedSub = -1;
 				SetNeedsDisplay ();
 				RemoveAllOpensSubMenus ();
-				openCurrentMenu.previousSubFocused?.SuperView?.SetFocus (openCurrentMenu.previousSubFocused);
+				openCurrentMenu.previousSubFocused.SetFocus ();
 				openSubMenu = null;
 				IsMenuOpen = true;
 				break;
@@ -1049,9 +1049,9 @@ namespace Terminal.Gui {
 			for (int i = openSubMenu.Count - 1; i > index; i--) {
 				isMenuClosing = true;
 				if (openSubMenu.Count - 1 > 0)
-					SuperView.SetFocus (openSubMenu [i - 1]);
+					openSubMenu [i - 1].SetFocus ();
 				else
-					SuperView.SetFocus (openMenu);
+					openMenu.SetFocus ();
 				if (openSubMenu != null) {
 					var menu = openSubMenu [i];
 					SuperView.Remove (menu);
@@ -1272,7 +1272,7 @@ namespace Terminal.Gui {
 				CloseMenu ();
 				if (openedByAltKey) {
 					openedByAltKey = false;
-					LastFocused.SuperView?.SetFocus (LastFocused);
+					LastFocused.SetFocus ();
 				}
 				break;
 

+ 2 - 2
Terminal.Gui/Views/RadioGroup.cs

@@ -224,7 +224,7 @@ namespace Terminal.Gui {
 								SelectedItem = i;
 								cursor = i;
 								if (!HasFocus)
-									SuperView.SetFocus (this);
+									SetFocus ();
 								return true;
 							}
 							nextIsHot = false;
@@ -267,7 +267,7 @@ namespace Terminal.Gui {
 			if (!me.Flags.HasFlag (MouseFlags.Button1Clicked))
 				return false;
 
-			SuperView.SetFocus (this);
+			SetFocus ();
 
 			if (me.Y < radioLabels.Count) {
 				cursor = SelectedItem = me.Y;

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

@@ -651,7 +651,7 @@ namespace Terminal.Gui {
 					return true;
 				}
 				if (!HasFocus) {
-					SuperView.SetFocus (this);
+					SetFocus ();
 				}
 				PositionCursor (ev);
 				if (isButtonReleased) {

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

@@ -1204,7 +1204,7 @@ namespace Terminal.Gui {
 			}
 
 			if (!HasFocus) {
-				SuperView.SetFocus (this);
+				SetFocus ();
 			}
 
 			if (ev.Flags == MouseFlags.Button1Clicked) {

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

@@ -275,7 +275,7 @@ namespace Terminal.Gui {
 			if (!ev.Flags.HasFlag (MouseFlags.Button1Clicked))
 				return false;
 			if (!HasFocus)
-				SuperView.SetFocus (this);
+				SetFocus ();
 
 			var point = ev.X;
 			if (point > FieldLen)

+ 1 - 1
Terminal.Gui/Windows/FileDialog.cs

@@ -88,7 +88,7 @@ namespace Terminal.Gui {
 				return false;
 
 			if (!HasFocus)
-				SuperView.SetFocus (this);
+				SetFocus ();
 
 			if (infos == null)
 				return false;

+ 1 - 1
UICatalog/Scenarios/AllViewsTester.cs

@@ -94,7 +94,7 @@ namespace UICatalog {
 				ColorScheme = Colors.TopLevel,
 			};
 			_classListView.OpenSelectedItem += (a) => {
-				Top.SetFocus (_settingsPane);
+				_settingsPane.SetFocus ();
 			};
 			_classListView.SelectedItemChanged += (args) => {
 				ClearClass (_curView);

+ 2 - 2
UICatalog/UICatalog.cs

@@ -97,7 +97,7 @@ namespace UICatalog {
 				scenario.Setup ();
 				scenario.Run ();
 				_top.Ready += () => {
-					_top.SetFocus (_rightPane);
+					_rightPane.SetFocus ();
 					_top.Ready = null;
 				};
 
@@ -176,7 +176,7 @@ namespace UICatalog {
 				CanFocus = true,
 			};
 			_categoryListView.OpenSelectedItem += (a) => {
-				_top.SetFocus (_rightPane);
+				_rightPane.SetFocus ();
 			};
 			_categoryListView.SelectedItemChanged += CategoryListView_SelectedChanged;
 			_leftPane.Add (_categoryListView);