diff --git a/src/MenuTracker.cs b/src/MenuTracker.cs
index ffe7d670..109c3b07 100644
--- a/src/MenuTracker.cs
+++ b/src/MenuTracker.cs
@@ -47,6 +47,22 @@ public void Register(MenuBar mb)
this.bars.Add(mb);
}
+ ///
+ /// Unregisters listeners for .
+ ///
+ /// to stop tracking.
+ public void UnregisterMenuBar( MenuBar? mb )
+ {
+ if ( !bars.TryTake( out mb ) )
+ {
+ return;
+ }
+
+ mb.MenuAllClosed -= MenuAllClosed;
+ mb.MenuOpened -= MenuOpened;
+ mb.MenuClosing -= MenuClosing;
+ }
+
///
///
/// Searches child items of all MenuBars tracked by this class
@@ -63,7 +79,7 @@ public void Register(MenuBar mb)
/// The immediate parent of .
/// Result may be a top level menu (e.g. File, View)
/// or a sub-menu parent (e.g. View=>Windows).
- public MenuBarItem? GetParent( MenuItem item, out MenuBar? hostBar )
+ private MenuBarItem? GetParent( MenuItem item, out MenuBar? hostBar )
{
foreach (var bar in this.bars)
{
@@ -83,7 +99,23 @@ public void Register(MenuBar mb)
return null;
}
- public bool TryGetParent(MenuItem item, [NotNullWhen(true)]out MenuBar? hostBar, [NotNullWhen(true)] out MenuBarItem? parentItem)
+ ///
+ /// Searches child items of all MenuBars tracked by this class to try and find the parent of the item passed.
+ ///
+ /// The item whose parent you want to find.
+ ///
+ /// When this method returns true, the that owns .
Otherwise, if
+ /// not found or parent not registered (see ).
+ ///
+ ///
+ /// When this method returns , the immediate parent of .
Otherwise,
+ ///
+ ///
+ ///
+ /// Search is recursive and dips into sub-menus.
For sub-menus it is the immediate parent that is returned.
+ ///
+ /// A indicating if the search was successful or not.
+ public bool TryGetParent( MenuItem item, [NotNullWhen( true )] out MenuBar? hostBar, [NotNullWhen( true )] out MenuBarItem? parentItem )
{
var parentCandidate = GetParent( item, out hostBar );
if ( parentCandidate is null )
@@ -106,22 +138,21 @@ public bool TryGetParent(MenuItem item, [NotNullWhen(true)]out MenuBar? hostBar,
/// the substitution object (). See
///
/// for more information.
- public Dictionary ConvertEmptyMenus()
+ public Dictionary ConvertEmptyMenus( )
{
- var toReturn = new Dictionary();
-
+ Dictionary dictionary = [];
foreach (var b in this.bars)
{
foreach (var bi in b.Menus)
{
- foreach (var converted in this.ConvertEmptyMenus(b, bi))
+ foreach ( ( MenuBarItem? convertedMenuBarItem, MenuItem? convertedMenuItem ) in this.ConvertEmptyMenus( dictionary, b, bi ) )
{
- toReturn.Add(converted.Key, converted.Value);
+ dictionary.TryAdd( convertedMenuBarItem, convertedMenuItem );
}
}
}
- return toReturn;
+ return dictionary;
}
///
@@ -137,12 +168,12 @@ public Dictionary ConvertEmptyMenus()
/// The result of the conversion (same text, same index etc but
/// instead of ).
/// if conversion was possible (menu was empty and belonged to tracked menu).
- internal static bool ConvertMenuBarItemToRegularItemIfEmpty(MenuBarItem bar, out MenuItem? added)
+ internal static bool ConvertMenuBarItemToRegularItemIfEmpty( MenuBarItem bar, [NotNullWhen( true )] out MenuItem? added )
{
added = null;
// bar still has more children so don't convert
- if (bar.Children.Any())
+ if ( bar.Children.Length != 0 )
{
return false;
}
@@ -152,8 +183,7 @@ internal static bool ConvertMenuBarItemToRegularItemIfEmpty(MenuBarItem bar, out
return false;
}
- var children = parent.Children.ToList