Skip to content

Commit

Permalink
Shell toolbar items fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaflo committed Dec 18, 2024
1 parent ed53c15 commit 3112503
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,17 +292,25 @@ protected virtual void UpdateToolbarItems()
NavigationItem.RightBarButtonItems[i].Dispose();
}

var shellToolbarItems = _context?.Shell?.ToolbarItems;
List<UIBarButtonItem> primaries = null;
if (Page.ToolbarItems.Count > 0)
if (Page.ToolbarItems.Count > 0) // Display toolbar items defined on the current page
{
foreach (var item in System.Linq.Enumerable.OrderBy(Page.ToolbarItems, x => x.Priority))
{
(primaries = primaries ?? new List<UIBarButtonItem>()).Add(item.ToUIBarButtonItem(false, true));
}

if (primaries != null)
primaries.Reverse();
}
else if (shellToolbarItems != null && shellToolbarItems.Count > 0) // If the page has no toolbar items use the ones defined for the shell
{
foreach (var item in System.Linq.Enumerable.OrderBy(shellToolbarItems, x => x.Priority))
{
(primaries = primaries ?? new List<UIBarButtonItem>()).Add(item.ToUIBarButtonItem(false, true));
}
}

if (primaries != null)
primaries.Reverse();

NavigationItem.SetRightBarButtonItems(primaries == null ? Array.Empty<UIBarButtonItem>() : primaries.ToArray(), false);

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue18946.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue18946">
<Shell.TitleView>
<HorizontalStackLayout Margin="10" HorizontalOptions="Center" VerticalOptions="Fill" >
<Image AutomationId="image" Source="small_dotnet_bot.png" HeightRequest="48" WidthRequest="48" />
<Label VerticalOptions="Center" VerticalTextAlignment="Center" Text="Maui App" FontSize="20" />
</HorizontalStackLayout>
</Shell.TitleView>

<Shell.ToolbarItems>
<ToolbarItem Text="Hello"/>
</Shell.ToolbarItems>

<ShellContent Title="Home" Route="MainPage">
<ContentPage>
<Label Text="Hello, MAUI!" AutomationId="label"/>
</ContentPage>
</ShellContent>
</Shell>
16 changes: 16 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue18946.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 18946, "Shell Toolbar items not displayed", PlatformAffected.All)]
public partial class Issue18946 : Shell
{
public Issue18946()
{
InitializeComponent();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue18946 : _IssuesUITest
{
public override string Issue => "Shell Toolbar items not displayed";

public Issue18946(TestDevice device) : base(device)
{ }

[Test]
[Category(UITestCategories.Shell)]
public void ToolbarItemsShouldBeVisible()
{
_ = App.WaitForElement("label");

// The test passes if the text 'hello' is visible in the toolbar
VerifyScreenshot();
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3112503

Please sign in to comment.