Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3974. TabView steals keypresses from active ContextMenu #3995

Open
wants to merge 3 commits into
base: v2_develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 92 additions & 2 deletions Terminal.Gui/Views/TabView/TabView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,103 @@
}
);

AddCommand (
Command.Up,
() =>
{
if (_style.TabsOnBottom)
{
if (_tabsBar is { HasFocus: true } && _containerView.CanFocus)
{
_containerView.SetFocus ();

return true;
}
}
else
{
if (_containerView is { HasFocus: true })
{
var mostFocused = _containerView.MostFocused;

if (mostFocused is { })
{
for (int? i = mostFocused.SuperView?.InternalSubViews.IndexOf (mostFocused) - 1; i > -1; i--)
{
var view = mostFocused.SuperView?.InternalSubViews [(int)i];

if (view is { CanFocus: true, Enabled: true, Visible: true })
{
view.SetFocus ();

return true;
}
}
}

SelectedTab?.SetFocus ();

return true;
}
}

return false;
}
);

AddCommand (
Command.Down,
() =>
{
if (_style.TabsOnBottom)
{
if (_containerView is { HasFocus: true })
{
var mostFocused = _containerView.MostFocused;

if (mostFocused is { })
{
for (int? i = mostFocused.SuperView?.InternalSubViews.IndexOf (mostFocused) + 1; i < mostFocused.SuperView?.InternalSubViews.Count; i++)
{
var view = mostFocused.SuperView?.InternalSubViews [(int)i];

if (view is { CanFocus: true, Enabled: true, Visible: true })
{
view.SetFocus ();

return true;
}
}
}

SelectedTab?.SetFocus ();

return true;
}
}
else
{
if (_tabsBar is { HasFocus: true } && _containerView.CanFocus)
{
_containerView.SetFocus ();

return true;
}
}

return false;
}
);

// Default keybindings for this view
KeyBindings.Add (Key.CursorLeft, Command.Left);
KeyBindings.Add (Key.CursorRight, Command.Right);
KeyBindings.Add (Key.Home, Command.LeftStart);
KeyBindings.Add (Key.End, Command.RightEnd);
KeyBindings.Add (Key.PageDown, Command.PageDown);
KeyBindings.Add (Key.PageUp, Command.PageUp);
KeyBindings.Add (Key.CursorUp, Command.Up);
KeyBindings.Add (Key.CursorDown, Command.Down);
}

/// <summary>
Expand Down Expand Up @@ -155,7 +245,7 @@

private bool TabCanSetFocus ()
{
return IsInitialized && SelectedTab is { } && (_selectedTabHasFocus || !_containerView.CanFocus);
return IsInitialized && SelectedTab is { } && (HasFocus || (bool)_containerView?.HasFocus) && (_selectedTabHasFocus || !_containerView.CanFocus);

Check warning on line 248 in Terminal.Gui/Views/TabView/TabView.cs

View workflow job for this annotation

GitHub Actions / Parallel Unit Tests (macos-latest)

Nullable value type may be null.

Check warning on line 248 in Terminal.Gui/Views/TabView/TabView.cs

View workflow job for this annotation

GitHub Actions / Parallel Unit Tests (ubuntu-latest)

Nullable value type may be null.

Check warning on line 248 in Terminal.Gui/Views/TabView/TabView.cs

View workflow job for this annotation

GitHub Actions / build_and_test_debug (macos-latest)

Nullable value type may be null.

Check warning on line 248 in Terminal.Gui/Views/TabView/TabView.cs

View workflow job for this annotation

GitHub Actions / Non-Parallel Unit Tests (macos-latest)

Nullable value type may be null.

Check warning on line 248 in Terminal.Gui/Views/TabView/TabView.cs

View workflow job for this annotation

GitHub Actions / Parallel Unit Tests (windows-latest)

Nullable value type may be null.
}

private void ContainerViewCanFocus (object sender, EventArgs eventArgs)
Expand Down Expand Up @@ -518,7 +608,7 @@
{
SelectedTab?.SetFocus ();
}
else
else if (HasFocus)
{
SelectedTab?.View?.SetFocus ();
}
Expand Down
80 changes: 80 additions & 0 deletions Tests/UnitTests/Views/ContextMenuTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2135,4 +2135,84 @@ public void Menu_Without_SubMenu_Is_Closed_When_Pressing_Key_Right_Or_Key_Left (

top.Dispose ();
}

[Fact]
[AutoInitShutdown]
public void Menu_Opened_In_SuperView_With_TabView_Has_Precedence_On_Key_Press ()
{
var win = new Window
{
Title = "My Window",
X = 0,
Y = 0,
Width = Dim.Fill (),
Height = Dim.Fill ()
};

// Tab View
var tabView = new TabView
{
X = 1,
Y = 1,
Width = Dim.Fill () - 2,
Height = Dim.Fill () - 2
};
tabView.AddTab (new () { DisplayText = "Tab 1" }, true);
tabView.AddTab (new () { DisplayText = "Tab 2" }, false);
win.Add (tabView);

// Context Menu
var menuItems = new MenuBarItem (
[
new ("Item 1", "First item", () => MessageBox.Query ("Action", "Item 1 Clicked", "OK")),
new MenuBarItem (
"Submenu",
new List<MenuItem []>
{
new []
{
new MenuItem (
"Sub Item 1",
"Submenu item",
() => { MessageBox.Query ("Action", "Sub Item 1 Clicked", "OK"); })
}
})
]);

var cm = new ContextMenu ();

win.MouseClick += (s, e) =>
{
if (e.Flags.HasFlag (MouseFlags.Button3Clicked)) // Right-click
{
cm.Position = e.Position;
cm.Show (menuItems);
}
};
Application.Begin (win);

cm.Show (menuItems);
Assert.True (cm.MenuBar!.IsMenuOpen);

Assert.True (Application.RaiseKeyDownEvent (Key.CursorDown));
Assert.True (cm.MenuBar!.IsMenuOpen);

Assert.True (Application.RaiseKeyDownEvent (Key.CursorUp));
Assert.True (cm.MenuBar!.IsMenuOpen);

Assert.True (Application.RaiseKeyDownEvent (Key.CursorDown));
Assert.True (cm.MenuBar!.IsMenuOpen);

Assert.True (Application.RaiseKeyDownEvent (Key.CursorRight));
Assert.True (cm.MenuBar!.IsMenuOpen);

Assert.True (Application.RaiseKeyDownEvent (Key.CursorLeft));
Assert.True (cm.MenuBar!.IsMenuOpen);

Assert.True (Application.RaiseKeyDownEvent (Key.CursorLeft));
Assert.False (cm.MenuBar!.IsMenuOpen);
Assert.True (tabView.HasFocus);

win.Dispose ();
}
}
8 changes: 4 additions & 4 deletions Tests/UnitTests/Views/TabViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,8 @@ public void ProcessKey_Down_Up_Right_Left_Home_End_PageDown_PageUp_F6 ()
Assert.Equal (btnSubView, top.MostFocused);

Assert.True (Application.RaiseKeyDownEvent (Key.CursorUp));
// TabRow now has TabGroup which only F6 is allowed
Assert.NotEqual (tab2, top.MostFocused);
Assert.Equal (btn, top.MostFocused);
Assert.Equal (tab2, top.MostFocused);
Assert.NotEqual (btn, top.MostFocused);

Assert.True (Application.RaiseKeyDownEvent (Key.CursorUp));
Assert.Equal (btnSubView, top.MostFocused);
Expand All @@ -459,7 +458,8 @@ public void ProcessKey_Down_Up_Right_Left_Home_End_PageDown_PageUp_F6 ()
// Press the cursor up key to focus the selected tab which it's the only way to do that
Assert.True (Application.RaiseKeyDownEvent (Key.CursorUp));
Assert.Equal (tab2, tv.SelectedTab);
Assert.Equal (btn, top.Focused);
Assert.False (tab2.View.HasFocus);
Assert.Equal (tv, top.Focused);

Assert.True (Application.RaiseKeyDownEvent (Key.CursorUp));
Assert.Equal (tv, top.Focused);
Expand Down
Loading