diff --git a/Terminal.Gui/Application/Application.Keyboard.cs b/Terminal.Gui/Application/Application.Keyboard.cs index b2b6aa41a9..a448cad319 100644 --- a/Terminal.Gui/Application/Application.Keyboard.cs +++ b/Terminal.Gui/Application/Application.Keyboard.cs @@ -24,8 +24,7 @@ public static bool RaiseKeyDownEvent (Key key) // } //#endif - // TODO: This should match standard event patterns - KeyDown?.Invoke (null, key); + KeyDown?.Invoke (Top?.MostFocused, key); if (key.Handled) { @@ -97,17 +96,11 @@ public static bool RaiseKeyDownEvent (Key key) } else { - // BUGBUG: this seems unneeded. - if (!KeyBindings.TryGet (key, out KeyBinding keybinding)) - { - return null; - } - bool? toReturn = null; - foreach (Command command in keybinding.Commands) + foreach (Command command in binding.Commands) { - toReturn = InvokeCommand (command, key, keybinding); + toReturn = InvokeCommand (command, key, binding); } handled = toReturn ?? true; @@ -118,7 +111,7 @@ public static bool RaiseKeyDownEvent (Key key) } /// - /// Invokes an Application-bound commmand. + /// Invokes an Application-bound command. /// /// The Command to invoke /// The Application-bound Key that was pressed. @@ -178,7 +171,7 @@ public static bool RaiseKeyUpEvent (Key key) return true; } - KeyUp?.Invoke (null, key); + KeyUp?.Invoke (Top?.MostFocused, key); if (key.Handled) { diff --git a/Terminal.Gui/View/View.Command.cs b/Terminal.Gui/View/View.Command.cs index 15bd0a311a..6d93d3106a 100644 --- a/Terminal.Gui/View/View.Command.cs +++ b/Terminal.Gui/View/View.Command.cs @@ -28,10 +28,7 @@ private void SetupCommands () return true; } - SetFocus (); - - // QUESTION: Why do we always return true here? - return true; + return SetFocus (); }); // Space or single-click - Raise Selecting diff --git a/Terminal.Gui/View/View.Navigation.cs b/Terminal.Gui/View/View.Navigation.cs index ca5d5d50e2..7e552a2b4d 100644 --- a/Terminal.Gui/View/View.Navigation.cs +++ b/Terminal.Gui/View/View.Navigation.cs @@ -80,7 +80,7 @@ public bool AdvanceFocus (NavigationDirection direction, TabBehavior? behavior) if (direction == NavigationDirection.Backward && focused == focusChain [0]) { // We're at the bottom of the focus chain - View [] views = GetFocusChain (NavigationDirection.Forward, TabBehavior.TabGroup); + View [] views = GetFocusChain (NavigationDirection.Backward, TabBehavior.TabGroup); if (views.Length > 0) { @@ -531,7 +531,7 @@ public bool SetFocus () // throw new InvalidOperationException (@"Do not SetFocus on a view that is already MostFocused."); //} - return (false, false); + return (_hasFocus, false); } if (currentFocusedView is { HasFocus: false }) diff --git a/Terminal.Gui/Views/Label.cs b/Terminal.Gui/Views/Label.cs index 673907fcde..db73564cce 100644 --- a/Terminal.Gui/Views/Label.cs +++ b/Terminal.Gui/Views/Label.cs @@ -68,10 +68,10 @@ public override Rune HotKeySpecifier if (CanFocus) { - SetFocus (); + return SetFocus (); - // QUESTION: Why do we always return true here? - return true; + //// QUESTION: Why do we always return true here? + //return true; } if (HotKey.IsValid) diff --git a/Tests/IntegrationTests/FluentTests/BasicFluentAssertionTests.cs b/Tests/IntegrationTests/FluentTests/BasicFluentAssertionTests.cs index d47ca128e7..10894d102e 100644 --- a/Tests/IntegrationTests/FluentTests/BasicFluentAssertionTests.cs +++ b/Tests/IntegrationTests/FluentTests/BasicFluentAssertionTests.cs @@ -143,4 +143,38 @@ public void ContextMenu_OpenSubmenu (V2TestDriver d) .WriteOutLogs (_out); Assert.True (clicked); } + + [Theory] + [ClassData (typeof (V2TestDrivers))] + public void Toplevel_TabGroup_Forward_Backward (V2TestDriver d) + { + var v1 = new View { Id = "v1", CanFocus = true }; + var v2 = new View { Id = "v2", CanFocus = true }; + + using GuiTestContext c = With.A (50, 20, d) + .Then ( + () => + { + var w1 = new Window { Id = "w1" }; + w1.Add (v1); + var w2 = new Window { Id = "w2" }; + w2.Add (v2); + Toplevel top = Application.Top!; + Application.Top!.Add (w1, w2); + }) + .WaitIteration () + .Then (() => Assert.True (v2.HasFocus)) + .RaiseKeyDownEvent (Key.F6) + .Then (() => Assert.True (v1.HasFocus)) + .RaiseKeyDownEvent (Key.F6) + .Then (() => Assert.True (v2.HasFocus)) + .RaiseKeyDownEvent (Key.F6.WithShift) + .Then (() => Assert.True (v1.HasFocus)) + .RaiseKeyDownEvent (Key.F6.WithShift) + .Then (() => Assert.True (v2.HasFocus)) + .WriteOutLogs (_out) + .Stop (); + Assert.False (v1.HasFocus); + Assert.False (v2.HasFocus); + } }