Add logic to set DefaultLabelPosition on the WinUI CommandBar (#15835)
* Make toolbar use right labels by default, and collapsed if all items are just icons * Add tests! * Add better mix to test * Fix issue w/ toolbar items not updating default label position --------- Co-authored-by: Mike Corsaro <mikecorsaro@microsoft.com>
This commit is contained in:
Родитель
432caf178c
Коммит
43b9397bf3
|
@ -3,7 +3,6 @@ using System.Collections.Generic;
|
|||
using System.ComponentModel;
|
||||
using Microsoft.Maui.Controls.Platform;
|
||||
using Microsoft.Maui.Graphics;
|
||||
using Microsoft.Maui.Handlers;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using NativeAutomationProperties = Microsoft.UI.Xaml.Automation.AutomationProperties;
|
||||
using WImage = Microsoft.UI.Xaml.Controls.Image;
|
||||
|
@ -18,13 +17,21 @@ namespace Microsoft.Maui.Controls
|
|||
NavigationRootManager? NavigationRootManager =>
|
||||
MauiContext?.GetNavigationRootManager();
|
||||
|
||||
partial void OnHandlerChanging(IElementHandler oldHandler, IElementHandler newHandler)
|
||||
{
|
||||
if (newHandler == null)
|
||||
{
|
||||
foreach (var item in ToolbarItems)
|
||||
item.PropertyChanged -= OnToolbarItemPropertyChanged;
|
||||
}
|
||||
}
|
||||
|
||||
internal void UpdateMenu()
|
||||
{
|
||||
if (Handler.PlatformView is not MauiToolbar wh)
|
||||
return;
|
||||
|
||||
var commandBar = wh.CommandBar;
|
||||
|
||||
if (commandBar == null)
|
||||
{
|
||||
return;
|
||||
|
@ -71,8 +78,62 @@ namespace Microsoft.Maui.Controls
|
|||
{
|
||||
commandBar.SecondaryCommands.Add(button);
|
||||
}
|
||||
|
||||
item.PropertyChanged -= OnToolbarItemPropertyChanged;
|
||||
item.PropertyChanged += OnToolbarItemPropertyChanged;
|
||||
}
|
||||
|
||||
SetDefaultLabelPosition(commandBar, toolbarItems);
|
||||
}
|
||||
|
||||
internal void OnToolbarItemPropertyChanged(object? sender, PropertyChangedEventArgs e)
|
||||
{
|
||||
if (Handler.PlatformView is not MauiToolbar wh)
|
||||
return;
|
||||
|
||||
var commandBar = wh.CommandBar;
|
||||
if (commandBar == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.PropertyName == nameof(ToolbarItem.Text) || e.PropertyName == nameof(ToolbarItem.IconImageSource))
|
||||
{
|
||||
var toolbarItems = new List<ToolbarItem>(ToolbarItems ?? Array.Empty<ToolbarItem>());
|
||||
SetDefaultLabelPosition(commandBar, toolbarItems);
|
||||
}
|
||||
}
|
||||
|
||||
private static void SetDefaultLabelPosition(CommandBar commandBar, IList<ToolbarItem> toolbarItems)
|
||||
{
|
||||
int itemsWithTextCount = 0;
|
||||
int itemsWithIconCount = 0;
|
||||
|
||||
foreach (ToolbarItem item in toolbarItems)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(item.Text))
|
||||
{
|
||||
itemsWithTextCount++;
|
||||
}
|
||||
if (item.IconImageSource != null)
|
||||
{
|
||||
itemsWithIconCount++;
|
||||
}
|
||||
}
|
||||
|
||||
bool allItemsHaveIcons = toolbarItems.Count == itemsWithIconCount;
|
||||
|
||||
// All items have icons, none have text
|
||||
if (allItemsHaveIcons && itemsWithTextCount == 0)
|
||||
{
|
||||
commandBar.DefaultLabelPosition = CommandBarDefaultLabelPosition.Collapsed;
|
||||
}
|
||||
else
|
||||
{
|
||||
commandBar.DefaultLabelPosition = CommandBarDefaultLabelPosition.Right;
|
||||
}
|
||||
}
|
||||
|
||||
public static void MapBarTextColor(ToolbarHandler arg1, Toolbar arg2) =>
|
||||
MapBarTextColor((IToolbarHandler)arg1, arg2);
|
||||
|
||||
|
|
|
@ -0,0 +1,129 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Maui;
|
||||
using Microsoft.Maui.Controls;
|
||||
using Microsoft.Maui.Controls.Handlers;
|
||||
using Microsoft.Maui.DeviceTests.Stubs;
|
||||
using Microsoft.Maui.DeviceTests.TestCases;
|
||||
using Microsoft.Maui.Handlers;
|
||||
using Microsoft.Maui.Hosting;
|
||||
using Microsoft.Maui.Platform;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Xunit;
|
||||
using Xunit.Sdk;
|
||||
|
||||
namespace Microsoft.Maui.DeviceTests
|
||||
{
|
||||
[Category(TestCategory.Toolbar)]
|
||||
public partial class ToolbarTests : ControlsHandlerTestBase
|
||||
{
|
||||
[Fact(DisplayName = "Toolbar Default Label Position")]
|
||||
public async Task ToolbarDefaultLabelPositionWithNoImages()
|
||||
{
|
||||
SetupBuilder();
|
||||
var item1 = new ToolbarItem() { Text = "Toolbar Item 1" };
|
||||
var item2 = new ToolbarItem() { Text = "Toolbar Item 2" };
|
||||
|
||||
var navPage = new NavigationPage(new ContentPage()
|
||||
{
|
||||
ToolbarItems =
|
||||
{
|
||||
item1,
|
||||
item2
|
||||
}
|
||||
});
|
||||
|
||||
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), (handler) =>
|
||||
{
|
||||
var toolbar = (Toolbar)(navPage.Window as IToolbarElement).Toolbar;
|
||||
var platformCommandBar = ((MauiToolbar)toolbar.Handler.PlatformView).CommandBar;
|
||||
Assert.True(platformCommandBar.DefaultLabelPosition == CommandBarDefaultLabelPosition.Right);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
}
|
||||
|
||||
[Fact(DisplayName = "Toolbar Default Label Position with Image and Text")]
|
||||
public async Task ToolbarDefaultLabelPositionWithTextAndImages()
|
||||
{
|
||||
SetupBuilder();
|
||||
var item1 = new ToolbarItem() { IconImageSource = "red.png" };
|
||||
var item2 = new ToolbarItem() { Text = "Toolbar Item 2" };
|
||||
var item3 = new ToolbarItem() { Text = "Toolbar Item 2", IconImageSource = "red.png" };
|
||||
|
||||
var navPage = new NavigationPage(new ContentPage()
|
||||
{
|
||||
ToolbarItems =
|
||||
{
|
||||
item1,
|
||||
item2,
|
||||
item3
|
||||
}
|
||||
});
|
||||
|
||||
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), (handler) =>
|
||||
{
|
||||
var toolbar = (Toolbar)(navPage.Window as IToolbarElement).Toolbar;
|
||||
var platformCommandBar = ((MauiToolbar)toolbar.Handler.PlatformView).CommandBar;
|
||||
Assert.True(platformCommandBar.DefaultLabelPosition == CommandBarDefaultLabelPosition.Right);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
}
|
||||
|
||||
[Fact(DisplayName = "Toolbar Default Label Position With Images")]
|
||||
public async Task ToolbarDefaultLabelPositionOnlyImages()
|
||||
{
|
||||
SetupBuilder();
|
||||
var item1 = new ToolbarItem() { IconImageSource = "red.png" };
|
||||
var item2 = new ToolbarItem() { IconImageSource = "red.png" };
|
||||
|
||||
var navPage = new NavigationPage(new ContentPage()
|
||||
{
|
||||
ToolbarItems =
|
||||
{
|
||||
item1,
|
||||
item2
|
||||
}
|
||||
});
|
||||
|
||||
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), (handler) =>
|
||||
{
|
||||
var toolbar = (Toolbar)(navPage.Window as IToolbarElement).Toolbar;
|
||||
var platformCommandBar = ((MauiToolbar)toolbar.Handler.PlatformView).CommandBar;
|
||||
Assert.True(platformCommandBar.DefaultLabelPosition == CommandBarDefaultLabelPosition.Collapsed);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
}
|
||||
|
||||
[Fact(DisplayName = "Toolbar Default Label Position Add Text to Images")]
|
||||
public async Task ToolbarDefaultLabelPositionAddText()
|
||||
{
|
||||
SetupBuilder();
|
||||
|
||||
var item1 = new ToolbarItem() { IconImageSource = "red.png" };
|
||||
var item2 = new ToolbarItem() { IconImageSource = "red.png" };
|
||||
|
||||
var navPage = new NavigationPage(new ContentPage()
|
||||
{
|
||||
ToolbarItems =
|
||||
{
|
||||
item1,
|
||||
item2
|
||||
}
|
||||
});
|
||||
|
||||
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), (handler) =>
|
||||
{
|
||||
navPage.CurrentPage.ToolbarItems[0].Text = "Toolbar Item 1";
|
||||
|
||||
var toolbar = (Toolbar)(navPage.Window as IToolbarElement).Toolbar;
|
||||
var platformCommandBar = ((MauiToolbar)toolbar.Handler.PlatformView).CommandBar;
|
||||
Assert.True(platformCommandBar.DefaultLabelPosition == CommandBarDefaultLabelPosition.Right);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче