Skip to content

Commit

Permalink
ix some more bug, cleanup code and add more unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
BDisp authored and tig committed Sep 2, 2024
1 parent e83bfa7 commit fa0085d
Show file tree
Hide file tree
Showing 9 changed files with 351 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Terminal.Gui/Views/ColorPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ private void UpdateValueFromTextField ()
}
}


/// <inheritdoc />
protected override void Dispose (bool disposing)
{
DisposeOldViews ();
Expand Down
8 changes: 4 additions & 4 deletions Terminal.Gui/Views/Menu/ContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public ContextMenu ()
public static bool IsShow { get; private set; }

/// <summary>Specifies the key that will activate the context menu.</summary>
public new Key Key
public Key Key
{
get => _key;
set
Expand Down Expand Up @@ -133,7 +133,7 @@ private void RemoveKeyBindings (MenuBarItem? menuBarItem)
return;
}

foreach (var menuItem in menuBarItem.Children!)
foreach (MenuItem? menuItem in menuBarItem.Children!)
{
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
if (menuItem is null)
Expand All @@ -150,7 +150,7 @@ private void RemoveKeyBindings (MenuBarItem? menuBarItem)
if (menuItem.ShortcutKey != Key.Empty)
{
// Remove an existent ShortcutKey
_menuBar?.KeyBindings.Remove (menuItem.ShortcutKey);
_menuBar?.KeyBindings.Remove (menuItem.ShortcutKey!);
}
}
}
Expand All @@ -171,7 +171,7 @@ public void Show (MenuBarItem? menuItems)
Dispose ();
}

if (menuItems is null || menuItems.Children.Length == 0)
if (menuItems is null || menuItems.Children!.Length == 0)
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Views/Menu/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal sealed class Menu : View
internal int _currentChild;
internal View? _previousSubFocused;

internal static Rectangle MakeFrame (int x, int y, MenuItem []? items, Menu? parent = null)
internal static Rectangle MakeFrame (int x, int y, MenuItem? []? items, Menu? parent = null)
{
if (items is null || items.Length == 0)
{
Expand Down
28 changes: 20 additions & 8 deletions Terminal.Gui/Views/Menu/MenuBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ public MenuBar ()
return true;
}
);
AddCommand (Command.ToggleExpandCollapse, ctx => Select (Menus.IndexOf (ctx.KeyBinding?.Context)));
AddCommand (Command.ToggleExpandCollapse, ctx =>
{
CloseOtherOpenedMenuBar ();
return Select (Menus.IndexOf (ctx.KeyBinding?.Context));
});
AddCommand (Command.Select, ctx =>
{
var res = Run ((ctx.KeyBinding?.Context as MenuItem)?.Action!);
Expand Down Expand Up @@ -387,6 +392,8 @@ public void OpenMenu ()

mbar?.CleanUp ();

CloseOtherOpenedMenuBar ();

if (!Enabled || _openMenu is { })
{
return;
Expand Down Expand Up @@ -528,11 +535,16 @@ internal void CloseAllMenus ()
_openedByAltKey = false;
OnMenuAllClosed ();

CloseOtherOpenedMenuBar ();
}

private void CloseOtherOpenedMenuBar ()
{
if (Application.Current is { })
{
// Close others menu bar opened
View? cm = Application.Current.Subviews.FirstOrDefault (v => v is Menu cm && cm.Host != this && cm.Host.IsMenuOpen);
(cm as Menu)?.Host.CleanUp ();
Menu? menu = Application.Current.Subviews.FirstOrDefault (v => v is Menu m && m.Host != this && m.Host.IsMenuOpen) as Menu;
menu?.Host.CleanUp ();
}
}

Expand Down Expand Up @@ -726,7 +738,7 @@ out OpenCurrentMenu._currentChild
else if (subMenu != null
|| (OpenCurrentMenu._currentChild > -1
&& !OpenCurrentMenu.BarItems!
.Children! [OpenCurrentMenu._currentChild]
.Children! [OpenCurrentMenu._currentChild]!
.IsFromSubMenu))
{
_selectedSub++;
Expand Down Expand Up @@ -999,7 +1011,7 @@ internal bool Run (Action? action)
}

internal bool SelectEnabledItem (
IEnumerable<MenuItem>? children,
MenuItem? []? children,
int current,
out int newCurrent,
bool forward = true
Expand All @@ -1012,11 +1024,11 @@ internal bool SelectEnabledItem (
return true;
}

IEnumerable<MenuItem> childMenuItems = forward ? children : children.Reverse ();
IEnumerable<MenuItem?> childMenuItems = forward ? children : children.Reverse ();

int count;

IEnumerable<MenuItem> menuItems = childMenuItems as MenuItem [] ?? childMenuItems.ToArray ();
IEnumerable<MenuItem?> menuItems = childMenuItems as MenuItem [] ?? childMenuItems.ToArray ();

if (forward)
{
Expand All @@ -1027,7 +1039,7 @@ internal bool SelectEnabledItem (
count = menuItems.Count ();
}

foreach (MenuItem child in menuItems)
foreach (MenuItem? child in menuItems)
{
if (forward)
{
Expand Down
4 changes: 2 additions & 2 deletions Terminal.Gui/Views/Menu/MenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,10 @@ public virtual void RemoveMenuItem ()
{
if (Parent is { })
{
MenuItem []? childrens = ((MenuBarItem)Parent).Children;
MenuItem? []? childrens = ((MenuBarItem)Parent).Children;
var i = 0;

foreach (MenuItem c in childrens!)
foreach (MenuItem? c in childrens!)
{
if (c != this)
{
Expand Down
12 changes: 6 additions & 6 deletions UICatalog/Scenarios/ContextMenus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public override void Main ()
Menus =
[
new (
"File",
new MenuItem [] { new ("Quit", "", () => Application.RequestStop (), null, null, Application.QuitKey) })
"_File",
new MenuItem [] { new ("_Quit", "", () => Application.RequestStop (), null, null, Application.QuitKey) })
]
};

Expand Down Expand Up @@ -170,6 +170,10 @@ private void ShowContextMenu (int x, int y)
MenuBarItem menuItems = new (
new []
{
new MenuBarItem (
"_Languages",
GetSupportedCultures ()
),
new (
"_Configuration",
"Show configuration",
Expand Down Expand Up @@ -214,10 +218,6 @@ private void ShowContextMenu (int x, int y)
)
}
),
new MenuBarItem (
"_Languages",
GetSupportedCultures ()
),
_miForceMinimumPosToZero =
new (
"Fo_rceMinimumPosToZero",
Expand Down
44 changes: 27 additions & 17 deletions UICatalog/Scenarios/DynamicMenuBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ public DynamicMenuBarSample ()
{
MenuItem newMenu = CreateNewMenu (item, _currentMenuBarItem);
var menuBarItem = _currentMenuBarItem as MenuBarItem;
menuBarItem.AddMenuBarItem (newMenu);
menuBarItem.AddMenuBarItem (MenuBar, newMenu);
DataContext.Menus.Add (new () { Title = newMenu.Title, MenuItem = newMenu });
Expand Down Expand Up @@ -915,6 +915,11 @@ public DynamicMenuBarSample ()
_lstMenus.SelectedItem = _lstMenus.Source.Count - 1;
}
if (_menuBar.Menus.Length == 0)
{
RemoveMenuBar ();
}
_lstMenus.SetNeedsDisplay ();
SetFrameDetails ();
}
Expand Down Expand Up @@ -992,7 +997,7 @@ public DynamicMenuBarSample ()
}
var newMenu = CreateNewMenu (item) as MenuBarItem;
newMenu.AddMenuBarItem ();
newMenu.AddMenuBarItem (MenuBar);
_currentMenuBarItem = newMenu;
_currentMenuBarItem.CheckType = item.CheckStyle;
Expand All @@ -1012,7 +1017,7 @@ public DynamicMenuBarSample ()

btnRemoveMenuBar.Accept += (s, e) =>
{
if (_menuBar == null || _menuBar.Menus.Length == 0)
if (_menuBar == null)
{
return;
}
Expand All @@ -1033,25 +1038,30 @@ public DynamicMenuBarSample ()
: null;
}
if (MenuBar != null && _currentMenuBarItem == null && _menuBar.Menus.Length == 0)
{
Remove (_menuBar);
_menuBar.Dispose ();
_menuBar = null;
DataContext.Menus = new ();
_currentMenuBarItem = null;
_currentSelectedMenuBar = -1;
lblMenuBar.Text = string.Empty;
}
else
{
lblMenuBar.Text = _menuBar.Menus [_currentSelectedMenuBar].Title;
}
RemoveMenuBar ();
SetListViewSource (_currentMenuBarItem, true);
SetFrameDetails ();
};

void RemoveMenuBar ()
{
if (MenuBar != null && _currentMenuBarItem == null && _menuBar.Menus.Length == 0)
{
Remove (_menuBar);
_menuBar.Dispose ();
_menuBar = null;
DataContext.Menus = new ();
_currentMenuBarItem = null;
_currentSelectedMenuBar = -1;
lblMenuBar.Text = string.Empty;
}
else
{
lblMenuBar.Text = _menuBar.Menus [_currentSelectedMenuBar].Title;
}
}

SetFrameDetails ();

var ustringConverter = new UStringValueConverter ();
Expand Down
2 changes: 1 addition & 1 deletion UICatalog/UICatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ string GetDiagnosticsTitle (Enum diag)
};
}

Enum GetDiagnosticsEnumValue (string title)
Enum GetDiagnosticsEnumValue (string? title)
{
return title switch
{
Expand Down
Loading

0 comments on commit fa0085d

Please sign in to comment.