Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
92 changes: 90 additions & 2 deletions Terminal.Gui/Views/TabView/TabView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,101 @@ public TabView ()
}
);

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?.SubViews.IndexOf (mostFocused) - 1; i > -1; i--)
{
var view = mostFocused.SuperView?.SubViews.ElementAt ((int)i);

if (view is { CanFocus: true, Enabled: true, Visible: true })
{
// Let toplevel handle it
return false;
}
}
}

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?.SubViews.IndexOf (mostFocused) + 1; i < mostFocused.SuperView?.SubViews.Count; i++)
{
var view = mostFocused.SuperView?.SubViews.ElementAt ((int)i);

if (view is { CanFocus: true, Enabled: true, Visible: true })
{
// Let toplevel handle it
return false;
}
}
}

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 +243,7 @@ public Tab? SelectedTab

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

private void ContainerViewCanFocus (object sender, EventArgs eventArgs)
Expand Down Expand Up @@ -518,7 +606,7 @@ internal IEnumerable<Tab> CalculateViewport (Rectangle bounds)
{
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