Skip to content

Commit bdd716c

Browse files
authored
Merge pull request #5441 from gui-cs/tig/fix-hotkeys-not-work-invisible-views
2 parents 1019953 + 9501775 commit bdd716c

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

Terminal.Gui/ViewBase/View.Keyboard.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,11 @@ private void ApplyLayer (Dictionary<Command, PlatformKeyBinding> layer, HashSet<
820820
return false;
821821
}
822822

823+
if (!Visible)
824+
{
825+
return false;
826+
}
827+
823828
bool? handled = null;
824829

825830
// Process this View

Tests/UnitTestsParallelizable/ViewBase/Keyboard/KeyBindingsTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,56 @@ public void Unhandled_Default_KeyBinding_Does_Not_Block_HotKey (string keyName)
218218
Assert.True (hotKeyFired);
219219
}
220220

221+
// Copilot - Opus 4.6
222+
/// <summary>
223+
/// A view with Visible = false should not have its HotKey invoked via InvokeCommandsBoundToHotKey.
224+
/// </summary>
225+
[Fact]
226+
public void HotKey_Visible_False_Does_Not_Invoke ()
227+
{
228+
IApplication app = Application.Create ();
229+
app.Begin (new Runnable<bool> ());
230+
231+
ScopedKeyBindingView view = new ();
232+
app!.TopRunnableView!.Add (view);
233+
234+
// Sanity check: hotkey works when visible
235+
app.Keyboard.RaiseKeyDownEvent (Key.H);
236+
Assert.True (view.HotKeyCommandInvoked);
237+
238+
// Now hide the view and try again
239+
view.HotKeyCommandInvoked = false;
240+
view.Visible = false;
241+
app.Keyboard.RaiseKeyDownEvent (Key.H);
242+
Assert.False (view.HotKeyCommandInvoked);
243+
}
244+
245+
// Copilot - Opus 4.6
246+
/// <summary>
247+
/// A SubView of an invisible SuperView should not have its HotKey invoked.
248+
/// </summary>
249+
[Fact]
250+
public void HotKey_Invisible_SuperView_SubView_Does_Not_Invoke ()
251+
{
252+
IApplication app = Application.Create ();
253+
app.Begin (new Runnable<bool> ());
254+
255+
View container = new () { Id = "container" };
256+
ScopedKeyBindingView subView = new ();
257+
container.Add (subView);
258+
app!.TopRunnableView!.Add (container);
259+
260+
// Sanity check: hotkey works when container is visible
261+
app.Keyboard.RaiseKeyDownEvent (Key.H);
262+
Assert.True (subView.HotKeyCommandInvoked);
263+
264+
// Hide the container and try again
265+
subView.HotKeyCommandInvoked = false;
266+
container.Visible = false;
267+
app.Keyboard.RaiseKeyDownEvent (Key.H);
268+
Assert.False (subView.HotKeyCommandInvoked);
269+
}
270+
221271
// tests that test KeyBindingScope.Focus and KeyBindingScope.HotKey (tests for KeyBindingScope.Application are in Application/KeyboardTests.cs)
222272

223273
public class ScopedKeyBindingView : View

0 commit comments

Comments
 (0)