in sources/Google.Solutions.Mvvm/Theme/ToolStripItemTheme.cs [37:124]
private void ApplyTo(ToolStripItem item)
{
//
// Apply Rules to current item.
//
foreach (var rule in this.rules)
{
rule(item);
}
//
// Recursively visit child items.
//
if (item is ToolStripDropDownItem dropDownItem)
{
//
// Consider current child items...
//
List<WeakReference<ToolStripItem>>? appliedItems = null;
if (this.IncludeControlsAddedLater)
{
appliedItems = new List<WeakReference<ToolStripItem>>();
}
if (dropDownItem.DropDownItems != null)
{
foreach (var subItem in dropDownItem.DropDownItems.OfType<ToolStripItem>())
{
ApplyTo(subItem);
if (this.IncludeControlsAddedLater && appliedItems != null)
{
//
// Memoize items we've applied already so that
// we don't reapply them again later.
//
appliedItems.Add(new WeakReference<ToolStripItem>(subItem));
}
}
}
//
// ...and future items.
//
// There's no ItemAdded event, so we have to check for new items
// every time the menu pops up.
//
if (this.IncludeControlsAddedLater)
{
dropDownItem.DropDownOpening += OnDropDownOpening;
}
void OnDropDownOpening(object sender, EventArgs __)
{
var openingItem = (ToolStripDropDownItem)sender;
if (openingItem.DropDownItems != null)
{
foreach (var subItem in openingItem.DropDownItems.OfType<ToolStripItem>())
{
if (!appliedItems.Any(i =>
i.TryGetTarget(out var appliedItem) && appliedItem == subItem))
{
ApplyTo(subItem);
}
Debug.Assert(appliedItems != null, "cannot be null here");
appliedItems!.Add(new WeakReference<ToolStripItem>(subItem));
Debug.Assert(
appliedItems.Count < 256,
"Cache should not grow excessively large");
if (subItem is ToolStripDropDownItem dropDownSubItem)
{
dropDownSubItem.DropDownOpening += OnDropDownOpening;
}
}
}
//
// Only allow each event to be fired once, otherwise
// the appliedItem list will grow too large.
//
openingItem.DropDownOpening -= OnDropDownOpening;
}
}
}