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
21 changes: 10 additions & 11 deletions src/Controls/src/Core/Menu/MenuFlyoutSubItem.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
#nullable disable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;
using Microsoft.Maui.Controls.StyleSheets;
using Microsoft.Maui.Graphics;

namespace Microsoft.Maui.Controls
{
Expand All @@ -16,11 +10,8 @@ public partial class MenuFlyoutSubItem : MenuFlyoutItem, IMenuFlyoutSubItem

public MenuFlyoutSubItem()
{
LogicalChildrenInternalBackingStore = new CastingList<Element, IMenuElement>(_menus);
}

private protected override IList<Element> LogicalChildrenInternalBackingStore { get; }

public IMenuElement this[int index]
{
get { return _menus[index]; }
Expand All @@ -38,6 +29,7 @@ public IMenuElement this[int index]
public void Add(IMenuElement item)
{
var index = _menus.Count;
_menus.Add(item);
Comment thread
PureWeen marked this conversation as resolved.
AddLogicalChild((Element)item);
NotifyHandler(nameof(IMenuBarItemHandler.Add), index, item);
}
Expand Down Expand Up @@ -70,14 +62,18 @@ public int IndexOf(IMenuElement item)

public void Insert(int index, IMenuElement item)
{
_menus.Insert(index, item);
InsertLogicalChild(index, (Element)item);
NotifyHandler(nameof(IMenuFlyoutSubItemHandler.Insert), index, item);
}

public bool Remove(IMenuElement item)
{
var index = _menus.IndexOf(item);
var result = RemoveLogicalChild((Element)item, index);
_menus.RemoveAt(index);
Comment thread
PureWeen marked this conversation as resolved.

// Logical Child Index might not match location in _menus list
var result = RemoveLogicalChild((Element)item);
NotifyHandler(nameof(IMenuFlyoutHandler.Remove), index, item);

return result;
Expand All @@ -86,7 +82,10 @@ public bool Remove(IMenuElement item)
public void RemoveAt(int index)
{
var item = _menus[index];
RemoveLogicalChild((Element)item, index);
_menus.RemoveAt(index);
Comment thread
PureWeen marked this conversation as resolved.

// Logical Child Index might not match location in _menus list
RemoveLogicalChild((Element)item);
NotifyHandler(nameof(IMenuFlyoutHandler.Remove), index, item);
}

Expand Down
15 changes: 15 additions & 0 deletions src/Controls/tests/Core.UnitTests/Menu/MenuBarItemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ namespace Microsoft.Maui.Controls.Core.UnitTests.Menu
public class MenuBarItemTests :
MenuTestBase<MenuBarItem, IMenuElement, MenuFlyoutItem, MenuBarItemHandlerUpdate>
{
[Fact]
public void StartsEnabled()
{
MenuBarItem menuBarItem = new MenuBarItem();
Assert.True(menuBarItem.IsEnabled);
}

[Fact]
public void DisableWorks()
{
MenuBarItem menuBarItem = new MenuBarItem();
menuBarItem.IsEnabled = false;
Assert.False(menuBarItem.IsEnabled);
}

protected override int GetIndex(MenuBarItemHandlerUpdate handlerUpdate) =>
handlerUpdate.Index;

Expand Down
15 changes: 0 additions & 15 deletions src/Controls/tests/Core.UnitTests/Menu/MenuFlyoutSubItemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,6 @@ namespace Microsoft.Maui.Controls.Core.UnitTests.Menu
public class MenuFlyoutSubItemTests :
MenuTestBase<MenuFlyoutSubItem, IMenuElement, MenuFlyoutItem, MenuFlyoutSubItemHandlerUpdate>
{
[Fact]
public void StartsEnabled()
{
MenuBarItem menuBarItem = new MenuBarItem();
Assert.True(menuBarItem.IsEnabled);
}

[Fact]
public void DisableWorks()
{
MenuBarItem menuBarItem = new MenuBarItem();
menuBarItem.IsEnabled = false;
Assert.False(menuBarItem.IsEnabled);
}

[Fact]
public void CommandPropertyChangesEnabled()
{
Expand Down
35 changes: 35 additions & 0 deletions src/Controls/tests/Core.UnitTests/Menu/MenuTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
using System.Collections;
using System.Collections.Generic;
using Microsoft.Maui.Handlers;
using Xunit;
Expand All @@ -17,6 +18,40 @@ protected abstract void SetHandler(

protected abstract int GetIndex(THandlerUpdate handlerUpdate);

[Fact]
public void IconImageSourceAddsCorrectlyToEachSetOfLogicalChildren()
{
var menuBar = new TTestType();

var child0 = new TChildType();
var child1 = new TChildType();

menuBar.Add(child0);
menuBar.Add(child1);

if (menuBar is MenuItem mi0)
mi0.IconImageSource = new FileImageSource { File = "coffee.png" };

if (child0 is MenuItem mi1)
mi1.IconImageSource = new FileImageSource { File = "coffee.png" };

if (child1 is MenuItem mi2)
mi2.IconImageSource = new FileImageSource { File = "coffee.png" };

foreach(var item in menuBar)
{
Assert.NotNull(item);
if (item is IList list)
{
foreach(var child in list)
{
Assert.NotNull(child);
}
}
}
}


[Fact]
public void UsingIndexUpdatesParent()
{
Expand Down