Skip to content

Commit

Permalink
Merge pull request #11040 from AvaloniaUI/fixes/menuitem-header-binding
Browse files Browse the repository at this point in the history
Fix NativeMenuBar.
  • Loading branch information
github-merge-queue[bot] authored Apr 17, 2023
2 parents 747c6a6 + d37de0b commit 7158628
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .ncrunch/SafeAreaDemo.Android.v3.ncrunchproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ProjectConfiguration>
<Settings>
<IgnoreThisComponentCompletely>True</IgnoreThisComponentCompletely>
</Settings>
</ProjectConfiguration>
5 changes: 5 additions & 0 deletions .ncrunch/SafeAreaDemo.Desktop.v3.ncrunchproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ProjectConfiguration>
<Settings>
<IgnoreThisComponentCompletely>True</IgnoreThisComponentCompletely>
</Settings>
</ProjectConfiguration>
5 changes: 5 additions & 0 deletions .ncrunch/SafeAreaDemo.iOS.v3.ncrunchproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ProjectConfiguration>
<Settings>
<IgnoreThisComponentCompletely>True</IgnoreThisComponentCompletely>
</Settings>
</ProjectConfiguration>
5 changes: 5 additions & 0 deletions .ncrunch/SafeAreaDemo.v3.ncrunchproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ProjectConfiguration>
<Settings>
<IgnoreThisComponentCompletely>True</IgnoreThisComponentCompletely>
</Settings>
</ProjectConfiguration>
67 changes: 39 additions & 28 deletions src/Avalonia.Controls/ItemsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,48 +378,48 @@ protected internal virtual void PrepareContainerForItemOverride(Control containe

if (container is HeaderedContentControl hcc)
{
hcc.Content = item;
SetIfUnset(hcc, HeaderedContentControl.ContentProperty, item);

if (item is IHeadered headered)
hcc.Header = headered.Header;
SetIfUnset(hcc, HeaderedContentControl.HeaderProperty, headered.Header);
else if (item is not Visual)
hcc.Header = item;
SetIfUnset(hcc, HeaderedContentControl.HeaderProperty, item);

if (itemTemplate is not null)
hcc.HeaderTemplate = itemTemplate;
SetIfUnset(hcc, HeaderedContentControl.HeaderTemplateProperty, itemTemplate);
}
else if (container is ContentControl cc)
{
cc.Content = item;
SetIfUnset(cc, ContentControl.ContentProperty, item);
if (itemTemplate is not null)
cc.ContentTemplate = itemTemplate;
SetIfUnset(cc, ContentControl.ContentTemplateProperty, itemTemplate);
}
else if (container is ContentPresenter p)
{
p.Content = item;
SetIfUnset(p, ContentPresenter.ContentProperty, item);
if (itemTemplate is not null)
p.ContentTemplate = itemTemplate;
SetIfUnset(p, ContentPresenter.ContentTemplateProperty, itemTemplate);
}
else if (container is ItemsControl ic)
{
if (itemTemplate is not null)
ic.ItemTemplate = itemTemplate;
if (ItemContainerTheme is { } ict && !ict.IsSet(ItemContainerThemeProperty))
ic.ItemContainerTheme = ict;
SetIfUnset(ic, ItemTemplateProperty, itemTemplate);
if (ItemContainerTheme is { } ict)
SetIfUnset(ic, ItemContainerThemeProperty, ict);
}

// These conditions are separate because HeaderedItemsControl and
// HeaderedSelectingItemsControl also need to run the ItemsControl preparation.
if (container is HeaderedItemsControl hic)
{
hic.Header = item;
hic.HeaderTemplate = itemTemplate;
SetIfUnset(hic, HeaderedItemsControl.HeaderProperty, item);
SetIfUnset(hic, HeaderedItemsControl.HeaderTemplateProperty, itemTemplate);
hic.PrepareItemContainer(this);
}
else if (container is HeaderedSelectingItemsControl hsic)
{
hsic.Header = item;
hsic.HeaderTemplate = itemTemplate;
SetIfUnset(hsic, HeaderedSelectingItemsControl.HeaderProperty, item);
SetIfUnset(hsic, HeaderedSelectingItemsControl.HeaderTemplateProperty, itemTemplate);
hsic.PrepareItemContainer(this);
}
}
Expand Down Expand Up @@ -458,30 +458,35 @@ protected internal virtual void ClearContainerForItemOverride(Control container)
{
if (container is HeaderedContentControl hcc)
{
if (hcc.Content is Control)
hcc.Content = null;
if (hcc.Header is Control)
hcc.Header = null;
hcc.ClearValue(HeaderedContentControl.ContentProperty);
hcc.ClearValue(HeaderedContentControl.HeaderProperty);
hcc.ClearValue(HeaderedContentControl.HeaderTemplateProperty);
}
else if (container is ContentControl cc)
{
if (cc.Content is Control)
cc.Content = null;
cc.ClearValue(ContentControl.ContentProperty);
cc.ClearValue(ContentControl.ContentTemplateProperty);
}
else if (container is ContentPresenter p)
{
if (p.Content is Control)
p.Content = null;
p.ClearValue(ContentPresenter.ContentProperty);
p.ClearValue(ContentPresenter.ContentTemplateProperty);
}
else if (container is HeaderedItemsControl hic)
else if (container is ItemsControl ic)
{
ic.ClearValue(ItemTemplateProperty);
ic.ClearValue(ItemContainerThemeProperty);
}

if (container is HeaderedItemsControl hic)
{
if (hic.Header is Control)
hic.Header = null;
hic.ClearValue(HeaderedItemsControl.HeaderProperty);
hic.ClearValue(HeaderedItemsControl.HeaderTemplateProperty);
}
else if (container is HeaderedSelectingItemsControl hsic)
{
if (hsic.Header is Control)
hsic.Header = null;
hsic.ClearValue(HeaderedSelectingItemsControl.HeaderProperty);
hsic.ClearValue(HeaderedSelectingItemsControl.HeaderTemplateProperty);
}

// Feels like we should be clearing the HeaderedItemsControl.Items binding here, but looking at
Expand Down Expand Up @@ -707,6 +712,12 @@ private void AddControlItemsToLogicalChildren(IEnumerable? items)
LogicalChildren.AddRange(toAdd);
}

private void SetIfUnset<T>(AvaloniaObject target, StyledProperty<T> property, T value)
{
if (!target.IsSet(property))
target.SetCurrentValue(property, value);
}

private void RemoveControlItemsFromLogicalChildren(IEnumerable? items)
{
if (items is null)
Expand Down
32 changes: 32 additions & 0 deletions tests/Avalonia.Controls.UnitTests/ListBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,36 @@ public void Initial_Binding_Of_SelectedItems_Should_Not_Cause_Write_To_SelectedI
Assert.Equal(new[] { "Bar" }, target.Selection.SelectedItems);
}

[Fact]
public void Content_Can_Be_Bound_In_ItemContainerTheme()
{
using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
{
var items = new[] { new ItemViewModel("Foo"), new ItemViewModel("Bar") };
var theme = new ControlTheme(typeof(ListBoxItem))
{
Setters =
{
new Setter(ListBoxItem.ContentProperty, new Binding("Caption")),
}
};

var target = new ListBox
{
Template = ListBoxTemplate(),
ItemsSource = items,
ItemContainerTheme = theme,
};

Prepare(target);

var containers = target.GetRealizedContainers().Cast<ListBoxItem>().ToList();
Assert.Equal(2, containers.Count);
Assert.Equal("Foo", containers[0].Content);
Assert.Equal("Bar", containers[1].Content);
}
}

private static FuncControlTemplate ListBoxTemplate()
{
return new FuncControlTemplate<ListBox>((parent, scope) =>
Expand Down Expand Up @@ -918,6 +948,8 @@ public void ContainerClearing_Is_Raised_When_Item_Removed()
Assert.Equal(1, raised);
}

private record ItemViewModel(string Caption);

private class ResettingCollection : List<string>, INotifyCollectionChanged
{
public ResettingCollection(int itemCount)
Expand Down
95 changes: 89 additions & 6 deletions tests/Avalonia.Controls.UnitTests/MenuItemTests.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Windows.Input;
using Avalonia.Collections;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Controls.Utils;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Platform;
using Avalonia.Styling;
using Avalonia.UnitTests;
using Avalonia.VisualTree;
using Moq;
using Xunit;

Expand All @@ -36,7 +36,6 @@ public void Separator_Item_Should_Set_Focusable_False()
Assert.False(target.Focusable);
}


[Fact]
public void MenuItem_Is_Disabled_When_Command_Is_Enabled_But_IsEnabled_Is_False()
{
Expand Down Expand Up @@ -393,6 +392,87 @@ public void Menu_ItemTemplate_Should_Be_Applied_To_TopLevel_MenuItem_Header()
}
}

[Fact]
public void Header_And_ItemsSource_Can_Be_Bound_In_Style()
{
using var app = Application();
var items = new[]
{
new MenuViewModel("Foo")
{
Children = new[]
{
new MenuViewModel("FooChild"),
},
},
new MenuViewModel("Bar"),
};

var target = new Menu
{
ItemsSource = items,
Styles =
{
new Style(x => x.OfType<MenuItem>())
{
Setters =
{
new Setter(MenuItem.HeaderProperty, new Binding("Header")),
new Setter(MenuItem.ItemsSourceProperty, new Binding("Children")),
}
}
}
};

var root = new TestRoot(true, target);
root.LayoutManager.ExecuteInitialLayoutPass();

var children = target.GetRealizedContainers().Cast<MenuItem>().ToList();
Assert.Equal(2, children.Count);
Assert.Equal("Foo", children[0].Header);
Assert.Equal("Bar", children[1].Header);
Assert.Same(items[0].Children, children[0].ItemsSource);
}

[Fact]
public void Header_And_ItemsSource_Can_Be_Bound_In_ItemContainerTheme()
{
using var app = Application();
var items = new[]
{
new MenuViewModel("Foo")
{
Children = new[]
{
new MenuViewModel("FooChild"),
},
},
new MenuViewModel("Bar"),
};

var target = new Menu
{
ItemsSource = items,
ItemContainerTheme = new ControlTheme(typeof(MenuItem))
{
Setters =
{
new Setter(MenuItem.HeaderProperty, new Binding("Header")),
new Setter(MenuItem.ItemsSourceProperty, new Binding("Children")),
}
}
};

var root = new TestRoot(true, target);
root.LayoutManager.ExecuteInitialLayoutPass();

var children = target.GetRealizedContainers().Cast<MenuItem>().ToList();
Assert.Equal(2, children.Count);
Assert.Equal("Foo", children[0].Header);
Assert.Equal("Bar", children[1].Header);
Assert.Same(items[0].Children, children[0].ItemsSource);
}

private IDisposable Application()
{
var screen = new PixelRect(new PixelPoint(), new PixelSize(100, 100));
Expand Down Expand Up @@ -447,6 +527,9 @@ public event EventHandler CanExecuteChanged
public void RaiseCanExecuteChanged() => _canExecuteChanged?.Invoke(this, EventArgs.Empty);
}

private record MenuViewModel(string Header);
private record MenuViewModel(string Header)
{
public IList<MenuViewModel> Children { get; set;}
}
}
}
18 changes: 17 additions & 1 deletion tests/Avalonia.IntegrationTests.Appium/NativeMenuTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public NativeMenuTests(DefaultAppFixture fixture)
}

[PlatformFact(TestPlatforms.MacOS)]
public void View_Menu_Select_Button_Tab()
public void MacOS_View_Menu_Select_Button_Tab()
{
var tabs = _session.FindElementByAccessibilityId("MainTabs");
var buttonTab = tabs.FindElementByName("Button");
Expand All @@ -33,5 +33,21 @@ public void View_Menu_Select_Button_Tab()

Assert.True(buttonTab.Selected);
}

[PlatformFact(TestPlatforms.Windows)]
public void Win32_View_Menu_Select_Button_Tab()
{
var tabs = _session.FindElementByAccessibilityId("MainTabs");
var buttonTab = tabs.FindElementByName("Button");
var viewMenu = _session.FindElementByXPath("//MenuItem[@Name='View']");

Assert.False(buttonTab.Selected);

viewMenu.Click();
var buttonMenu = viewMenu.FindElementByName("Button");
buttonMenu.Click();

Assert.True(buttonTab.Selected);
}
}
}

0 comments on commit 7158628

Please sign in to comment.