Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C] Add ImageSource as LogicalChildren of MenuItem #24688

Merged
merged 3 commits into from
Sep 13, 2024
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
6 changes: 5 additions & 1 deletion src/Controls/src/Core/Menu/MenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public partial class MenuItem : BaseMenuItem, IMenuItemController, IStyleSelecta
public static readonly BindableProperty IsDestructiveProperty = BindableProperty.Create(nameof(IsDestructive), typeof(bool), typeof(MenuItem), false);

/// <summary>Bindable property for <see cref="IconImageSource"/>.</summary>
public static readonly BindableProperty IconImageSourceProperty = BindableProperty.Create(nameof(IconImageSource), typeof(ImageSource), typeof(MenuItem), default(ImageSource));
public static readonly BindableProperty IconImageSourceProperty = BindableProperty.Create(nameof(IconImageSource), typeof(ImageSource), typeof(MenuItem), default(ImageSource),
propertyChanged: (bindable, oldValue, newValue) => {
((MenuItem)bindable).AddRemoveLogicalChildren(oldValue, newValue);
}
);

/// <summary>Bindable property for <see cref="IsEnabled"/>.</summary>
public static readonly BindableProperty IsEnabledProperty = BindableProperty.Create(
Expand Down
12 changes: 12 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui20508.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui20508">
<ContentPage.ToolbarItems>
<ToolbarItem>
<ToolbarItem.IconImageSource>
<FileImageSource File="{Binding Icon}" />
</ToolbarItem.IconImageSource>
</ToolbarItem>
</ContentPage.ToolbarItems>
</ContentPage>
42 changes: 42 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui20508.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Threading.Tasks;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Dispatching;
using Microsoft.Maui.UnitTests;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Xaml.UnitTests;

public partial class Maui20508
{
public Maui20508()
{
InitializeComponent();
}

public Maui20508(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}

[TestFixture]
class Test
{
[SetUp]
public void Setup()
{
Application.SetCurrentApplication(new MockApplication());
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
}

[TearDown] public void TearDown() => AppInfo.SetCurrent(null);

[Test]
public void ToolBarItemBinding([Values(false, true)] bool useCompiledXaml)
{
var page = new Maui20508(useCompiledXaml) {BindingContext = new {Icon = "boundIcon.png"}};
Assert.That(((FileImageSource)page.ToolbarItems[0].IconImageSource).File, Is.EqualTo("boundIcon.png"));
}
}
}
23 changes: 23 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui23201.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui23201">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Search">
<ToolbarItem.IconImageSource>
<FontImageSource FontFamily="SegoeFluentIcons"
Glyph="&#xE721;"
Color="{AppThemeBinding Light={StaticResource Black},
Dark={StaticResource White}}" />
</ToolbarItem.IconImageSource>
</ToolbarItem>
<ToolbarItem Text="23195">
<ToolbarItem.IconImageSource>
<FontImageSource FontFamily="SegoeFluentIcons"
Glyph="&#xE721;"
Color="{AppThemeBinding Light=Black,
Dark=White}" />
</ToolbarItem.IconImageSource>
</ToolbarItem>
</ContentPage.ToolbarItems>
</ContentPage>
55 changes: 55 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui23201.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Threading.Tasks;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Dispatching;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.UnitTests;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Xaml.UnitTests;

public partial class Maui23201
{
public Maui23201()
{
InitializeComponent();
}

public Maui23201(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}

[TestFixture]
class Test
{
[SetUp]
public void Setup()
{
Application.SetCurrentApplication(new MockApplication());
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
}

[TearDown] public void TearDown() => AppInfo.SetCurrent(null);

[Test]
public void ToolBarItemAppThemeBinding([Values(false, true)] bool useCompiledXaml)
{
Application.Current.Resources.Add("Black", Colors.DarkGray);
Application.Current.Resources.Add("White", Colors.LightGray);

Application.Current.UserAppTheme = AppTheme.Light;
var page = new Maui23201(useCompiledXaml);
Application.Current.MainPage = page;

Assert.That(((FontImageSource)(page.ToolbarItems[0].IconImageSource)).Color, Is.EqualTo(Colors.DarkGray));
Assert.That(((FontImageSource)(page.ToolbarItems[1].IconImageSource)).Color, Is.EqualTo(Colors.Black));

Application.Current.UserAppTheme = AppTheme.Dark;
Assert.That(((FontImageSource)(page.ToolbarItems[0].IconImageSource)).Color, Is.EqualTo(Colors.LightGray));
Assert.That(((FontImageSource)(page.ToolbarItems[1].IconImageSource)).Color, Is.EqualTo(Colors.White));

}
}
}
Loading