Skip to content

Commit

Permalink
Fixes #2539. Menu should use Frame instead of DrawFrame. (#2540)
Browse files Browse the repository at this point in the history
  • Loading branch information
BDisp authored Apr 13, 2023
1 parent 91bf832 commit 5317489
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 20 deletions.
44 changes: 24 additions & 20 deletions Terminal.Gui/Views/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,9 @@ public Menu (MenuBar host, int x, int y, MenuBarItem barItems, Menu parent = nul
WantMousePositionReports = host.WantMousePositionReports;
}

BorderFrame.Thickness = new Thickness (1);
BorderFrame.BorderStyle = LineStyle.Single;

if (Application.Current != null) {
Application.Current.DrawContentComplete += Current_DrawContentComplete;
}
Expand Down Expand Up @@ -533,20 +536,15 @@ internal Attribute DetermineColorSchemeFor (MenuItem item, int index)
}

public override void Redraw (Rect bounds)
{
}

// Draws the Menu, within the Frame
private void Current_DrawContentComplete (object sender, DrawEventArgs e)
{
if (barItems.Children == null) {
return;
}
var savedClip = Driver.Clip;
var savedClip = Application.Driver.Clip;
Application.Driver.Clip = Application.Top.Frame;

Driver.SetAttribute (GetNormalColor ());
DrawFrame (Bounds, padding: 0, fill: true);
OnDrawFrames (Frame);

for (int i = Bounds.Y; i < barItems.Children.Length; i++) {
if (i < 0)
Expand All @@ -555,10 +553,11 @@ private void Current_DrawContentComplete (object sender, DrawEventArgs e)
Driver.SetAttribute (item == null ? GetNormalColor ()
: i == current ? ColorScheme.Focus : GetNormalColor ());
if (item == null) {
Move (0, i + 1);
Move (-1, i);
Driver.AddRune (Driver.LeftTee);
} else if (Frame.X + 1 < Driver.Cols)
Move (1, i + 1);
} else if (Frame.X < Driver.Cols) {
Move (0, i);
}

Driver.SetAttribute (DetermineColorSchemeFor (item, i));
for (int p = Bounds.X; p < Frame.Width - 2; p++) { // This - 2 is for the border
Expand All @@ -576,8 +575,8 @@ private void Current_DrawContentComplete (object sender, DrawEventArgs e)
}

if (item == null) {
if (SuperView?.Frame.Right - Frame.X > Frame.Width - 1) {
Move (Frame.Width - 1, i + 1);
if (SuperView?.Frame.Right - Frame.X > Frame.Width) {
Move (Frame.Width - 2, i);
Driver.AddRune (Driver.RightTee);
}
continue;
Expand All @@ -604,9 +603,9 @@ private void Current_DrawContentComplete (object sender, DrawEventArgs e)
textToDraw = item.Title;
}

ViewToScreen (2, i + 1, out int vtsCol, out _, false);
ViewToScreen (0, i, out int vtsCol, out _, false);
if (vtsCol < Driver.Cols) {
Move (2, i + 1);
Move (1, i);
if (!item.IsEnabled ()) {
DrawHotString (textToDraw, ColorScheme.Disabled, ColorScheme.Disabled);
} else if (i == 0 && host.UseSubMenusSingleFrame && item.Parent.Parent != null) {
Expand All @@ -616,7 +615,7 @@ private void Current_DrawContentComplete (object sender, DrawEventArgs e)
Text = textToDraw
};
// The -3 is left/right border + one space (not sure what for)
tf.Draw (ViewToScreen (new Rect (2, i + 1, Frame.Width - 3, 1)),
tf.Draw (ViewToScreen (new Rect (1, i, Frame.Width - 3, 1)),
i == current ? ColorScheme.Focus : GetNormalColor (),
i == current ? ColorScheme.HotFocus : ColorScheme.HotNormal,
SuperView == null ? default : SuperView.ViewToScreen (SuperView.Bounds));
Expand All @@ -628,16 +627,16 @@ private void Current_DrawContentComplete (object sender, DrawEventArgs e)

// The help string
var l = item.ShortcutTag.ConsoleWidth == 0 ? item.Help.ConsoleWidth : item.Help.ConsoleWidth + item.ShortcutTag.ConsoleWidth + 2;
var col = Frame.Width - l - 2;
ViewToScreen (col, i + 1, out vtsCol, out _, false);
var col = Frame.Width - l - 3;
ViewToScreen (col, i, out vtsCol, out _, false);
if (vtsCol < Driver.Cols) {
Move (col, 1 + i);
Move (col, i);
Driver.AddStr (item.Help);

// The shortcut tag string
if (!item.ShortcutTag.IsEmpty) {
l = item.ShortcutTag.ConsoleWidth;
Move (Frame.Width - l - 2, 1 + i);
Move (Frame.Width - l - 3, i);
Driver.AddStr (item.ShortcutTag);
}
}
Expand All @@ -648,6 +647,11 @@ private void Current_DrawContentComplete (object sender, DrawEventArgs e)
PositionCursor ();
}

private void Current_DrawContentComplete (object sender, DrawEventArgs e)
{
Redraw (e.Rect);
}

public override void PositionCursor ()
{
if (host == null || host.IsMenuOpen)
Expand Down Expand Up @@ -692,7 +696,7 @@ public override bool OnKeyDown (KeyEvent keyEvent)
public override bool ProcessHotKey (KeyEvent keyEvent)
{
// To ncurses simulate a AltMask key pressing Alt+Space because
// it cant detect an alone special key down was pressed.
// it can't detect an alone special key down was pressed.
if (keyEvent.IsAlt && keyEvent.Key == Key.AltMask) {
OnKeyDown (keyEvent);
return true;
Expand Down
26 changes: 26 additions & 0 deletions UnitTests/Views/MenuTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1818,5 +1818,31 @@ Nullable Checked
mi.CheckType = MenuItemCheckStyle.Radio;
Assert.Throws<InvalidOperationException> (mi.ToggleChecked);
}


[Fact, AutoInitShutdown]
public void Menu_With_Separator ()
{
var menu = new MenuBar (new MenuBarItem [] {
new MenuBarItem("File",new MenuItem [] {
new MenuItem("_Open", "Open a file", () => { }, null, null, Key.CtrlMask | Key.O),
null,
new MenuItem("_Quit","",null)
})
});

Application.Top.Add (menu);
Application.Begin (Application.Top);

menu.OpenMenu ();
Application.Refresh ();
TestHelpers.AssertDriverContentsWithFrameAre (@"
File
┌────────────────────────────┐
│ Open Open a file Ctrl+O │
├────────────────────────────┤
│ Quit │
└────────────────────────────┘", output);
}
}
}

0 comments on commit 5317489

Please sign in to comment.